$(document).ready(function(){
	//Animated Tabs
	var animSpeed = 300; //animation speed
	var panelWidth = "444px"; //width of open tab panel
	var panel1Height = $("#panel1").height();
	var panel2Height = $("#panel2").height();
	var panelHeight = panel1Height > panel2Height ? panel1Height : panel2Height;
	panelHeight = panelHeight <200 ? 200 : panelHeight;	
	$('.panel').height(panelHeight);
	$("#panel2").width(36);
	$("#details").click(function(){
		if(!$(this).hasClass('selected')){
			$("#panel2").animate({
				width: "36px"
			}, animSpeed);	
			$("#panel1").animate({
				width: panelWidth
			}, animSpeed);			
			$(".selected").removeClass("selected");
			$(this).addClass("selected");
			
		}
	});	
	$("#components").click(function(){
		if(!$(this).hasClass('selected')){
			$("#panel1").animate({
				width: "36px"
			}, animSpeed);
			$("#panel2").animate({
				width: panelWidth
			}, animSpeed);	
			$(".selected").removeClass("selected");
			$(this).addClass("selected");
			
		}
	});	
	//carousel
	$('#mycarousel').jcarousel({
		visible: 4
	});
	//insert outer glow around large image
	$('div.jqzoom').before(
		'<div id=\"imgTopL\" class=\"glow\"></div><div id=\"imgTopM\" class=\"glow\"></div><div id=\"imgTopR\" class=\"glow\"></div><div id=\"imgMidL\" class=\"glow\"></div>'
		).after('<div id=\"imgMidR\" class=\"glow\"></div><div id=\"imgBotL\" class=\"glow\"></div><div id=\"imgBotM\" class=\"glow\"></div><div id=\"imgBotR\" class=\"glow\"></div>');
	//lightbox
	$('.lightbox').lightbox({fitToScreen: true});
	//component popup
	$('a.relatedPart').click(function(){
		$('#popWindow').remove();
		$('#container').append('<div id=\"popWindow\"></div>');
		$('#popWindow').load($(this).attr('href')+' .mainContent',{mung:25},function(){
				//var boxHeight = $('#popWindow').height();
				$('#popWindow').append('<div class=\"closeWindow\"></div>')
				.fadeIn('fast',function(){
					$('.closeWindow').click(function(){
						$('#popWindow').fadeOut('fast',function(){
							$('#popWindow').remove();
						});
					});
				});
			});
		return false;
	});	
	//hide large image until dimensions are known in order to center it
	$('.gallery').loadMainImage();
});
(function($){
	$.fn.loadMainImage = function(){
		$(".imgFader, .glow").hide();
		var newImg = $(this).attr("rev");
		var newAlt = $(this).attr("title");		
		var newZoomImg = $(this).attr("href"); //get the path to the large image (zoomed)
		$(".jqzoom img").attr({
						src: newImg,
						alt: newAlt				  
					});
		imgPreloader = new Image();
		// once image is preloaded, show big image
		imgPreloader.onload=function(){
			newImgWidth = imgPreloader.width;
			newImgHeight = imgPreloader.height;
			$(".jqzoom").css({"width":newImgWidth*1,'height':newImgHeight});
			$('#mainImgContainer').css({
				"width":(newImgWidth*1)+30,
				"margin-left":(410-(newImgWidth*1))/2 ,
				"height":newImgHeight+30
				});
			$('#imgMidL, #imgMidR').css({'height':newImgHeight});
			$('#imgTopM, #imgBotM').css({'width':newImgWidth});
			$(".imgFader").fadeIn("slow",function(){
				$(".jqzoom").jqueryzoom({ 
					xzoom: 480, //zooming div default width(default width value is 200) 
					yzoom: 360, //zooming div default height(default height value is 200) 
					offset: 0, //zooming div default offset(default offset value is 10) 
					position: "right", //zooming div position(default position value is "right")
					lens:1, // lens over the image 1 by default 
					bigimage: newZoomImg																																																																	  						}); 
				$('.glow').show();
			});
			zoomPreloader = new Image();
			zoomPreloader.src = newZoomImg;
		};			
		imgPreloader.src = newImg;
		
		$(this).click(function(){			
			newImg = $(this).attr("rev");
			newAlt = $(this).attr("title");
			newZoomImg = $(this).attr("href"); //get the path to the large image (zoomed)
			newId = $(this).attr('id');
			newId = newId.replace('galleryImg','');
			if(newImg != $(".jqzoom img").attr("src")){ //if image is different, change it
				$(".imgFader, .glow").hide();
				$('.jqzoom').unbind();
				$(".imgFader").fadeOut("slow",function(){
					$('#imgLoader').show();
					$(".jqzoom img").attr({
						src: newImg,
						alt: newAlt				  
					});
					//change the href of the lightbox link
					$('#imglink').attr({ href:newZoomImg,title:newAlt });
					$('#dlLink').attr({ href:'/cantifix/images/download_file/'+newId,title:'Download '+newAlt });
					imgPreloader = new Image();
					// once image is preloaded, show big image
					imgPreloader.onload=function(){
						$('#imgLoader').hide();
						newImgWidth = imgPreloader.width;
						newImgHeight = imgPreloader.height;
						$(".jqzoom").css({"width":newImgWidth*1,'height':newImgHeight});
						$('#mainImgContainer').css({
							"width":(newImgWidth*1)+30,
							"margin-left":(410-(newImgWidth*1))/2 ,
							"height":newImgHeight+30
							});
						$('#imgMidL, #imgMidR').css({'height':newImgHeight});
						$('#imgTopM, #imgBotM').css({'width':newImgWidth});
						$(".imgFader").fadeIn("slow",function(){
							$(".jqzoom").jqueryzoom({ 
								xzoom: 480, //zooming div default width(default width value is 200) 
								yzoom: 360, //zooming div default height(default height value is 200) 
								offset: 0, //zooming div default offset(default offset value is 10) 
								position: "right", //zooming div position(default position value is "right")
								lens:1, // lens over the image 1 by default 
								bigimage: newZoomImg																																																																	  							});
							$('.glow').show();
						});
						zoomPreloader = new Image();
						zoomPreloader.src = newZoomImg;
					};			
					imgPreloader.src = newImg;				
				})			
			}	
			return false;
		});
	}
})(jQuery);
