/**
 * Frankie Morello - JQuery scripts
 */
jQuery(function($) {

	/**
	 * Mobile Safari has problems since body.scrollTop returns 0.
	 */
	$.iShit = (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1) || (navigator.platform.indexOf("iPad") != -1);
	if ($.iShit) {
		(function($){
			$.fx.prototype.curOriginal = $.fx.prototype.cur;
			$.fx.prototype.cur = function( force ) {
				if ($(this.elem).is("body") && this.prop == "scrollTop") return $(window).scrollTop();
				return this.curOriginal(force);
			}
		})(jQuery);
	}

	/**
	 * Simple plugin to preload images
	 */
	(function($) {
		var cache = [];
		/*
		 * Arguments are image paths relative to the current page.
		 */
		$.preLoadImages = function() {
			var args_len = arguments.length;
			for (var i = args_len; i--;) {
				var cacheImage = document.createElement('img');
				cacheImage.src = arguments[i];
				cache.push(cacheImage);
			}
		}
	})(jQuery)

	/**
	 * Open externals in new tab/window
	 */
	$(window).delegate('.external, a[rel="external"]', 'click', function(){
		$(this).attr('target', '_blank');
	});

	/**
	 * Photogallery for collections
	 */
	(function($) {
		$.fullCollectionGallery = function(photo) {
			/*
			 * Clean
			 */
			$("#parallaxe").remove();

			/*
			 * Preload
			 */
			photoURL = $(photo).attr('href');
			$.preLoadImages(photoURL);

			/*
			 * Initial left position is used to determine if this was called by a collection or a backstage.
			 */
			$container = $(".container");
			containerScrollLeft = false;
			containerIsBackstage = false;
			if ($container.is(":visible")) {
				containerScrollLeft = $('#collectionContainer').scrollLeft();
				containerIsBackstage = (0 != containerScrollLeft);
			}

			/*
			 * Hide page content
			 */
			$(".container").hide();

			/*
			 * Create new block elements
			 */
			pnPositioning = ($.iShit) ? 'absolute' : 'fixed';
			parallaxeHTML = '<div id="parallaxe" class="fromCollection">\
<img id="parallaxePrev" style="position: ' + pnPositioning + '" />\
<img id="parallaxeZoom" src="' + photoURL + '" />\
<img id="parallaxeNext" style="position: ' + pnPositioning + '" />\
<a class="close" href="#" style="position: absolute; top: 0; right: 0; background: black; color: white; padding: 10px;">CLICK ANYWHERE TO CLOSE</a>\
</div>';
			$(parallaxeHTML).appendTo($('body'));

			/*
			 * Get references
			 */
			$parallaxe = $("#parallaxe");

			/*
			 * Save data now that the zoom has been prepared.
			 */
			if (false !== containerScrollLeft) {
				$('body').data("containerScrollLeft", containerScrollLeft);
				$('body').data("containerIsBackstage", containerIsBackstage);
			}

			/*
			 * Map mouse movement to scroll
			 */
			$parallaxe.mousemove(function(e) {
				w = $(window);
				wH = w.height();
				wW = w.width();
				pY =  e.pageY;
				pX =  e.pageX;
				i = $("#parallaxeZoom");
				iH = i.height();
				iW = i.width();
				sT = w.scrollTop();
				sL = w.scrollLeft();

				deltaY = (pY - parseInt(sT)) * (iH / wH);
				deltaX = (pX - parseInt(sL)) * (iW / wW);
				ratio = (iW / iH) > (wW / wH);

				if (iH < iW && ratio) {
					if (iW > wW) {
						w.scrollLeft(deltaX);
					}
				} else {
					if (iH > wH) {
						w.scrollTop(deltaY);
					}
				}
			});

			/*
			 * When clicked revert page content to either collection or backstage
			 */
			$parallaxe.click(function(e) {
				e.preventDefault();

				$("#parallaxe").fadeOut("fast", function() {
					$(this).remove();
					$(".container").show();

					if ($.iShit) {
						$('#collectionContainer').scrollLeft($('body').data("containerScrollLeft"));
						$('body').scrollTop($('#main-header').height());
					} else {
						$('#collectionContainer').animate({scrollLeft: $('body').data("containerScrollLeft")});
						$('html, body').animate({scrollTop: $('#main-header').height()});
					}
				});
			});

			/*
			 * Make position adjustments and link prev/next
			 */
			$("#parallaxeZoom").load(function() {
				img = $(this);
				imgW = img.width();
				imgH = img.height();
				w = $(window);
				wH = w.height();
				wW = w.width();
				ratio = (imgW / imgH) > (wW / wH);

				if (imgH < imgW && ratio) {
					// Horizontal movement
					img.css({height: "100%"});
					$("#parallaxe").css({
						width: img.width(),
						height: "100%"
					})
				} else {
					// Vertical movement
					img.css({width: "100%"});
					$("#parallaxe").css({
						width: "100%"
					})
				}

				/*
				 * Retrieve prev/next to use
				 */
				if ($('body').data("containerIsBackstage")) {
					prevThumb = $("#"+$("a.backstage-prev").attr("href"));
					nextThumb = $("#"+$("a.backstage-next").attr("href"));
				} else {
					prevThumb = $("#"+$("a.collection-prev").attr("href"));
					nextThumb = $("#"+$("a.collection-next").attr("href"));
				}
				$.preLoadImages(prevThumb.data("original"), nextThumb.data("original"));

				/*
				 * Previous
				 */
				$("#parallaxePrev").attr("src", prevThumb.attr("src"))
					.click(function (e) {
						e.preventDefault();
						prevThumb.parent().click();
						$("#" + (($('body').data("containerIsBackstage")) ? "backstage" : "collection") + "PhotoBlock .main-photo").click();
				});

				/*
				 * Next
				 */
				$("#parallaxeNext").attr("src", nextThumb.attr("src"))
					.click(function (e) {
						e.preventDefault();
						nextThumb.parent().click();
						$("#" + (($('body').data("containerIsBackstage")) ? "backstage" : "collection") + "PhotoBlock .main-photo").click();
				});

				/*
				 * Done
				 */
				window.setTimeout(function() {$("#parallaxeZoom").fadeIn();}, 100);
			})
		}
	})(jQuery);

	/**
	 * Photogallery for pages
	 */
	(function($) {
		$.fullPageGallery = function(photo) {
			
			/*
			 * Clean
			 */
			$("#parallaxe").remove();

			/*
			 * Preload
			 */
			photoURL = $(photo).attr('href');
			$.preLoadImages(photoURL);

			/*
			 * Hide page content
			 */
			$(".post-container, .container").hide();

			/*
			 * Create new block elements
			 */
			pnPositioning = ($.iShit) ? 'absolute' : 'fixed';
			parallaxeHTML = '<div id="parallaxe" class="fromPage">\
<img id="parallaxePrev" style="position: ' + pnPositioning + '" src="'+BASE_URL+'images/gallery-back.gif" />\
<img id="parallaxeZoom" src="' + photoURL + '" />\
<img id="parallaxeNext" style="position: ' + pnPositioning + '" src="'+BASE_URL+'images/gallery-next.gif" />\
<a class="close" href="#" style="position: absolute; top: 0; right: 0; background: black; color: white; padding: 10px;">CLICK ANYWHERE TO CLOSE</a>\
</div>';
			$(parallaxeHTML).appendTo($('body'));

			/*
			 * Get references
			 */
			$parallaxe = $("#parallaxe");

			/*
			 * Map mouse movement to scroll
			 */
			$parallaxe.mousemove(function(e) {
				w = $(window);
				wH = w.height();
				wW = w.width();
				pY =  e.pageY;
				pX =  e.pageX;
				i = $("#parallaxeZoom");
				iH = i.height();
				iW = i.width();
				sT = w.scrollTop();
				sL = w.scrollLeft();

				deltaY = (pY - parseInt(sT)) * (iH / wH);
				deltaX = (pX - parseInt(sL)) * (iW / wW);
				ratio = (iW / iH) > (wW / wH);

				if (iH < iW && ratio) {
					if (iW > wW) {
						w.scrollLeft(deltaX);
					}
				} else {
					if (iH > wH) {
						w.scrollTop(deltaY);
					}
				}
			});

			/*
			 * When clicked revert page content to either collection or backstage
			 */
			$parallaxe.click(function(e) {
				e.preventDefault();

				$("#parallaxe").fadeOut("fast", function() {
					$(this).remove();
					$(".post-container, .container").show();
				});
			});

			/*
			 * Make position adjustments and link prev/next
			 */
			$("#parallaxeZoom").load(function() {
				img = $(this);
				imgW = img.width();
				imgH = img.height();
				w = $(window);
				wH = w.height();
				wW = w.width();
				ratio = (imgW / imgH) > (wW / wH);

				if (imgH < imgW && ratio) {
					// Horizontal movement
					img.css({height: "100%"});
					$("#parallaxe").css({
						width: img.width(),
						height: "100%"
					})
				} else {
					// Vertical movement
					img.css({width: "100%"});
					$("#parallaxe").css({
						width: "100%"
					})
				}

				/*
				 * Retrieve prev/next to use
				 */
				$curr = $("a[href='"+img.attr("src")+"']");
				rel = $curr.attr("rel");
				$prev = $curr.prev();
				$next = $curr.next();
				if ($prev.attr("href")) {
					$("#parallaxePrev")
						.attr("href", $prev.attr("href"))
						.click(function(e) {
							e.preventDefault();
							$.fullPageGallery(this);
						});
				} else {
					$("#parallaxePrev").hide();
				}
				if ($next.attr("href")) {
					$("#parallaxeNext")
						.attr("href", $next.attr("href"))
						.click(function(e) {
							e.preventDefault();
							$.fullPageGallery(this);
						});
				} else {
					$("#parallaxeNext").hide();
				}

				/*
				 * Done
				 */
				window.setTimeout(function() {$("#parallaxeZoom").fadeIn();}, 100);
			})
		}
	})(jQuery);

function resetVideo(divId, close) {
  if (typeof(divId)=="string") {divId=document.getElementById(divId);hiddenId=document.getElementById('hiddenvideo');}
    org=divId.innerHTML;
    divId.innerHTML='';
	if(close != '1'){
		hiddenId.value=org;
	}else{
		if(hiddenId.value!=''){
			divId.innerHTML=hiddenId.value;
		}else{
			divId.innerHTML=org;
		}
	}
  return false;
}

	$('#videopopup').click(function(){
		if($('.videopopup').css('display')=='none'){
			resetVideo('videodiv', 1);
			$('#main-nav-container').animate({marginTop: '-52px'});
			$('.videopopup').slideToggle('slow');
			var html = "<div class='fl exit'><a class='main-nav_link active' id='exit' href='#'>exit</a></div>";
			if($.browser.version=='8.0'){
				var temp = $('#main-nav').html();
				$('#main-nav').html(temp+html);
			}else{
				$('#main-nav').append(html).animate({width: 'slideup'}, 1000);
			}
		}else{
			$('#main-nav-container').animate({marginTop: '411px'}, 1000);
			$('.videopopup').slideToggle('slow');
			$('.exit').remove().animate({width: 'toggle'});	
			resetVideo('videodiv', 0);
		}
	});
	$("body").delegate('#exit', 'click', function(e){
			e.preventDefault();
			$("#main-nav-container").animate({marginTop: "411px"}, 1000);
			$(".videopopup").slideToggle("slow");
			$(".exit").remove().animate({width: "toggle"});	
			resetVideo('videodiv', 0);
		}
	);
	/*
	 * Select gallery type by window width
	 *
	 * a[rel^='headvideo'] => YouTube Video in HP
	 * a[href^='#youtube'] => YouTube Video in collection/backstage
	 *
	 *
	 */
	
	if ($(window).width() < 1600) {
		$("body").delegate("#collectionContainer a[rel^='photoGallery']", "click", function (e) {
			e.preventDefault();
			$.fullCollectionGallery(this);
			//alert("collection GALLERY");
		});
		$("body").delegate(".post-container a[rel^='photoGallery']", "click", function (e) {
			e.preventDefault();
			$.fullPageGallery(this);
			//alert("post GALLERY");
		});
	} else {
		$("a[rel^='photoGallery']").prettyPhoto();
	}
	$("a[rel^='headvideo'], a[href^='#youtube'], a[rel^='advGallery']").prettyPhoto();

	/**
	 * Collection & Backstage show all
	 */
	$('#collectionPhotoBlock, #backstagePhotoBlock').delegate('.showall', 'click', function(e) {
		e.preventDefault();

		$("#newsletter_slide, #contacts_slide, storelocator_slide").hide();
		$('html, body').animate({scrollTop: $('#main-header').height() + $('#collectionThumbsBlock').position()['top'] + 75});
	});
	
	/**
	 * Collection & Backstage thumbs
	 */
	
	
	$("#collectionPhotoBlock .post-gallery-link, #backstagePhotoBlock .post-gallery-link").each(function(){
		$(this).addClass(function() {
			if($(this).index() ==0) $(this).addClass('main-photo');
  			return "item-" + $(this).index();
		});	
	});
	
	$("#collectionThumbsBlock .thumb-photo, #collectionBackstage .thumb-photo").each(function(){
		$(this).addClass(function() {
			if($(this).index() ==0) $(this).addClass('main-photo');
  			return "item-" + $(this).index();
		});	
	});
	
	$('#collectionThumbsBlock, #collectionBackstage').delegate('a', 'click', function(e) {
		e.preventDefault();
		$this = $(this).children('img');
		$block = $(this).parent('#collectionThumbsBlock, #collectionBackstage').prev();

		$('.currentindex', $block).html($this.attr('data-index'));
		$prev = $('.prev', $block).attr('href', $this.data('prev'));
		$next = $('.next', $block).attr('href', $this.data('next'));

		$("#newsletter_slide, #contacts_slide, #storelocator_slide").hide();
		$("html, body").animate({scrollTop: $('#main-header').height()});
		
		$("#collectionPhotoBlock .post-gallery-link, #backstagePhotoBlock .post-gallery-link").hide();
		$("#collectionThumbsBlock .thumb-photo, #collectionBackstage .thumb-photo").removeClass('main-photo');
		$("#collectionPhotoBlock .post-gallery-link, #backstagePhotoBlock .post-gallery-link").removeClass('main-photo');
		
		
		n_thumb = $this.parent().attr('class');
		n_thumb = '.' + n_thumb.substr(12,15);
		$(n_thumb).addClass('main-photo');
		
		
		$('.main-photo', $block)
			.hide()
			.html('<img />')
			.children('img')
				.attr('title', $this.attr('alt'))
				.attr('alt', $this.attr('alt'))
				.attr('src', $this.data('photo'))
				.attr('class', 'border-image')
				.load(function() {
					$photo = $(this);
					$parent = $photo.parent();
					$parent.show();

					if ($photo.width() > $photo.height()) {
						//$(".main-photo").empty();
						//var html = "<div class=\"clipout\" style=\"border-style: solid;   border-color: black;   border-width: 10px;\"><div class=\"clipin\" id='clipin'><img class='clip-img' src='" + $photo.attr('src') + "'></div></div>";
						//$(".main-photo").append(html);
						//$("#clipin").append('<img />');
						/*$photo.css({
							width: "570px",
							position: "absolute",
							clip: "rect(0px,570px,330px,0px)"
						});*/
						$('#backstagePhotoBlock .main-photo img').css('height','340px');
					}
					$photo.hide().fadeIn(500);
				})
				.show()
			.end()
			.attr('href', $this.data('original'));

		$.each(['a', 'b', 'c'], function(index, detail) {
			$('.detail-' + detail, $block).hide();
			if ($this.data('detail_' + detail)) {
				$('.detail-' + detail, $block)
					.hide()
					.html('<img />')
					.children('img')
						.load(function() {
							$(this).parent().fadeIn(500);
						})
						.attr('title', $this.attr('alt'))
						.attr('alt', $this.attr('alt'))
						.attr('src', $this.data('detail_' + detail))
						.attr('class', 'border-image')
						.show()
					.end()
					.attr('href', $this.data('detail_' + detail + '_src'));
			}
		});

		$('.fbshare', $block).attr('href', $('.fbshare', $block).data('url') + $this.data('index'));
		$('.twittershare', $block).attr('href', $('.twittershare', $block).data('url') + $this.data('index'));
	});

	/**
	 * Collection & Backstage prev/next
	 */
	$('#collectionPhotoBlock, #backstagePhotoBlock').delegate('.prev, .next', 'click', function(e) {
		e.preventDefault();

		$thumb = $('#' + $(this).attr("href")).click();
	});

	/**
	 * Collection & Backstage switch
	 */
	$('#collectionPageContainer').delegate('.backstage-link', 'click', function(e){
		e.preventDefault();
		
		$("#collectionPhotoBlock .post-gallery-link, #backstagePhotoBlock .post-gallery-link").hide();
		$("#collectionThumbsBlock .thumb-photo, #collectionBackstage .thumb-photo").removeClass('main-photo');
		$("#collectionPhotoBlock .post-gallery-link, #backstagePhotoBlock .post-gallery-link").removeClass('main-photo');
		
		$('.item-0').show();
		$('.item-0').addClass('main-photo');
		
		$("#newsletter_slide, #contacts_slide, #storelocator_slide").hide();
		if ($.iShit) {
			$('#collectionContainer').scrollLeft($(window).width());
			$('body').scrollTop($('#main-header').height());
		} else {
			$('#collectionContainer').animate({
				scrollLeft: $(window).width()
			}, 800, 'swing');
			$('html, body').animate({scrollTop: $('#main-header').height()});
		}
	});
	$('#backstagePageContainer').delegate('.collection-link', 'click', function(e){
		e.preventDefault();
		
		$("#collectionPhotoBlock .post-gallery-link, #backstagePhotoBlock .post-gallery-link").hide();
		$("#collectionThumbsBlock .thumb-photo, #collectionBackstage .thumb-photo").removeClass('main-photo');
		$("#collectionPhotoBlock .post-gallery-link, #backstagePhotoBlock .post-gallery-link").removeClass('main-photo');
		
		$('.item-0').show();
		$('.item-0').addClass('main-photo');
		
		$("#newsletter_slide, #contacts_slide, #storelocator_slide").hide();
		if ($.iShit) {
			$('#collectionContainer').scrollLeft(0);
			$('body').scrollTop($('#main-header').height());
		} else {
			$('#collectionContainer').animate({
				scrollLeft: 0
			}, 800, 'swing');
			$('html, body').animate({scrollTop: $('#main-header').height()});
		}
	});
	$(window).bind('resize', function(e){
		delta = $(window).width();
		$('#collectionPageContainer')
			.width(delta)

		$('#backstagePageContainer')
			.width(delta)
			.css('position', 'absolute')
			.css('top', '0px')
			.css('right', '-' + delta + 'px')
			.css('visibility', 'visible');

		if ($('#collectionContainer').scrollLeft() > 0) {
			$('#collectionContainer').animate({
				scrollLeft: delta
			}, 800, 'swing');
		}
	});

	/**
	 * Collection and backstage press release
	 */
	$('#main-content').delegate('.press-release','click',function(e){
		e.preventDefault();
		$("#newsletter_slide, #contacts_slide, #storelocator_slide").hide();

		if ($.iShit) {
			$('body').scrollTop($('#main-header').height());
		} else {
			$('html, body').animate({scrollTop: $('#main-header').height()});
		}
		pressarea = $("#press-area-container");
		pressarea
			.appendTo('body')
			.css({
			    'width':$(window).width(),
			    'height':$('body').height()
			})
			.children('.press-area-data')
			    .css({
					'top': $("#main-header").height() + 20,
					'left': ($(window).width() - 650) / 2
			    })
			.end()
			.delegate('.press-area-bg','click',function(){
				$(pressarea).fadeOut();
			});
		if ($.iShit) {
			pressarea.show();
		} else {
			pressarea.fadeIn();
		}
	});

	/**
	 * Storelocator, Contact and Newsletter signup slider
	 */
	$('body').delegate('.contact_link', 'click', function(){
		$item = $(this);
		$item.data('done', false);
		if ($.iShit) {
			$('html, body').scrollTop(0);
			$("#newsletter_slide").hide();
			$("#storelocator_slide").hide();
			$("#contacts_slide").toggle();
		} else {
			$('html, body').animate({
				scrollTop: 0
			}, 300, function() {
				if ($item.data('done') != true) {
					if ($("#newsletter_slide").is(':visible')) {
						$("#newsletter_slide").slideUp(300, function() {
							$("#contacts_slide").slideDown(300);
						});
					} else {
						if ($("#storelocator_slide").is(':visible')) {
							$("#storelocator_slide").slideUp(300, function() {
								$("#contacts_slide").slideDown(300);
							});
						}
						 else {
							$("#contacts_slide").slideToggle(300);
						}
					}
					$item.data('done', true);
				}
			});
		}
	});
	$('body').delegate('.newsletter_link', 'click', function(){
		$item = $(this);
		$item.data('done', false);
		if ($.iShit) {
			$('html, body').scrollTop(0);
			$("#contacts_slide").hide();
			$("#storelocator_slide").hide();
			$("#newsletter_slide").toggle();
		} else {
			$('html, body').animate({
				scrollTop: 0
			}, 300, function() {
				if ($item.data('done') != true) {
					if ($("#contacts_slide").is(':visible')) {
						$("#contacts_slide").slideUp(300, function() {
							$("#newsletter_slide").slideDown(300);
						});
					} else {
						if ($("#storelocator_slide").is(':visible')) {
							$("#storelocator_slide").slideUp(300, function() {
								$("#newsletter_slide").slideDown(300);
							});
						}
						 else {
							$("#newsletter_slide").slideToggle(300);
						}
					}
					$item.data('done', true);
				}
			});
		}
	});
	
$('body').delegate('.storelocator_link', 'click', function(){
		$item = $(this);
		$item.data('done', false);
		if ($.iShit) {
			$('html, body').scrollTop(0);
			$("#contacts_slide").hide();
			$("#newsletter_slide").hide();
			$("#storelocator_slide").toggle();
		} else {
			$('html, body').animate({
				scrollTop: 0
			}, 300, function() {
				if ($item.data('done') != true) {
					if ($("#contacts_slide").is(':visible')) {
						$("#contacts_slide").slideUp(300, function() {
							$("#storelocator_slide").slideDown(300);
						});
					} else {
						if ($("#newsletter_slide").is(':visible')) {
							$("#newsletter_slide").slideUp(300, function() {
								$("#storelocator_slide").slideDown(300);
							});
						}
						 else {
							$("#storelocator_slide").slideToggle(300);
						}
					}
					$item.data('done', true);
				}
			});
		}
	});
	

	/***
	 * ADV OVER
	 */
	$('.adv-container, .gallery-photos').delegate('a', 'mouseenter', function(e) {
		$(this).append('<span class="adv-over"></span>');
	}).delegate('a','mouseleave',function(e){
		$(this).children('.adv-over').remove();
	});

/*
 * Close JQuery namespace
 */
});
