
var Analytics = Analytics || {}

Analytics.Dashboard = (function(){

    var dashboardObj = {

        isSayli: false,

        dash_seen: null,

        dashboard_el: {
            // dashboard
            "FOUNDATION FINDER"       : [ "Dashboard - Foundation",        "Main" ],
            "MY SKIN CONSULTATION"    : [ "Dashboard - Skin Consultation", "Main" ],
            "MY CONSULTANT"           : [ "Dashboard - My Consultant",     "Main" ],
            "MY HOW TO"               : [ "Dashboard - How to Engine",     "Main" ],
            "LIVE CHAT EMAIL"         : [ "Dashboard - Live Chat",         "Email Consultant Form" ],
            "MY PAST PURCHASES"       : [ "Dashboard - Past Purchases",    "Main" ],
            "MY NEWS"                 : [ "Dashboard - News",              "Main" ],
            "ASK AN EXPERT PRE CHAT"  : [ "Dashboard - Live Chat",         "Pre Chat Form" ],
            "A MESSAGE FROM CLINIQUE" : [ "Dashboard - Main",              "Main" ],
            "ASK AN EXPERT"           : [ "Dashboard - Live Chat",         "Main" ],
            "NEED HELP?"              : [ "Dashboard - Help",              "Main" ]
        },

        initialize: function() {
            this.dash_seen = Cookie.get('DASH_SEEN');
            this.initListeners();
        },

        initListeners: function() {
            var self = this;        

            document.observe("dom:loaded", function(){

                var dash = $('dashboard');
                if (!dash) {
                    return;
                }

                if (dash.down('#sayli_main')) {
                    self.isSayli = true;
                }

                dash.observe('sidebar:onBeforeExpand', function ( evt ) {
                    var proto_element = $( evt.element() ) ;

                    console.log("Analytics.Dashboard: Got sidebar:onBeforeExpand.. Element: ", proto_element);

                    if ( ( self.dash_seen ==null ) && ( (location.pathname == "/") || (location.pathname =="/index.tmpl") || (location.pathname=="/setcountry/index.tmpl")) ){
                        // Skip - Dashboard will open automatically
                    } else {
                        if (self.isSayli) {
                            console.log("Analytics.Dashboard: SAYLI: Creating pageview");
                            cmCreatePageviewTag("Service As You Like It", null, "5000", null);
                            cmCreateConversionEventTag("SAYLI CLICK", 1, "DASHBOARD AB TEST", 20);
                        } else {
                            console.log("Analytics.Dashboard: Old Dash: Creating pageview");
                            cmCreatePageviewTag("My Clinique Dashboard", null, "5000", null);
                            cmCreateConversionEventTag("DASH CLICK", 1, "DASHBOARD AB TEST", 20);
                        }
                    }

                    // self.dash_seen = Cookie.get('DASH_SEEN');
                });

                dash.observe('sidebar:onAfterCollapse', function ( evt ) {
                    self.dash_seen = true;
                });

                if (self.isSayli) {
                    self.initSayliListeners();
                } else {
                    self.initDashboardListeners();
                }
            });

        },

        initSayliListeners: function(){
            var self = this;

            // Grab each section of the sayli dashboard
            $('dashboard').observe('accordion:onBeforeActivate',function(evt){
                var eventEle = $( evt.element() ) ;
                var sectionId = eventEle.identify();
                var sectionName = eventEle.getAttribute('title');

                // If it has a title..
                if (sectionName) {
                    // Create a page element tag when the section is opened
                    if ( eventEle.hasClassName('bucketSubOn') == false ) {
                        console.log("Analytics.Dashboard: SAYLI: Creating element tag for section: " + sectionName);
                        cmCreatePageElementTag('Main', "SAYLI NEW - " + sectionName);
                    }
                }
            });

            // Create a page element tag for each item / icon in the sections
            $$('#dashboard .bucketSubOff').each(function(sectionItem){
                var sectionId = sectionItem.identify();
                var sectionName = sectionItem.getAttribute('title');
                $$('#dashboard #'+sectionId+'_drawer a[title]').each(function(item){
                    var itemTitle = item.getAttribute('title');
                    item.observe('click',function(evt) {
                        console.log("Analytics.Dashboard: SAYLI: Creating element tag for: '"+itemTitle+"' from '"+sectionName+"'");
                        cmCreatePageElementTag(itemTitle, "SAYLI NEW - "+sectionName);
                    });
                });
            });

            $$('#sayli_search form').each(function(item){
                item.observe('submit', function(evt){
                    cmCreatePageElementTag('Go', "SAYLI NEW - Search");
                });
            });
        },

        initDashboardListeners: function(){
            var self = this;

            $$('#db-my-purchases-content a[href="/account/order_history/purchases.tmpl"]').each(function(item){
                item.observe('click', function(evt){
                    cmCreatePageElementTag("View All Results", "Dashboard - Past Purchases");
                });
            });

            $$('#db-my-orders-container a[href="/account/order_history/index.tmpl"]').each(function(item){
                item.observe('click', function(evt){
                    cmCreatePageElementTag("Order Details", "Dashboard - News");
                });
            });

            $$('#special_offers a[href="/cms/product/supercat/offers.tmpl"]').each(function(item){
                item.observe('click', function(evt){
                    cmCreatePageElementTag("Offer Details", "Dashboard - News");
                });
            });

            $$('#db-ask-expert-container a[livechat="1"]').each(function(item){
                item.observe('click', function(evt){
                    cmCreatePageElementTag("Pre Chat Form", "Dashboard - Live Chat");
                });
            });

            // Dashboard Element tags
            document.observe('accordion:onBeforeExpand',function(evt){
                var proto_element = $(evt.element() ) ;

                console.log("Analytics.Dashboard: Got accordion:onBeforeExpand.. Element: ", proto_element);

                var el_tag;
                var a = proto_element.down("a");
                if (a) {
                    var aTxt = a.innerHTML.toUpperCase();
                    el_tag = self.dashboard_el[aTxt];
                }

                if (typeof(el_tag) != "undefined"){
                    if ( a.match("CLINIQUE") ){
                        //console.log("Dashboard: main open");
                        //if ( ( self.dash_seen ==null ) && (location.pathname == "/") ){
                        if ( ( self.dash_seen ==null ) && ( (location.pathname == "/") || (location.pathname =="/index.tmpl") || (location.pathname=="/setcountry/index.tmpl")) ){
                            //skip
                        }else{
                            //console.log('Dashboard: Creating pageview tag');
                            cmCreatePageviewTag("My Clinique Dashboard", null, "5000", null);
                            //cmCreatePageElementTag("Main","Dashboard -  Main");
                        }
                    }
                    if ( (el_tag[1].length > 3) ){
                        if ( ( self.dash_seen ==null ) && ( (location.pathname == "/") || (location.pathname =="/index.tmpl") || (location.pathname=="/setcountry/index.tmpl")) ){
                            //skip
                        }else{
                            //console.log('Dashboard: Creating element tag ', el_tag);
                            cmCreatePageElementTag(el_tag[1],el_tag[0]);
                        }
                    }
                    // console.log(el_tag[1]);
                }
                // console.log(dashboard_el[a]);
                self.dash_seen = Cookie.get('DASH_SEEN');
            });

        }
    };

    dashboardObj.initialize();
    return dashboardObj;
} ) ();

