
$("doucment").ready(function () {

    if (jQuery("#freeTeaserContainer a").length > 0) {
        var overlay = jQuery("#free_prods_popup");
        jQuery("#freeTeaserContainer a").click(function (event) {
            event.preventDefault();

            overlay.fadeIn();

        });
        overlay.find("div.close_layer a").click(function (event) {
            overlay.fadeOut();
        });
    }

    var prodSubMenu = jQuery("#product_sub_menu");

    if (prodSubMenu.length > 0) {
        prodSubMenu.find(".productMaster ul").hide();
        prodSubMenu.find(".open.productMaster ul").show();

    }

    var serviceSubMenu = jQuery("#service_submenu");

    if (serviceSubMenu.length > 0) {

        serviceSubMenu.find("li.serviceMaster a.serviceMaster").click(function (event) {

            event.preventDefault();
            jQuery(this).parent().removeClass("open")
            jQuery(this).parent().toggleClass("open");
            jQuery(this).parent().find("ul").toggle();

        });
        serviceSubMenu.find(".serviceMaster ul").hide();
        serviceSubMenu.find(".open.serviceMaster ul").show();

    }

});
// Adam Pullen <adam.g.pullen@finalconcept.com.au>
// Durring the payment stage the user has the option to change their billing/shipping country
// This logic hides/shows the US state picker based on the value of the billing/shipping country
function checkBillingState(select) {
    var billingCountrySelect = jQuery(select);
    var billingStateSelect = jQuery("div.checkout select.state.billing");
    if (billingCountrySelect.val().toLowerCase() == "united states") {
        billingStateSelect.removeAttr("disabled");
    } else {
        billingStateSelect.attr("disabled", "disabled");
    }
}
function checkShippingState(select) {
    var shippingCountrySelect = jQuery(select);
    var shippingStateSelect = jQuery("div.checkout select.state.shipping");
    if (shippingCountrySelect.val().toLowerCase() == "united states") {
        shippingStateSelect.removeAttr("disabled");
    } else {
        shippingStateSelect.attr("disabled", "disabled");
    }
}

// start pulldown countries
function ShowPullDown(objID) {
    document.getElementById(objID + '_off').style.display = 'none';
    document.getElementById(objID + '_on').style.display = 'block'
}
function HidePullDown(objID) {
    document.getElementById(objID + '_on').style.display = 'none';
    document.getElementById(objID + '_off').style.display = 'block';
}
// start pulldown language
function ShowPullDownLanguage() {
    document.getElementById('slcPulldownLanguage_off').style.display = 'none';
    document.getElementById('slcPulldownLanguage_on').style.display = 'block'
}
function HidePullDownLanguage() {
    document.getElementById('slcPulldownLanguage_on').style.display = 'none';
    document.getElementById('slcPulldownLanguage_off').style.display = 'block';
}
// show login pop up
function ShowLoginLayer(ObjID1, ObjID2, navItem) {
    document.getElementById(ObjID1).style.display = 'block';
    document.getElementById(ObjID2).style.display = 'none';
    navItem.className = "selected";
}
// show forgott password pop up
function ShowPasswordLayer(ObjID1, ObjID2) {
    document.getElementById(ObjID1).style.display = 'block';
    document.getElementById(ObjID2).style.display = 'none';
}

// function for display/hide layer when clicking on link
function ShowHide(subelemsPrefix, Counter, Link) {
    var div = null;
    var list = null;
    var i = 0;
    div = document.getElementById(subelemsPrefix + i);
    list = document.getElementById('LinkList');
    //alert(Link.parentNode.parentNode.className + "," + list.childNodes[i+1].className);
    while (div != null) {
        div.style.display = 'none';
        if (document.all) {
            list.childNodes[i].className = 'empty';
        }
        else {
            list.childNodes[i].className = 'empty';
        }
        i = i + 1;
        div = document.getElementById(subelemsPrefix + i);
        //alert(list.childNodes[3].className);
    }
    document.getElementById(subelemsPrefix + Counter).style.display = 'block';
    if (document.all) {
        list.childNodes[Counter].className = 'active';
    }
    else {
        list.childNodes[Counter].className = 'active';
    }
    //	alert(list.childNodes[Counter].className);
}

