//	Jquery action
$(document).ready(function(){
	// Easter Egg - http://video.google.com/videoplay?docid=1640258596135511889&ei=_b3YSozNAY_CqAOO0OiTCg&hl=en#
	///////////////////////////
	// FIRST OFF, HIDE STUFF //
	///////////////////////////
	$('.nojs').hide(); // the "no javascript message"
	
	////////////////////////
	// POSITION MAIN LOGO //
	////////////////////////
	$('div#header div h1').css('left', $('div#header div ul').offset().left+'px');
	//on resize, adjust
	$(window).resize(function(){
		$('div#header div h1').css('left', $('div#header div ul').offset().left+'px');
	});
	
	/////////////////////////////////
	// ROLLOVER FADE FOR MAIN NAV //
	////////////////////////////////
	$('div#header div ul li.nav_item').hover(
		function () {
			$(this).find('a').stop().fadeTo('fast', 1);
			$(this).find('a').css({
				"border-left": '1px solid #b4b4b5',
				"left": '-1px'
			});
		}, function () {
	    	$(this).find('a').stop().fadeTo('fast', 0);
			$(this).find('a').css({
				"border-left": 'none',
				"left": '0'
			});
		}
	);
	
	/////////////////////
	// HOMEPAGE SLIDER //
	/////////////////////
	$("#rotating_home div#rotating_holder").easyCrossfade({
		'speed': 500,
		'auto':	true,
		'pause': 8000,
		'continuous': true,
		'numeric' : true,
		'numericId' : 'home_project_select',
    	'faderPrefix' : 'home_image',
		'olToReplace' : 'home_project_select_static'
	});
	
	$('#rotating_home div#rotating_holder').hover(
		function () {
			if ($(this).find('img'))
			$(this).find('img').stop().fadeTo('normal', 0);
		}, function () {
	    	$(this).find('img').stop().fadeTo('normal', 1);
		}
	);
	
	////////////////////////////////////////////////////
	// ROLLOVER FADE IN AND OUT FOR HOME AND PROJECTS //
	////////////////////////////////////////////////////
	$('dl dd.big_image, div#homepage_projects div dl dt').hover(
		function () {
			$(this).find('a').stop().fadeTo('normal', 1);
		}, function () {
	    	$(this).find('a').stop().fadeTo('normal', 0);
		}
	);
	
	////////////////////////////////////////////////////
	// POSITION THE FOOTER IF PAGE IS NOT TALL ENOUGH //
	////////////////////////////////////////////////////
	var footer_place_adjust = 0;
	var quick_pad_adjust = $('div#footer').css('padding-bottom');
	quick_pad_adjust = parseInt(quick_pad_adjust.replace('px',''));
	var quick_marg_top = $('div#footer').css('margin-top');
	quick_marg_top = parseInt(quick_marg_top.replace('px',''));
	if ($('div#footer').outerHeight() + $('div.content').height() + $('div.content').offset().top < $(window).height() - $('div#footer').outerHeight() - quick_marg_top){
		$('#footer').css({
			'position' : 'absolute',
			'bottom' : "0px"
		});
	};
	$(window).resize(function(){
		if ($('div#footer').outerHeight() + $('div.content').height() + $('div.content').offset().top < $(window).height() - $('div#footer').outerHeight() - quick_marg_top){
			$('#footer').css({
				'position' : 'absolute',
				'bottom' : "0px"
			});
		}else{
			$('#footer').css({
				'position' : 'relative'
			});
		};
	});
	
	/////////////////////////////////////////////
	//  ABOUT PAGE, USING CUSTOM JQUERY PLUGIN //
	/////////////////////////////////////////////
	// this here works to control default settings across the entire site
	//$.fn.elTwoColumn.defaults.tagType = "div";
	$('p.split_two').elTwoColumn();//(tagType,firstClass,secondClass)
	
	/////////////////////////////////////////////////
	//  SINGLE PROJECT PAGE, THUMBS AND IMAGES FUN //
	/////////////////////////////////////////////////
	var this_project = $('div.sub_content').attr('id');//Name of the project, folder of the images
	var this_path = $('div.the_thumbs_holder_container ul').attr('title');
	var theme_path = 'http://preview.wtmworldwide.com/wp-content/themes/WTM/images/';
	
	//place page header
	//$('div.project_header').css("background", "url('images/"+this_project+"/project_title.gif') top left no-repeat");
	//$('div.project_header p').html(this_project);
	
	//set the background thumbnail sprite for the thumbnail images
	$('div.the_thumbs ul li').css('background-image', 'url("'+this_path+this_project+'/sprite_thumbnails.jpg")');
	var bg_top_pos = 0;//background position for the thumbnail bg images
	for(i = 1; i < $('div.the_thumbs ul li').length+1; i++){
		$('div.the_thumbs ul li[class="'+i+'"]').css('background-position', '0px '+bg_top_pos+'px');
		bg_top_pos = bg_top_pos - 110;
	};
	
	
	
	var $thumb_holder = $('div.the_thumbs ul'); //the actual <ul>
	var $thumb_holder_container = $('div.the_thumbs div.the_thumbs_holder_container'); //holds the holder
	var number_of_thumbs = $('ul.thumb_sprite').attr('name'); //count number of thumbs/images
	var current_thumb_top = 1;
	var current_thumb = 1;
	var box_top_position = 0;
	
	var $thumb = $('div.the_thumbs').find("li[class="+current_thumb+"]");
	var footer_text = $thumb.attr('alt');
	//set initial footer text
	$('div.image_footer p').text(footer_text);
	
	//if number is larger than 5, add up and down arrows arrow
	if (number_of_thumbs > 5){
		//Insert arrow arrow
		$('<li class="projects_switch_arrow"><!-- switch arrow --></li>').insertAfter($thumb_holder.find("li[class=4]"));
		var arrow_down = true;
	};
	
	//Set the indicator arrow position
	var lil_arrow_position = $('div.the_thumbs li:first').height() / 2 - 7;
	$('div.the_thumbs div.arrow').css('top', lil_arrow_position + "px");
	
	//Click up arrow, scroll though thumbs
	$('div.the_thumbs li.projects_switch_arrow').click(function () { 
		if(arrow_down == true){
			arrow_down = false;
			$thumb_holder.animate({ 
				top: "-440px"
			}, 500);
			$(this).css("background", "url('"+theme_path+"projects_up_arrow.gif')");
		}else{
			arrow_down = true;
			$thumb_holder.animate({ 
				top: "0px"
			}, 500);
			$(this).css("background", "url('"+theme_path+"projects_down_arrow.gif')");
		};
		$('div.the_thumbs div.arrow').animate({ 
			left: "-50px"
		}, 200 );
	});
	
	// Sliding the thumbs function, called in both clicking up and down arrows
	function slide_the_thumbs(){
		$thumb_holder.animate({ 
			top: box_top_position + "px"
		}, 800);
	}
	
	$('div.the_thumbs li:not(.projects_switch_arrow)').click(function () {
		current_thumb = $(this).attr('class');
		$thumb = $(this);
		switch_image();
	});
	
	function switch_image(){
		//swicth image
		$('div.the_image').find('img').attr('src', this_path+this_project+'/'+current_thumb+'_mid.jpg');
		
		//slide the arrow
		$('div.the_thumbs div.arrow').animate({ 
			top: $thumb.offset().top - $thumb.parent().parent().parent().offset().top + ($thumb.height() / 2 - 17) + "px",
			left: "0px"
		}, 200 );
		if (current_thumb < 2){
			$('div.the_image_overlay div.prev_image').css('background-position', '-220px 0');
			$('li.giant_image_last').css('background-position', '0 -114px');
			$('li.giant_image_last').find('a').css('cursor','default');
		}else{
			$('div.the_image_overlay div.prev_image').css('background-position', '-170px 0');
			$('li.giant_image_last').css('background-position', '0 -38px');
		}
		if (current_thumb == number_of_thumbs){
			$('div.the_image_overlay div.next_image').css('background-position', '-370px 0');
			$('li.giant_image_next').css('background-position', '0 -133px');
			$('li.giant_image_next').find('a').css('cursor','default');
		}else{
			$('div.the_image_overlay div.next_image').css('background-position', '-320px 0');
			$('li.giant_image_next').css('background-position', '0 -76px');
		}
		footer_text = $thumb.attr('title');
		$('div.image_footer p').text(footer_text);
		//slide thumbs is needed
		if (current_thumb == 4){
			$thumb_holder.animate({ 
				top: "0px"
			}, 500);
			if (arrow_down == false){
				$('div.the_thumbs div.arrow').animate({ 
					top: $('div.the_thumbs').find("li[class=7]").offset().top - $('div.the_thumbs').find("li[class=7]").parent().parent().offset().top + ($('div.the_thumbs').find("li[class=7]").height() / 2 - 7) + "px",
					left: "0px"
				}, 200 );
				$('li.projects_switch_arrow').css("background", "url('"+theme_path+"projects_down_arrow.gif')");
				arrow_down = true;
			};
		}else if (current_thumb == 5){
			$thumb_holder.animate({ 
				top: "-440px"
			}, 500);
			if (arrow_down == true){
				$('div.the_thumbs div.arrow').animate({ 
					top: $('div.the_thumbs').find("li[class=2]").offset().top - $('div.the_thumbs').find("li[class=2]").parent().parent().offset().top + ($('div.the_thumbs').find("li[class=2]").height() / 2 - 7) + "px",
					left: "0px"
				}, 200 );
				$('li.projects_switch_arrow').css("background", "url('"+theme_path+"projects_up_arrow.gif')");
				arrow_down = false;
			};
		}
		$('div#giant_image_holder div.giant_image').css('background', 'url("'+this_path+this_project+'/'+current_thumb+'.jpg") top left no-repeat');
		$('div.giant_image_controls ul li.giant_image_title span.giant_caption').html(footer_text);
	}
	
	//BIG IMAGE OVERLAY BUTTONS
	$('div.the_image_overlay').hover(
		function () {
			$(this).stop().fadeTo('fast', 1);
			$(this).find('div.image_footer').animate({ 
				bottom: "0px"
			}, 300 );
			if(number_of_thumbs == 1){
				//console.log('one');
				$('div.the_image_overlay div.next_image').css('background-position', '-370px 0');
			};
		}, function () {
	    	$(this).stop().fadeTo('fast', 0);
			$(this).find('div.image_footer').animate({ 
				bottom: "-21px"
			}, 300 );
		}
	);
	
	//For IE, because the image hover doesn't work with IE
	if(number_of_thumbs == 1){
		//console.log('one');
		$('div.the_image_overlay div.next_image').css('background-position', '-370px 0');
	};
	
	///////////////////
	//HUGE IMAGE VIEW//
	///////////////////
	//hover magnifiy icon
	$('div.the_image_overlay div.magnify').hover(
		function () {
			$(this).css('background-position', '0 0');
		}, function () {
	    	$(this).css('background-position', '-40px 0');
		}
	);
	//roll over giant image next and prev buttons
	var other_huge_overlay_click = false;
	$('li.giant_image_last').hover(
		function () {
			other_huge_overlay_click = true;
			if (current_thumb > 1){
				$(this).css('background-position', '0 -19px');
				$(this).find('a').css('cursor', 'pointer');
			}else{
				$(this).find('a').css('cursor', 'default');
			}
		}, function () {
			other_huge_overlay_click = false;
			if (current_thumb > 1){
	    		$(this).css('background-position', '0 -38px');
			}
		}
	);
	
	$('li.giant_image_next').hover(
		function () {
			other_huge_overlay_click = true;
			if (current_thumb < number_of_thumbs){
				$(this).css('background-position', '0 -57px');
				$(this).find('a').css('cursor', 'pointer');
			}else{
				$(this).find('a').css('cursor', 'default');
			}
		}, function () {
			other_huge_overlay_click = false;
			if (current_thumb < number_of_thumbs){
	    		$(this).css('background-position', '0 -76px ');
			}
		}
	);
	$('li.giant_image_close').hover(
		function () {
			$(this).css('background-position', '0 -95px');
		}, function () {
	    	$(this).css('background-position', '0 0');
		}
	);
	
	$('li.giant_image_last').click(function () {
		if (current_thumb > 1){
			current_thumb--;
			$thumb = $('div.the_thumbs').find("li[class="+current_thumb+"]");
			switch_image();
			//$('div.the_image_overlay div.next_image').css('background-position', '-320px 0');
		}
	});
	$('li.giant_image_next').click(function () {
		if (current_thumb < number_of_thumbs){
			current_thumb++;
			$thumb = $('div.the_thumbs').find("li[class="+current_thumb+"]");
			switch_image();
			//$('div.the_image_overlay div.prev_image').css('background-position', '-170px 0');
		}
	});
	
	//hide giant image
	$('div#giant_image_holder').hide();
	//stretch out the bg
	$('div#giant_image_holder').css('height', $(document).height()-$('div#header').outerHeight()+$('div#footer').outerHeight()+'px');
	//set first giant image path
	$('div#giant_image_holder div.giant_image').css('background', 'url("'+this_path+this_project+'/'+current_thumb+'.jpg") top left no-repeat');
	
	//show giant image (click on magnify icon)
	$('div.the_image_overlay div.magnify, div.the_image_overlay').click( function(){
		if(other_overlay_click == false){
			$('div#giant_image_holder').fadeIn();
			if (current_thumb == 1){
				$('li.giant_image_last').css('background-position', '0 -114px');
			};
			if (current_thumb == number_of_thumbs){
				$('li.giant_image_next').css('background-position', '0 -133px');
			};
		}
	});
	
	//close giant image
	$('div#giant_image_holder li.giant_image_close a').click( function(){
		$('div#giant_image_holder').fadeOut();
		return false;
	});
	$('div#giant_image_holder').click( function(){
		if(other_huge_overlay_click == false){
			$('div#giant_image_holder').fadeOut();
		}
	});
	$('div.giant_image').hover(
		function () {
			other_huge_overlay_click = true;
		}, function () {
			other_huge_overlay_click = false;
		}
	);
	
	var other_overlay_click = false;
	
	//NEXT AND PREV MESSAGES
	$('div.the_image_overlay div.prev_image').hover(
		function () {
			other_overlay_click = true;
			if (current_thumb > 1){
				$(this).css('background-position', '-120px 0');
				$(this).css('cursor', 'pointer');
			}else{
				$(this).css('cursor', 'default');
			}
		}, function () {
			other_overlay_click = false;
			if (current_thumb > 1){
	    		$(this).css('background-position', '-170px 0');
			}
		}
	);
	
	$('div.the_image_overlay div.next_image').hover(
		function () {
			other_overlay_click = true;
			if (current_thumb < number_of_thumbs){
				$(this).css('background-position', '-270px 0');
				$(this).css('cursor', 'pointer');
			}else{
				$(this).css('cursor', 'default');
			}
		}, function () {
			other_overlay_click = false;
	    	if (current_thumb < number_of_thumbs){
				$(this).css('background-position', '-320px 0');
			}
		}
	);
	$('div.the_image_overlay div.prev_image').click(function () {
		if (current_thumb > 1){
			current_thumb--;
			$thumb = $('div.the_thumbs').find("li[class="+current_thumb+"]");
			switch_image();
			$('div.the_image_overlay div.next_image').css('background-position', '-320px 0');
		}
	});
	$('div.the_image_overlay div.next_image').click(function () {
		if (current_thumb < number_of_thumbs){
			current_thumb++;
			$thumb = $('div.the_thumbs').find("li[class="+current_thumb+"]");
			switch_image();
			$('div.the_image_overlay div.prev_image').css('background-position', '-170px 0');
		}
	});

	
	
	
	//
	$('div#delivery_dropdown').hide()
//	var delivery_top = $('li.delivery').offset().top + 17;
//	var delivery_left = $('li.delivery').offset().left - 10;
//	$('div#delivery_dropdown').css({'top': delivery_top+'px', 'left': delivery_left+'px'});

	
	$('ul.the_bottom_bar li.delivery').mouseenter(function() {
		$('div#delivery_dropdown').fadeIn('fast');
	});
	$('div#delivery_dropdown').mouseleave(function(){
		$('div#delivery_dropdown').fadeOut('fast');
	});
	
	$('div#delivery_dropdown div.delivery_list ul li:last').css("border-bottom","none");
	
	//////////////////
	// CONTACT PAGE //
	//////////////////
	// using mailto plugin to avoid spam with mailto links.
	$('span.mailto').mailto({'domain':'wtmworldwide', 'extension':'com'});
	
	///////////////
	// BLOG PAGE //
	///////////////
	$("ul#archives").hide();
	$("ul#categories").hide();
	$('ul#categories li:last a, ul#archives li:last a').css("border-bottom","none");
	
	$("#archive_trigger").click(function () {
		$("ul#archives").fadeIn('fast');
		$("ul#categories").fadeOut('fast');
	});
	$("#archives h3").click(function () {
		$("ul#archives").fadeOut('fast');
	});
	$("#category_trigger").click(function () {
		$("ul#categories").fadeIn('fast');
		$("ul#archives").fadeOut('fast');
	});
	$("#categories h3").click(function () {
		$("ul#categories").fadeOut('fast')
	});
	
	//search click and stuff, blank and changes style
	$("input.search_input").focus(function () {
	    if( this.value == this.defaultValue ) {
			this.value = "";
			$(this).css({"background-color":"white","color":"#a1a1a3"});
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			$(this).css({"background-color":"#a1a1a3","color":"white"});
		}
	});
});

