

var siteNav = {

	_isInit: false,
	_onLoad: false,
	currentPage: null,
	currentPageName: '',
	config: [],
	
	init: function(options)
	{
		this.currentPage = MAIN_URL+MAIN_GOTO+'/';
		this.currentPageName = '';
		
		var k;
		for( k in options)
		{
			if(isset(this.config[k]))
				this.config[k] = options[k];
		}
		
		this._isInit = true;
		
		if(screen.width && !empty(screen.width) && screen.width < 1200)
		{
			if(!getCookie('krktsSiteScreenAdv'))
				msgBox.showMsg(dialogMsg['screenAdv'], null, 5);
			
			setCookie('krktsSiteScreenAdv', 'sent', 2);
		}
	},
	
	isInit: function()
	{
		return this._isInit;
	},
	
	makeLink: function(link)
	{
		if(link.charAt(link.length-1) != '/')
			link += '/';
		
		return link;
	},
	
	getPage: function(page, name)
	{
		if(this._onLoad)
			return;
		
		this.currentPage = this.makeLink(page);
		this.currentPageName = name;
		$('langSelectorFr').href = this.currentPage+'fr/';
		$('langSelectorEn').href = this.currentPage+'en/';
		
		$('waitBox').innerHTML = '		<a href="'+this.currentPage+'">\
			<img src="'+IMG_URL+'loader.gif" />\
				<br />\
					<br />\
			<img src="'+IMG_URL+'wait_'+MAIN_LANG+'.jpg" />\
		</a>';
		
		//$('waitBox').style.display = 'block';
		//$('mainContents').style.display = 'none';
		Element.show('waitBox');
		Element.hide('mainContents');
		
		this._onLoad = true;
		this.setPageTitle();
		
		new Ajax.Updater(
						'mainContents', 
						this.currentPage+MAIN_LANG+'/true/', 
						{
							evalScripts: true,
							onLoading: function() {},
							onFailure: function() 
							{ 
								siteNav.goTo(siteNav.currentPage); 
							},
							onSuccess: function(transport)
							{
								siteNav.loadImgFromHtml(transport.responseText);
								$('mainContents').innerHTML = transport.responseText;
								
								switch(siteNav.currentPageName)
								{
									case 'photos':
										var timeOut = 3000; break;
									
									case '':
									case 'home':
									case 'sounds':
										var timeOut = 1000;
									/**/
									default:
										var timeOut = 500;
								}
								
								setTimeout( function() { 
									new Effect.Fade('waitBox', { duration: 0.3}); 
									new Effect.Appear('mainContents', { duration: 1.0, from: 0.0, to: 1.0 });
									siteNav._onLoad = false;
								},
								timeOut);
								
								transport.responseText.evalScripts();
							}
						}
					);
	},
	
	getLang: function(lang)
	{
		//$('waitBox').innerHTML = '<a href="'+this.currentPage+'"><img src="'+IMG_URL+'loader.gif" /><br /><br /><img src="'+IMG_URL+'wait_'+MAIN_LANG+'.jpg" /></a>'; 
		//$('waitBox').style.display = 'block';
		//$('mainContents').style.display = 'none';
		//this.setPageTitle();
		
		if(!lang)
			lang = 'fr';
		if(lang != 'fr')
			lang = 'en';
		
		MAIN_LANG = lang;
		preloadImages('wait_'+MAIN_LANG+'.jpg', 'wait2_'+MAIN_LANG+'.jpg');
		
		new Ajax.Updater(
						'mainContents', 
						this.currentPage+MAIN_LANG+'/true/', 
						{
							evalScripts: true,
							onLoading: function()  { },
							onFailure: function() 
							{ 
								siteNav.goTo(siteNav.currentPage+MAIN_LANG); 
							},
							onSuccess: function(transport)
							{
								$('headerImg').src = IMG_URL+'title_'+MAIN_LANG+'.jpg';
								$('mainContents').innerHTML = transport.responseText;
							}
						}
					);
	},
	
	sendMsg: function()
	{
		
		var subject = $F('subject');
		if(subject.empty())
		{
			$('subject').focus();
			$('subject').style.backgroundColor = '#ffffcc';
			msgBox.errorMsg(dialogMsg['emptyField']);
			return;
		}
		else $('subject').style.backgroundColor = '#fffff1';
		
		var email = $F('email');
		if(email.empty())
		{
			$('email').focus();
			$('email').style.backgroundColor = '#ffffcc';
			msgBox.errorMsg(dialogMsg['emptyField']);
			return;
		}
		else $('email').style.backgroundColor = '#fffff1';
		
		var msg = $F('msg');
		if(msg.empty())
		{
			$('msg').focus();
			$('msg').style.backgroundColor = '#ffffcc';
			msgBox.errorMsg(dialogMsg['emptyField']);
			return;
		}
		else $('msg').style.backgroundColor = '#fffff1';
		
		var skey = $F('skey');
		if(skey.empty())
		{
			$('skey').focus();
			$('skey').style.backgroundColor = '#ffffcc';
			msgBox.errorMsg(dialogMsg['emptyField']);
			return;
		}
		else $('skey').style.backgroundColor = '#fffff1';
		
		new Ajax.Request(
						MAIN_URL+'contact/send/'+MAIN_LANG+'/true/', 
						{
							method: 'post',
							parameters: 'skey='+escape(skey)+'&subject='+escape(subject)+'&email='+escape(email)+'&msg='+escape(msg),
							onLoading: function() 
							{ 
								msgBox.display('<img src="'+IMG_URL+'loading.gif" /><img src="'+IMG_URL+'wait2_'+MAIN_LANG+'.jpg" />');
							},
							onComplete: function(transport)
							{
								eval(transport.responseText);
							}
						}
					);
	},
	
	setPageTitle: function()
	{
		window.document.title = '-[ Tribute to Kriktus ';
		
		if(!empty(this.currentPageName))
			window.document.title += '- '+ this.currentPageName.ucFirst() + ' ';
				else
					window.document.title += '- Rest In Peace ';
		
		window.document.title += ']-';
	},
	
	loadImgFromHtml: function(html)
	{
		var fragment = IMG_URL+'(.[^"]*)';
		var matchAll = new RegExp(fragment, 'img');
		if(!document.preloadedImg) 
				document.preloadedImg = new Array();
		
		(html.match(matchAll) || []).map(function(img) {
			if (!document.preloadedImg[img])
			{
				document.preloadedImg[img] = new Image;
				document.preloadedImg[img].src = img;
			}
		});
	},
	
	goTo: function(page)
	{
		window.location.href = page;
	}
};

