﻿var currentBannerIndex = 0;

function HandleSiteMaster() {
    SetupDisclaimer();

    SetupPromotionDialog();
	
	SetupTabs();

    SetupAccordion();

    SetupISOMessage();

    SetupTableStyle();
}

function SetupTableStyle() {
    if ($(".TwoColumnInfoTable").length > 0) {
        $(".TwoColumnInfoTable td").css("width", "50%");

        $(".TwoColumnInfoTable tr").each(function(rowIndex) {
            if (rowIndex % 2 == 0) {
                $(this).find("td:odd").addClass("TwoColumnInfoTableStyledField");
            }
            else {
                $(this).find("td:even").addClass("TwoColumnInfoTableStyledField");
            }
        });
    }
}

function SetupTabs() {
    if ($(".InfoTabBlockX").length > 0) {
        $(".InfoTabBlockX").tabs();
    }
}

function SetupAccordion() {
    if ($(".InfoAccordionBlock").length > 0) {
        $(".InfoAccordionBlock").accordion({ autoHeight: false, collapsible: true });
    }
}

function SetupPromotionDialog() {
    if (($("#MCBUPromoTrigger").length > 0) && ($("#MCBUPromo").length > 0)) {
        $("#MCBUPromoTrigger").click(function() {
            $("#MCBUPromo").dialog({ autoOpen: false, modal: true, width: 480 });
            $("#MCBUPromo").dialog("open");
            CentralizeDialog("MCBUPromo");
        });
    }
}

function SetupDisclaimer() {
    if (($("#DisclaimerTrigger").length > 0) && ($("#Disclaimer").length > 0)) {
        $("#DisclaimerTrigger").click(function() {
            $("#Disclaimer").dialog({ autoOpen: false, modal: true, width: 500 });
            $("#Disclaimer").dialog("open");
            CentralizeDialog("Disclaimer");
        });
    }
}

function SetupISOMessage() {
    if (($("#ISOMessageBlockTrigger").length > 0) && ($("#ISOMessageBlock").length > 0)) {
        $("#ISOMessageBlockTrigger").click(function() {
            $("#ISOMessageBlock").dialog({ autoOpen: false, modal: true, width: 700 });
            $("#ISOMessageBlock").dialog("open");
            CentralizeDialog("ISOMessageBlock");
        });
    }
}

function CentralizeDialog(targetId) {
    if ($("#" + targetId).length > 0) {
        var boxWidth = $("#" + targetId).parent().width();
        var boxHeight = $("#" + targetId).parent().height();
        var windowWidth = $(window).width();
        var windowHeight = $(window).height();
        var targetTop = ((windowHeight - boxHeight) < 0) ? 0 : ((windowHeight - boxHeight) / 2);
        var targetLeft = ((windowWidth - boxWidth) < 0) ? 0 : ((windowWidth - boxWidth) / 2);

        $("#" + targetId).parent().css("position", "fixed").css("top", targetTop).css("left", targetLeft);
    }
}

function AnimateBanner(targetSection) {
    $("#BannerContainer img:not(:eq(" + parseInt(currentBannerIndex, 10) + "))").hide();
    $("#BannerContainer img:eq(" + parseInt(currentBannerIndex, 10) + ")").fadeIn("slow");
    currentBannerIndex = (currentBannerIndex + 1) % 5;
    setTimeout(AnimateBanner, 5000);
}

function HandleCareerPage() {

    $(".CareerContent:not(:eq(0))").hide();

    $("#CareerSideLinkBar a").click(function() {
        var targetId = $(this).attr("href");

        $(".CareerContent[id!='" + targetId + "']").hide();

        $(targetId).show();
    });
}

function HandleEnquiryFeedbackPages() {
}
//===================================================================================================
var POSBannerIndex = -1;
var POSBannerCount = 2;
var POSBannerSlideInterval = 5000;

