// JavaScript Document

var scrollCatalogueMoving = false;
var splitPage = false;



function menuSwitcher(){
	
	$("#menu_selection").css({width:0, height:0});
	if($menu.width() == 600){
		
		var tLeft = 210;
		var tTop = 96;
		var tWidth = (!splitPage) ? $page.width() - tLeft - 120 : $cont.width();
		var tHeight = $page.height() - tTop - 90;
		
		var fadeInCB = false;
		
		$("#menu_toggle").addClass("left");
		
		if($contB.is(":visible")) {
			$contB.fadeOut(300);
			fadeInCB = true;			
		}
		
		$cont.animate({left: tLeft, top: tTop, width: tWidth, height: tHeight },300,'linear',function(){
			$menu.children("li.divider").each(function(){ $(this).hide() });
			$menu.stop().animate({width: 150, height: 400}, 400, 'linear', function(){
				$menu.children("li").each(function(){ 
					if(!$(this).hasClass("divider")) { $(this).css({paddingBottom: 8 }) }
				});  
				var sel = $menu.children("li.selected");
				if($(sel).offset()) $("#menu_selection").stop().animate({ left: $(sel).offset().left, top: $(sel).offset().top, width: $(sel).width()+10, height: $(sel).height() }, 250);
				$contB.css({top: tTop, left: tLeft+tWidth+40, height:tHeight}).css({right: 35, width: 'auto'});
				if(fadeInCB) $contB.fadeIn(300);
			});
			
		});
		
	} else {
		var tLeft = 35;
		var tTop = 156;
		var tWidth = (!splitPage) ? $page.width() - tLeft - 120 : $cont.width();
		var tHeight = $page.height() - tTop - 90;
		
		$("#menu_toggle").removeClass("left");
		
		
		if($contB.is(":visible")) {
			$contB.fadeOut(300);
			fadeInCB = true;			
		}

		$cont.animate({left: tLeft, top: tTop, width: tWidth, height: tHeight },300,'linear',function(){
			$menu.stop().animate({width: 600, height: 60, paddingBottom: 0}, 400, 'linear',function(){
				$menu.children("li").each(function(){ 
					if(!$(this).hasClass("divider")) { $(this).css({paddingBottom: 0 }) }
					else { $(this).show() }
				}); 
				var sel = $menu.children("li.selected");
				if($(sel).offset()) $("#menu_selection").stop().animate({ left: $(sel).offset().left, top: $(sel).offset().top, width: $(sel).width()+10, height: $(sel).height() }, 250);
				$contB.css({top: tTop, left: tLeft+tWidth+40, height:tHeight}).css({right: 35, width: 'auto'});
				if(fadeInCB) $contB.fadeIn(300);
			});
			
		});
	}
}


function menuClickAnim(obj, dontAnimate){
	if(dontAnimate){
		$("#menu_selection").stop().css({ left: $(obj).offset().left, top: $(obj).offset().top, width: $(obj).width()+10, height: $(obj).height() });
	} else {
		$("#menu_selection").stop().animate({ left: $(obj).offset().left, top: $(obj).offset().top, width: $(obj).width()+10, height: $(obj).height() }, 250);
	}
	$menu.children("li.selected").each(function(){$(this).removeClass("selected")});
	$(obj).addClass("selected");
}

function backgroundSet(){
	if($(window).width() > 1280 ){
		$page.removeClass("lowres");
	} else {
		if(!$page.hasClass("lowres")) 
			$page.addClass("lowres");
	}
}

