var projectShowed = false;
var projectPlayer;
var projectTop;
var projectProgress;

function projectBox()
{
	projectTop = new Fx.Tween('project-box', { link : 'cancel' });
	
	window.addEvent('scroll', function(){
		if (projectShowed)
		{
			var x = window.getScroll().y;
			projectTop.start('top', x-30);
		}
	});
}

function projectPlayProgress()
{
	projectProgress.start('width', 0, 653);
}

function projectPlayStop()
{
	var obj = $('project-play');
	
	$('scroll-project-progress').setStyle('visibility', 'hidden');
	obj.removeClass('project-play-active');
	clearInterval(projectPlayer);
}

function projectPlay()
{
	var obj = $('project-play');
	
	if (obj.hasClass('project-play-active'))
	{
		projectPlayStop();
	}
	else
	{
		projectProgress = new Fx.Tween('scroll-project-progress-percent', { link : 'cancel', duration : 4000 });
		$('scroll-project-progress').setStyle('visibility', '');
		projectPlayProgress();
		obj.addClass('project-play-active');
		projectPlayer = (function(){ projectPlayProgress(); scrollNextPlay('project'); }).periodical(4000);
	}
}

function scrollNextPlay(id)
{	
	scrollNext(id);
	projectSmallHighlight(scrollCounter[id]);
}

function scrollPrevPro(id)
{
	projectPlayStop();
	
	scrollPrev(id);
	projectSmallHighlight(scrollCounter[id]);
}

function scrollNextPro(id)
{
	projectPlayStop();
	
	scrollNext(id);
	projectSmallHighlight(scrollCounter[id]);
}

function projectSmallHighlight(id)
{
	$$('div.project-box-gallery a').removeClass('project-box-gallery-active');
	$('project-box-gallery-entry-'+id).addClass('project-box-gallery-active');
}

function projectShowSmall(id)
{	
	projectPlayStop();
	
	projectSmallHighlight(id);
	
	if (!projectShowed)
	{
		scrollSet('project', id);
	}
	else
	{
		smoothScroll.toElement('project-gallery-pic-'+id);	
	}
}

function projectHide()
{			
	$('project-box').setStyle('top', '-30px');
	
	$('scroll-project').setStyle('display', '');
	$('project-gallery-left').setStyle('display', '');
	$('project-gallery-right').setStyle('display', '');	
	
	$$('div.project-gallery-pic').setStyle('display', 'none');
	$('project-hide').setStyle('display', 'none');
	$('project-show').setStyle('display', '');
	
	smoothScroll.toElement('scroll-project');
	
	projectPlayStop();
	
	projectShowed = false;	
}

function projectShow()
{
	projectShowed = true;
	projectPlayStop();
	
	$('scroll-project').setStyle('display', 'none');
	$('project-gallery-left').setStyle('display', 'none');
	$('project-gallery-right').setStyle('display', 'none');
	
	$$('div.project-gallery-pic').setStyle('display', '');
	$('project-hide').setStyle('display', '');
	$('project-show').setStyle('display', 'none');	
}

/*Usage:

var hasher = new hashChanger();
hasher.set('hash');
window.addEvent('load', function(){
	hasher.addEvent('change', function(hash){ myFunction(hash); });
});
*/

var hashChanger = new Class({

	Implements: Events,
	
	hash: '',
	
	hashFlag: 0,
	
	hashPrev: '',
	
	isSmall: '',
	
	direction: 1,
	
	delay: 500,
	
	initialize: function() {
		this.hashCheck.periodical(this.delay, this);
	},
	
	set: function(value) {
		//this.hashPrev = this.hash;
		this.hash = value;
		document.location.hash = value; 
		try{ pageTracker._trackPageview( value ); } catch (er){}; 
		//console.log('set:'+value);
	},
	
	hashCheck: function() {
		var hashTmp = document.location.hash.replace('#', ''); 
		
		if (hashTmp != this.hash && ( hashTmp!='' || this.hash!='' ) && hashTmp.indexOf('pic') != 0)
		{
			if(this.hash!='') this.direction = 0;
			this.hashPrev = this.hash;
			this.hash = hashTmp;
			this.fireEvent('change', this.hash);
		}
	}

});

function linksHandle()
{
	var arr = $$('a');
	
	arr.each(function(item){		
				
		if (item.href.indexOf(host_url) >= 0 && item.href.indexOf('/upload/') == -1 && item.href.indexOf('/blog') == -1 && item.href.indexOf('#') == -1)
		{		
			item.addEvent('click', function(event){ 
				hasher.hashPrev = hasher.hash;
				linksHandleItem(item); 
				event.preventDefault();
				return false;
				});
		}
		
	});
}

