$(document).ready(function(){
    $(document).attr("openedPopUp", 0);
    $("a.popUpLink").click(function(e){
        openPopUp(e, "block", this, true)
    });
    $("a.popUpLinkHover").hover(function(e){
        openPopUp(e, "block", this, true);
    });
    $("a.closeLink").click(function(e){
        openPopUp(e, "none", this)
    });
    $("a.send_share_link").click(function(e){
        showPopupCss("#popup_send_friend")
    });
    var d = document;
    var odv = d.createElement("div");
    if (document.getElementById("page")) {
        document.getElementById("page").appendChild(odv);
    }
    $(odv).addClass("darklayer");
    //$("div.darklayer").css({'height': $(document).height(), 'width': $(document).width()});	
    $("a.close_features,#page.faqPage .content_wrapper .content-faq .hobs-bar .open-close a.close-a,#page.sense_landing_page .wrap-middle .content_landing_page ul.content_nav li a").click(function(e){
        expandCollapse(e, this, 1)
    });
    $("#page.faqPage .content_wrapper .content-faq .hobs-bar .open-close a.open-a,#page.sense_landing_page .wrap-middle .content_landing_page ul.content_nav li a.openall").click(function(e){
        expandCollapse(e, this, 2)
    });
    BindPopups();
    expandFeature();
});
function expandFeature()/*expands the first element of the key feature tab*/{
    if ($("#page.product_detail_page .principal_list")) {
        var li = $("#page.product_detail_page .principal_list li.main").get(0);
        $(li).find("div.expandContent").css("display", "block");
        
    }
}

/* ------------------------------------------------------------------------------------------------------------------- */
/* ---- Binds all of the click functions, called on page load and subsequent AJAX calls to rebind click functions ---- */
function BindPopups(){
    $("a.popUpLink").click(function(e){
        openPopUp(e, "block", this)
    });
    
    $("a.popUpLinkHover").hover(function(e){
        openPopUp(e, "block", this, true);
    });
    
    $("a.closeLink").click(function(e){
        openPopUp(e, "none", this)
    });
    $(".product_detail_page .product-carousel .carousel-item a.product-image-gallery").click(function(e){
        videoToggle(e, ".expose-image", this)
    });
    $("a.video-image-link,a.open_video_container").click(function(e){
        videoToggle(e, ".expose-video", this)
    });
    $("a.expan_link").click(expandCollapse);
    $("a.close_features").click(closeAllExpanded);
    
    
    $("#page.sense_landing_page .wrap-middle .content_landing_page ul.content_nav li a").click(function(e){
        expandCollapse(e, this, 1)
    });
    $("#page.sense_landing_page .wrap-middle .content_landing_page ul.content_nav li a.openall").click(function(e){
        expandCollapse(e, this, 2)
    });
    
    
    $("a.close_features").click(function(e){
        expandCollapse(e, this, true)
    });
}

var closeAllExpanded = function(e, obj, closeAll){
    e.preventDefault();
    $(".expanded", $(obj).attr("closeGroup")).removeClass("expanded");
    $("div.expandContent", $(obj).attr("closeGroup")).css("display", "none");
}

var expandCollapse = function(e, obj, closeAll){
    e.preventDefault();
    if ($("div.expandContent", $(this).closest("li, div")).css("display") == "none") {
        /*$( "div.expandContent", $(this).closest( "li, div" ) ).slideDown( "fast" );*/
        $("div.expandContent", $(this).closest("li, div")).css("display", "block");
        $(this).addClass("expanded");
        fixHeightProdDetail();
    }
    else {
        /*$( "div.expandContent",$(this).closest( "li, div" ) ).slideUp( "fast" );*/
        $("div.expandContent", $(this).closest("li, div")).css("display", "none");
        $(this).removeClass("expanded");
        fixHeightProdDetail();
    }
    e.stopPropagation();
}

/*collapsable for my account site*/
$(document).ready(function(){
    $("#page.my-account-page #box a.expan_link").click(expandCollapseAccount);
});

function expandCollapseAccount(e){
    e.preventDefault();
    if ($(this).closest(".contentgrad_header").next(".expandContent").css("display") == "none") {
        $(this).closest(".contentgrad_header").next(".expandContent").css("display", "block");
        $(this).addClass("expanded");
    }
    else {
        $(this).closest(".contentgrad_header").next(".expandContent").css("display", "none");
        $(this).removeClass("expanded");
    }
}


var videoToggle = function(e, flag, obj){
    e.preventDefault();
    $(".displayedDiv").css("display", "none");
    $(".displayedDiv").removeClass("displayedDiv");
    $(flag).css("display", "block");
    $(flag).addClass("displayedDiv");
    try {
        if ($($(obj).attr("href")).css("display") == "none") {
            openPopUp(e, "block", obj, true);
        }
    } 
    catch (e) {
    }
    zoom.init();
}
var checkObj = function(e){
    var popup = $(document).attr("openedPopUp");
    if (!popup == "#tab_video") {
        $(popup).css("display", "none")
        $(document).attr("openedPopUp", 0);
        $(document).unbind("click", checkObj)
        $(popup).unbind("click", stopClick);
        $("div.darklayer").css("display", "none");
    }
    
}
var calcPos = function(obj1, obj2){
    var pos = $(obj1).offset();
    var pos2 = $(obj2).offset();
    var position = {
        top: 0,
        left: 0
    }
    position.top = pos.top + $(obj1).height() / 2 - pos2.top;
    position.left = pos.left + $(obj1).width() - pos2.left;
    return position
}
var stopClick = function(e){
    e.stopPropagation();
}
var openPopUp = function(e, flag, obj, nodarklayer){
    e.preventDefault();
    e.stopPropagation();
    if ($(document).attr("openedPopUp") != 0) 
        $($(document).attr("openedPopUp")).css("display", "none");
    var popup = $(obj).attr("href");
    $(popup).css("display", flag)
    if (flag == "block") {
        if (!nodarklayer) 
            $("div.darklayer").css("display", "block");
        $(document).attr("openedPopUp", popup);
        $(document).bind("click", checkObj);
        $(popup).bind("click", stopClick);
    }
    else {
        $(document).attr("openedPopUp", 0);
        $(document).unbind("click", checkObj)
        $(popup).unbind("click", stopClick);
        $("div.darklayer").css("display", "none");
    }
}
function showPopupCss(obj){
    popup = $(obj);
    if ($(popup).css("display") == "none") {
        $(popup).css("display", "block");
        document.openedPopUp = popup;
    }
}
/*Zoom Picture Config*/
var zoom;
$(document).ready(function (){
	if($("#page.product_detail_page").length > 0){
		$("#wrapThumb").append($("#large"));	
		$("#mover").remove();
	}
	try {
        zoom = new jzoom($("#wrapThumb"));
    }catch(error) {
    	alert("Zoom init error");
	}
});
/*Zoom Picture Config END*/
