﻿window.$ = $telerik.$;
var showWizard;
var isSimulation;
var pageLoaded = false;
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
    if (args.get_error() == undefined) {
        initializeJQuery();
    }
}
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);
function CheckStatus(sender, arg) {
//    var prm = Sys.WebForms.PageRequestManager.getInstance();
//    if (prm.get_isInAsyncPostBack()) {
//        alert('Is InAsynchPostBack');
//        $("#wizardHolder").show();
//    }
//    else {
//        alert('Is Not InAsynchPostBack');
//        $("#wizardHolder").show("slow");
//    }
    showWizard = true;
    pageLoaded = true;
}
function initializeJQuery() {
    $(document).ready(function() {
        if (!pageLoaded) {
            showWizard = getCookie('showWizard');
            isSimulation = getCookie('IsSimulation');
        }
        
        //Scroll to top - is disabled in IE 6
        $("#toTop").scrollToTop({
            speed: "slow"
        });
        //Smooth anchor scrolling
        $.localScroll();
        $("#sliderImgs").cycle({
            timeout: 8000,
            delay: -2000,
            random: 1
        });
        $("#jqTeaserHolder").cycle({
            fx: 'turnDown',
            timeout: 6000,
            delay: -2000,
            pause: 1
        });

        $("#simBtnHome").tooltip({
            position: "bottom left",
            offset: [-2, 152],
            effect: 'slide',
            direction: 'down',
            delay: 500
            //slideOffset: 30,
            //slideOutSpeed: 300
        });
        $("#orderBtnHome").tooltip({
            position: "bottom left",
            offset: [-2, 152],
            effect: 'slide',
            direction: 'down',
            delay: 500
            //slideOffset: 30,
            //slideOutSpeed: 300
        });

        //Order - Simulate Buttons
        $("#orderBtn").click(function() {
            if (!isSimulation || isSimulation == 'false') {
                if (!$("#wizardHolder").is(':visible'))
                    $("#wizardHolder").slideToggle("slow");
            }
            else {
                isSimulation = false;
                $("#wizardHolder").slideToggle(function() {
                    $("#steps").show();
                    $("#simDiv").hide();
                });
                $("#wizardHolder").slideToggle("slow");
            }
        });
        $("#simBtn").click(function() {
            if (!isSimulation || isSimulation == 'false') {
                if (!$("#wizardHolder").is(':visible')) {
                    $("#steps").hide();
                    $("#simDiv").show();
                    $("#wizardHolder").slideToggle("slow");
                }
                else {
                    $("#wizardHolder").slideToggle(function() {
                        $("#steps").hide();
                        $("#simDiv").show();
                    });
                    $("#wizardHolder").slideToggle("slow");
                }
            }
            isSimulation = true;
        });
        //----------------------

        if (showWizard)
            $("#wizardHolder").show();
        if (isSimulation != undefined && (isSimulation == 'true' || isSimulation) && isSimulation != 'false') {
            $("#steps").hide();
            $("#simDiv").show();
        } else {
            $("#steps").show();
            $("#simDiv").hide();
        }
        delCookie('showWizard');
        delCookie('IsSimulation');
    });
}
initializeJQuery();

//Form validation
function submitClick() {
    //return $("#<%=form1.ClientID %>").valid();
    return $(".dotnetform").valid();
}
function showInfo(detailDiv) {
    $("#" + detailDiv).show();
    //$("#extraInfo").slideToggle("slow");
    $("#extraInfo").show();
}
function hideInfo(detailDiv) {
    //$("#extraInfo").slideToggle(function(){
    //  window.console.log('toggle extra info');
    //  $("#" + detailDiv).hide();
    //});
    //window.console.log('hiding the extra info');
    $("#" + detailDiv).hide();
    $("#extraInfo").stop().hide();
}
function showSimOnLoad() {
    setCookie('showWizard', true);
    setCookie('IsSimulation', true);
}
function showOrderOnLoad() {
    setCookie('showWizard', true);
    setCookie('IsSimulation', false);
}
function setCookie(name, value, expires) {
    if (!expires) expires = new Date();
    expires.setDate(expires.getDate()+1);
    //alert(expires);
    document.cookie = name + '=' + escape(value) + '; expires='
        + expires.toGMTString() + '; path=/';
}
function getCookie(name) {
    var dcookie = document.cookie;
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
    while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
        if (dcookie.substring(cbegin, vbegin) == cname) {
            var vend = dcookie.indexOf(";", vbegin);
            if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
        }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
        if (cbegin == 0) break;
    }
    return false;
}
function delCookie(name) {
    document.cookie = name + '=' + '; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
}
