function applyWatermark()
{
	$('.watermark').each(function(){
		   if ($(this).val() == $(this).attr('title')) {$(this).addClass('on');}
		   $(this).focus(function(){
				   $(this).filter(function() {return $(this).val() == "" || $(this).val() == $(this).attr('title')}).removeClass("on").val("");
				   $(this).removeClass("swap_value");
		   });
		   $(this).blur(function(){
				   $(this).filter(function() {return $(this).val() == ""}).addClass("on").val($(this).attr('title'));
				   $(this).addClass("swap_value");
		   });
	});
}

function listTypeSelectorHome(){
	 /*index page - switch between gallery and list view of products */
	 $("#listTypeSelector .listType a").click(function(){
		$this = $(this);
		if(!$this.hasClass("active")) {
			 $("#listTypeSelector .listType a").each(function(){
			    $(this).removeClass("active");
			 });
			$(this).addClass("active");
			var relActive = $this.attr("rel");
			
			$("#listContainer .homeBlock").each(function(){
			    if($(this).hasClass(relActive)){
			        $(this).css("display","block");
			    } else{
			        $(this).css("display","none");
			    } 
			});
		} 
		return false; 
	});
	 $("#listTypeSelector .sort a").click(function(){
	     $("#listTypeSelector .sort a").each(function(){
	        $(this).removeClass("active");
	     });
	     $(this).addClass("active");
	});
}

function listTypeSelectorFrame() {
    /*index page - switch between gallery and list view of products */
    $("#listTypeSelector .listType a").click(function() {
        $this = $(this);
        if (!$this.hasClass("active")) {
            $("#listTypeSelector .listType a").each(function() {
                $(this).removeClass("active");
            });
            $(this).addClass("active");
            var relActive = $this.attr("rel");
            var frame = document.getElementsByName('CompareIFrame')[0];
            var jframe = $("#" + frame.id);

            if (relActive)
                jframe.contents().find("body .HiddenField").val(relActive);

            jframe.contents().find("body #listContainer .contentProduct").each(function() {
                if ($(this).hasClass(relActive)) {
                    $(this).css("display", "block");
                } else {
                    $(this).css("display", "none");
                }
            });

            SetLinkToSelectedView();
        }
        return false;
    });
    $("#listTypeSelector .sort a").click(function() {
        $("#listTypeSelector .sort a").each(function() {
            $(this).removeClass("active");
        });
        $(this).addClass("active");
    });
}

function SetLinkToSelectedView() {
    $(".filterBlockDescription a, .filterResults a").each(function() {
        var t = $(this);
        if (t.attr('href')) {
            t.attr('href', t.attr('href').replace('&view=list', ''));
            t.attr('href', t.attr('href').replace('&view=gallery', ''));

            if (IsGallerySelected()) {
                t.attr('href', t.attr('href') + '&view=gallery');
            }
            else {
                t.attr('href', t.attr('href') + '&view=list');
            }
        }
    });
}

function IsGallerySelected() {
    return $("#listTypeSelector .listType .blockGallery a").hasClass("active");
}

function IsListSelected() {
    return $("#listTypeSelector .listType .blockList a").hasClass("active");
}

function mainNavReposition(){
	$("#navigation.horizontal .level2:first").css("left","1px");	
}

function mainNavigationHoverState(){
	/* main navigation hover state */
	$("#navigation ul.level1 > li").hover(
		function(){
			var $this = $(this);
			var cssHover = $this.children('ul').length ? 'over' : 'over_empty';
			$this.addClass('over').children('a').addClass(cssHover);
		},
		function(){
			var $this = $(this);
			var cssHover = 'over';
			var cssHover = $this.children('ul').length ? 'over' : 'over_empty';
			$this.removeClass('over').children('a').removeClass(cssHover);
		});	
}

function bntHover(){
    backgroundHoverBehavior('.buttons .back', '-348px', '-374px');
	backgroundHoverBehavior('.buttons .print', '-52px', '-78px');
	backgroundHoverBehavior('.buttons .seeall', '-104px', '-130px');
	backgroundHoverBehavior('.buttons .promotions', '-156px', '-182px');
    backgroundHoverBehavior('.buttons .next', '-400px', '-426px');
	backgroundHoverBehavior('.buttons .update', '-452px', '-478px');
	backgroundHoverBehavior('.buttons .details', '-364px', '-398px');
	backgroundHoverBehavior('.buttons .compare', '-504px', '-530px');
	backgroundHoverBehavior('.buttons .addCart', '-208px', '-234px');
	backgroundHoverBehavior('.buttons .unavailableCart', '-650px', '-650px');
}


function backgroundHoverBehavior(selector,posOff,posOn)
{
    $(selector).each(function(){	
		$(this).css("background-position","right " + posOff);
		$(this).find("span").css("background-position","left " + posOff);
		$(this).hover(
			function(){
				$(this).css("background-position","right " + posOn);	
				$(this).find("span").css("background-position","left " + posOn);
				
			},
			function(){
				$(this).css("background-position","right " + posOff);	
				$(this).find("span").css("background-position","left " + posOff);
			}
		);
	});		
}

function accessoriesGallery(){
	$(".accessoriesGalleryLarge li").each(function(){
		$(this).css("display","none");
	});
	$(".accessoriesGallerySmall	li").eq(0).addClass("selected");
	$(".accessoriesGalleryLarge li:first").css("display","inline-block");
	
	$(".accessoriesGallerySmall li").each(function(){
		$(this).click(
			function(){
				var indexImg = $(this).index(".accessoriesGallerySmall li");
				$(".accessoriesGalleryLarge li").each(function(){
					$(this).css("display","none");
				});
				
				$(".accessoriesGallerySmall li").each(function(){
					$(this).removeClass("selected");
				});				
				$(".accessoriesGalleryLarge li").eq(indexImg).css("display","inline-block");
				$(".accessoriesGallerySmall	li").eq(indexImg).addClass("selected");		
			}
		);	
	});
}