function positioner(){	
	var tTop = $cont.css('top');
	tTop = parseInt(tTop.substring(0,tTop.length-2));
	var tWidth = (!splitPage ) ? $page.width() - $cont.offset().left - 65 : $cont.width();
	var tHeight = $page.height() - tTop - 90 ;
	
	$cont.css({top: tTop, width: tWidth });
	$("#containment, #containment_b").css({ height: tHeight });

	if($contB.hasClass('fullScreen')){
		$contB.css({top: 10, left: 10, width: $page.width()-50, right: 'auto', height: $page.height()-50});
	}else {
		$contB.css({ left: tWidth + $cont.offset().left + 40 });
		$contB.css({ top: tTop, width:  $page.width() - $contB.offset().left - 65  })
		$contB.css({ right: 35 })
	}
	
	
	if($("#currentCatalogue").val()) { resizeCatalogueViewElems(); }
	if($("#currentCollection").val()) { resizeCollectionViewElems(); }
	
	if(isWebKit || isFirefox){
		$("#wrapper, #wrapper_b").addClass("grab");
	}	
	$page_title.css({ left: $menu.width() });
	setTimeout(function(){
		if(iScrA) iScrA.refresh(); 
		if(iScrB) iScrB.refresh(); 
	}, 300);
	
}

var predictedImgWidth = 0;

function resizeCatalogueViewElems(){
	$contB =  $contB;
	if($contB.get(0)) {
		var setHeight  = $contB.height();
		var setWidth  = $contB.width();
		$imgL = $("#imgL");
		$imgR = $("#imgR");
		if($imgL.get(0)) {
			var tgHeight = 0;
			var tgWidth = 0;
			natHeight = $imgL.get(0).naturalHeight;
			natWidth = $imgL.get(0).naturalWidth;
			
			if($contB.hasClass('fullScreen')){
				tgWidth = Math.round( setWidth / 2 - 10);
				tgHeight = Math.round((natHeight/natWidth) * tgWidth);
				if(tgHeight > setHeight){
					tgHeight = setHeight;
					tgWidth = Math.round((natWidth/natHeight) * tgHeight);
				}

			} else {
				tgHeight = setHeight;
				tgWidth = Math.round((natWidth/natHeight) * tgHeight);
			}
			$("#imgR_loading").css({paddingTop: Math.round((setHeight-tgHeight)/2) })
			$("#imgL, #imgR, #imgL_loading, #imgR_loading").css({ width: tgWidth, height: tgHeight })
			$("#img_display, #wrapper_b").height(setHeight);
			$("#img_display, #wrapper_b").width(setWidth);
		}
	}
}

function resizeCollectionViewElems(){
	$contB =  $contB;
	if($contB.get(0)) {
		var setHeight  = $contB.height();
		var setWidth  = $contB.width();
		$imgL = $("#imgL");
		if($imgL.get(0)) {
			var tgHeight = 0;
			var tgWidth = 0;
			natHeight = $imgL.get(0).naturalHeight;
			natWidth = $imgL.get(0).naturalWidth;
			
			if($contB.hasClass('fullScreen')){
				tgWidth = Math.round( setWidth - 10);
				tgHeight = Math.round((natHeight/natWidth) * tgWidth);
				if(tgHeight > setHeight){
					tgHeight = setHeight;
					tgWidth = Math.round((natWidth/natHeight) * tgHeight);
				}

			} else {
				tgHeight = setHeight;
				tgWidth = Math.round((natWidth/natHeight) * tgHeight);
			}
			$("#img_display, #wrapper_b").height(setHeight);
			$("#img_display, #wrapper_b").width(setWidth);
			$("#imgL,  #imgL_loading").css({ width: tgWidth, height: tgHeight })
		}
	}
}

