﻿Tracking = Class.create({
    initialize: function() {
        // if s hasn't been created, we make an empty one
        if(typeof(window.s) == 'undefined') { s = {} }
        // creating an instance of s so we can track multiple events on the page
        this.s = s;
    },
	
	getPageUrl: function() {
        return window.location.href;
    },
    getPageTitle: function() {
        return $$('title')[0].innerHTML;
    },
    getSection: function() {
        return this.getPageUrl().replace('http://', '').split('/')[1];
    },
	
	Register: function() {
        this.s.linkTrackVars = "eVar13,events";
        this.s.linkTrackEvents = "event14";
        this.s.eVar13 = "Registration";
        this.s.events = "event14";
        this.Send('Registration');
    },
	
	Reminder: function(varName) {
        this.s.linkTrackVars = "eVar16,events";
        this.s.linkTrackEvents = "event19";
        this.s.eVar16 = varName;
        this.s.events = 'event19';
        this.s.prop9 = this.getPageUrl();
        this.Send('Reminder');
    },
	
	Favorite: function(varName) {
        this.s.linkTrackVars = "eVar16,events";
        this.s.linkTrackEvents = "event21";
        this.s.eVar16 = varName;
        this.s.events = 'event21';
        this.s.prop10 = this.pageUrl;
        this.Send('Favorite');
    },
	
	Subscribe: function(type, provider) {
        this.s.linkTrackVars = "eVar6,events,prop6";
        this.s.linkTrackEvents = "event15";
        this.s.events = "event15";
        this.s.prop6 = type+':'+provider;
        this.s.eVar6 = type+':'+provider;
        this.Send('Get Starz');
    },
	
	ScheduleDownload: function(filename ) {
		 // ScheduleDownload.aspx
        this.s.linkTrackVars = "eVar15,events";
        this.s.linkTrackEvents = "event24";
        this.s.eVar15 = 'Download : ' + filename;
        this.s.events = 'event24';
        this.s.trackDownloadLinks = true;
        this.s.linkDownloadFileTypes = 'exe,zip,wav,mp3,mov,mpg,avi,doc,pdf,xls';
        this.Send('Download Schedule');
    },
	
	SchedulePrint: function(scheduleName) {
        this.s.linkTrackVars = "eVar15,events";
        this.s.linkTrackEvents = "event25";
        this.s.eVar15 = scheduleName;
        this.s.events = 'event25';
        this.Send('Print Schedule');
    },
	
	
	Promotion: function(event) {
        var promos = $$('.promo');
        if(promos.length != 0) {
            this.s.linkTrackVars = "eVar14,events,prop8";
            this.s.linkTrackEvents = 'event16';
            this.s.prop8 = this.getPageUrl();

            if(event && event.type == 'click') {
                this.s.events = 'event16';
                if (event.currentTarget != undefined) {
                    this.s.eVar14 = event.currentTarget.text;
                } else {
                    this.s.eVar14 = event.target.childNodes[0].nodeValue;
                }
            } 
            this.Send('Promotion');
        } 
    },
	PromotionImpression: function(promoName) {
        var promos = $$('.promo');
        
        if(promos.length != 0) {
            this.s.products = ';';            
            this.s.linkTrackVars = "eVar14,events,prop8";
            this.s.linkTrackEvents = "event23";
            this.s.prop8 = this.getPageUrl();
            this.s.events = 'event23';
            
            // Get the promo name for which the impression is being captured
            promoName = promoName.replace(/box/,"");
            promoName = promoName.replace(/promo/,"");
            this.s.eVar14 = "Promotion Impression:"+ promoName;

            this.Send('Promotion Impression');
        } // end of if #1
    },
	
	
	
    TrackInternalSearch: function(keyword, numberOfResults){
		// @see MovieSearch.aspx
        this.s.linkTrackVars = "prop17,prop18";
        this.s.prop17 = keyword;
        this.s.prop18 = numberOfResults;
        this.Send('TrackInternalSearch');
    },
	
	
    
   
    Send: function(linkName) {
        this.s.prop1 = this.getPageUrl();
        this.s.pagename = this.getPageTitle();
        this.s.channel = this.getSection();
        // attach this.s objects to global s
        Object.extend(s, this.s);

        // insert omniture call here
        if (linkName == 'Download Schedule') {
            s.tl(this, 'd', linkName);
        } else if (linkName == 'External Link') {
            s.tl(this, 'e', linkName)
        } else {       
            s.tl(this, 'o', linkName);
        }
    }
});

Tracking.init = function() {
    if(typeof(window.Schedule) == 'undefined') {
        $$('.reminder').invoke('observe', 'click', function(event) {
            if(event.target.rel == 'add') {
                var tracking = new Tracking();
                tracking.Reminder();
            }
        });
        $$('.favorite').invoke('observe', 'click', function(event) {
            if(event.target.rel == 'add') {
                var tracking = new Tracking();
                tracking.Favorite();
            }
        });
    }
    $$('.download').invoke('observe', 'click', function(event) {
        var tracking = new Tracking();
        //tracking.ScheduleDownload();
    });
    $$('.print').invoke('observe', 'click', function(event) {
        var tracking = new Tracking();
        tracking.SchedulePrint();
    });
    
    $$('.promo').each(function(promo) {
        var tracking = new Tracking();
        promo.down('a').observe('click', tracking.Promotion.bindAsEventListener(tracking));
        tracking.PromotionImpression(promo.className);
    });
};
Event.observe(window, 'load', Tracking.init);



