/**
 * jQuery.Preload - Multifunctional preloader
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 * Date: 3/25/2009
 * @author Ariel Flesler
 * @version 1.0.8
 */
;(function($){var h=$.preload=function(c,d){if(c.split)c=$(c);d=$.extend({},h.defaults,d);var f=$.map(c,function(a){if(!a)return;if(a.split)return d.base+a+d.ext;var b=a.src||a.href;if(typeof d.placeholder=='string'&&a.src)a.src=d.placeholder;if(b&&d.find)b=b.replace(d.find,d.replace);return b||null}),data={loaded:0,failed:0,next:0,done:0,total:f.length};if(!data.total)return finish();var g=$(Array(d.threshold+1).join('<img/>')).load(handler).error(handler).bind('abort',handler).each(fetch);function handler(e){data.element=this;data.found=e.type=='load';data.image=this.src;data.index=this.index;var a=data.original=c[this.index];data[data.found?'loaded':'failed']++;data.done++;if(d.enforceCache)h.cache.push($('<img/>').attr('src',data.image)[0]);if(d.placeholder&&a.src)a.src=data.found?data.image:d.notFound||a.src;if(d.onComplete)d.onComplete(data);if(data.done<data.total)fetch(0,this);else{if(g&&g.unbind)g.unbind('load').unbind('error').unbind('abort');g=null;finish()}};function fetch(i,a,b){if(a.attachEvent&&data.next&&data.next%h.gap==0&&!b){setTimeout(function(){fetch(i,a,1)},0);return!1}if(data.next==data.total)return!1;a.index=data.next;a.src=f[data.next++];if(d.onRequest){data.index=a.index;data.element=a;data.image=a.src;data.original=c[data.next-1];d.onRequest(data)}};function finish(){if(d.onFinish)d.onFinish(data)}};h.gap=14;h.cache=[];h.defaults={threshold:2,base:'',ext:'',replace:''};$.fn.preload=function(a){h(this,a);return this}})(jQuery);

