jQuery.fn.addMarginToLeft = function () {  
    this.css("margin-left", ($(window).width() - this.width()) / 2+$(window).scrollLeft() + "px");  
    return this;  
}

jQuery.fn.addRemoveActiveClass = function(){
	$("#leftNav .active").removeClass("active");
	this.parent().addClass("active");
}

function randInt (n){ // 'n' is the number you'd like to choose from. Is not zero based
	return (Math.floor(Math.random()*n+1));
}

jQuery.fn.equalCols = function(){
    var sortNumber = function(a,b){return b - a;};
    var heights = [];
    $(this).each(function(){
        heights.push($(this).height());
    });
    heights.sort(sortNumber);
    var maxHeight = heights[0];
    return this.each(function(){
        $(this).css({'height': maxHeight});
    });
};

jQuery.fn.moveWork = function (fromLeft, fromTop, theClass) {
	$(".box").not(theClass).removeClass("activeBox").find("img").animate({opacity: 0.2}, 200);
    $("#workBoxes").stop(true, true).delay(100).animate({"left":fromLeft,"top":fromTop}, 600);
    $("." + theClass).addClass("activeBox").find("img").delay(500).animate({opacity: 1}, 200);
	this.addRemoveActiveClass();
	$(".hoverBox").fadeOut(200);
	$(".box").removeClass("hovered");
	$(".activeBox img").animate({opacity: 1},100);
    return this;  
}

$(function(){

	// Hide nav on main 'Work' page, fade in later
	$(".jsNav").css("display","none");
	
	// Set all images to 0.2 opacity
	$(".box").not(".activeBox").find("img").css({opacity: 0.2});
	
	// Hide home content on load
	var homeContentContainer = $("#homeContent");
	var homeContentContainerHeight = homeContentContainer.height();
	homeContentContainer.css({"height":homeContentContainerHeight,"display":"none"});
	$("#homeContentTrigger").toggle(function(){
		homeContentContainer.slideDown();
		$(this).html('Less -');
	}, function(){
		homeContentContainer.slideUp();
		$(this).html('More +');
	});

});

