//Helper function for side nav - needs to be out here so setTimeout can see it
function IsNotCurrentPage(index){
    return !($(this).find(".currentitem").length > 0 || $(this).prev().hasClass("currentitem"));
}

//Main jQuery entry-point
$(document).ready(function() {
    //Fix IE6 png transparency
	$(window).load(function(){
	    $.ifixpng('/images/pixel.gif');
	    $("img[@src$=.png]").ifixpng();
	    if($.browser.msie && $.browser.version < 7){
	        $(".secondarynav")
	            .css("backgroundImage", "none")
	            .css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader (src='/images/translucent.png', sizingMethod='scale')");
	        $(".bubble>img").hide();
	        $(".bubble>div").css({backgroundImage: "none", padding: "12% 20px 20px"});
	        $(".bubble").css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader (src='/plans-and-prices/images/bubble_full.png', sizingMethod='scale')");
	    }
	});
    
    //Overlays and helper functions for enlarging box behavior
    var vOverlay = {
        overlayImg: $("[src='/images/v_mouseover_big.jpg']"), 
        left: 0,
        top: 0,
        newLeft: 0,
        newTop: 0
    };
    
    var pOverlay = {
        overlayImg: $("[src='/images/p_mouseover_big.jpg']"), 
        left: 0,
        top: 0,
        newLeft: 0,
        newTop: 0
    };

    var bOverlay = {
        overlayImg: $("[src='/images/b_mouseover_big.jpg']"), 
        left: 0,
        top: 0,
        newLeft: 0,
        newTop: 0
    };

    var xOverlay = {
        overlayImg: $("[src='/images/x_mouseover_big.jpg']"), 
        left: 0,
        top: 0,
        newLeft: 0,
        newTop: 0
    };
    
    function enlargeSquareNav(overlay){
        overlay.overlayImg
            .css("left", overlay.left)
            .css("top", overlay.top)
            .css("width", 96)
            .css("height", 96)
            .css("display", "block")
            .animate({
                left: overlay.newLeft,
                top: overlay.newTop,
                width: 125,
                height: 125
            });
    }

    function shrinkSquareNav(overlay){
        overlay.overlayImg
            .animate({
                left: overlay.left,
                top: overlay.top,
                width: 96,
                height: 96
            }, {complete: function(){$(this).css("display", "none");}});
    }
    
    //Set up enlarging squares behavior
    $(".squarenav img").mouseover(function () {
        var offset = $(this).offset();

        if(!$(this).hasClass("squarenavoverlay"))
        {
            switch($(this).attr("src")) {
                case "/images/v_dim.jpg":
                    vOverlay.left = offset.left;
                    vOverlay.top = offset.top;
                    vOverlay.newLeft = offset.left - 29;
                    vOverlay.newTop = offset.top - 29;
                    enlargeSquareNav(vOverlay);
                    break;
                case "/images/p_dim.jpg":
                    pOverlay.left = offset.left;
                    pOverlay.top = offset.top;
                    pOverlay.newLeft = offset.left;
                    pOverlay.newTop = offset.top - 29;
                    enlargeSquareNav(pOverlay);
                    break;
                case "/images/b_dim.jpg":
                    bOverlay.left = offset.left;
                    bOverlay.top = offset.top;
                    bOverlay.newLeft = offset.left - 29;
                    bOverlay.newTop = offset.top - 1;
                    enlargeSquareNav(bOverlay);
                    break;
                case "/images/x_dim.jpg":
                    xOverlay.left = offset.left;
                    xOverlay.top = offset.top;
                    xOverlay.newLeft = offset.left;
                    xOverlay.newTop = offset.top;
                    enlargeSquareNav(xOverlay);
                    break;
            }
        }
    });
   
    $(".squarenav img.squarenavoverlay").mouseout(function () {
            switch($(this).attr("src")) {
                case "/images/v_mouseover_big.jpg":
                    shrinkSquareNav(vOverlay);
                    break;
                case "/images/p_mouseover_big.jpg":
                    shrinkSquareNav(pOverlay);
                    break;
                case "/images/b_mouseover_big.jpg":
                    shrinkSquareNav(bOverlay);
                    break;
                case "/images/x_mouseover_big.jpg":
                    shrinkSquareNav(xOverlay);
                    break;
            }
    });

    //Set up glow box behavior
    $(".sidesubnavitem").hover(
        function() {
            switch($(this).attr("id")) {
                case "sidesubnavitemred":
                    $(this).find(".hasBackgroundColor").andSelf().animate({backgroundColor: "#FF4844"});
                    break;
                case "sidesubnavitemyellow":
                    $(this).find(".hasBackgroundColor").andSelf().animate({backgroundColor: "#FFCD3B"});
                    break;
                case "sidesubnavitemgreen":
                    $(this).find(".hasBackgroundColor").andSelf().animate({backgroundColor: "#B4E650"});
                    break;
                case "sidesubnavitemblue":
                    $(this).find(".hasBackgroundColor").andSelf().animate({backgroundColor: "#00B4FE"});
                    break;
            }
        },
        function() {
            switch($(this).attr("id")) {
                case "sidesubnavitemred":
                    $(this).find(".hasBackgroundColor").andSelf().animate({backgroundColor: "#E82C2E"});
                    break;
                case "sidesubnavitemyellow":
                    $(this).find(".hasBackgroundColor").andSelf().animate({backgroundColor: "#FDB813"});
                    break;
                case "sidesubnavitemgreen":
                    $(this).find(".hasBackgroundColor").andSelf().animate({backgroundColor: "#A9CF3C"});
                    break;
                case "sidesubnavitemblue":
                    $(this).find(".hasBackgroundColor").andSelf().animate({backgroundColor: "#0098D6"});
                    break;
            }
        }
    ).click(function(){
    	location.href = $(this).find("a").attr("href");
    }).corner();

    //Fix glow box bug in which they move around on resize
    $(window).resize(function() {
        $(".sidesubnavitem").hide().show();
    });

    //Flag current page and open tertiary menu for side nav accordion initial state
    function CurrentPageFilter(){
        return $(this).find("a").filter(function(){
            if($(this).hasClass("multipage"))
                return location.pathname.indexOf($(this).attr("href")) == 0;
            else
                return location.pathname == $(this).attr("href");
        }).length > 0;
    }

    $(".tertiarysidenavmenu").hide();
    $(".secondarysidenavmenuitem")
        .filter(CurrentPageFilter)
        .addClass("currentitem")
        .next(".tertiarysidenavmenu")
        .show();
    $(".tertiarysidenavmenu>div")
        .filter(CurrentPageFilter)
        .addClass("currentitem")
        .parent()
	.show();

    //Side nav accordion behavior, accounting for missing tertiary menus
    $(".secondarysidenavmenuitem").hover(function(e){
        if($(this).next().hasClass("tertiarysidenavmenu"))
            $(this).next().siblings(".tertiarysidenavmenu").filter(IsNotCurrentPage).slideUp("fast");
        else
            $(".tertiarysidenavmenu").filter(IsNotCurrentPage).slideUp("fast");

        $(this).next().filter(".tertiarysidenavmenu").slideDown("normal");
    }, function(e){
    });

    var timeout = null;
    $(".secondarysidenavmenu").hover(function(e){
        if(timeout != null)
        {
            clearTimeout(timeout);
            timeout = null;
        }        
    }, function(e){
        timeout = setTimeout('$(".tertiarysidenavmenu").filter(IsNotCurrentPage).slideUp("fast")', 1000);
    });
    
    //Set up content-level accordions
    $(".compactaccordion").accordion({
        header: ".compactaccordionheader",
		active: false,
	    alwaysOpen: false,
        autoHeight: false
    });

    $(".contentaccordion").accordion({
        header: ".contentaccordionheader",
		active: false,
	    alwaysOpen: false
    }).bind("accordionchange", function(event, ui) {
        ui.oldHeader
            .find("[src='/services/images/arrowopen.png']")
            .attr("src", "/services/images/arrowclosed.png");

        ui.newHeader
            .find("[src='/services/images/arrowclosed.png']")
            .attr("src", "/services/images/arrowopen.png");
    });
    
    //Set up features pop-up bubble
    $("body:has(.features)").mousemove(function(event){
        var found = false;

        if(event.pageX >= $("#featurescontainer").offset().left && event.pageY >= $("#featurescontainer").offset().top)
            $(".features>div").each(function(){
                if(event.pageX > $(this).offset().left && 
                    event.pageX < $(this).offset().left + $(this).innerWidth() &&
                    event.pageY > $(this).offset().top &&
                    event.pageY < $(this).offset().top + $(this).innerHeight() &&
                    $(this).find(".bubbletext").text() != ""){
                        $(".bubble")
                            .css("left", $(this).offset().left)
                            .css("top", $(this).offset().top + 15)
                            .find("div")
                                .text($(this).find(".bubbletext").text())
                                .end()
                            .show(); //.fadeIn("fast");
                        found = true;
                    }
            });

        if(!found)
            $(".bubble").hide(); //.fadeOut("fast");
            
        event.stopPropagation();
    });
    
    //Set up product matrix slide panel
    $(".freetrial").click(function(){
        $(this).siblings(".freetrial").animate({backgroundColor: "#0098D6"});
        $(this).animate({backgroundColor: "#00B4FE"});
        $(".slidepanel").slideDown("normal");
        if ($(this).attr("id").substr(9) == 12)
                $("#freetriallink").attr("href", "https://osu.virtualpbx.com/Message.aspx?plan="
                + $(this).attr("id").substr(9));
			else
                $("#freetriallink").attr("href", "https://osu.virtualpbx.com/ChooseNumbers.aspx?plan="
                + $(this).attr("id").substr(9));
    });

    //Add rounded top-left corner to topgreenseparator
    $("#topgreenseparator").corner({
        tl: { radius: 5 },
        tr: false,
        bl: false,
        br: false
    });

    //Set up tabs
    $(".imagetabs>ul,.tertiarytabs>ul").tabs({
        tabTemplate: '<li><a href="#{href}"><div>#{label}</div></a></li>'
    });
    
    //Add line on right of tertiary tabs
    $(".tertiarytabs>ul").each(function(){
        $(this).tabs("add", "#dummy", "&nbsp;");
        $(this)
            .tabs("disable", $(this).find("li").length - 1)
            .find("li")
            .eq($(this).find("li").length - 1)
            .find("div")
            .css("width", (635 - 96 * ($(this).find("li").length - 1)) + "px");
    });
	
	//Set up large tabs
    $(".imagetabs>ul,.tertiarytabslrg>ul").tabs({
        tabTemplate: '<li><a href="#{href}"><div>#{label}</div></a></li>'
    });
     //Add line on right of large tertiary tabs
    $(".tertiarytabslrg>ul").each(function(){
        $(this).tabs("add", "#dummy", "&nbsp;");
        $(this)
            .tabs("disable", $(this).find("li").length - 1)
            .find("li")
            .eq($(this).find("li").length - 1)
            .find("div")
            .css("width", (635 - 129 * ($(this).find("li").length - 1)) + "px");
    });  

    //Glow button click handlers
    $(".whatbutton").click(function(){ location.href = "/minisite.asp?section=whatisavpbx"; });
    $(".howbutton").click(function(){ location.href = "/minisite.asp?section=howdoesitwork"; });
    $(".whybutton").click(function(){ location.href = "/minisite.asp?section=whychoosevpbx"; });
    $(".helpbutton").click(function(){ location.href = "/minisite.asp?section=helpmechoose"; });
});
 