function HandlePOSPage() {
    setTimeout(AnimateBanner_POS, POSBannerSlideInterval);
    TabifyContent_POS()
}

function TabifyContent_POS() {
    $("#ProductInfoTab").tabs();
}

function AnimateBanner_POS() {
    var slideCount = $("#BannerBlock_POS .BannerSlideContainer .BannerSlide").size();

    if (slideCount > 0) {
        var slideContainer = $("#BannerBlock_POS .BannerSlideContainer");

        var slideContainerLeft = $(slideContainer).position().left;

        var slideWidth = $("#BannerBlock_POS .BannerSlideContainer .BannerSlide").eq(0).width();

        var currentSlide = Math.abs(slideContainerLeft) / slideWidth;

        var targetDuration = "slow";

        if ((currentSlide + 1) < POSBannerCount) {
            slideContainerLeft = slideContainerLeft - slideWidth;
        }
        else {
            slideContainerLeft = 0; //reset
        }
        $("#BannerBlock_POS .BannerSlideContainer").animate({ left: slideContainerLeft }, targetDuration, "easeInBounce");

        POSBannerIndex = ((POSBannerIndex + 1) % POSBannerCount);
        setTimeout(AnimateBanner_POS, POSBannerSlideInterval);
    }
}
//===================================================================================================
var McbuBannerIndex = -1;
var McbuBannerCount = 4;
var McbuBannerSlideInterval = 5000;

function HandleMcbuPages() {
    setTimeout(AnimateBanner_MCBU, McbuBannerSlideInterval);
}

function AnimateBanner_MCBU() {
    var slideCount = $("#BannerBlock_MCBU .BannerSlideContainer .BannerSlide").size();

    if (slideCount > 0) {
        var slideContainer = $("#BannerBlock_MCBU .BannerSlideContainer");

        var slideContainerLeft = $(slideContainer).position().left;

        var slideWidth = $("#BannerBlock_MCBU .BannerSlideContainer .BannerSlide").eq(0).width();

        var currentSlide = Math.abs(slideContainerLeft) / slideWidth;

        var targetDuration = "slow";

        if ((currentSlide + 1) < McbuBannerCount) {
            slideContainerLeft = slideContainerLeft - slideWidth;
        }
        else {
            slideContainerLeft = 0; //reset
        }
        $("#BannerBlock_MCBU .BannerSlideContainer").animate({ left: slideContainerLeft }, targetDuration, "linear");

        McbuBannerIndex = ((McbuBannerIndex + 1) % McbuBannerCount);
        setTimeout(AnimateBanner_MCBU, McbuBannerSlideInterval);
    }
}

function ResizeProductInfoTable() {
    if ($(".ProductInfoTable").length > 0) {
        $(".ProductInfoTable tr:first").addClass("header");

        $(".ProductInfoTable tr").each(function() {
            $(this).find("td:eq(0)").addClass("header");
        });

        var parentWidth = $(".ProductInfoTable").parent().width();

        if ($.browser.msie) {
            var colCount = $(".ProductInfoTable tr:first").find("td:visible").length;

            if (colCount > 1) {
                var targetWidth = ((colCount - 1) * 250) + 150;
                targetWidth = (targetWidth < parentWidth) ? parentWidth : targetWidth;
                $(".ProductInfoTable").width(targetWidth);
            }
            else {
                $(".ProductInfoTable").width("100%");
            }
        }
    }
}

function ProductInfoTableShowAll(targetId) {
    if ($("#" + targetId).length > 0) {
        $("#" + targetId + " input[type='checkbox']").attr("checked", true);
        $(".ProductInfoTable td[class^=ColumnIndex]").show();
        ResizeProductInfoTable();
    }
}

function ProductInfoTableClear(targetId) {
    if ($("#" + targetId).length > 0) {
        $("#" + targetId + " input[type='checkbox']").attr("checked", false);
        $(".ProductInfoTable td[class^=ColumnIndex]").hide();
        ResizeProductInfoTable();
    }
}

