/*
 * layout.js
 * @Version: 26/06/2008
 */


/* ---------------------------------------------------------
 * Library 
 */
var Library = function(j){
	
	function contentToggle(i){
		
		/*init Toggle state by cookies 
		 *j.cookie() from jquery.cookie.js
		 */
		if(j.cookie('toggle') == 'up'){
			j('.content').hide(); 
			j('.content-toggle a').addClass('show').html('Show intro');
		}
		if(j.cookie('toggle') == 'down'){
			j('.content').show(); 
			j('.content-toggle a').removeClass('show').html('Hide intro');
		}
		j('.content-toggle a').toggle(function()
		{	
			obj = j(this);
			if (j.cookie('toggle') == null){
				if (i == 0){
					j('.content').slideUp('90'); obj.addClass('show').html('Show intro');
					j.cookie('toggle', 'up'); return false;
				}
				if (i == 1){
					
					j('.content').slideDown('90'); obj.removeClass('show').html('Hide intro'); 
					j.cookie('toggle', 'down');return false;
				}
			}
			if(j.cookie('toggle') == 'down'){
				j('.content').slideUp('90'); obj.addClass('show').html('Show intro');
				j.cookie('toggle', 'up');return false;
				
			}
			if(j.cookie('toggle') == 'up'){
				j('.content').slideDown('90'); obj.removeClass('show').html('Hide intro');  
				j.cookie('toggle', 'down');return false;
			}
			
		},function(){
			obj = j(this);
			if (j.cookie('toggle') == null){
				if (i == 1){
					j('.content').slideUp('90'); obj.addClass('show').html('Show intro');
					j.cookie('toggle', 'up'); return false;
				}
				if (i == 0){
					j('.content').slideDown('90'); obj.removeClass('show').html('Hide intro'); 
					j.cookie('toggle', 'down');return false;
				}
			}
			if(j.cookie('toggle') == 'down'){
				j('.content').slideUp('90'); obj.addClass('show').html('Show intro');
				j.cookie('toggle', 'up');return false;
			}
			if(j.cookie('toggle') == 'up'){
				j('.content').slideDown('90'); obj.removeClass('show').html('Hide intro'); 
				j.cookie('toggle', 'down');return false;
			}
		});
	};
	
	function mapSearch(){
		/* Enlarge appearing on map thumbnail -> IE7 has issues with fadeIn() -> use hide() to replace */
		j('a.map').show();
		j('a.map').find('span').hide();
		var id = window.setTimeout(function(){
			if (!j.browser.msie){
				j('a.map').find('span').fadeIn('slow');
			}
			else{
				j('a.map').find('span').show();
			}
			
		}, 3000);
		j('a.map').hover(function(){
			j(this).find('span').show();
		},function(){ 
			j(this).find('span').hide();
		});
	};

	return {
		init : function(){
			//load default & public variables & methods
		},

		/* public method */
		getContentToggle : function(i){
			contentToggle(i);
		},
		getMapSearch : function(){
			mapSearch();	
		}
		
	};
	
}(jQuery);


/* ---------------------------------------------------------
 * CoverFlow -> for cover flow effect.
 */