function initCatalogueView(imgSingle, pg, pgLeft, pgRight, total, id){
	var prevPg;
	var nextPg;
	
	pg = parseInt(pg);
	pgLeft = parseInt(pgLeft);
	pgRight = parseInt(pgRight);
	total = parseInt(total);
	
	$("#imgL_loading, #imgR_loading").removeClass("loaded");
	if(predictedImgWidth > 0) $("#imgL_loading, #imgR_loading").width(predictedImgWidth);
	
	if(isIE){ $("#imgFull").draggable(); } 
	else { iScrB = new iScroll('wrapper_b', { useTransition:true, hScrollbar: false, vScrollbar: false }); }
	
	$("#gotoNext, #gotoPrev, #zoomOut, #imgL ,#imgR, #imgL_loading, #imgR_loading, #imgFull").disableSelection();

	
	$("#imgL").load(function(){
		resizeCatalogueViewElems();
		$("#imgL_loading").addClass("loaded");
		$this = $(this);
		
		if($this.width()*2 > $("#img_display").width() ){
			if($contB.hasClass('fullScreen')) {
				prevPg = (pgLeft-1 >= 1) ? pgLeft-1 : total;
				nextPg = (pgRight) ? ((pgRight+1 <= total) ? pgRight+1 : 1) : pgLeft+1;
			} else {
				$(this).attr('src', imgSingle);
				nextPg =  (pg+1 > total) ? 1 : pg+1;
				prevPg =  (pg-1 > 0) ? pg-1  : total;
			}
		} else {
			prevPg = (pgLeft-1 >= 1) ? pgLeft-1 : total;
			nextPg = (pgRight) ? ((pgRight+1 <= total) ? pgRight+1 : 1) : pgLeft+1;
		}
		
		$this.fadeIn(null, function(){ 
			predictedImgWidth = $this.width(); 
			$("#imgL_loading").width(predictedImgWidth);
		});
	});
	
	$("#imgR").load(function(){
		$("#imgR_loading").addClass("loaded");
		$this = $(this);
		$this.fadeIn(null, function(){ 
			$("#imgR_loading").width($this.width());
		});
	});
	
	
	$("#imgL").click(function(){
		if(!scrollCatalogueMoving){
			$img = $("#imgL");
			imgElem = $img.get(0);
			$("#img_display").hide();
			$("#imgFull").attr('src', $(this).attr('src')).show();
			$("#wrapper_b, #zoomOut").show();
			$("#imgFull").load(function(){
				$(this).stop().animate({height: imgElem.naturalHeight }, null, null, function(){ 
					if(!isIE) iScrB.refresh();
				} );
			});
			
			$("#imgFull").css({cursor: "move"});
		}		
	});
	
	$("#imgR").click(function(){
		if(!scrollCatalogueMoving){
			$img = $("#imgR");
			imgElem = $img.get(0);
			$("#img_display").hide();
			$("#imgFull").attr('src', $(this).attr('src')).show();
			$("#wrapper_b, #zoomOut").show();
			$("#imgFull").load(function(){
				$(this).stop().animate({height: imgElem.naturalHeight }, null, null, function(){ 
					if(!isIE) iScrB.refresh();
				});
			});
			
			$("#imgFull").css({cursor: "move"});
		}
		
	});


	$("#zoomOut").click(function(){ 
		$("#imgFull").stop().animate({height: $("#wrapper_b").height() }, 'fast','linear',function(){
			$("#wrapper_b, #zoomOut, #imgFull").hide();
			$("#img_display").fadeIn();
			positioner();
		});
	});		
	
	$img =  $("#zoomFull").children("img");
	if($contB.hasClass('fullScreen')){ $img.attr('src','images/icon_zoomout.png'); } 
	else { $img.attr('src','images/icon_zoomin.png'); }
	
	$("#zoomFull").click(function(){ 
		$img = $(this).children("img");
		if($contB.hasClass('fullScreen')){
			$img.attr('src','images/icon_zoomin.png');
			$contB.removeClass('fullScreen');
			positioner();
		} else {
			$img.attr('src','images/icon_zoomout.png');
			$contB.addClass('fullScreen');
			positioner();
		}
	});

	$("#gotoPrev").click(function(){ location.href='#catalogue/'+id+'/'+prevPg; });
	$("#gotoNext").click(function(){ location.href='#catalogue/'+id+'/'+nextPg; });
	
	
}