function linksHandleContent()
{	
	var arr = $$('div#scroll-item-page-0 a');
	
	arr.each(function(item){		
				
		if (item.href.indexOf(host_url) >= 0 && item.href.indexOf('/upload/') == -1 && item.href.indexOf('/blog') == -1 && item.href.indexOf('#') == -1)
		{		
			item.addEvent('click', function(event){ 
				hasher.hashPrev = hasher.hash;
				linksHandleItem(item);
				event.preventDefault();
				return false;
				});
		}
		
	});
}

function linksHandleSmallContent()
{	
	var arr = $$('div#content-0 a');
	
	arr.each(function(item){		
				
		if (item.href.indexOf(host_url) >= 0 && item.href.indexOf('/upload/') == -1 && item.href.indexOf('/blog') == -1 && item.href.indexOf('#') == -1)
		{		
			item.addEvent('click', function(event){ 
				hasher.hashPrev = hasher.hash;
				linksHandleItem(item);
				event.preventDefault();
				return false;
				});
		}
		
	});
}


function linksHandleItem(item, href)
{		
	clearInterval(projectPlayer);
	
	base_url = '';
	if (href || !item.href)
	{
		base_url = href;
	}
	else{
		base_url = item.href.replace(host_url, '');
	}
	
	if( base_url && hasher.hashPrev == base_url ) return;
	
	hasher.isSmall = '';
	if( hasher.hashPrev.indexOf('agencja')>0 && hasher.hash.indexOf('agencja')>0 ){
		if(base_url.indexOf('agencja')>0 && $('content-0')){
			hasher.direction = 2;
			hasher.isSmall = '&small=1';
		}
	}
	
	var req = new Request.HTML({
		url: host_url + '/www/page.php', 
		evalResponse: false,
		evalScripts: false,
		method: 'get',
		data: 'uri='+base_url+hasher.isSmall,
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) 
		{	
			hasher.set(base_url);
			//console.log(this.options['data'].indexOf('small=1'));
			
			if( this.options['data'].indexOf('small=1')>0 /*hasher.direction==2 && $('content-0')*/ ){
				var el = new Element('div', { 'id' : 'content-1', 'class' : 'content' });		
				el.set('html', responseHTML);
				
				el.inject($('content-0'), 'after');
				$('content-1').fade('hide');
				$('content-0').fade('out');
				
				$('content-0').destroy();
				$('content-1').set('id', 'content-0');			
				$('content-0').fade('in');
				$exec(javaResponse);
				linksHandleSmallContent();
				
				initOpacity('#content-0 ');
				
				hasher.direction = 1;	
				if (!Browser.Engine.trident) Cufon.refresh();
				else  (function(){Cufon.refresh()}).delay(100);
			}
			else if( hasher.direction==1 ){
				var el = new Element('div', { 'id' : 'scroll-item-page-1', 'class' : 'set-left' });		
				el.set('html', responseHTML);
			
				el.inject($('scroll-clear'), 'before');
				$('scroll-item-page-0').setStyle('width', window.getSize().x + 'px');
						
				pageScroll.toElement('scroll-item-page-1');
				Cufon.refresh();
			}
			else{
				var el = new Element('div', { 'id' : 'scroll-item-page-1', 'class' : 'set-left' });		
				el.set('html', responseHTML);
			
				el.inject($('scroll-item-page-0'), 'before');
				$('scroll-item-page-1').setStyle('width', window.getSize().x + 'px');
				pageScroll.set( window.getSize().x );						
				pageScroll.toElement('scroll-item-page-1');
				
				hasher.direction = 1;
				Cufon.refresh();
			}						
			javaResponse = responseJavaScript;
												
		}
	});	
		
	req.send();
}


var pageScroll;
var pageScroll2;
var javaResponse = '';

function initPage()
{
	initScreen();
	pageScroll2 = new Fx.Scroll('scroll-page', {link: 'chain', transition: Fx.Transitions.Cubic.easeInOut} );
	
	pageScroll = new Fx.Scroll('scroll-page', { wheelStops : false, link: 'chain', transition: Fx.Transitions.Cubic.easeInOut,
		onComplete : function(){
		
			$('scroll-item-page-0').destroy();
			$('scroll-item-page-1').set('id', 'scroll-item-page-0');			
			pageScroll2.set(0, 0);		
			$exec(javaResponse);			
			linksHandleContent();
			initOpacity('');
			
			var scroll = document.getScroll();
			
			var pageSmoothScroll;
			pageSmoothScroll = new Fx.SmoothScroll({  wheelStops: false, duration: 500, onStart : function() { if (!scroll.y) this.cancel(); } });
			pageSmoothScroll.toElement('scroll-page');
			
	} });
	
	linksHandle();	
}

