/* package-version: 2010-07-19-A-axa-direct-2.2 */
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*/
        var clientData = $('a[title="ClientData"]')
        if ( clientData.length == 0 )
        {
        	$(".loginBox").css("display", "block"); 
        	$(".loginBox").load(this.templatesURL+"log-in.html");
        }
        else
        {
        	 	clientData.text(axa.GetFName('AA') + ' ' + axa.GetLName('AA'));
        }	
        
      
    },
    
    
    navMainTimeout: null,
    navMain: function () {
        
        $(".navMain ul li a.active").next("ul").css({visibility:"visible"});
        $(".navMain ul li a.active").each(function(){
            this.activated = true;
        });
        
        $(".navMain ul:first>li>a").mouseover(function() {
            
            $(".navMain ul li a").each(function(){
                    if (!(this.activated == true)) $(this).removeClass('active');
            });
            
            window.clearTimeout(axa.navMainTimeout);
            if (!$(this).hasClass('active')) $(this).addClass('active');
        
        $("ul.scndLvl").css({visibility:"hidden"});
        
        var activeList=$(this).parent().find("ul");
        
        $(activeList).css({visibility:"visible"}); 
        $(activeList).css({visibility:"visible"}); 
        
        }); //end mouseover
        
        $("div.main").bind("mouseleave",function() {
            
            axa.navMainTimeout = window.setTimeout(function() { 
                
                $(".navMain ul li a").each(function(){
                    if (!(this.activated == true)) $(this).removeClass('active');
                });
                
                $("ul.scndLvl").css({ visibility: "hidden"   });
                $(".navMain ul li a.active").next("ul").css({visibility:"visible"});
            }, 1000);
        });
        
    },
    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 zapomniałeś hasła skontaktuj się z Działem Obsługi Klienta pod numerem 22 599 9522.<br/><br/>");
        }
    },
    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);
