/*globals $, window, document */
//"use strict";
var accMin = 988;
var accMax = 1000;
var accSmall = 247;
var accHeight = 423;
var accLarge = 494;
var bgWidth = 642;
var active = null;
var conDef = 65;
var full_width = 988;
var focus = [364, 200, 435, 450];
var active_num = 0;
var clickedSlide = 0;
var miniPosition = 0;
var miniIncrement = 92;
var miniImages = 0;

function getPull(point, full) {
	var newPoint = -point + full / 2;
	if (newPoint > 0 && (full / 2 > point)) {
		newPoint = 0;
	}
	if (point > bgWidth / 2) {
		if ((point - full / 2) - (bgWidth - full) > 0) {
			newPoint += (point - full / 2) - (bgWidth - full);
		}
	}
	return Math.round(newPoint);
}

function clicked() {
	clickedSlide = $(this).attr("num");
	if (active === this) {
		return;
	}
	if (active) {
		$(active).find(".con:first").stop().clearQueue().animate({top: accHeight - conDef}, 400);
	}
	$(".expandable").each(function (x) {
		if (x.toString() !== clickedSlide) {
			$(this).removeClass("active");
			$(this).stop().clearQueue().animate({width: accSmall + "px"}, 300);
			$(this).find(".accImage").stop().clearQueue().animate({left:getPull(focus[x], accSmall)+"px"},300);
			$(this).find(".bw").stop().clearQueue().animate({opacity:1,left:getPull(focus[x], accSmall)+"px"},300);
			$(this).find(".expandImage:first").fadeIn();
			$(this).find(".exCon").stop().clearQueue().fadeOut();
		} else {
			expandSlide(this,x);
		}
	});
}

function expandSlide(targ,x) {
	active = targ;
	$(targ).addClass("active");
	$(targ).find(".exCon:first").fadeIn();
	$(targ).find(".con:first").stop().clearQueue().delay(100).animate({top: accHeight - $(targ).find(".con:first").height()}, 400);
	$(targ).stop().clearQueue().animate({width: accLarge + "px"}, 400);			
	$(targ).find(".accImage").stop().clearQueue().animate({left:getPull(focus[x], accLarge)+"px"},400);
	$(targ).find(".bw").stop().clearQueue().animate({opacity:0,left:getPull(focus[x], accLarge)+"px"},400);
	$(targ).find(".expandImage:first").fadeOut();
}

function set_fullWidth(w) {
	full_width = w - 80;	
	full_width = Math.max(accMin, Math.min(full_width, accMax));
	full_width = (full_width - (full_width % 12));
	accLarge = full_width / 2;
	accLarge = bgWidth;
	accSmall = (full_width - accLarge) / 3;
}

function resizeAccordion() {
	set_fullWidth($(window).width());
	$("#accordion").stop().clearQueue().css("width", full_width + "px");
	$("#accordion").stop().clearQueue().css("margin-left", -Math.round(full_width / 2) + "px");
	$('.expandable').each(function (x) {
		var toPull = getPull(focus[x], accSmall);
		if (!active) {
			toPull = getPull(focus[x], (full_width / 4));
			$(this).stop().clearQueue().css("width", full_width / 4 + "px");
		} else {
			
			if ($(this).attr("num") == $(active).attr("num")) {
				toPull = getPull(focus[x], accLarge);
				$(this).stop().clearQueue();
				$(this).css("width", accLarge + "px");
			} else {
				$(this).stop().clearQueue();
				$(this).css("width", accSmall + "px");
			}
		}
		$(this).find("img").stop().clearQueue().css("left", toPull + "px");
	});
}

function accordionLoaded() {
	set_fullWidth($(window).width());
	
	$('.expandable').each(function(x){
		
		var useThisWidth = accSmall;
		if(x === 0) {
			useThisWidth = accLarge
		}		
		var toPull = Math.min(0, getPull(focus[x], useThisWidth));

		$(this).find(".accImage").attr("src",$(this).find(".accImage").attr("alt"));
		$(this).find('.accImage').show();
		
		$(this).stop().clearQueue().delay(500).animate({width: useThisWidth + "px"},600);
		
		var todo = null;
		if(x===3) {
			todo = function(){
				expandSlide($('.expandable:first'),0);
			};
		}
		$(this).find('img').stop().clearQueue().delay(500).animate({left:toPull+"px"},600, todo);
	});
	
	
	$(window).resize(resizeAccordion);
	$("#accordion").stop().clearQueue().delay(480).animate({width: full_width + "px", marginLeft: -Math.round(full_width / 2) + "px"},570);
	
	
	$('.expandable').click(clicked);
	$('.expandable').hover(
		function () {
			$(this).find(".bw").animate({opacity: 0});
		},
		function () {
			if (!active || (active && $(active).attr("num") != $(this).attr("num"))) {
				$(this).find(".bw").animate({opacity: 1});
			}
		});	
}

function changeMiniPosition() {
	if($(this).attr("id") == "hcgPrev") {
		miniPosition = Math.max(0,miniPosition - 1);
	} else {
		miniPosition = Math.min($("#homeGalleryImages img").length - 4, miniPosition + 1);
	}
	
	if(miniPosition <= 0) $("#hcgPrev").addClass("inactive");
	else $("#hcgPrev").removeClass("inactive");
	if(miniPosition >= $("#homeGalleryImages img").length - 4) $("#hcgNext").addClass("inactive");
	else $("#hcgNext").removeClass("inactive");
	
	$("#homeGalleryImages").animate({marginLeft:-(miniPosition*miniIncrement)+"px"},"fast");
	return false;
}

	var load_count = 0;