function SetupProductInfoTable() {
    if ($("#ProductInfoMasterBlock").length > 0) {
        $("#ProductInfoMasterBlock").tabs();
    }
    //======================================================================================
    if ($(".ProductInfoTable").length > 0) {
        $(".ProductInfoTable tr:odd").addClass("AlternateRow");
        //======================================================================================
        ResizeProductInfoTable();
        //======================================================================================
        if ($(".ProductInfoTable tr:first").find("td").length > 2) {

            $(".ProductInfoTable tr").each(function() {
                $(this).find("td:gt(0)").each(function(index) {
                    $(this).addClass("ColumnIndex" + index.toString());
                });
            });
            //======================================================================================
            var addOnBlock = "<fieldset><legend>Filters...</legend><table id='ProductDetailsTable'>";
            var tempIndex = 0;
            var itemPerRow = 3;
            var openRow = false, closeRow = false;

            addOnBlock = addOnBlock + "<tr>";
            addOnBlock = addOnBlock + "<td><a href='' onClick='ProductInfoTableShowAll(\"ProductDetailsTable\"); return false;'>Select All</a></td>";
            addOnBlock = addOnBlock + "<td><a href='' onClick='ProductInfoTableClear(\"ProductDetailsTable\"); return false;'>Clear</a></td>";
            addOnBlock = addOnBlock + "<td>&nbsp;</td>";
            addOnBlock = addOnBlock + "</tr>";

            $(".ProductInfoTable tr:first").find("td:gt(0)").each(function(index) {
                openRow = ((index == 0) || ((index % itemPerRow) == 0));
                closeRow = (((index + 1) % 3) == 0);

                if (openRow) { addOnBlock = addOnBlock + "<tr>"; }

                addOnBlock = addOnBlock + "<td><input type='checkbox' checked='true' onClick='ProductInfoTableShowHideModel(this, " + index + ");'>" + $(this).text() + "</input></td>";

                if (closeRow) { addOnBlock = addOnBlock + "</tr>"; }

                tempIndex = index;
            });

            if ((tempIndex > 0) && (!closeRow)) {
                addOnBlock = addOnBlock + "</tr>";
            }

            addOnBlock = addOnBlock + "</table></fieldset><br />";
            //======================================================================================
            $(".ProductInfoTable").parent().before(addOnBlock);
        }
    }
    //======================================================================================
}

function ProductInfoTableShowHideModel(targetCheckBox, targetColumnIndex) {
    if ((targetCheckBox != null) && (targetColumnIndex != null) && (targetColumnIndex >= 0)) {
        if (targetCheckBox.checked) {
            $(".ProductInfoTable .ColumnIndex" + targetColumnIndex).show();
        }
        else {
            $(".ProductInfoTable .ColumnIndex" + targetColumnIndex).hide();
        }
        ResizeProductInfoTable();
    }
}

