/*

FERNANDO JORGE

CSS
http://leofavre.com/

WORDPRESS
http://bonacode.com/

DESIGN
http://mariajoudina.co.uk/

*/


$(document).ready(function(){
	if(area == 'home'){		
		check_home_slides();
		update_media_size();
	}
	if(area == 'collections' || area == 'press'){
		check_pdf();
		check_video();
		updateCollectionsPress();	
		set_thumbnail_arrows();
		set_img_arrows();
	}
	if(area == 'news'){
		start_colorbox();
	}
	setCollectionsMenu();
});

$(window).load(function(){
	updateStage();
});


$(window).resize(function(){
	updateStage();
});

function updateStage(){
	if(area == 'home'){
		update_media_size();
	}
	if(area == 'collections' || area == 'press'){
		updateCollectionsPress();
	}	
}


function updateCollectionsPress(){
	correct_gallery_size();
	update_media_size();
	add_or_remove_thumbnail_arrows();
	$(".thumbnails").css('visibility', 'visible');
}

function setCollectionsMenu(){
	$("#sub-menu").hide();
	$("#collections-menu").click(function(evt){
		$("#sub-menu").show();
		evt.preventDefault();
	});
	$("#navigation").mouseleave(function(evt){
		$("#sub-menu").hide();
		evt.preventDefault();
	});
	$("#about-menu").mouseenter(function(evt){
		$("#sub-menu").hide();
		evt.preventDefault();
	});
	$("#press-menu").mouseenter(function(evt){
		$("#sub-menu").hide();
		evt.preventDefault();
	});	
}

function setTooltip(){	
	$("#gallery img").tooltip({ 
	       track: true, 
	       delay: 0, 
	       showURL: false, 
	       fade: 250,
	       bodyHandler: function() { 
				if(ln == 'pt'){
					return "clique para ampliar";
				}else{
					return "click to enlarge";
				}	            
	       }
	   });
}

function setTooltipClose(){	
	$("#gallery img").tooltip({  
	       track: true, 
	       delay: 0, 
	       showURL: false, 
	       fade: 250,
	       bodyHandler: function() { 
	           	if(ln == 'pt'){
					return "clique para fechar";
				}else{
					return "click to close";
				}
	       }
	   });
}

/* LAYOUT */

function add_or_remove_thumbnail_arrows(){
	if( thumbnails_width()>=$('#footer .inner').width()-5 ){
		// add arrows
		$('#footer').addClass('overset');
		$('.thumbnails').css('width', thumbnails_width()+5+'px');
	} else {
		// remove arrows
		$('#footer').removeClass('overset');
		$('.thumbnails').css('width', '100%');
	}
}

function thumbnails_width(){
	var i = 1;
	var thumbs_width = 0;
	var max_thumbs = $('.thumbnails a').length;

	$('.thumbnails a').each(function(){
		thumbs_width += $(this).width();
		thumbs_width = (i<max_thumbs) ? thumbs_width+7 : thumbs_width;
		i++;
	});
	return thumbs_width;
}

function set_thumbnail_arrows(){
	$("#prev-arrow").click(function(){
		pos_atual = $('#thumbnails').position().left;
		nova = pos_atual + parseInt($('#footer .inner').width()/2);
		if(nova > 0){
			nova = 0;
		}
		$('#thumbnails').animate({ left: nova }, 500);
	});
	$("#next-arrow").click(function(){
		pos_atual = $('#thumbnails').position().left;
		nova = (pos_atual + parseInt($('#footer .inner').width()/2)) * -1;
		limite = parseInt(thumbnails_width() - $('#arrows').width() - 40) * -1;
		if(nova < limite){
			nova = limite;
		}
		$('#thumbnails').animate({ left: nova }, 500);
	});
}

function set_img_arrows(){
	$("#prev-img-arrow").click(function(){
		change_img(-1);
	});
	$("#next-img-arrow").click(function(){
		change_img(1);
	});
}


function correct_gallery_size(){
	var top = parseInt( $('#gallery .inner').css('top') );
	if(img_zoom == false){
		var bottom = $('body').hasClass('home') ? 70 : 130;
	}else if(img_zoom == true){
		var bottom = 60;
	}
	var window_height = parseInt( $(window).height() );
	var gallery_height = window_height-top-bottom;
	$('#gallery .inner').css('height', gallery_height+'px');
	$('.prev-wrapper').css('height', gallery_height+'px');
	$('.next-wrapper').css('height', gallery_height+'px');
}



