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('.ProVideo .buttons .back', '-1102px', '-1128px');
	backgroundHoverBehavior('.buttons .print', '-52px', '-78px');
	backgroundHoverBehavior('.ProVideo .buttons .print', '-1154px', '-1180px');
	backgroundHoverBehavior('.buttons .seeall', '-104px', '-130px');
	backgroundHoverBehavior('.ProVideo .buttons .seeall', '-946px', '-972px');
	backgroundHoverBehavior('.buttons .promotions', '-156px', '-182px');
	backgroundHoverBehavior('.ProVideo .buttons .promotions', '-894px', '-920px');
    backgroundHoverBehavior('.buttons .next', '-400px', '-426px');
	backgroundHoverBehavior('.buttons .update', '-452px', '-478px');
	backgroundHoverBehavior('.buttons .details', '-364px', '-398px');
	backgroundHoverBehavior('.buttons .compare', '-504px', '-530px');
	backgroundHoverBehavior('.ProVideo .buttons .compare', '-1049px', '-1075px');
	backgroundHoverBehavior('.buttons .addCart', '-208px', '-234px');
	backgroundHoverBehavior('.ProVideo .buttons .addCart', '-790px', '-816px');
	backgroundHoverBehavior('.buttons .unavailableCart', '-650px', '-650px');
	backgroundHoverBehavior('.ProVideo .buttons .unavailableCart', '-998px', '-998px');
	backgroundHoverBehavior('.buttons .calendar', '-1206px', '-1232px');
}


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 galleryImageBehavior(){
    galleryBehavior('.accessoriesGalleryLarge li','.accessoriesGallerySmall	li');
    galleryBehavior('.workShopGalleryLarge li','.workShopGallerySmall li');
}

function  galleryBehavior(FirstGallery,SecondGallery){
	$(FirstGallery).each(function(){
		$(this).css("display","none");
	});
	$(SecondGallery).eq(0).addClass("selected");
	$(FirstGallery).eq(0).css("display","inline-block");
	
	$(SecondGallery).each(function(){
		$(this).click(
			function(){
				var indexImg = $(this).index(SecondGallery);
				$(FirstGallery).each(function(){
					$(this).css("display","none");
				});
				
				$(SecondGallery).each(function(){
					$(this).removeClass("selected");
				});				
				$(FirstGallery).eq(indexImg).css("display","inline-block");
				$(SecondGallery).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");
	$(".ProVideo .filterBlockInner:first").css("border-right","solid 1px #999");
	$(".ProVideo .filterBlockInner:last").css("border-left","solid 1px #666");
	$(".ProVideo .filterBlockDescription div:last").css("border-bottom","none");
	$(".workShopListColFirstLevel:last .workShopList").css("margin-right","0");
	$(".workShopSchedule th").eq(0).css("width","145px");
	$(".workShopSchedule th").eq(1).css("width","130px");
}

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'})
    });
    $('.ProVideo .compare tbody > tr').each(function(){
        $('td:odd', $(this)).css({'background': '#333'})
    });    
    
    $(".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'});
    });
    $(".ProVideo .specTable tr:even").each(function(){
        $(this).css({'background-color': '#333'});
    });
}

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(){
                /*necessaire pour IE7*/
                if($(this).hasClass("last")){
                    $(this).find(".level2").css("right","1px");
                } else if($(this).hasClass("first")){
                    $(this).find(".level2").css("left","1px");
                } else {
                    $(this).find(".level2").css("left","0");
                } 
                /****************/
                $(this).find("ul").addClass("LIHover");                
                var widthL2 = $(".level2.LIHover").width()-6+"px";
                $(".level2Center").css("width",widthL2);
                $(".level2Left").css("display","block");
                $(".level2Right").css("display","block");
                $(".level2Center").css("display","block");   
                            
        },
        function(){
                 $(this).find("ul").removeClass("LIHover");
        }
    );
}

function proVideoResizeHeightH2(){
    maxHeight = 0;
    $(".ProVideo .h2Category h2").each(function(){
        if($(this).height()>maxHeight){
            maxHeight = $(this).height();
        }
    });
    $(".ProVideo .h2Category h2").each(function(){
         $(this).css("height",maxHeight+"px");
    });
}