function HandleMcbuProductPages(targetMenuBlock) {    
    var theIndex = -1;

    switch (targetMenuBlock) {
        case "netbook":
            {
                theIndex = 0;
                break;
            }
        case "qosmio":
            {
                theIndex = 1;
                break;
            }
        case "satellite":
            {
                theIndex = 2;
                break;
            }
        case "portege":
            {
                theIndex = 3;
                break;
            }
        case "tecra":
            {
                theIndex = 4;
                break;
            }
        case "accessories":
            {
                theIndex = 5;
                break;
            }
        case "serviceSupport":
            {
                theIndex = 6;
                break;
            }
        default:
            {
                theIndex = -1;
                break;
            }
    }

    $("#McbuProductSideLinkBar .SideLinkBarSubContent").hide();

    $("#McbuProductSideLinkBar .SlideLinkBarSectionTitle div a").each(function() {
        $(this).click(function() {
            $("#McbuProductSideLinkBar .SideLinkBarSubContent").hide();
            $(this).parent().parent().next().slideDown("fast");
        });
    });

    if (theIndex >= 0) {
        $("#McbuProductSideLinkBar .SideLinkBarSubContent").eq(theIndex).show();
    }

    SetupProductInfoTable();
}
//===================================================================================================
function HandleEibuProductPages(targetMenuBlock) {
    var theIndex = -1;

    switch (targetMenuBlock) {
        case "colorCopier":
            {
                theIndex = 0;
                break;
            }
        case "largeWorkgroup": 
            {
                theIndex = 1;
                break;
            }
        case "mediumWorkgroup": 
            {
                theIndex = 2;
                break;
            }
        case "smallWorkgroup": 
            {
                theIndex = 3;
                break;
            }
        case "multifunctionFax":
            {
                theIndex = 4;
                break;
            }
        default:
            {
                theIndex = -1;
                break;
            }
    }

    $("#EibuProductSideLinkBar .SideLinkBarSubContent").hide();

    $("#EibuProductSideLinkBar .SlideLinkBarSectionTitle div a").each(function() {
        $(this).click(function() {
            $("#EibuProductSideLinkBar .SideLinkBarSubContent").hide();
            $(this).parent().parent().next().slideDown("fast");
        });
    });

    if (theIndex >= 0) {
        $("#EibuProductSideLinkBar .SideLinkBarSubContent").eq(theIndex).show();
    }
}

var EIBUBannerIndex = -1;
var EIBUBannerCount = 2;
var EIBUBannerSlideInterval = 5000;

function HandleEibuPages() {
    setTimeout(AnimateBanner_EIBU, EIBUBannerSlideInterval);
    TabifyContent_EIBU();
}

function TabifyContent_EIBU() {
    $("#ProductInfoTab").tabs();
}

function AnimateBanner_EIBU() {
    var slideCount = $("#BannerBlock_EIBU .BannerSlideContainer .BannerSlide").size();

    if (slideCount > 0) {
        var slideContainer = $("#BannerBlock_EIBU .BannerSlideContainer");

        var slideContainerLeft = $(slideContainer).position().left;

        var slideWidth = $("#BannerBlock_EIBU .BannerSlideContainer .BannerSlide").eq(0).width();

        var currentSlide = Math.abs(slideContainerLeft) / slideWidth;

        var targetDuration = "slow";

        if ((currentSlide + 1) < EIBUBannerCount) {
            slideContainerLeft = slideContainerLeft - slideWidth;
        }
        else {
            slideContainerLeft = 0; //reset
        }
        $("#BannerBlock_EIBU .BannerSlideContainer").animate({ left: slideContainerLeft }, targetDuration, "easeOutBack");

        EIBUBannerIndex = ((EIBUBannerIndex + 1) % EIBUBannerCount);
        setTimeout(AnimateBanner_EIBU, EIBUBannerSlideInterval);
    }
}

function TabifyContent_EibuSolutions() {
    $("#SolutionInfoTab").accordion({ autoHeight: false, collapsible: true, active: -1 });
}
//===================================================================================================
var BCSBannerIndex = -1;
var BCSBannerCount = 2;
var BCSBannerSlideInterval = 5000;

function HandleBCSPage() {
    setTimeout(AnimateBanner_BCS, BCSBannerSlideInterval);
    TabifyContent_BCS();
}

function TabifyContent_BCS() {
    $("#ProductInfoTab").tabs();
}

function TabifyContent_BcsSolutions() {
    $("#SolutionInfoTab").accordion({ autoHeight: false, collapsible: true, active: -1 });
}