$(document).ready(function () {
	
	$(".zebra").each(function(){
		$(this).find("tbody tr:even").addClass("evenRow");
	});
	
	if($("#homeGalleryImages")) prepLightbox();
	if($("#stylistsContainer")) prepStylists();
	
	$('body').supersleight();
	
	$('.fade').each(function(){
		$(this).attr("og",$(this).css("opacity"));
		$(this).hover(
			function(){
				$(this).animate({opacity:1},"fast","linear");
			},
			function(){
				$(this).animate({opacity:$(this).attr("og")},"fast","linear");
			}
		)
	});
	
	$("#header ul a").each(function(){
		$(this).find("span:first").hide().css("margin-top","0");
		$(this).hover(function(){
			$(this).find("span:first").fadeIn();
		},
		function(){
			$(this).find("span:first").fadeOut("slow");
		});
		
	});
	
	
	$("#hcgPrev").click(changeMiniPosition);
	$("#hcgNext").click(changeMiniPosition);
	if(miniImages <= 4) $("#hcgNext").addClass("inactive");
	
	set_fullWidth(accMin);
	$('#rsText').placeHoldize();
	$('#hcBookEmail').placeHoldize();
	$("#bfPreferredDate").datepicker({
//				showOn: 'button',
				buttonImage: '/wp-content/themes/lht/images/cal.gif',
				buttonImageOnly: false
			});
	var dates = $('#bfRange1, #bfRange2').datepicker({
//				showOn: 'button',
//				buttonImage: '/wp-content/themes/lht/images/cal.gif',
//				buttonImageOnly: false,
				defaultDate: "+1w",
				numberOfMonths: 1,
				onSelect: function(selectedDate) {
					var option = this.id == "bfRange1" ? "minDate" : "maxDate";
					var instance = $(this).data("datepicker");
					var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
					dates.not(this).datepicker("option", option, date);
				}
			});
	
	
	
	$('.expandable').each(function (x) {
		$(this).attr("num", x);
		$(this).attr("focus", focus[x]);
		var toPull = Math.min(0, getPull(focus[x], (full_width / 4)));
		
		$(this).find("img").css("left",toPull+"px");
		var bw = $(this).find(".bw:first");
		$(bw).load(function(){
			$(bw).fadeIn('normal', function(){
				load_count += 1;
				if(load_count == 4) accordionLoaded();
			});
		});
		$(bw).attr("src",$(bw).attr("alt"));
	});
	


	
});

var active_thumbs = 0;
var max_active_thumbs = 1;
var stylist_z_count = 1;
var active_stylist = null;
function prepStylists() {
	
	
	if($(document).has("#stylistsContainer").length === 0) return;
	
	$(".b_stylistsBioExpand").click(function(){
		$(this).fadeOut();
		$(this).parent().find(".stylistsBioMore:first").slideDown();
	});
	
	
	$("#stylistsThumbCropper a").each(function(x){
		$(this).find("span:first").css("background-image","url("+$(this).find("img:first").attr("src")+")");

		$(this).hover(
			function(){
				$(this).find("img:first").fadeOut();
			},
			function(){
				if(!$(this).hasClass("active")) $(this).find("img:first").fadeIn();
			}
		);
		$(this).click(function(){
			if($(this).hasClass("active")) return false;
			$("#stylistsThumbCropper a.active").removeClass("active").find("img:first").fadeIn();
			$(this).addClass("active");
			
			var bio = $(".stylistBio:eq("+x+")");
			var img = $("#stylistsImage img:eq("+x+")");
			
			$(img).hide().css("z-index",stylist_z_count++);
			$(bio).hide().css("z-index",stylist_z_count++)
			$(bio).find(".stylistsBioMore:first").hide();
			$(bio).css("bottom",-$(bio).outerHeight()-40);
			$(bio).find(".b_stylistsBioExpand").show();
			
			
			if(active_stylist != null) {
				$("#stylistsImage img:eq("+active_stylist+")").fadeOut("200");
				$(".stylistBio:eq("+active_stylist+")").animate({bottom:-$(bio).outerHeight()-150});
			}
			$(img).delay("510").fadeIn("200");
			$(bio).delay("1210").show().animate({bottom:0},"400");
			active_stylist = x;
			
			return false;
		});
	});
	
	$("#thumbsDown").click(function(){
		active_thumbs = Math.min(active_thumbs+1, max_active_thumbs);
		$("#stylistsThumbCropper").animate({top:(active_thumbs*-120)+"px"});
		return false;
	});
	
	$("#thumbsUp").click(function(){
		active_thumbs = Math.max(active_thumbs-1, 0);
		$("#stylistsThumbCropper").animate({top:(active_thumbs*-120)+"px"});
		return false;
	});
	
	$("#stylistsThumbCropper a:first").click().mouseover();
	$('#stylistsThumbCropper a:even').tipsy({gravity: 'e',fade: true});
	$('#stylistsThumbCropper a:odd').tipsy({gravity: 'w',fade: true});
		
}



function prepLightbox() {
	if($(document).has("#homeGalleryImages").length === 0) return;
	for(var i = 0; i < lbdata.length; i++) {
		var el = lbdata[i];
		$("#homeGalleryImages").append("<a rel='home_group' href='"+el.image+"' title='"+el.title+"'><img src='"+ el.thumb +"' /></a>");
	}
	
	$("a[rel=home_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'overlayOpacity'	: '.7',
		'overlayColor'		: '#000',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	
}
