$(document).ready(function(){	
    var hackIE = $.browser.msie;
	var distance = 10;
	var time = 150;
	var hideDelay = 0;		
	var hideDelayTimer = null;		
	var beingShown = false;
	var shown = false;		
		
	var info = $('#bubbleInfo .gradient');
	var prodnav	= $('.subpage #product_nav_holder');	
	var enquiry = $('#enquiryBox .feedbackContainer');	
	var enquiryProduct = $('#prod_enquiry .timeContainer');

    //hacking product thumbnail sizes    
    $("#product_listing .thumb img, #product_detail .thumb").each(function(){        
        if($(this).width() > 166) {$(this).addClass("maxWidth");}
        else {
            if($(this).height() > 166) {$(this).addClass("maxHeight");}    
        }
    });
       				
    //hover
	$("#product_nav li, #homepage_tab_menu .tab, ul.tabs .tab, #history_tab_menu .tab").hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");}); 
	
	//adding class last
	$("#product_nav li:last-child, .second_size li:last-child, .size li:last-child").addClass("last").hover(function(){$(this).addClass("lastHover");},function(){$(this).removeClass("lastHover");}); ;
	
	//remove click border
	$("input.submit, #promotions a, #product_nav a, #menu a, ul.tabs .tab a, .tiles a").focus(function() {$(this).blur();});
						
    //menu
    $("#menu ul.clearer").children("li:last-child").addClass("last_menu");
	$("#menu li").hover(
		function(){
			if ($(this).attr("class") == "last_menu"){$("#menu").addClass("hover");}           
			$(this).addClass("hover");
		},
		function(){
			if ($(this).attr("class") == "last_menu hover"){$("#menu").removeClass("hover");}
			$(this).removeClass("hover");
		}
	);
    
	//product navigation	
	$("#product_nav li ul").each(function(){				
		var holder = $(this).parents('.second_holder');
		var li_parent = $(this).parent('li');
		var count_children = $(this).children().length; 			
		var margin_top = 0;		
		if (count_children == 1){margin_top = 1;}
		else if (count_children == 2){margin_top = 17;}
		else if (count_children > 10){margin_top = ((count_children*27)/1.5)-2;}
		else {margin_top = ((count_children*27)/2/1.5)+2;}					
		holder.css("margin-top","-"+margin_top+"px");	
	});	 
	$("#product_nav li ul li:first-child").addClass("first");
		
   	//subpage - product navigation 
   	$('.subpage h2.product_range, .subpage #product_nav_holder').mouseover(function(){
   		if (hideDelayTimer) clearTimeout(hideDelayTimer);
   		if (beingShown || shown) {return;} 
		else {beingShown = true; prodnav.slideDown("fast", function(){beingShown = false; shown = true;});}
		return false;
   	}).mouseout(function(){
   		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		hideDelayTimer = setTimeout(function () {
            hideDelayTimer = null;
			prodnav.slideUp("slow", function(){shown = false; prodnav.css('display', 'none');});           
        }, hideDelay);
        return false;
   	});  			
	
	//subpage promotions hover
	var total_promotions = $("#promotions dt").length;	
	$("#promotions").css("height",((total_promotions*35)+130)+"px");
	$("#promotions dt").mouseover(function(){
		$(this).addClass("hover").next("dd").slideDown(200).siblings("dd").slideUp("fast");
		$(this).siblings("dt").removeClass("hover");
	}); 	
	
	//switch to digital	
	$('.custom_s2d dt').click(function(){        	
		if($(this).hasClass('selected')) {
			$(this).removeClass('selected');
			$(this).removeClass($(this).next('dd').attr('class')+'_selected');			
			$(this).next('dd').hide();
		}
		else {
			$(this).addClass($(this).attr('class')+'_selected');
			$(this).addClass('selected');	    			
			$(this).next('dd').show();
		}
	    return false;}); 
	    
	$('.custom_s2d .tab_holder a').focus(function() {$(this).blur();});
	$('.custom_s2d .tab_holder .tab_1, .custom_s2d #s2d_individual .viewOtherTab a').click(function(){
        $('.custom_s2d .tab_holder').removeClass('selected');
        $('.custom_s2d #s2d_package').show();
        $('.custom_s2d #s2d_individual').hide();
	    return false;
    }); 
    $('.custom_s2d .tab_holder .tab_2, .custom_s2d #s2d_package .viewOtherTab a').click(function(){
        $('.custom_s2d .tab_holder').addClass('selected');
		$('.custom_s2d #s2d_individual').show();
		$('.custom_s2d #s2d_package').hide();
	    return false;
    });  
    
    //ducted air cond	
	$(".custom_ducted .cost .costLink img").click(function(){        	
		if($(this).parent().hasClass('openLink')) {
			$(this).parent().removeClass('openLink');
			$(this).parent().siblings().removeClass('openData');
		}
		else {		    
			$(this).parent().addClass('openLink');	
            $(this).parent().siblings().addClass('openData');
		}
	    return false;
	}); 
				
	//tabs - homepage
	$("#weekly_special_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$("#product_review_tab").hide();$("#weekly_special_tab").show();return false;});
	$("#product_review_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$("#weekly_special_tab").hide();$("#product_review_tab").show();return false;});
	
	//tabs - product enquiry
	$("#prod_enquiry_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$("#prod_info_tab").hide();$("#prod_enquiry_tab").show();return false;});
	$("#prod_info_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$("#prod_enquiry_tab").hide();$("#prod_info_tab").show();return false;});
	
	//tabs - product reviews - behaves a bit different than other tabs
	//$("#prod_review_videos_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$(".prod_review_article_tab").addClass("hide");$(".prod_review_videos_tab").removeClass("hide");return false;});
	//$("#prod_review_article_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$(".prod_review_videos_tab").addClass("hide");$(".prod_review_article_tab").removeClass("hide");return false;});

    //tabs - bing lee history - behaves a bit different than other tabs
	$("#timeline_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$(".our_history_tab").addClass("hide");$(".timeline_tab").removeClass("hide");return false;});
	$("#our_history_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$(".timeline_tab").addClass("hide");$(".our_history_tab").removeClass("hide");return false;});	

    //tabs - buying tips - behaves a bit different than other tabs
	//$("#tips_product_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$(".tips_general_tab").addClass("hide");$(".tips_product_tab").removeClass("hide");return false;});
	//$("#tips_general_link").click(function(){$(this).parent().siblings().removeClass("selected");$(this).parent().addClass("selected");$(".tips_product_tab").addClass("hide");$(".tips_general_tab").removeClass("hide");return false;});		
	
	//product filter selected hover state
	$("#filter_selection dt span.remove input").hover(function(){$(this).attr('src','images/2009/bg/crossOff.png');},
	function(){$(this).attr('src','images/2009/bg/crossOn.png');});

    //virtual tour
	$('a.view_tour').click(function(){
	    $('#tour_listing li').removeClass("selected");
	    var selected_tour = $(this).attr("id");
	    var selected_tour_video = "#" + selected_tour + "_tour";
	    $(selected_tour_video).addClass("selected");
	    
	    var li_parent = $(this).parent().get(0);
	    $(li_parent).addClass("selected");
	    return false;
	});
	
	var current_url = window.location.href;	
	var is_virtual_tour = current_url.indexOf("Virtual+Tours.aspx?tour=");
	if (is_virtual_tour > 0){	    
	    var selected_tour = "#" + current_url.substring(is_virtual_tour+24,current_url.length);
	    var selected_tour_video = selected_tour + "_tour";
	    
	    var if_tour_exist = $(selected_tour).length;
	    
	    if(if_tour_exist > 0){
	        $('#tour_listing li').removeClass("selected");
	        $(selected_tour).parent().addClass("selected");
	        $(selected_tour_video).addClass("selected");
	    }	    
	}		
    
	//negotiable button trigger	
   	$('.trigger, #bubbleInfo .gradient').mouseover(function(){
   		if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {return;} 
            else {
                beingShown = true; // reset position of info box				
				if(hackIE) {info.css({top: $(this).y()-50, left: $(this).x()+70, display: 'block'}).animate({top: '-=' + distance + 'px'}, time, 'easein', function() {beingShown = false;shown = true;});}
				else {info.css({top: $(this).y()-50, left: $(this).x()+70, display: 'block'}).animate({top: '-=' + distance + 'px',opacity: 1}, time, 'easein', function() {beingShown = false; shown = true;});}
            }
            return false;
   	}).mouseout(function(){   	
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer = setTimeout(function () {
            hideDelayTimer = null;
			if(hackIE) {info.animate({top: '+=' + distance + 'px'}, time, 'easeout', function () {shown = false;info.css('display', 'none');});}
			else {info.animate({top: '+=' + distance + 'px',opacity: 0}, time, 'easeout', function () {shown = false;info.css('display', 'none');});}                
        }, hideDelay);
        return false;
   	});
   	
	//email to friend
	var send_email_to_friend = $('#email_to_friend .gradient');	
	$('li.email_friend a').click(function(){	    
		if(hackIE) {send_email_to_friend.css({top: $('#content').y()+190, left:$('#content').x()+300, display: 'block'}).animate({top: '-=' + distance + 'px'}, time, 'easein');}
		else {send_email_to_friend.css({top: $('#content').y()+190, left: $('#content').x()+300, display: 'block'}).animate({top: '-=' + distance + 'px',opacity: 1}, time, 'easein');}
	    return false;	
	});
	$('a.tellFriendLink').click(function(){	    
		if(hackIE) {send_email_to_friend.css({top: $(this).y()-300, left:$(this).x()-130, display: 'block'}).animate({top: '-=' + distance + 'px'}, time, 'easein');}
		else {send_email_to_friend.css({top: $(this).y()-300, left: $(this).x()-130, display: 'block'}).animate({top: '-=' + distance + 'px',opacity: 1}, time, 'easein');}
	    return false;	
	});
	$('#email_to_friend #close').click(function(){
		if(hackIE) {send_email_to_friend.animate({top: '+=' + distance + 'px'}, time, 'easeout', function () 
		{send_email_to_friend.css('display', 'none');});}
		else {send_email_to_friend.animate({top: '+=' + distance + 'px',opacity: 0}, time, 'easeout', function () {send_email_to_friend.css('display', 'none');});}                
	});
	
	//help pop up
	var helpPopUp= $("#helpPopUp .gradient");
	$('img#helpFilter').click(function(){
	    if(hackIE) {helpPopUp.css({top: $('#content').y()+190, left:$('#content').x()+300, display: 'block'}).animate({top: '-=' + distance + 'px'}, time, 'easein');}
		else {helpPopUp.css({top: $('#content').y()+190, left: $('#content').x()+300, display: 'block'}).animate({top: '-=' + distance + 'px',opacity: 1}, time, 'easein');}
	});
	$('#helpPopUp #shut').click(function(){
		if(hackIE) {helpPopUp.animate({top: '+=' + distance + 'px'}, time, 'easeout', function () 
		{helpPopUp.css('display', 'none');});}
		else {helpPopUp.animate({top: '+=' + distance + 'px',opacity: 0}, time, 'easeout', function () {helpPopUp.css('display', 'none');});}                
	});
	
	//pop up close button hover
	$('#helpPopUp #shut, #email_to_friend #close').mouseover(function(){
	    $(this).attr('src','images/2009/pop_up_close_hover.gif');    
	}).mouseout(function(){
	    $(this).attr('src','images/2009/pop_up_close.gif');    
	});	
	
    //watermarks
	var swapValues = []; $(".watermarks").each(function(i) { swapValues[i] = $(this).val(); $(this).focus(function() { if ($(this).val() == swapValues[i]) { $(this).val("") } }).blur(function() { if ($.trim($(this).val()) == "") { $(this).val(swapValues[i]) } }) })


	//harhour series
	$(".hbTabOne").click(function() {
	
	    $(".hbTabOne").removeClass("hbTabOneSelected");
	    $(".hbTabOne").addClass("hbTabOneSelected");

	    $(".hbTabTwo").removeClass("hbTabTwoSelected");

	    $("#hbBoxTwo").hide();
	    $("#hbBoxOne").show();
	    return false;
	});

	$(".hbTabTwo").click(function() {
	    $(".hbTabTwo").removeClass("hbTabTwoSelected");
	    $(".hbTabTwo").addClass("hbTabTwoSelected");
	    
	    $(".hbTabOne").removeClass("hbTabOneSelected");

	    $("#hbBoxOne").hide();
	    $("#hbBoxTwo").show();
	    return false;
	});

	$("#Forerunner310").wTooltip({ content: "<div style=\"width:250px;\">Ideal for hiking, cycling, surfing and kayaking, the GPS-enabled Forerunner&reg; 310XT is rugged enough for any adventure. It's the only all-in-one device that accurately tracks distance, speed, elevation and heart rate and wirelessly sends data to your computer. Forerunner 310XT is waterproof and boasts a battery life of up to 20 hours.</div>", fadeIn: 600, fadeOut: "slow" });
	$("#Forerunner305").wTooltip({ content: "<div style=\"width:250px;\">The rugged 305 calculates time, distance, pace, calories and heart rate. The high-sensitivity receiver offers great performance, even when running outdoors. Turn the 305 unto a multisport trainer by pairing it with optional speed / cadence sensor for the bike and mounting kit (sold separately), which includes wrist strap with quick-release bracket and bike mount.</div>", fadeIn: 600, fadeOut: "slow" });

    
});