/* 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];
}

function paymentCardSkin(){
    $(".paymentCard li:last").css("border-right","none");
    $(".paymentCard li:last").css("margin-right","0");
    $(".paymentCard li:last").css("padding-right","0");
}

function homeOngletsBehavior(){
    $(".tabs li").click(function(){
        var indexOngletSelected = $(this).index(".tabs li");
        $(".tabs li").each(function(){
            $(this).removeClass("selected");
        });
        $(".homeOnglets").each(function(){
            $(this).removeClass("selected");
        });        
        $(".tabs li").eq(indexOngletSelected).addClass("selected");
        $(".homeOnglets").eq(indexOngletSelected).addClass("selected");
    });
}

function emptyCell(){
   emptyCellBehavior(".partnerList td");
}

function emptyCellBehavior(emptyCells){
   $(emptyCells).each(function(){
        if($(this).html()==""){
            $(this).css("background","none");
        }
   }); 
}

function workShopSlideShow(){
	$("#workShopVideoSlideshow").each(function(){
		var sizeLiVideo = $(this).find("li").size();	
		if(sizeLiVideo>4){
			$("#workShopVideoSlideshow").jCarouselLite({
				circular: true,
				vertical:true,
				visible: 4,
				start: 0,
				scroll: 1,
				auto:0,
				speed: 100,
				btnNext: ".nextVideo",
                btnPrev: ".prevVideo"
			});	
		}
	});
    $("#workShopVideoSlideshow li").click(function(){
        var indexSlideSelected = $(this).index("#workShopVideoSlideshow li");
        $("#workShopVideoSlideshow li").each(function(){
            $(this).removeClass("selected");
        });
        $("#workShopVideoDisplay li").each(function(){
            $(this).removeClass("selected");
        });        
        $("#workShopVideoSlideshow li").eq(indexSlideSelected).addClass("selected");
        $("#workShopVideoDisplay li").eq(indexSlideSelected+1).addClass("selected");
    });
}

function suggestedWorkShopSlideShow(){
    $("#suggestedWorkShopsSlideShow").each(function(){
	    $("#suggestedWorkShopsSlideShow").jCarouselLite({
		    circular: true,
		    vertical:false,
		    visible: 1,
		    start: 0,
		    scroll: 1,
		    auto:0,
		    speed: 100,
		    btnNext: ".nextSuggestedWorkShops",
            btnPrev: ".prevSuggestedWorkShops"
	    });	
	});
}

function resizeColsFullMasterPage(){
        $("#center").css("width", "753px");
        $("#sideRight").css("width", "0px");
}

function categoryTableSize(){
       $(".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");
       });
}

$(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-child, 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();
    adjustBgBorderMarginPadding();
    ongletClickAction();
    leftMenuLevel1AddClassHover(); 
    categoryTableSize();
    paginationHidden();
    mainMenuLevel2Bottom();
    proVideoResizeHeightH2();
    homeOngletsBehavior();
    workShopSlideShow();
    suggestedWorkShopSlideShow();
    galleryImageBehavior();
    paymentCardSkin();
    emptyCell();
    totalTD = 0;
    tdToAdd = 0;
    $(".proVideoCategoryList tr").each(function(){
        totalTD = $(this).find("td").size();
        if(totalTD<3 || totalTD>0){
            tdToAdd = 3 - totalTD;
            for(i=0;i<tdToAdd; i++){
                $(this).eq(0).append("<td style='background:none'></td>");
            }
        } 
    });
    $(".ProVideo #listContainer .gallery table tr").each(function(){
        totalTD = $(this).find("td").size();
        if(totalTD<3){
            tdToAdd = 3 - totalTD;
            for(i=0;i<tdToAdd; i++){
                $(this).eq(0).append("<td style='background:none; border-top:none;border-bottom:none;'></td>");
            }
        }  
    });
    $(".ProVideo #listContainer .gallery table tr").each(function(){
        $(this).find("td").eq(2).css("border-right","none");
    });
    $(".proVideoCategoryList tr").each(function(){
      $(this).find("td").eq(2).css("border-right","none");

     });  


    
});