function initScreen()
{
	$('scroll-item-page-0').setStyle('width', window.getSize().x + 'px');
	$('scroll-content-page').setStyle('width', (window.getSize().x * 2) + 'px');
	
	if (window.getSize().x < 1180)
		$(document.body).addClass('screen');
	else
		$(document.body).removeClass('screen');	
	
	window.addEvent('resize', function(){
		
		if ($('scroll-item-page-0'))
		$('scroll-item-page-0').setStyle('width', window.getSize().x + 'px');
		$('scroll-content-page').setStyle('width', (window.getSize().x * 2) + 'px');
		
		if (window.getSize().x < 1180)
			$(document.body).addClass('screen');
		else
			$(document.body).removeClass('screen');
	});
}

function showMovie(href, img)
{
	if (!$('box-movie'))
	{
		$('box-movies').set('html', '')
		var el = new Element('div', { 'id' : 'box-movie' });
		el.inject($('box-movies'));
	}
	
	var flashvars = {
			play: true, menu: true, loop: true,
			file: href,
			image: img, 
			backcolor: 'e7e8e7', frontcolor: '666666', lightcolor: '656d65', screencolor: '000000'
			};
	swfobject.embedSWF(host_url+"/www/player.swf", "box-movie", "400", "340", "10.0.0","expressInstall.swf", flashvars, { wmode : 'transparent' }, {});	
}

var slideshowCounter = 0;
var slideshowInterval = 0;
function slideshowInit()
{
	clearInterval(slideshowInterval);
	
	var slideshowItems = $$('div.main-left-project-entry');	
	var slideshowLen = slideshowItems.length;
		
	var picFx = new Fx.Tween($('main-left-project-entry-'+slideshowCounter), { duration: 1000, onComplete: function(){
		
		$('main-left-project-entry-'+slideshowCounter).setStyle('zIndex', 0);
		if(slideshowLen>1) slideshowInterval = slideshowInit.delay(5000);
		
	} }).start('opacity', 1);	
	
	slideshowCounter++;
	if (slideshowCounter >= slideshowLen) slideshowCounter = 0;	
	
	slideshowItems.each(function(el, index){
		if (index != 'main-left-project-entry-'+slideshowCounter && el.getStyle('opacity') == 1)
		{
			$('main-left-project-entry-'+index).setStyle('zIndex', 1);	
			new Fx.Tween('main-left-project-entry-'+index, { duration: 1000 }).start('opacity', 0);
		}
	});
}

function initOpacity(place)
{
	if (!Browser.Engine.trident)
	{		
	/*
		$$(place+'a img').each(function(el){ 
			el.addEvent('mouseenter', function(){ if (el.getStyle('opacity') == 1) { new Fx.Tween(el, { duration: 200, link : 'chain' }).start('opacity', 0.5); } });
			el.addEvent('mouseleave', function(){ new Fx.Tween(el, { duration: 200, link : 'chain' }).start('opacity', 1); });
		});
	*/	
		
		$$(place+'a').each(function(el){ 
			el.addEvent('mouseenter', function(){if (el.getStyle('opacity') == 1) { new Fx.Tween(el, { duration: 200, link : 'chain' }).start('opacity', 0.5); } } );
			el.addEvent('mouseleave', function(){ new Fx.Tween(el, { duration: 200, link : 'chain' }).start('opacity', 1); });
		});
		
		$$('div.logo a').removeEvents( 'mouseenter' );
		$$('div.logo a').removeEvents( 'mouseleave' );
		
		$$('div.articlelist-project-entry a').removeEvents( 'mouseenter' );
		$$('div.articlelist-project-entry a').removeEvents( 'mouseleave' );
		
		//$$('div.logo a').removeEvent('mouseenter', fadeIn);
		//$$('div.logo a').removeEvent('mouseleave', fadeOut);
	}	
}

function removeOrphans( element ){
	content = $(element).get('html');
	if(content.length > 0) {
	    replaced = content.replace(/(\s)([\S])[\s]+/g, "$1$2&nbsp;");
		$(element).set('html', replaced);
	}
}
	