function initCollectionView(pg, total, id){
	var prevPg;
	var nextPg;
	
	pg = parseInt(pg);
	total = parseInt(total);
	
	$("#imgL_loading").removeClass("loaded");
	if(predictedImgWidth > 0) $("#imgL_loading").width(predictedImgWidth);
	
	if(isIE){ $("#imgFull").draggable(); } 
	else { iScrB = new iScroll('wrapper_b', { useTransition:true, hScrollbar: false, vScrollbar: false }); }
	
	$("#gotoNext, #gotoPrev, #zoomOut, #img_display, #wrapper_b").disableSelection();

	
	$("#imgL").load(function(){
		resizeCollectionViewElems();
		$("#imgL_loading").addClass("loaded");
		$this = $(this);

		nextPg =  (pg+1 > total) ? 1 : pg+1;
		prevPg =  (pg-1 > 0) ? pg-1  : total;
			
		$this.fadeIn(null, function(){ 
			predictedImgWidth = $this.width(); 
			$("#imgL_loading").width(predictedImgWidth);
		});
	});
	
	$("#imgL").click(function(){
		$img = $("#imgL");
		imgElem = $img.get(0);
		$("#img_display").hide();
		$("#imgFull").attr('src', $(this).attr('src')).show();
		$("#wrapper_b, #zoomOut").show();
		$("#imgFull").load(function(){
			$(this).stop().animate({height: imgElem.naturalHeight }, null, null, function(){ 
				if(!isIE) iScrB.refresh();
			} );
		});
		
		$("#imgFull").css({cursor: "move"});
	});
	

	$("#zoomOut").click(function(){ 
		$("#imgFull").stop().animate({height: $("#wrapper_b").height() }, 'fast','linear',function(){
			$("#wrapper_b, #zoomOut, #imgFull").hide();
			$("#img_display").fadeIn();
			positioner();
		});
	});		
	
	$img =  $("#zoomFull").children("img");
	if($contB.hasClass('fullScreen')){ $img.attr('src','images/icon_zoomout.png'); } 
	else { $img.attr('src','images/icon_zoomin.png'); }
	
	$("#zoomFull").click(function(){ 
		$img = $(this).children("img");
		if($contB.hasClass('fullScreen')){
			$img.attr('src','images/icon_zoomin.png');
			$contB.removeClass('fullScreen');
			positioner();
		} else {
			$img.attr('src','images/icon_zoomout.png');
			$contB.addClass('fullScreen');
			positioner();
		}
	});

	$("#gotoPrev").click(function(){ location.href='#collection/'+id+'/'+prevPg; });
	$("#gotoNext").click(function(){ location.href='#collection/'+id+'/'+nextPg; });
	
}

function initNormalPage(contWidth) {
	
	splitPage = (contWidth) ? true : false;
	if(contWidth) $cont.width(contWidth);
	else $cont.width(300);
	
	positioner();
	$contB.hide();
	$("#containment_b #content").html('');
	
	if(isWebKit || isFirefox){
		$("#jscroller").css({height: 'auto'})
		setTimeout( function() { 
			iScrA = new iScroll('wrapper', {
				useTransition:true,
				scrollbarClass: 'myScrollbar',
				hideScrollbar: true,
				fadeScrollbar:true,
				onScrollMove: function() { scrollMoving = true; $("#wrapper").addClass('grabbing'); } ,
				onScrollEnd : function() {  setTimeout( function(){ scrollMoving = false; $("#wrapper").removeClass('grabbing'); }, 200) }
			}); 
			
			positioner();
		}, 300);
	} else {
		$("#jscroller").removeClass("scroller");
		setTimeout(function(){
			$("#jscroller").jScrollPane({hideFocus: true, autoReinitialise: true, mouseWheelSpeed: 50});
			positioner();
		}, 300);
	}
	
	$("a").click(function(){
		if(scrollMoving) return false;
	});
	
	
}