function image_done(){
	if(img_zoom == false){
		setTooltip();
	}else if(img_zoom == true){
		setTooltipClose();
	}
	updateStage();
}

function toogleZoom(){
	if(img_zoom == false){
		img_zoom = true;
		$("#footer").hide();
		$("body").addClass("super-gallery");
		setTooltipClose();
	}else if(img_zoom == true){
		img_zoom = false;
		$("#footer").show();
		$("body").removeClass("super-gallery");
		setTooltip();
	}
	updateStage();
}

/* MEDIA */

function check_home_slides(){
	var links = $(".inner a");
	if(links.length > 1){
		slide_atual = 0;
		layer_atual = 0;
		slides = Array();
		links.each(function(index){
			slides[index] = $(this).attr('href');
			$(this).remove();
		});
		loadHomeSlide();
	}else{
		var video = $("#gallery .inner").find('iframe');
		var video_src = video.attr('src');
		if(video_src != undefined){
			video.remove();
			var vimeocf = "?title=0&amp;byline=0&amp;portrait=0&amp;color=181818&amp;autoplay=1&amp;loop=1";
			var content = '<div id="vimeo"><iframe src="'+ video_src +''+ vimeocf +'" width="100%" height="100%" frameborder="0"></iframe></div>';
			$('#gallery #inner').html(content);
			updateStage();
		}
	}
		
}

function loadHomeSlide(){
	if(layer_atual == 0){
		target = $('#inner');
	}else if(layer_atual == 1){
		target = $('#lower');
	}
	target.html("");
	src = slides[slide_atual];
	var content = '<img id="image-'+layer_atual+'" src="'+src+'" alt="" />';
	target.html(content);	
	$('#image-'+layer_atual).hide();
	$('#image-'+layer_atual).load(function () {
		$(this).fadeIn("regular", function(){
			fadeOldSlide();
		});
	});
}

function changeSlide(){
	if(slide_atual < (slides.length -1)){
		slide_atual += 1;
	}else{
		slide_atual = 0;
	}
	loadHomeSlide();
}

function fadeOldSlide(){
	if(layer_atual == 0){
		layer_atual = 1;
	}else if(layer_atual == 1){
		layer_atual = 0;
	}
	$('#image-'+layer_atual).fadeOut("regular");
	setTimeout('changeSlide()', 3000);
}


function check_pdf(){
	var pdf = $("a[href*=.pdf]:first");
	if(pdf != undefined){
		$("#pdf-span").html("<a href='"+ pdf.attr("href") +"' target='_blank'>Download Lookbook PDF<a/>");
		pdf.parent().remove();
	}
}

function check_video(){
	var video = $(".thumbnails").find('iframe');
	var video_src = video.attr('src');
	if(video_src != undefined){
		video = video.parent();
		video.remove();
		partes = video_src.split("/")
		video_id = partes[partes.length -1];		
		$.getJSON('http://www.vimeo.com/api/v2/video/'+ video_id +'.json?callback=?', function(data){
			var thumb = data[0].thumbnail_medium;
			thumb = "<a href='"+ video_id +"'><img id='vimeo-thumb' src='"+ thumb +"' alt='' style='height:78px;width:auto;' /></a>";
			$(".thumbnails").prepend(thumb);
			$("#vimeo-thumb").load(function(){
				updateStage();
			});
			capture_clicks();
		});		
	}else{
		capture_clicks();
	}	
	$(".thumbnails").css('visibility', 'hidden');
}

function capture_clicks(){
	$(".thumbnails a").each(function(index){
			$(this).bind("click", function(){				
				load_media(index, $(this), $(this).attr('href'), $(this).find('img').attr('alt'), $(this).find('img').attr('title'));
				return false;
			});
			if(index == 0){
				load_media(index, $(this), $(this).attr('href'), $(this).find('img').attr('alt'), $(this).find('img').attr('title'));
			}
		}
	);
}

function change_img(dir){
	links = $(".thumbnails").find("a");
	total = links.length;
	if(dir == 1){
		img_atual += 1;
	}else{
		img_atual -= 1;
	}
	if(img_atual >= total){
		img_atual = 0;
	}
	load_media(img_atual, $(links[img_atual]), $(links[img_atual]).attr('href'), $(links[img_atual]).find('img').attr('alt'), $(links[img_atual]).find('img').attr('title'));
}