$(window).bind("load",function(){

	/*****
		The whole top portfolio but, hovers & all
	*****/
	// Vars == Cached selectors == speed
	var topPortfolio = $("#topPortfolio");
	var topPortfolioContent = $("#topPortfolio ul");
	var topPortfolioToggle = $("#topPortfolioToggle a");
	
	// Make top portoflio blgger & smaller on click
	topPortfolioToggle.click(function(){
		if (topPortfolio.hasClass("canBeOpened")) {
			topPortfolio.stop(true, false).animate({"height":"500px"}, 400).addClass("on").fadeIn().removeClass("canBeOpened");
			topPortfolioToggle.html("X close").removeClass("closed").addClass("open");
		} else {
			topPortfolio.animate({"height":"22px"}, 400);
			topPortfolioToggle.html("&darr; click me").removeClass("open").addClass("closed");
			setTimeout(function(){
				topPortfolio.addClass("canBeOpened");
			}, 400);
		}
		return false;
	});


	/*****
		Horizontal accordion
	*****/
	var accordionToslide = $("#homeAccordion .toSlide");
	var accodrionPanels	= $("#homeAccordion .outer");
	accordionToslide.hover(function(){
		$(this).stop(true, false).animate({"width":"360px"}, 200);
		$("#homeAccordion .toHide").stop(true, false).animate({"width":"0px"}, 200);
	}, function(){
		accodrionPanels.stop(true, false).animate({"width":"180px"}, 200);
	});
	

	/*****
		Work
	*****/
	// Fade in nav, now the page has loaded
	$(".jsNav").css("display","block");
	// The moving boxes
	var workSpeed = 600;
	$(".jsNav #charity").click(function(){
		$(this).moveWork('-1134px','0','charity');
		return false;
	});
	$(".jsNav #media").click(function(){
		$(this).moveWork('-2268px','0','media');
		return false;
	});
	$(".jsNav #music").click(function(){
		$(this).moveWork('-3402px','0','music');
		return false;
	});
	$(".jsNav #fashion").click(function(){
		$(this).moveWork('-1134px','-756px','fashion');
		return false;
	});
	$(".jsNav #fitness").click(function(){
		$(this).moveWork('-2268px','-756px','fitness');
		return false;
	});
	$(".jsNav #business").click(function(){
		$(this).moveWork('-3402px','-756px','business');
		return false;
	});
	$(".jsNav #ecommerce").click(function(){
		$(this).moveWork('-1134px','-1512px','ecommerce');
		return false;
	});
	$(".jsNav #foodDrink").click(function(){
		$(this).moveWork('-2268px','-1512px','fooddrink');
		return false;
	});
	$(".jsNav #foreignlanguage").click(function(){
		$(this).moveWork('-3402px','-1512px','foreignlanguage');
		return false;
	});
	$(".jsNav #wordpress").click(function(){
		$(this).moveWork('-1134px','-2269px','wordpress');
		return false;
	});
	
	// The info boxes
	$("#workBoxes .box").bind("click", function(){
		if ($("div.hoverBox:visible").length < 1) {
			if ($(this).hasClass("activeBox")) {
				$(".hoverBox", this).fadeIn(200);
				$(this).addClass("hovered").css("z-index","1000");
				$(".activeBox").not('.hovered').find("img").stop(true, false).animate({opacity: 0.2},100);			
			}
		} else {
			if ($(this).hasClass("hovered")) {
				$(".hoverBox").fadeOut(200);
				$(this).removeClass("hovered").css("z-index","10");;
				$(".activeBox img").stop(true, false).animate({opacity: 1},100);
			}
		}
	});	
	
	// Contact form basic validation
	$("#contactForm button").click(function(){
		var nameText = $("#contactForm input[name='contactName']").val();
		var emailText = $("#contactForm input[name='contactEmail']").val();
		var messageText = $("#contactForm textarea").val();
		
		if (nameText == '') { if (!error) { alert("Name is empty"); } var error = true; return false;}
		if (emailText == '') { if (!error) { alert("Email is empty"); } var error = true; return false;}
		if (messageText == '') { if (!error) { alert("Message is empty"); } var error = true; return false;}
		
		if (!error) {
			return;
		}
	});
	
	// comment form basic validation
	$("#commentForm button").click(function(){
		var nameText = $("#commentForm input[name='author']").val();
		var emailText = $("#commentForm input[name='email']").val();
		var messageText = $("#commentForm textarea").val();
		
		if (nameText == '') { if (!error) { alert("Name is empty"); } var error = true; return false;}
		if (emailText == '') { if (!error) { alert("Email is empty"); } var error = true; return false;}
		if (messageText == '') { if (!error) { alert("Message is empty"); } var error = true; return false;}
		
		if (!error) {
			return;
		}
	});
		
		
	/*****
		Bits & pieces
	*****/
	// Set height of columns in footer
	// $("footer section").equalCols();
	
	// If is iPad, make the BG fit the window
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
	if (isiPad || isiPhone) {
		$(window).scroll(function(){
		    $("#bgImage").css({top:$(document).scrollTop()});
		});
	}
	
	// Move to relevent portfolio section by searchTerm cookie
	$(".jsNav .workLeftNav nav li.active a").click();
	
	// Hover over 'Services' links on main 'Services' page, and highlight nav items too
	$(".post-23 ul li a").hover(function(){
		var theHrefToHighlight = $(this).attr("href");
		$("#leftNav ul").find("a[href='" + theHrefToHighlight + "']").parent("li").addClass("hovered");
	}, function(){
		$("#leftNav li").removeClass("hovered");
	});
	$(".page-id-23 #leftNav ul li a").hover(function(){
		var theNavHrefToHighlight = $(this).attr("href");
		$(".post-23 ul li").find("a[href='" + theNavHrefToHighlight + "']").addClass("hovered");
	}, function(){
		$(".post-23 ul li a").removeClass("hovered");
	});
		
});