function adjustBgBorderMarginPadding(){
	$(".productOngletCategoryBlock .productOngletBlockInner:last-child").css("border","none");
	$("#onglet li:first").css("margin-left","8px");
	$("#onglet li:last").css("margin-left","8px");
	$(".accessories td:first-child").css("padding-left","0");
	$("#recommendedProductsList li:last").css("background","none");
}

function ongletImgCenter(){
	$(".productOngletBlockInner").each(function(){
		if($(this).find(".productOngletImage").height() < $(this).find(".productOngletSideRight").height()){
			$(this).find(".productOngletImage").css("height",$(this).find(".productOngletSideRight").height()+"px");
			var paddingTop = ($(this).find(".productOngletImage").height() - $(this).find("img").height())/2;
			$(this).find(".productOngletImage").css("padding-top", paddingTop+"px");
			$(this).find(".productOngletImage").css("height", $(this).find(".productOngletImage").height()-paddingTop+"px");
		}
	});	
}

function ongletClickAction(){
	var ongletToSelect = 0;
	var ongletIndex = 0;
	indexOngletLiSelected = 0;
	$("#onglet li").each(function(){
		$(this).click(
			function(){
				var indexOngletLiSelected = $(this).index("#onglet li");
				$("#onglet li").each(function(){
					if($(this).index("#onglet li") == indexOngletLiSelected){
						$(this).addClass("selected");	
					} else {
						$(this).removeClass("selected");
					}
				});
				$("#productOngletBlock .ongletView").each(function(){
					var ongletToSelect = indexOngletLiSelected;
					var ongletIndex = $(this).index("#productOngletBlock .ongletView");
					if(ongletIndex == ongletToSelect){
						$(this).css("display","block");
						if(ongletIndex == 2 || ongletIndex == 3){
							ongletImgCenter();	
						}
					} else{
						$(this).css("display","none");	
					}
				});				
			}
		);			
		
	});
}

function addSpanButtons(){
    $(".buttons a").each(function(){
        var contentLink = $(this).text();
        $(this).html("<span>"+contentLink+"</span>");
    });
}


function leftMenuLevel1AddClassHover(){
    $(".one li").hover(
        function(){
            $(this).addClass("oneLiHover");
        },
        function(){
            $(this).removeClass("oneLiHover");
        }
    );
}

function skinCompareTable(){
    $('.compare tbody > tr').each(function(){
        $('td:odd', $(this)).css({'background-color': '#eee'})
        $('td:first', $(this)).css({'border-left':'none'})
        $('td:first', $(this)).css({'width':'155px'})
        $('td:last', $(this)).css({'border-right':'none'})
    });
    
    $(".compare thead td:first").css({'border-left':'none'})
    $(".compare thead td:last").css({'border-right':'none'})
}

function skinSpecTable(){
    $(".specTable tr:even").each(function(){
        $(this).css({'background-color': '#eee'});
        
    });
}

function paginationHidden(){
   if($(".paginationPrev").attr("disabled")=="disabled"){
        $(".paginationPrev").css("display","none");
   }
   
   if($(".paginationNext").attr("disabled")=="disabled"){
        $(".paginationNext").css("display","none");
   } 
}

function mainMenuLevel2Bottom(){
    $(".level1 li").hover(
        function(){
                var widthL2 = $(this).find("ul").width()-6+"px";
                $(this).find(".level2Center").css("width",widthL2);
                $(this).find(".level2Left").css("display","block");
                $(".level2Right").css("display","block");
                $(".level2Center").css("display","block");
        },
        function(){
                $(".level2Left").css("display","none");
                $(".level2Right").css("display","none");
                $(".level2Center").css("display","none");
        }
    );
}

/* Returns query parameter value */
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}


$(document).ready(function() {
    $('#headerQuicklinks li:first-child, #breadcrumbs li:first-child, ul.one li:first-child, #bottomContent .col:first-child, .box li:first-child, #navigation li:first-child, .accessories .box:first-child').addClass('first'); /* to apply proper styling in IE6 which does not undestand :first-child */

    $('.box li:last, #navigation li:last, ul.level2 li:last-child').addClass('last'); /* IE7 + different last item on main nav */

    $('#navigation ul.level1 > li').each(function(index) {
        $(this).addClass('item' + ++index);
    });


    applyWatermark();
    mainNavigationHoverState();

    if (frames['CompareIFrame']) {
        listTypeSelectorFrame();
    }
    else {
        listTypeSelectorHome();
    }
   
    mainNavReposition();
    skinCompareTable();
    skinSpecTable();
    addSpanButtons();
    bntHover();
    accessoriesGallery();
    adjustBgBorderMarginPadding();
    ongletClickAction();
    leftMenuLevel1AddClassHover(); 
   $(".category tr").each(function(){
      $(this).find(".h2Category").eq(0).css("width","191px");
      $(this).find(".h2Category").eq(1).css("width","191px");
   });
   
   $(".categoryList tr").each(function(){
      $(this).find("td").eq(0).css("width","191px");
      $(this).find("td").eq(1).css("width","191px");   
      $(this).find(".categorySubTitle:last").css("margin-right","0");
   });

    paginationHidden();
    mainMenuLevel2Bottom();
    
});