function load_media(index, link, src, alt, title){
	img_atual = index;
	$('#gallery .inner').html("");
	$(".thumbnails a").each(function(index){
		$(this).removeClass("selected");
	});
	link.addClass("selected");	
	var is_image = src.lastIndexOf('.');	
	if(is_image > 0){
		var content = '<a href="#" onclick="toogleZoom();" ><img id="image" src="'+src+'" alt="'+alt+'" /></a>';
		$('#gallery .inner').html(content);	
		$('#image').hide();
		$('#image').load(function () {
			$(this).fadeIn();
			image_done();
		});	
		if(area == 'press'){
			$("#press-title").text(title);
			$("#footer-caption").text(title);
			if(title == alt || alt.length < 1){
				$("#press-url").text("");				
			}else{
				var press_link = "<a href='"+ alt +"' target='_blank'>"+ alt +"</a>";
				$("#press-url").html(press_link);
			}
		}else{
			$("#footer-caption").text(alt);
		}
	}else{
		if(img_zoom == true){
			toogleZoom();
		}
		var vimeocf = "?title=0&amp;byline=0&amp;portrait=0&amp;color=181818&amp;autoplay=0&amp;loop=0";
		var content = '<div id="vimeo"><iframe src="http://player.vimeo.com/video/'+ src +''+ vimeocf +'" width="100%" height="100%" frameborder="0"></iframe></div>';
		$('#gallery .inner').html(content);	
		add_or_remove_thumbnail_arrows();
		updateStage();
		image_done();
	}
}


function update_media_size(){
	var media = $('#gallery .inner img');
	media.each(function(){
		correct_media_size($(this));
	});
	var video = $("#vimeo");
	if(video.length == 1){
		correct_video_size();
	}
}

function correct_media_size(media){
	if(media.length>0){
		var media_width = media.width();
		var media_height = media.height();
		var media_ratio = media_width/media_height;
		var container = $('#gallery .inner');
		var container_width = container.innerWidth();
		var container_height = container.innerHeight();
		var container_ratio = container_width/container_height;

		if(media_width>0 && media_height>0){
			if(container_ratio>media_ratio){
				// adjust image by height
				media.css('width', 'auto');
				media.css('height', '100%');
				media.css('top', 0);
			} else {
				// adjust image by width
				media.css('width', '100%');
				media.css('height', 'auto');
				media.css('top', ((container_height-media.height())/2)+'px');
				media.css('left', 0);
			}
			sobra = parseInt(($(window).width() - $("#image").width() -160)/2);
			$("#prev-img-arrow").css("margin-left", sobra);
			$("#next-img-arrow").css("margin-right", sobra);
		}
	}
}

function correct_video_size(video){
	var video_width = 720;
	var video_height = 404;
	var video_ratio = video_width/video_height;
	var container = $('#gallery .inner');
	var container_width = container.innerWidth();
	var container_height = container.innerHeight();
	var container_ratio = container_width/container_height;

	if(video_width>0 && video_height>0){
		if(container_ratio<video_ratio){
			// adjust image by height
			newW = parseInt(container_width);
			newH = parseInt((container_width * video_height) / video_width);
			$("#vimeo").css('width', newW +'px');
			$("#vimeo").css('height', newH +'px');
			$("#vimeo").css('top', ((container_height - newH)/2) +'px');
			$("#vimeo").css('margin-left',  0);
		} else {
			// adjust image by width
			newH = parseInt(container_height);
			newW = parseInt((container_height * video_width) / video_height) ;
			marginL = parseInt((container_width - newW)/2);
			$("#vimeo").css('width', newW +'px');
			$("#vimeo").css('height', newH +'px');			
			$("#vimeo").css('top', 0);
			$("#vimeo").css('margin-left',  marginL);
		}
		sobra = parseInt(($(window).width() - newW -160)/2);
		$("#prev-img-arrow").css("margin-left", sobra);
		$("#next-img-arrow").css("margin-right", sobra);
	}
}

function start_colorbox(){
	$(document).bind('cbox_complete', function(){
		$("#cboxClose").css("visibility", "hidden");
	});
	
	$(document).bind('cbox_closed', function(){
		$("#cboxClose").css("visibility", "hidden");
	});
	
	$('#cboxPhoto').live('click', $.fn.colorbox.close); 
	
	
	$(".text a[href$='.jpeg'] img, .text a[href$='.jpg'] img, .text a[href$='.png'] img, .text a[href$='.gif'] img").parent().colorbox({transition: "none", width: "100%", height: "100%", title: function(){ return $(this).find("img").attr("alt");}});
	
}


