/*********************************/
/*  Functions for theme	         */
/*     Hazlitt Eastman           */
/*********************************/

/* preload images jQuery function extension */
(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)

/* home sub nav animation */

function home_sub_nav() {
	$(function() {
	
		var $el, leftPos, newWidth;
		        $mainNav3 = $("#home_sub_nav");
	
	    $mainNav3.append("<li id='magic-line-2'></li>");

	    var $magicLineThree = $("#magic-line-2");

	    $magicLineThree
	        .width($(".current").width())
	        .height($mainNav3.height())
	        .css("left", $(".current a").position().left)
	        .data("origLeft", $(".current a").position().left)
	        .data("origWidth", $magicLineThree.width())
			.data("origColor", $(".current a").attr("rel"));

	    $("#home_sub_nav a").hover(function() {
	        $el = $(this);
	        leftPos = $el.position().left;
	        newWidth = $el.parent().width();
	        $magicLineThree.stop().animate({
	            left: leftPos,
	            width: newWidth/*,
	            backgroundColor: $el.attr("rel")*/
	        })
	    }, function() {
	        $magicLineThree.stop().animate({
	            left: $magicLineThree.data("origLeft"),
	            width: $magicLineThree.data("origWidth"),
	            backgroundColor: $magicLineThree.data("origColor")
	        });    
	    });

		/* Kick IE into gear */
	    $(".current a").mouseenter();
	});
}

/**** Dom ready functions ****/

$(document).ready(function() {
	
	/* set the status of the elements */
	$('.home_image').hide();
	$('#branding').show();
	$('div.description').hide();
	
	/* correct layout  differences */
	if ($.browser.mozilla) {
		$('#previous_image').addClass('mozilla');
		$('#next_image').addClass('mozilla');
		$('#tree').addClass('mozilla');
		$('#services_first').addClass('mozilla');
	}
	
	/* header nav animation */
	$(function(){
		var $el, leftPos, newWidth;
		        $mainNav2 = $("#header_nav");
		
	    $mainNav2.append("<li id='magic-line'></li>");

	    var $magicLineTwo = $("#magic-line");

	    $magicLineTwo
	        .width($(".current_page").width())
	        .height($mainNav2.height())
	        .css("left", $(".current_page a").position().left)
	        .data("origLeft", $(".current_page a").position().left)
	        .data("origWidth", $magicLineTwo.width())
	        .data("origColor", $(".current_page a").attr("rel"));

	     $("#header_nav a").hover(function() {
	        $el = $(this);
	        leftPos = $el.position().left;
	        newWidth = $el.parent().width();
			$magicLineTwo.stop().animate({
	            left: leftPos,
	            width: newWidth/*,
	            backgroundColor: $el.attr("rel")*/
	        })
	    }, function() {
	        $magicLineTwo.stop().animate({
	            left: $magicLineTwo.data("origLeft"),
	            width: $magicLineTwo.data("origWidth"),
	            backgroundColor: $magicLineTwo.data("origColor")
	        });    
	    }); 

	    /* Kick IE into gear */
	     $(".current_page a").mouseenter();
	 
	});
	
	/* home sub nav animation if on home page */
	 	if ($('#home_sub_nav').length) {
			home_sub_nav();
		}
	/* nav item click */
	$("#branding_button").click(function(){ 
		$(this).siblings("li").removeClass("current");
		$(this).addClass("current");
		home_sub_nav();
		$('.home_image').fadeOut(); 
		$('#branding').stop(true,true).fadeIn();
	});
	$("#websites_button").click(function(){ 
		$(this).siblings("li").removeClass("current");
		$(this).addClass("current");
		home_sub_nav();
		$('.home_image').fadeOut();
		$('#websites').fadeIn();
	});
	$("#printed_button").click(function(){ 
		$(this).siblings("li").removeClass("current");
		$(this).addClass("current");
		home_sub_nav();
		$('.home_image').fadeOut();
		$('#printed').fadeIn();
	});
	$("#partnerships_button").click(function(){ 
		$(this).siblings("li").removeClass("current");
		$(this).addClass("current");
		home_sub_nav();
		$('.home_image').fadeOut();
		$('#partnerships').fadeIn();
	});
	
	/* testimonials cycling */
	$('#testimonials') 
	.cycle({ 
	    fx:     'scrollHorz', 
	    speed:  'slow', 
	    timeout: 0, 
	 	next:   '#next_testimonial',
		prev:   '#previous_testimonial'
	});
	
	/* clear sign up form values */
	$(".newsletter").click(function(){
		$(this).val("");
	});
	
	/* our work image roll over */
	$('div.our_work_switch').hover(
		function(){
			$(this).children('img.our_work_bw').fadeOut();
			$(this).children('div.description_outer').children('div.description').slideToggle();
		},
		function(){
			$(this).children('img.our_work_bw').fadeIn();
			$('div.description').fadeOut();
	});
	
	/* Project image cycling */
	$('#large_images').cycle({ 
	    fx:     'scrollHorz', 
	    speed:  'slow', 
	    timeout: 0, 
	    pager:  '#image_nav ul',
		pagerAnchorBuilder: function(idx, slide) { 
		        return '<li><a href="#"></a></li>'; 
		    },
	 	next:   '#next_image',
		prev:   '#previous_image'
		
	});
	
	
	/*$('div.our-work_switch').mouseout(function(){
		$(this).children('img.our_work_bw').fadeIn();
		$('div.description').fadeOut();
	});*/
	
	/* our work image roll over */
	/*$('ul#our-work li img').mouseover(function(){
		$(this).fadeOut();
		$(this).siblings('div.description_outer').children('div.description').show("blind", {direction: "up"}, 500);
	});
	$('ul#our-work li').mouseout(function(){
		$(this).children('img.our_work_bw').fadeIn();
		$('div.description').fadeOut();
	});*/
	
/* $(this).siblings('div.description_outer').children('div.description').show("blind", {direction: "up"}, 500);*/
	
	/* back button */
	/*$('h3.back a').click(function(){
		parent.history.back();
		return false;
	});*/
});