var photosNav = {
	
	slideshow: null,
	currentBlock: 0,
	
	switchBlock: function(id)
	{
		var block;
		if(!$('kriktusPhotosBlock'+id))
			id = 0;
		
		var lastBlock = this.currentBlock;
		this.currentBlock = id;
		
		if(lastBlock)
		{
			$('photosBlockLink1'+lastBlock).style.color = '#3b5998';
			$('photosBlockLink2'+lastBlock).style.color = '#3b5998';
		}
		$('photosBlockLink1'+this.currentBlock).style.color = '#AAACB9';
		$('photosBlockLink2'+this.currentBlock).style.color = '#AAACB9';
		
		if(this.currentBlock != lastBlock)
		{
			Element.hide('kriktusPhotosBlock'+lastBlock); 
			Element.show('kriktusPhotosBlock'+this.currentBlock);
		}
	},
	
	getBlock: function(id)
	{
		var block;
		if(!$('kriktusPhotosBlock'+id))
			id = 0;
		
		var lastBlock = this.currentBlock;
		this.currentBlock = id;
		
		if(!lastBlock)
			lastBlock = 0;
		
		$('photosBlockLink1'+lastBlock).style.color = '#3b5998';
		$('photosBlockLink2'+lastBlock).style.color = '#3b5998';
		$('photosBlockLink1'+this.currentBlock).style.color = '#AAACB9';
		$('photosBlockLink2'+this.currentBlock).style.color = '#AAACB9';
		
		if(this.currentBlock != lastBlock)
		{
			new Effect.Fade('kriktusPhotosBlock'+lastBlock, { duration: 0.9}); 
			new Effect.Appear('kriktusPhotosBlock'+this.currentBlock, { duration: 1.0, from: 0.0, to: 1.0 });
			
		}
	},
	
	startSlideShow: function()
	{
		myLightbox.startSlideShow($('kriktusPhoto'+this.currentBlock+'0'));
	}
};

var bottomAnim = {
	
	timer: null,
	block: '',
	list: [],
	current: 0,
	
	start: function(block, imgs, delay)
	{
		if(!delay)
			delay = 5;
		
		this.block = block;
		this.current = 0;
		this.list = imgs;
		this.delay = delay*1000;
		
		if(false == (block = $(this.block)))
			return;
		
		block.innerHTML = '';
		var top = block.style.top;
		var left = block.style.left;
		var i, animImage;
		
		for(i=0;i<this.list.length;i++)
		{
			animImage = document.createElement("img");
			animImage.setAttribute('src', IMG_URL+this.list[i]);
			animImage.setAttribute('id', 'bottomAnimImg'+i);
			animImage.setAttribute('style', 'position:absolute;');
			animImage.style.position = 'absolute';
			animImage.style.top = '0px';
			animImage.style.left = '0px';
			animImage.style.display = (i == 0) ? 'block' : 'none';
			block.appendChild(animImage);
		}
		
		this.timer = setTimeout( function() { bottomAnim.change(); }, this.delay);
	},
	
	change: function()
	{
		if($('bottomAnimImg'+this.current))
		{
			this.stop();
			var last = this.current;
			this.current++;
			
			if(!this.list[this.current])
				this.current = 0;
			
			/*if(userBrowser.isIe())
			{
				Element.hide('bottomAnimImg'+last); 
				Element.show('bottomAnimImg'+this.current);
			}
			else
			{*/
				new Effect.Fade('bottomAnimImg'+last, { duration: 0.5});
				new Effect.Appear('bottomAnimImg'+this.current, { duration: 1.0, from: 0.0, to: 1.0 });
			//}
			this.timer = setTimeout( function() { bottomAnim.change(); }, this.delay);
		}
		else this.stop();
	},
	
	stop: function()
	{
		clearTimeout(this.timer);
	}
};

