﻿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');
    },
    Subscribe: function(type, provider) {
        //console.log('Subscribe', 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');
    },
    ClipSelected: function(category, title, clip) {
        this.s.linkTrackVars = "eVar4,events,prop11";
        this.s.linkTrackEvents = "event4";
        this.s.events = "event4";
        this.s.prop11 = category+' : '+ title + ' : ' + clip;
        this.s.eVar4 = category+' : '+ title + ' : ' + clip;
        this.Send('ClipSelected');
    },
    ClipSent: function(category, title, clip) {
        this.s.linkTrackVars = "eVar4,events,prop12";
        this.s.linkTrackEvents = "event5";
        this.s.events = "event5";
        this.s.prop12 = category+' : '+ title + ' : ' + clip;
        this.s.eVar4 = category+' : '+ title + ' : ' + clip;
        this.Send('ClipSent');
    },
    ClipPreviewed: function(category, title, clip) {
        this.s.linkTrackVars = "eVar4,events,prop13";
        this.s.linkTrackEvents = "event6";
        this.s.events = "event6";
        this.s.prop13 = category+' : '+ title + ' : ' + clip;
        this.s.eVar4 = category+' : '+ title + ' : ' + clip;
        this.Send('ClipPreviewed');
    },
    TitleSelected: function(title, pageName) {
        this.s.linkTrackVars = "eVar5,events,prop14";
        this.s.linkTrackEvents = "event4";
        this.s.events = "event4";
        this.s.prop14 = title + ' : ' + pageName;
        this.s.eVar5 = title + ' : ' + pageName;
        this.Send('TitleSelected');
    },
    CardViewed: function(title) {
        this.s.linkTrackVars = "eVar4,events,prop15";
        this.s.linkTrackEvents = "event7";
        this.s.events = "event7";
        this.s.prop15 = title;
        this.s.eVar4 = title;
        this.Send('CardViewed');
    },
    PromotionImpression: function(promoName) {
        var promos = $$('.promo');
        
        if(promos.length != 0) {
            this.s.products = ';';
            /*for(var i = 0; i < promos.length; i++) {
              //  alert("Promo: " + promos[i].down('h2'));
                if (promos[i].down('h2') != null && promos[i].down('h2') != undefined && promos[i].down('h2') != void) {
                    this.s.products += promos[i].down('h2').innerHTML;
                }
                if(i != promos.length-1) {
                    this.s.products += ',;';
                } // end of if #2
            } // end of for */
            
            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
    },
    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');
        } 
    },
    ScheduleDownload: function(filename ) {
        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');
    },
    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');
    },
   /* Favorite: function(pageUrl) {
        this.s.eVar16 = pageUrl;
        this.s.events = 'event21';
        this.s.prop10 = pageUrl;
        this.Send();
    }, */
    Vongo: function() {
        this.s.events = 'event18';
        this.s.trackExternalLinks = true;
        this.s.linkInternalFilters = 'javascript:,starz.com';
        this.s.linkLeaveQueryString = false;
        this.Send();
    },
    Video: function(videoName, pageName, type) {
        this.s.linkTrackVars = "eVar7,eVar8,events";
        this.s.eVar7 = pageName;
        var linkName = '';
        if(type === 'start') {
            this.s.linkTrackEvents = "event20";
            this.s.events = 'event20';
            linkName = "Video Started: " + videoName;
        } else {
            this.s.linkTrackEvents = "event22";
            this.s.events = 'event22';
            linkName = "Video Ended: " + videoName;
        }
        this.s.eVar8 = pageName+':'+videoName;
        this.Send(linkName);
    },

    VideoBuffer: function(videoName, sessionId, videSessionId, bitRate) {
        this.s.linkTrackVars = "eVar17,events";
        this.s.eVar17 = videoName + ':' + sessionId + ':' + videSessionId + ':' + bitRate;
        this.s.linkTrackEvents = "event27";
        this.s.events = 'event27';
        var linkName = '';
        linkName = "Video Buffering: " + videoName;
        this.Send(linkName);
    },
    VideoStartBandwidth: function(videoName, sessionId, videSessionId, bitRate) {
        this.s.linkTrackVars = "eVar17,events";
        this.s.eVar17 = videoName + ':' + sessionId + ':' + videSessionId + ':' + bitRate;
        this.s.linkTrackEvents = "event20";
        this.s.events = 'event20';
        var linkName = '';
        linkName = "Video Start with Bandwidth: " + videoName;
        this.Send(linkName);
    },
    Send: function() {
        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
    },
    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);


/* Flash Video */
function videoStart(videoName, pageName) {
    var tracking = new Tracking();
    tracking.Video(videoName, pageName, 'start');
}

function videoEnd(videoName, pageName) {
    var tracking = new Tracking();
    tracking.Video(videoName, pageName, 'end');
}

function videoBuffer(videoName, sessionId, videSessionId, bitRate) {
    var tracking = new Tracking();
    tracking.VideoBuffer(videoName, sessionId, videSessionId, bitRate);
}

function videoStartBandwidth(videoName, sessionId, videSessionId, bitRate) {
    var tracking = new Tracking();
    tracking.VideoStartBandwidth(videoName, sessionId, videSessionId, bitRate);
}