// function for opening and closing the teaser
function OpenCloseTeaser(action) {
    var teaserContent = document.getElementById('teaser_content');
    if (action == "show") {
        teaserContent.style.display = "block";
        document.getElementById("teaser_link_off").style.display = "none";
        $("#teaser_content .teaser_link").show();
    }
    else if (action == "hide") {
        teaserContent.style.display = "none";
        document.getElementById("teaser_link_off").style.display = "block";
        $("#teaser_content .teaser_link").hide(); ;
    }
}

// function for product carrousel 
function class_product_overview() {
    this.easing = 'easeInOutCubic';
    this.animationSpeed = 700;
    this.actWidth = 544;
    this.totalElementWidth = 979;
    this.timeOutTime = 6000; // delay time when product change
    this.timeOut = false;
    this.useRotate = true;
    this.init = function () {
        this.elementCount = jQuery('#prod_overview .prod').length;
        this.inActWidth = (this.totalElementWidth - this.actWidth) / (this.elementCount - 1);
        this.changeProduct(jQuery('#prod_overview .prod:eq(0)')); // instead of "$" write "jQuery" to handle correct with the different librarys
        jQuery('#prod_overview .prod:gt(0)').animate({
            'width': this.inActWidth
        }, this.animationSpeed, this.easing, function () {
        });
    }
    this.changeProduct = function (newActElement) {
        window.clearTimeout(this.timeOut);
        //		jQuery('#prod_overview .prod').stop();
        //		jQuery('#prod_overview .prod img.bg').stop();

        jQuery('#prod_overview .prod.oldAct').stop();
        jQuery('#prod_overview .prod.act').stop();
        jQuery('#prod_overview .prod.act').addClass("oldAct");
        jQuery('#prod_overview .prod.act').removeClass("act");
        jQuery(newActElement).removeClass("oldAct");
        jQuery(newActElement).addClass("act");
        jQuery('#prod_overview .prod.oldAct').animate({
            "width": this.inActWidth
        }, this.animationSpeed, this.easing, function () {
            jQuery(this).removeClass("oldAct");
        }).find('img.bg').hide();
        jQuery(newActElement).animate({
            "width": this.actWidth
        }, this.animationSpeed, this.easing, function () {
            if (prod_overview.useRotate) {
                nextIndex = jQuery(this).index() + 1;
                if (nextIndex == jQuery('#prod_overview .prod').length) {
                    nextIndex = 0;
                }
                //prod_overview.timeOut = window.setTimeout("prod_overview.changeProduct(jQuery('#prod_overview .prod:eq("+nextIndex+")'))", prod_overview.timeOutTime);
            }
        }).find('img.bg').fadeIn(this.animationSpeed * 1.5, function () {
            jQuery('#prod_overview .prod').each(function () {
                if (!jQuery(this).hasClass("act")) {
                    jQuery(this).find("img.bg").hide()
                }
            });
        });
    }
}
var prod_overview = new class_product_overview()
jQuery(function () {
    if (jQuery('#prod_overview').length > 0) {
        jQuery('#prod_overview .prod').each(function () {
            jQuery(this).mouseenter(function () {
                prod_overview.changeProduct(jQuery(this));
            });
        });
        prod_overview.init();
    }
});
var win = null;
function showPopupWindow(url) {


    if (win == null || win.closed) {
        win = window.open(url, "payment_window", 'menubar=no,status=no,toolbar=no');
    } else {
        win.focus();
    }
    // Start polling for the window
    pollPaymentWindow();

}

function pollPaymentWindow() {

    var isOursAgain = false;
    /*try {
        win.document
        isOursAgain = true;
    } catch (e) {
        isOursAgain = false;
    }*/

    if (win && !win.closed) {
        window.setTimeout(function () { pollPaymentWindow() }, 200);
    } else {
        win.close();
        window.location.reload();
    }
}