function AnimateBanner_BCS() {
    var slideCount = $("#BannerBlock_BCS .BannerSlideContainer .BannerSlide").size();

    if (slideCount > 0) {
        var slideContainer = $("#BannerBlock_BCS .BannerSlideContainer");

        var slideContainerLeft = $(slideContainer).position().left;

        var slideWidth = $("#BannerBlock_BCS .BannerSlideContainer .BannerSlide").eq(0).width();

        var currentSlide = Math.abs(slideContainerLeft) / slideWidth;

        var targetDuration = "slow";

        if ((currentSlide + 1) < BCSBannerCount) {
            slideContainerLeft = slideContainerLeft - slideWidth;
        }
        else {
            slideContainerLeft = 0; //reset
        }
        $("#BannerBlock_BCS .BannerSlideContainer").animate({ left: slideContainerLeft }, targetDuration, "easeInOutElastic");

        BCSBannerIndex = ((BCSBannerIndex + 1) % BCSBannerCount);
        setTimeout(AnimateBanner_BCS, BCSBannerSlideInterval);
    }
}

function HandleBcsProductPages(targetMenuBlock) {
    var theIndex = -1;

    switch (targetMenuBlock) {
        case "system":
            {
                theIndex = 0;
                break;
            }
        case "phone":
            {
                theIndex = 1;
                break;
            }
        case "solution":
            {
                theIndex = 2;
                break;
            }
        default:
            {
                theIndex = -1;
                break;
            }
    }

    $("#BcsProductSideLinkBar .SideLinkBarSubContent").hide();

    $("#BcsProductSideLinkBar .SlideLinkBarSectionTitle div a").each(function() {
        $(this).click(function() {
            $("#BcsProductSideLinkBar .SideLinkBarSubContent").hide();
            $(this).parent().parent().next().slideDown("fast");
        });
    });

    if (theIndex >= 0) {
        $("#BcsProductSideLinkBar .SideLinkBarSubContent").eq(theIndex).show();
    }
}
//===================================================================================================
function AnimateProductCage(targetIndex) {

    if (targetIndex == null) {
        targetIndex = 0;
    }

    var easingType = "easeInBounce";

    var randomnumber = Math.floor(Math.random() * 11);

    if (randomnumber == 0) { easingType = "easeInBounce"; }
    else if (randomnumber == 1) { easingType = "easeInOutQuint"; }
    else if (randomnumber == 2) { easingType = "easeOutSine"; }
    else if (randomnumber == 3) { easingType = "easeInOutExpo"; }
    else if (randomnumber == 4) { easingType = "easeOutCirc"; }
    else if (randomnumber == 5) { easingType = "easeInOutCirc"; }
    else if (randomnumber == 6) { easingType = "easeOutElastic"; }
    else if (randomnumber == 7) { easingType = "easeInBack"; }
    else if (randomnumber == 8) { easingType = "easeInOutBounce"; }
    else if (randomnumber == 9) { easingType = "linear"; }
    else if (randomnumber == 10) { easingType = "easeInQuad"; }

    var slideCount = $("#Cage1.MasterProductCage .CageContainer .CageSlider").size();

    if (slideCount > 0) {
        var slideContainer = $("#Cage1.MasterProductCage .CageContainer");

        var slideContainerLeft = $(slideContainer).position().left;

        var slideWidth = $("#Cage1.MasterProductCage .CageContainer .CageSlider").eq(0).width();

        var currentSlide = Math.abs(slideContainerLeft) / slideWidth;

        var targetDuration = "slow";

        if ((currentSlide + 1) < slideCount) {
            slideContainerLeft = slideContainerLeft - slideWidth;
        }
        else {
            slideContainerLeft = 0; //reset
        }
        $("#Cage1.MasterProductCage .CageContainer").animate({ left: slideContainerLeft }, targetDuration, easingType);

        targetIndex = ((targetIndex + 1) % slideCount);
        setTimeout(AnimateProductCage, 5000);
    }
}

function HandleProductCatalogPage() {
//    $("#ProductMap img").hover(function() {
//        $(this).show("pulsate");
//    }, function() {
//        $(this).stop();
//    });
}