(function($){
	
	// ============================================== //

		// NAV ANIM //

	// ============================================== //	
	
	$.fn.initNavAnim = function(){
		var target = this;
		var ulHeight = target.find('ul').height();
				target.find('ul').css({
			'overflow': 'hidden',
			'height': 0
		});
		//
		target.mouseenter(function(){
			target.find('ul').animate({ height: ulHeight }, 200);
		}).mouseleave(function(){
			target.find('ul').css({'height': 0});
		});
	};
	
	$.fn.initProductZoom = function(){
		// set vars
		var target = this;
		var mainImgLoaded = false;
		var mouseOverProduct = false;
		var mainImgPath = target.find('a').attr('href');
		var loaderPath = "http://www.tinalilienthal.com/images/loader.gif";
		//
		var contentWidth = 770;
		var contentHeight = 990;
		var windowWidth = 300;
		var windowHeight = 386;
		var winOffSet = null;
		var winOffSetLeft = null;
		var winOffSetTop = null;
		//
		var moveValX = null;
		var moveValY = null;
		var xPos = null;
		var yPos = null;
		var scrollImage = false;
		//
		//// Calculate ratios
		//
		calculateVars = function(){
			moveValX = -1*((contentWidth - windowWidth)/windowWidth);
			moveValY = -1*((contentHeight - windowHeight)/windowHeight);
			winOffSet = target.offset();
			winOffSetLeft = winOffSet.left;	
			winOffSetTop = winOffSet.top;
		};
		$(window).bind('resize', function() {
			calculateVars();					  
		});
		calculateVars();
		//
		//// Set Up elements
		//
		target.after('<p>Rollover image to zoom.<\/p>');
		//
		target.append('<div id="image_loader_bg"><\/div><img id="product_viewer_large" src="'+loaderPath+'" alt="" width="'+windowWidth+'" height="'+windowHeight+'"/>');
		$('#image_loader_bg').animate({'opacity':0},0.001);
		$('#product_viewer_large').animate({'opacity':0},0.001);
		//
		$.preload([loaderPath], {
			onComplete: function(data) {
				if(data.found){
					loadMainImage();
					setImageRollOver();
				}else{
					alert('image not found'+loaderPath);
				};
			}
		});
		//
		//
		loadMainImage = function(){
			$.preload([mainImgPath], {
				onComplete: function(data) {
					if(data.found){
						mainImgLoaded = true;
						initLightBox();
						if(mouseOverProduct){
							// if mouse is already over the product fade in the new image and zoom in
							$('#image_loader_bg').animate({'opacity':0},200);
							$('#product_viewer_large').animate({'opacity':0},200).attr('src', mainImgPath);
							zoomInProduct();
						}else{
							// just switch in the image ready to fade up on rollover
							$('#product_viewer_large').attr('src', mainImgPath);
						};
					}else{
						alert('image not found '+mainImgPath);
					};
				}
			});
		};
		//
		//
		setImageRollOver = function(){
			target.mousemove(function(e){	
				xPos = e.pageX-winOffSetLeft;		
				yPos = e.pageY-winOffSetTop;
				if(scrollImage){
					$('#product_viewer_large').css({'left': (xPos*moveValX), 'top': (yPos*moveValY) });
					//$('#product_viewer').scrollLeft(xPos*moveValX);
				};
			}).mouseenter(function(){
				zoomInProduct();					   
			}).mouseleave(function(){
				zoomOutProduct();					   
			});
			/*.click(function(){
				// Click
				if(mainImgLoaded){
					showLargeImage();
				};
				return false;
			});*/
		};
		//
		//
		zoomInProduct=function(){
			mouseOverProduct = true;
			if(!mainImgLoaded){
				$('#image_loader_bg').animate({'opacity':0.7},200);
				$('#product_viewer_large').animate({'opacity':1},200);
			}else{
				//
				$('#product_viewer_large').stop().animate({'opacity':1},0.01, function(){
					$(this).animate({'width':contentWidth, 'height':contentHeight, 'left': (xPos*moveValX), 'top': (yPos*moveValY)}, 150, function(){
						scrollImage = true;
					});
				});
			};
		};
		//
		zoomOutProduct=function(){
			mouseOverProduct = false;
			scrollImage = false;
			if(!mainImgLoaded){
				$('#image_loader_bg').animate({'opacity':0},200);
				$('#product_viewer_large').animate({'opacity':0},200);
			}else{
				$('#product_viewer_large').stop().animate({'width': windowWidth, 'height': windowHeight, 'left': 0, 'top': 0}, 300, function(){
					$(this).animate({'opacity':0},0.01)																												 
				});
			};
		};
		//
		initLightBox = function(){
			$('body').append('<div id="jquery-overlay"><\/div><div id="jquery-lightbox"><div id="lightbox-container"><div id="lightbox-container-content"><\/div><\/div><\/div>');
			//
			$('#jquery-overlay').css({
				opacity:			0,
				display:			'none'
			});
			//
			$('#lightbox-container').css({
				opacity:			0,
				display:			'none'
			}).find('#lightbox-container-content').append('<img src="'+mainImgPath+'" alt="" title="Click to close" width="770" height="990"\/>');
			//
			// Assigning click events in elements to close overlay
			$('#jquery-overlay,#jquery-lightbox,#lightbox-container-content').click(function() {
				closeLargeImage();
				return false;	
			});
		};
		//
		showLargeImage = function(){
			$('#jquery-overlay').show().css({
				height:				1030
			});						
			//
			$('#jquery-overlay').show().animate({opacity: 0.9}, 250, function(){						 
				$('#lightbox-container').show().animate({opacity: 0.9}, 250);												  
			});
		};
		//
		closeLargeImage = function() {
			$('#lightbox-container').css({
				opacity:			0,
				display:			'none'
			});
			$('#jquery-overlay').animate({opacity: 0}, 250, function(){
				$('#jquery-overlay').hide(); 
			});
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'visible' });
		};
	
	};
	
	// ============================================== //

		// PRESS LIGHT BOX ROLL OVER //

	// ============================================== //
	
	$.fn.initPressViewer = function(){
		//
		_getPageSize = function() {var xScroll, yScroll;if (window.innerHeight && window.scrollMaxY) {xScroll = window.innerWidth + window.scrollMaxX;yScroll = window.innerHeight + window.scrollMaxY;} else if (document.body.scrollHeight > document.body.offsetHeight){xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} else {xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;};var windowWidth, windowHeight;if (self.innerHeight) {if(document.documentElement.clientWidth){windowWidth = document.documentElement.clientWidth;} else {windowWidth = self.innerWidth;};windowHeight = self.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) {windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;} else if (document.body) {windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;};if(yScroll < windowHeight){pageHeight = windowHeight;} else { pageHeight = yScroll;};if(xScroll < windowWidth){	pageWidth = xScroll;		} else {pageWidth = windowWidth;};arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize;};
		//
		_getPageScroll = function() {var xScroll, yScroll;if (self.pageYOffset) {yScroll = self.pageYOffset;xScroll = self.pageXOffset;} else if (document.documentElement && document.documentElement.scrollTop) {yScroll = document.documentElement.scrollTop;xScroll = document.documentElement.scrollLeft;} else if (document.body) {yScroll = document.body.scrollTop;xScroll = document.body.scrollLeft;};arrayPageScroll = new Array(xScroll,yScroll);return arrayPageScroll;};
		// set vars
		var target = this;
		//
		$('body').append('<div id="jquery-overlay"><\/div><div id="jquery-lightbox"><div id="lightbox-container" style="width:510px; height:auto;"><div id="lightbox-container-content" style="width:490px; height:auto;"><\/div><\/div><\/div>');
		//
		$('#jquery-overlay').css({
			opacity:			0,
			display:			'none'
		});
		//
		$('#lightbox-container').css({
			opacity:			0,
			display:			'none'
		}).find('#lightbox-container-content').append('<img src="" alt="" id="lightbox_img" title="Click to close" width="490" height="630"\/><p><\/p>');
		//
		// Assigning click events in elements to close overlay
		$('#jquery-overlay,#jquery-lightbox,#lightbox-container-content').click(function() {
			closeLargeImage();
			return false;	
		});
		//
		target.find('a').bind('click', function(){
			showLargeImage($(this).attr('href'),$(this).attr('desc'));
			return false;							   									   
		});
		//
		showLargeImage = function(imgPath, pressText){
			// Get page sizes
			var arrPageSizes = _getPageSize();
			// Style overlay and show it
			$('#jquery-overlay').css({
				width:		arrPageSizes[0],
				height:		arrPageSizes[1]
			});
			// Get page scroll
			var arrPageScroll = _getPageScroll();
			// Calculate top and left offset for the jquery-lightbox div object and show it
			$('#jquery-lightbox').css({
				top:	arrPageScroll[1],
				left:	arrPageScroll[0]
			});
			//
			$('#lightbox_img').parent().find('p').html(pressText);
			//alert(pressText);
			$('#lightbox_img').attr({src : imgPath});
			//
			$('#jquery-overlay').show();
			//
			$('#jquery-overlay').show().animate({opacity: 0.9}, 250, function(){						 
				$('#lightbox-container').show().animate({opacity: 0.9}, 250);												  
			});
		};
		
		closeLargeImage = function() {
			$('#lightbox-container').css({
				opacity:			0,
				display:			'none'
			});
			$('#jquery-overlay').animate({opacity: 0}, 250, function(){
				$('#jquery-overlay').hide();
				$('#lightbox_img').attr({src : ''});
			});
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'visible' });
		};
		
		//
		
	
	};	
	// ============================================== //

		// COLLECTION ROLL OVER //

	// ============================================== //	
	
	
	$.fn.collectionRollOver = function(){
		var target = this;
		
		$(this).find('div.content_grid_item').each(function(i){
			$(this).find('a:first').hover(function() {
				$(this).next().find('a').addClass('collectionTextHover');
			}, function() {
				$(this).next().find('a').removeClass('collectionTextHover');
			});
			//
			$(this).find('p a').hover(function() {
				$(this).parent().prev().find('img').addClass('collectionImgHover');
			}, function() {
				$(this).parent().prev().find('img').removeClass('collectionImgHover');
			});
		});
	};
	
})(jQuery);
//
$(document).ready(function(){
	$('#header ul li#nav_collections').initNavAnim();
	
	//
	if($('#product_viewer').length) {
		$('#product_viewer').initProductZoom();
	};
	//
	if($('#content_grid').length) {
		$('#content_grid').collectionRollOver();
	};
	//
	if($('div.press_grid').length) {
		$('div.press_grid').initPressViewer();
	};
});
