/* package-version: 2010-02-23-A-axa-direct-with-CPA */
var axa = {
    cpa_application_location : "https://" + (document.location.hostname) +  "/AD.CPA.PS.AccountManagment", //Zawsze https
    cpa_template_location    : (("https:" == document.location.protocol) ? "https://" : "http://") + (document.location.hostname), //Templaty niech laduje sie po aktualnym protokole
    start: function () {
        this.navMain();
        this.switchTabs();
        this.contactTabs();
        this.switchTabsSide();
        this.loginStatus ();
    },
    loginStatus: function () {
        var logStatus = 0;
        if (this.ReadCookie('AA').length == 0) {
            logStatus = 0;
        } else {
            logStatus = 1;
        }
        axa.loginBox (logStatus);
        axa.cpaLink (logStatus);
    },
    loginBox: function(status) {
        this.templatesURL = axa.cpa_template_location + "/templates/"; /* dla poprawnego działania wymagany jest absolutny*/
        switch (status) {
            case 0:
                this.htmlFile ="log-in.html"
                break;
            case 1:
                this.htmlFile = "logged.html"
                break;
            default:
                this.htmlFile ="log-in.html"
                break;
        };

        $(".loginBox").css("display", "block");
        $(".loginBox").load(this.templatesURL+this.htmlFile);
        if (status == 1) {
            $(".nameInfo").css("display", "block");
            $(".nameLine").css("display", "block");

            var content;
            $.get(this.templatesURL+this.htmlFile, function(data) {
                content = data.replace('ClientData',axa.GetFName('AA') + ' ' + axa.GetLName('AA'));
                $(".loginBox").html(content);
                });
        } // end if status == 1
    },
    cpaLink: function(logStatus) {
        var btnLink
        this.templatesURL = axa.cpa_template_location + "/templates/"; /* dla poprawnego działania wymagany jest absolutny*/
        this.htmlFile ="cpa.navi.button.html ";
        $.ajax ({
            type:"GET",
            url: this.templatesURL+this.htmlFile,
            success: function(answer){
                $(".navCpaLink").html(answer);
                if(logStatus==1) {
                    btnLink = axa.cpa_application_location + '/AccountManagement.aspx';
                    $('#btnMojeKonto').attr('class','current');
                } else {
                    btnLink = axa.cpa_template_location + '/twoje-konto/informacje.html';
                    $('#btnMojeKonto').attr('class','');
                }
                $('#btnMojeKonto').attr('href', btnLink);
            }
        });
    },
    navMain: function () {
        $(".navMain ul li a.active").next("ul").css({visibility:"visible"});
        $(".navMain ul:first>li>a").mouseover(function() {
            $("ul.scndLvl").css({visibility:"hidden"});
            var activeList=$(this).parent().find("ul");
            $(activeList).css({visibility:"visible"});
        }); //end mouseover

        $("#wrapper").bind("mouseenter",function() {
            $("ul.scndLvl").css({ visibility: "hidden"   });
            $(".navMain ul li a.active").next("ul").css({visibility:"visible"});
        }); //end mouseenter
    },
    switchTabs: function () {
        $(".tabsPanel ul li a").click(
           function (e) {
                var wasClicked=$(".tabsPanel ul li a").index(this);
                $(".tabsPanel ul li").removeAttr("class");
                $(e.target).parent().addClass("active");
                $(".tabs .panel").css({display:"none"});
                $(".tabs .panel:eq("+wasClicked+")").css({display:"block"});
                return false;
            }
        )

    },
    switchTabsSide: function () {
        $(".body ul #kontakt1 a").click(
            function(){
                $(".tabsPanel ul li a:eq(1)").trigger('click');
            }
        )
        $(".body ul #kontakt2 a").click(
            function(){
                $(".tabsPanel ul li a:eq(2)").trigger('click');
            }
        )
    },
    contactTabs: function () {
        var indexTab = document.location.hash.substr(1);
        if (document.location.href.match("Kontakt")||document.location.href.match("kontakt")) {
            if (indexTab) {
                $(".tabsPanel ul li a:eq("+indexTab+")").trigger('click');
            }

        }

    },
    /////////////////////////////////////////////////////////
    ///
    /// CPA logon/logout functionalities provided by AXA IT
    ///
    ////////////////////////////////////////////////////////
    CPALogin : function()
    {
        //Create the form that will POST logon data
        var form = document.createElement("form");
        form.setAttribute("method", "post");
        form.setAttribute("action", axa.cpa_application_location + "/AccountLogon.aspx?Func=Login&" +  Math.random());
        //form.setAttribute("target", iFrameID);

        //login
        var hiddenField1 = document.createElement("input");
        hiddenField1.setAttribute("name", "login");
        hiddenField1.setAttribute("value", $('#login').attr('value'));

        //password
        var hiddenField2 = document.createElement("input");
        hiddenField2.setAttribute("name", "password");
        hiddenField2.setAttribute("value", $('#pass').attr('value'));

        form.appendChild(hiddenField1);
        form.appendChild(hiddenField2);
        document.body.appendChild(form);
        form.submit();

        //remove this form
        document.body.removeChild(form);

    },
    CPACheckLogin : function()
    {
        if ( $.getUrlVar("status") != null && $.getUrlVar("status").length > 0 ) {
            $(".error").css("display", "block");
            $(".error").html("Logowanie nie powiodło się! Jeśli nie pamiętasz hasła skontaktuj się z nami pod numerem 1 95 22.");
        }
    },
    CPAout : function() {
        window.location = axa.cpa_application_location + "/AccountLogout.aspx";
    },
    ReadCookie : function(cookieName) {
        var theCookie=""+document.cookie;
        var ind=theCookie.indexOf(cookieName);
        if (ind==-1 || cookieName=="") return "";
        var ind1=theCookie.indexOf(';',ind);
        if (ind1==-1) ind1=theCookie.length;
        return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
    },
    GetId : function(cookieName) {
        var cookie = this.ReadCookie(cookieName);
        return  cookie.substring(cookie.indexOf('A2=')+3,cookie.indexOf('&'));
    },
    GetFName : function(cookieName) {
        var cookie = this.ReadCookie(cookieName);
        var amper = cookie.indexOf('&');
        amper = cookie.indexOf('&',amper+1);
        return  cookie.substring(cookie.indexOf('FNAME=')+6,cookie.indexOf('&',amper+1));
    },
    GetLName : function(cookieName) {
        var cookie = this.ReadCookie(cookieName);
        return  cookie.substring(cookie.indexOf('LNAME=')+6,cookie.length);
    }
}

$.extend({
    getUrlVars: function(){
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name){
        return $.getUrlVars()[name];
    }
});

function initialize() {
    axa.start();
}
$().ready(initialize);