var CoverFlow = function(j){
	//--
	var indexPage;
	var temp = 0;
	function moveSlider(){
		var moveFlag = false;
		var chgeFlag = false;
		j(document).mousemove(function(e){
            if (!moveFlag) {
				moveFlag = false;
				chgeFlag = false;
				return;
			}
			else{
				j(this).css('cursor','e-resize');
			}
			
		});
		
			j('div.knob').mousedown(function (e) {
				moveFlag = true;
				chgeFlag = true;
				j(this).css('cursor','e-resize');
			});
			j(document).mouseup(function () {

				moveFlag = false;
				if (chgeFlag && indexPage != temp){
					changeContent(indexPage);
				}
				j('div.knob').css('cursor','pointer');
				temp = indexPage;
				
				
			});
			j('div.slider').mousedown(function(){
				if (moveFlag == false){
					changeContent(indexPage);
				}
				
			});
			
		
	};
	
	//--
	function multiPage(index){
		var imgCount = 0;
		var index, i;
		arrayPic = new Array();
//		for (i = 0; i < 6 ; i++){
//			arrayPic[i] = new Image();
//			arrayPic[i].src = 'media/maps/16_Wytfliet_a-0'+(i+1)+'.jpg';			
//		}
		
		if (j('div#demo').children().is('div#multi-page')){

			var obj = j('div#MooFlow').children('div').eq(parseInt(j('div#multi-page').attr('title'))+1).children('div');
			// check out the function has multi-page or not
			if (index == j('div#multi-page').attr('title')){
				var pageNumber = 1;

				//set style
				if (!j.browser.msie){
					j('div#enlarge').css({height:'136px',top: '60px'});
					j('div#enlarge span#enlarge-icon').css({height:'136px'});
				}
				else{
					
					j('div#enlarge').css({height:'136px'});
					j('span#cover').css({height:'136px',top:'20px'});
				}
				
				j('div#multi-page > ul, div#multi-page > span,').appendTo(obj);
				obj.children('ul').fadeIn();
				multiPageHover('item-right',-40);
				multiPageHover('item-left',-20);
				j('li.item-left').click(function(){
			
					if (pageNumber > 1 && pageNumber <= 6){
						pageNumber--;
						j(this).html('pg.' + (pageNumber-1));
						j('li.item-right').html('pg.' + (pageNumber+1));
						obj.find('img').attr('src',arrayPic[pageNumber-1].src);
						if (pageNumber == 1){
							j(this).html('&nbsp;');
						}
						
					}
					
				});
				j('li.item-right').click(function(){
					if (pageNumber >= 1 && pageNumber <= 5){
						pageNumber++;
						j(this).html('pg.' + (pageNumber+1));
						j('li.item-left').html('pg.' + (pageNumber-1));
						//alert(pageNumber-1);
						obj.find('img').attr('src',arrayPic[pageNumber-1].src);
						if (pageNumber == 6){
							j(this).html('&nbsp;');
						}
						//j('item-right').unbind();
						//j('item-left').unbind();
						//multiPageHover('item-left', -20);
						//multiPageHover('item-right', -40);
					}
				});
				
			}
			else{
				obj.children('ul').hide();
				if (!j.browser.msie){
					j('div#enlarge').css({height:'159px',top: '39px'});
					j('div#enlarge span#enlarge-icon').css({height:'159px'});
				}
				else{
					j('div#enlarge').css({height:'159px'});
					j('span#cover').css({height:'159px',top:'-30px'});
				}
			}
		}
		else{
			
		}
		
	};

	function multiPageHover(direction,px){
		j('li.'+direction).hover(function(){
			
			if (direction == 'item-right'){
				j('ul.page-bar').next('span.page-curl').css({background:'url(images/curl_2.png)',left:'148px'});
			}
			if (direction == 'item-left'){
				j('ul.page-bar').next('span.page-curl').css({background:'url(images/curl_1.png)',left:'1px'});
			}
			j('ul.page-bar').next('span.page-curl').show();
			j('ul.page-bar').css('background-position', '0px '+px+'px');
				
		},function(){
			j('ul.page-bar').next('span.page-curl').hide();
			j('ul.page-bar').css('background-position', '0px 0px');
				
		})
	};
	//--
	function page(index,total){

		j('div.knobleft').html(index + ' of ' + total);
		//console.info(j('div.knob').left)
	}
	//--
	function moveIt(index){
		//control enlarge icon
		var index;
		indexPage = index;
		var obj = j('div#enlarge');
		obj.find('span').eq(1).hide();
		window.setTimeout(function(){
			if (!j.browser.msie){
				obj.find('span').eq(1).fadeIn();
			}
			else{
				obj.find('span').eq(1).show();
			}
		},3000);
		obj.hover(function(){
			if (!j.browser.msie){
				obj.find('span').eq(1).fadeIn();
			}
			else{
				obj.find('span').eq(1).show();
			}
		},function(){
			if (!j.browser.msie){
				obj.find('span').eq(1).fadeOut();
			}
			else{
				obj.find('span').eq(1).hide();
			}
		});
		obj.find('span').eq(1).hover(function(){
			if (!j.browser.msie){
				obj.find('span').eq(1).fadeIn();
			}
			else{
				obj.find('span').eq(1).show();
			}
		},function(){
			if (!j.browser.msie){
				obj.find('span').eq(1).fadeOut();
			}
			else{
				obj.find('span').eq(1).hide();
			}
		});
		//obj.find('a').attr('');//control zommify
		
	};
	//
	function changeContent(index){
		//change content
		j.getJSON("media/data/json-data.json", function(json){
			window.setTimeout(function(){
				var obj2 = j('#slider-contents #slider-1');
				obj2.children('h2').html(json.items[index].title).hide().fadeIn('70');
				obj2.find('p.author').html(json.items[index].author).hide().fadeIn('70');
				obj2.find('p.meta').html(json.items[index].meta).hide().fadeIn('70');
				obj2.find('span.description').html(json.items[index].content).hide().fadeIn('70');
				j('div#enlarge a').attr('href',json.items[index].url);
			},0);
			
			try {urchinTracker('/exhibits/onthemap/ajax/changecontent');return false;}
			catch(err){return(false);}
		   
		});
	}
	//
	function isMSIE(){
		var brBoolean = j.browser.msie ? true : false;
		return brBoolean;
	}
	return {
		init : function(){
			//moveSlider();
		},
		p_moveIt : function(index){
			moveIt(index);
		},
		p_page : function(index,total){
			page(index,total);
		},
		p_changeContent : function(index){
			changeContent(index);
		},
		p_isMSIE : function(){
			return isMSIE;
		},
		p_multiPage : function(index){
			multiPage(index);
		},
		p_moveSlider : function(){
			moveSlider();
		}

	}
}(jQuery);

//execute when window load
jQuery(document).ready(function() {
	Library.init();
});


