// Omniture Vendor Specific File
// Desc: Used to store a single vendors information.
// Vendor: Adperfect
/////////////////////////////////////////////////////////////////////////////

// Vendor Specific Variables
mistats.pagename        = "Vendor: Adperfect: ";
mistats.msr             = mistats.bizunit + "|" + mistats.pubname + "|" + "TP-ADP";
mistats.channel         = "Vendor: Adperfect";
mistats.contentsource   = mistats.sitename + " : " + "Adperfect";
mistats.pagelevel       = "Vendor";

// Taxonomic Variables
mistats.taxonomy        = "ClassAd||||";
mistats.altcategories   = "";
mistats.keywords        = "";

// Functions
// getQSV(key, default_)
// Desc: Used to get a particular querystring value
//////////////////////////////////////////////////////////////////////////////////////////////////////

function getQSV(key, default_) {

  	if (default_==null) default_="";
  	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  	var qs = regex.exec(window.location.href);

	if(qs == null)
    		return default_;
  	else
    		return qs[1];
}

// analyzeAdPerfect()
// Desc: Used to add granular tracking to AdPerfect pages
// Req: Requires the use of getQSV from above
//////////////////////////////////////////////////////////////////////////////////////////////////////

function analyzeAdPerfect() {

	// Get path and querystring values
        mi_path = document.location.pathname;
        mi_query = document.location.search;

	// Check stage to determine correct tagging labels
        if      ( mi_path.match(/complete.html/g ))  { mistats.pagename += "|Complete"; }
        else if ( mi_path.match(/payment.html/g ))   { mistats.pagename += '|Payment'; }
        else if ( mi_path.match(/webad.html/g ))     { mistats.pagename += '|Web Ad'; }
        else if ( mi_path.match(/linerad.html/g ))   { mistats.pagename += '|Liner Ad'; }
        else if ( mi_path.match(/addetails.html/g )) { mistats.pagename += '|Ad Details'; }
	else if ( mi_path.match(/default.html/g ))   { mistats.pagename += '|Start/Choose Category'; }

	// Make sure to get Package ID if available
	// Also grab the classification ID if available
	else if ( mi_query.match(/clsid/g)) {

		var clsid = getQSV('clsid');
		var catid = getQSV('catid');

                mistats.pagename += '|Choose Package';
                mistats.cmsid += '|CLSID: ' + clsid + '|CatID: ' + catid; }

        // Make sure to get Classification ID if available
	else if ( mi_query.match(/catid/g)) {

		var catid = getQSV('catid');

                mistats.pagename += '|Choose Classification';
		mistats.cmsid += '|CATID: ' + catid; }

	// Check to see if curent page is the start/homepage
	else if ( mi_path.length < 2 || mi_query == '' )   { mistats.pagename += '|Start/Choose Category'; }

	// If the page cannot be determined assign Other
	else { mistats.pagename += '|Other'; }
}

// Start Tracking
analyzeAdPerfect();

// Analytics Dynamic Control Panel
function analyticsCP( cp_name, args ) {
    this.cpname     = cp_name;
    this.mi_bs      = document.getElementsByTagName('body')[0];
    this.mi_cp      = document.createElement('div');
    this.mi_ct      = document.createElement('p');
    this.mi_options = args;
}

// Builds the control panel container
analyticsCP.prototype.display = function() {
               
        // Set the default styles of the control panel
        this.mi_cp.setAttribute('id','mistats_cp');
        this.mi_cp.setAttribute('style','padding-top: 10px; color:#fff;font-family:Verdana;width: 100%;position:fixed;left: 0px;bottom: 0px;-moz-border-radius-topleft: 20px;-moz-border-radius-topright: 20px;-webkit-border-top-left-radius: 20px;-webkit-border-top-right-radius: 20px;-khtml-border-top-left-radius: 20px;-khtml-border-top-right-radius: 20px;border-top-left-radius: 20px;border-top-right-radius: 20px;');
        
        // Control Panel Style Overrides
        if(typeof(this.mi_options) !== 'undefined') {
            
            var i = this.mi_options.length;
            
            while(i--) {
                switch(this.mi_options[i].style) {
                    case 'backgroundColor':
                        this.mi_cp.style.backgroundColor = this.mi_options[i].value;
                        break;
                    case 'height':
                        this.mi_cp.style.height = this.mi_options[i].value;
                        break;
                    case 'width':
                        this.mi_cp.style.width = this.mi_options[i].value;
                        break;
                    case 'fontSize':
                        this.mi_cp.style.fontSize = this.mi_options[i].value;
                        break;
                }
            }
        }
        else {
            this.mi_cp.style.backgroundColor = '#1248A4';
            this.mi_cp.style.height = '80px';
            this.mi_cp.style.fontSize = '10pt';
        }
        
        // Append the Control Panel to the DOM
        this.mi_bs.appendChild(this.mi_cp);
                
        // Display the Control Panels Options
        this.displayOptions();
};

// Sets the tracking enforcement cookie
analyticsCP.prototype.setTrackingCookie = function(cn,cv,ce,cd,cp,cs) {
    
    var today = new Date(); today.setTime( today.getTime() );
    
    if(ce) {ce = ce * 1000 * 60 * 60 * 24; var expires = new Date( today.getTime() + (ce) );}

    document.cookie = cn + "=" +escape( cv ) +
    ( ( ce ) ? ";expires=" + expires.toGMTString() : "" ) +
    ( ( cp ) ? ";path=" + cp : "" ) +
    ( ( cd ) ? ";domain=" + cd : "" ) +
    ( ( cs ) ? ";secure" : "" );
};

// Display Adperfect Specific Options
analyticsCP.prototype.displayOptions = function() {
    
    this.setTrackingCookie('mi_aptc','false','1000','adperfect.com');
    
    var mi_tb = document.createElement('input'),
        mi_am = document.createElement('p');
        mi_ac = document.cookie;
    
    // Set control panel title and style
    this.mi_ct.setAttribute('id','mistats_cp_title');
    this.mi_ct.setAttribute('style','display: inline;');
    this.mi_ct.innerHTML = this.cpname;
   
    // Append the Control Panel title to the DOM
    this.mi_cp.appendChild(this.mi_ct);    
      
    // Set control panel toggle message
    if( mi_ac.match('(^|;) ?' + 'mi_aptc' + '=([^;]*)(;|$)') ) {
        mi_am.setAttribute('id','mistats_cp_message');
        mi_am.setAttribute('style','display: inline; margin-left: 10px; font-size: 8pt;');
        mi_am.innerHTML = 'Not Tracking';
        this.mi_cp.appendChild(mi_am);
        mitagsent = true;
    }
};

// Enable Analytics Control Panel
if( window.location.search.match(/adperfect_notrack/g) || document.cookie.match ( '(^|;) ?' + 'mi_aptc' + '=([^;]*)(;|$)' ) ) {
    
    // Control Panel Settings Object
    function cpArgs() {
        this.options = [
            { style : 'backgroundColor', value : '#70A100'},
            { style : 'height', value : '20px'},
            { style : 'fontSize', value : '8pt'},
            { style : 'width', value : '100px'}
        ]   
        return this.options;
    };
    
    // Create an Analytics Control Panel
    mistats.controlPanel = new analyticsCP('', cpArgs());
    mistats.controlPanel.display();
}


