// JavaScript Document

(function($) { 


$(document).ready(function(){
	

	//alert($.browser.msie  + " " + $.browser.version);
	
	if ($.browser.msie) {
		var IEVer = parseInt($.browser.version);
		//alert("ie version is: " + IEVer);
	} else {
		IEVer = false;	
	}
	
	if (IEVer > 8 || IEVer == false) { 
		var canfade = true;
		//IE7 & 8 CANT fade pngs...
	}
	
	if (canfade) {
		$(".product_range a, .garden_section a img, a.img_contain, .pest_section a img").mouseenter(function(){
			if ($(this).attr("id") != "logo" && !$(this).parent().hasClass("product_sm")) {
				$(this).stop(true,true).fadeTo(200,0.7);	
			}
		});
		
		$(".product_range a, .garden_section a img, a.img_contain, .pest_section a img").mouseleave(function(){
		$(this).stop(true,true).fadeTo(200,1);	
		});
	}
	
	
	
	//tooltips
	
	$("a.product_sm").mouseenter(function() {
		if (canfade) {
			$(this).find(".tooltip").stop(true).fadeTo(200,1, function() {$(this).css('filter', '')});
		} else {
			$(this).show().css('filter', '')
		}
	});
	
	
	$("a.product_sm").mouseleave(function() {
		$(this).find(".tooltip").stop(true).fadeTo(200,0, function() {$(this).hide();});
	});
	

	//home selector

	
	$("#home_selector a#garden").mouseenter(function() {
		$("#home_selector a#garden img").stop().animate({bottom : 8}, 250);
		
		if (canfade) { 
			$("#home_selector a#pest").stop().fadeTo(250,0.6);
		}
	});
	
	$("#home_selector a#garden").mouseleave(function() {
		$("#home_selector a#garden img").stop().animate({bottom : 0}, 250);
		
		if (canfade) { 
			$("#home_selector a#pest").stop().fadeTo(250,1);
		}
	});
	
	$("#home_selector a#pest").mouseenter(function() {
		$("#home_selector a#pest img").stop().animate({bottom : 8}, 250);
		
		if (canfade) { 
			$("#home_selector a#garden").stop().fadeTo(250,0.6);
		}
	});
	
	$("#home_selector a#pest").mouseleave(function() {
		$("#home_selector a#pest img").stop().animate({bottom : 0}, 250);
		
		if (canfade) { 
			$("#home_selector a#garden").stop().fadeTo(250,1);
		}
	});
	
	//tab controls 
	
	$(".tab_page").hide();
	$(".tab_pages .tab_page:first-child").show();
	$(".tab_pages .tab_page:first-child").addClass('current_tab');
	
	//tab navigation
	$(".tabs a").click(function(event) {
		event.preventDefault();
		$(".tabs a").removeClass('active');
		$(this).addClass('active');
		

		tabToShow = $(this).attr('href');
		tabToShow = tabToShow.substring(tabToShow.indexOf("#") + 1);
		
		$(".current_tab").hide();
		if (canfade){
			$(".tab_page#"+tabToShow).stop().fadeTo(600, 1, function() {$(this).css('filter', '')});
		} else {
			$(".tab_page#"+tabToShow).show();	
		}
		
		//alert("tab to show is: " + tabToShow);
		
		$(".tab_page#"+tabToShow).addClass('current_tab');
	});
	
	//tab hash detect
	
		if(window.location.hash) {
	 
			 //set the value as a variable, and remove the #
			 var hash_value = window.location.hash.replace('#', '');
			 
			 //trigger click on tab
			 $(".tabs a." + hash_value).trigger("click");
			 
		}
	
	//slider
	
	//$('#slides .slide:first-child').clone().appendTo('#slides');
	
	if (canfade) {
		$(".arrow-left, .arrow-right").fadeTo(0,0.4);
		} else {
		$(".arrow-left, .arrow-right").hide();
	}
	
		$(window).load(function() {
			
			var animating = false;	
			var sliderwidth = 1122;
			var totalslides = $("div.slide").size();
			var maxwidth = (totalslides * sliderwidth) - sliderwidth;
			var currentslide = 1;
			
			var slideTimer = window.setInterval("$('.arrow-right').trigger('click');", 8000);
			
		
			
		   $('#slides').width(sliderwidth*(totalslides+2));//set the width of the container to the number of images - plus 2 to account for the cloned images
		   $('div.slide').first().addClass('endless_slider_first');//identify the first and last images
		   $('div.slide').last().addClass('endless_slider_last');
		   $('.endless_slider_first').clone().appendTo('#slides');//clone the first image and put it at the end
		   $('.endless_slider_last').clone().prependTo('#slides');//clone the last image and put it at the front
  		   $('#slides').css({'left':-1*sliderwidth+'px'});//reset the slider so the first image is still visible
			
			//create an infinite slider
			//$('div.slide:first-child').clone().appendTo("#slides");
			//$('div.slide:last-child').clone().prependTo("#slides");
			//$("#slides").css('left', sliderwidth);
			
			//alert("max width:" + maxwidth);
	
			function wordsIn(slidemodifier) {
				animating = false;
				var position = $("#slides").position();
				currentpos = position.left;
				$('.slide').removeClass('currentslide');
				
				
				eq = (currentpos / sliderwidth);
				eq = eq - (eq * 2)
				//alert("cur pos: " + currentpos + " eq: " + eq);
				
				if (eq == totalslides + 1) {
					
					currentpos = 0 - (sliderwidth);
					$("#slides").animate({'left': currentpos +'px'}, 0);
		
					
					eq = 1;
					
					//alert("reset! cur pos: " + currentpos + " eq: " + eq);
				}
				
				else if (eq == 0) {
				
					currentpos = 0-(totalslides*sliderwidth);
					$("#slides").animate({'left':currentpos +'px'}, 0);
				
					
					eq = totalslides;
					
					//alert("reset! cur pos: " + currentpos + " eq: " + eq);
				}
				
				
				
				
				curslidediv = $('.slide:eq(' + eq + ')');
				
				curslidediv.addClass('currentslide');
				cta_msg = $(".currentslide").attr('data');
				cta_adjust = $(".currentslide").attr('data-cta');
				cta_href = $(".currentslide").attr('data-link');
				
				//alert("current slide " + currentslide);
				
				//$(".arrow-left, .arrow-right").stop().fadeTo(100,0);
				
				$(".cta").stop(true, true).fadeTo(0,1).animate({top:380}, 0).css('filter', '');
			
				
				$(".cta a").html(cta_msg);
				$(".cta a").attr("href", cta_href);
				
				if (canfade) {
					$(".currentslide .one").stop(true, true).fadeTo(1000,1);
					$(".currentslide .two").stop(true, true).delay(500).fadeTo(1000,1);
					$(".currentslide .three").stop(true, true).delay(1000).fadeTo(1000,1);
					$(".currentslide .four").stop(true, true).delay(1700).fadeTo(1000,1, function() {
						
						$(".cta").stop(true, true).animate({top:280 - cta_adjust}, 1000, 'easeOutBack');
					});
				} 
				else {
					$(".cta").stop(true, true).animate({top:280 - cta_adjust}, 1000, 'easeOutBack');
				}
				
			}
			
			function slideMove() {
		
				//$(".arrow-left, .arrow-right").stop().fadeTo(100,0);
				if (canfade) {
					$(".decor").stop(true, true).delay(250).fadeTo(500,0).delay(1250).fadeTo(1000,1);
					$(".cta").stop(true, true).fadeTo(250,0);	
				}
		
			}
			
			function wordsOut() {
				if (canfade) {
					$(".message img, .cta").stop(true, true).fadeTo(500, 0);
				} else {
					$(".cta").hide();
				}
				
				
			}
			
			wordsIn(0);
			
			var currentpos = -1122;
			
			
				if (canfade) {
					
					$(".arrow-left, .arrow-right").mouseenter(function(){
						slideTimer = window.clearInterval(slideTimer);
						$(this).stop().fadeTo(100,0.8);
					});
					
					$(".arrow-left, .arrow-right").mouseleave(function(){
						$(this).stop().fadeTo(100,0.6);
					});
				
					$("#garden_slider").mouseenter(function(){
						$(".arrow-left, .arrow-right").stop().fadeTo(200,0.6);
					});
					
					$("#garden_slider").mouseleave(function(){
						$(".arrow-left, .arrow-right").stop().fadeTo(200,0.4);
					});
					
					
					$(".cta a").mouseenter(function(){
						slideTimer = window.clearInterval(slideTimer);
						$(this).stop().fadeTo(150,0.6);
					});
					
					$(".cta a").mouseleave(function(){
						$(this).stop().fadeTo(150,1);
					});
				
				} else {
					$(".arrow-left, .arrow-right").show();	
				}
			
			
			
			
			
			$(".arrow-left").click(function(){
				
				
				if(animating == false) {
					animating = true;
					slideMove();
					wordsOut();
					$("#slides").animate({left: (currentpos + sliderwidth)}, 1600, 'easeInOutExpo', function(){ wordsIn(-1); });
					currentpos += sliderwidth;
				}	
				
			});
			
			$(".arrow-right").click(function(){
				
				
				if(animating == false) {
					animating = true;
					slideMove();
					wordsOut();
					$("#slides").animate({left: (currentpos - sliderwidth)}, 1600, 'easeInOutExpo', function(){ wordsIn(1); });
					currentpos -= sliderwidth;
				}
				
			});
		});
	
	
	
	
	//popup
	
	$(".enlarge_image").click(function(event) {
		event.preventDefault();
		popup_title = $(this).find("img").attr("alt");
		popup_content = "";
		popup_img = $(this).attr("href");
		popup_caption = $(this).attr("alt");
		
	
		$(".popup_holder .heading span").html(popup_title);
		$(".popup_holder .body").addClass("enlarged");
		$(".popup_holder .body img").attr("src", popup_img);
	
		$(".popup_holder .body .pop_content").html(popup_content);
		$(".popup_holder .button, .pop_content").hide();
		
		if (IEVer != 7) {$("#mask").fadeTo(250,0.8);}
		
		$(".popup_holder").stop(true).fadeTo(250,1);
		
	});
	
	$(".popup_launcher a").click(function(event) {
		event.preventDefault();
		$(".popup_holder .body").removeClass("enlarged");
		
		popup_title = $(this).parent().find(".pop_title").html();
		popup_content = $(this).parent().find(".description").html();
		popup_img = $(this).parent().find(".bigimg").html();
	
		
		$(".popup_holder .heading span").html(popup_title);
		$(".popup_holder .body img").attr("src", popup_img);
		$(".popup_holder .body .pop_content").html(popup_content);
		
		$(".popup_holder .button").attr("href", $(this).attr("href"));
		
		if (IEVer != 7) {$("#mask").fadeTo(250,0.8);}
	
		
		$(".popup_holder").stop(true).fadeTo(250,1);
	});
	
	$(".close").click(function(event) {
		event.preventDefault();
		
		$("#mask").fadeTo(250,0, function() {$(this).hide();});
		
		
		if (canfade) {
			fadeInTime = 250;	
		} else {
			fadeInTime = 0;	
		}
		$(".popup_holder").stop(true).fadeTo(fadeInTime,0, function() {
			$(this).css('filter', '');
			$(this).hide();
			$(".pop_caption").hide();
			$(".popup_holder .button, .pop_content").show();
			$(".popup_holder .body img").attr("src", "themes/kiwicare/images/ajax-loader.gif");
		});
	});
	


});

})(jQuery);
