﻿var _gaq = _gaq || [];
var GAPage = new Object();
GAPage.showInitialPageTrack = true;
GAPage.GOOGLE_ANALYTICS_ID = null;
GAPage.VISITOR_ID = null;
GAPage.normalizeURL = function(url) {
	// strip query string
	var indx = url.indexOf("?")
	if (indx > -1) {
		url = url.substring(0, indx)
	}


	var parts = url.split("/");
	var newparts = [];
	for (var i = 0; i < parts.length; i++) {
		var currVal = parts[i];
		if (currVal != "") {
			if (currVal.toLowerCase() != "default.aspx") {
				if (/\.aspx$/.test(currVal.toLowerCase())) {
					currVal = currVal.replace(/\.aspx/gi, "");
				}
				newparts.push(currVal);
			}
		}
	}
	var trackingURL = "/" + (newparts.length <= 0 ? "" : newparts.join("/"));
	// if not a document
	if (trackingURL != "/" && !GAPage.isDocumentURL(trackingURL)) {
		trackingURL += "/";
	}
	return trackingURL;
}
GAPage.isDocumentURL = function(url) {
	return /\.(doc|pdf)$/i.test(url.toLowerCase())
}
GAPage.trackPage = function(url) {
    _gaq.push(['_setAccount', GAPage.GOOGLE_ANALYTICS_ID]);
    _gaq.push(['_setCustomVar', 1, "UniqueUserID", GAPage.VISITOR_ID, 1]);

    if (typeof (url) == "undefined" || url == "")
        url = window.location.pathname;
    
    url = GAPage.normalizeURL(url);
    _gaq.push(['_trackPageview', url]);
}
GAPage.trackDownload = function (pagename, link) {
    _gaq.push(['_setAccount', GAPage.GOOGLE_ANALYTICS_ID]);
    _gaq.push(['_setCustomVar', 1, "UniqueUserID", GAPage.VISITOR_ID, 1]);

    var url = pagename + "/download/" + link;
    url = GAPage.normalizeURL(url);
    _gaq.push(['_trackPageview', url]);

    if (link == "/Files/PDFs/VELCADE_PRESCRIBING_INFORMATION.pdf") {
        var html = "<script type='text/javascript' src='" + window.location.protocol + "//o2.eyereturn.com/?site=3146&page=ALL_ADS_PI&ssva_sid=" + GAPage.VISITOR_ID + "'/></script>";
        $(document).append(html);
    }
}
GAPage.trackEvent = function(category, event, label) {
	_gaq.push(['_setAccount', GAPage.GOOGLE_ANALYTICS_ID]);
	_gaq.push(['_setCustomVar', 1, "UniqueUserID", GAPage.VISITOR_ID, 1]);
	_gaq.push(['_trackEvent', category, event, label]);
}

GAPage.trackSectionView = function(section) {
	var pagename = window.location.pathname;
	var url = pagename + "/" + section;
	url = GAPage.normalizeURL(url);
	GAPage.trackPage(url);
}

GAPage.trackLinkerPageSectionView = function(otherGoogleAnalyticsID, section) {
    var pagename = window.location.pathname;
    var url = pagename + "/" + section;
    url = GAPage.normalizeURL(url);

    _gaq.push(['_setAccount', otherGoogleAnalyticsID]);
    _gaq.push(['_setCustomVar', 1, "UniqueUserID", GAPage.VISITOR_ID, 1]);
    _gaq.push(['_setAllowLinker', true]);

    _gaq.push(['_trackPageview', url]);
}

GAPage.trackLinkerPage = function(otherGoogleAnalyticsID, url) {
	_gaq.push(['_setAccount', otherGoogleAnalyticsID]);
	_gaq.push(['_setCustomVar', 1, "UniqueUserID", GAPage.VISITOR_ID, 1]);
	_gaq.push(['_setAllowLinker', true]);

	if (typeof (url) == "undefined" || url == "")
		url = window.location.pathname;
	url = GAPage.normalizeURL(url);
	_gaq.push(['_trackPageview', url]);
}

GAPage.trackModal = function(url) {
    var mainurl = window.location.pathname;
    if (url.toLowerCase() == "advisor.aspx")
        GAPage.trackPersonalAdvisorPopupStart();
    else {
        GAPage.trackPage(mainurl + "/popup/" + url);        
    }
}
GAPage.closeModal = function () {
	var mainurl = window.location.pathname;
	GAPage.trackPage(mainurl);
}

var Cookies = new Object();
Cookies.getCookie = function(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
			return unescape(y);
		}
	}
}
Cookies.setCookie = function(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}





$(document).ready(function () {
    initializeFileDownloads();
    initializeMailto();
    initializeExitLinks();
});


function initializeFileDownloads() {
	/***********************************************************************
	Download Files
	***********************************************************************/
	//On click of link, throw google page track
	$('.downloadFile').live("click", function() {
		if (parent.GAPage || GAPage) {
			//var file = $(this).attr('href');
			var file = $(this).get(0).href.replace(window.location.protocol+"//"+window.location.hostname,"");

			var pagename = window.location.pathname;
			var parentPath = ""
			if (file.indexOf("/") != 0) {
				parentPath = pagename;
				if (!/\/$/.test(pagename)) {
					parentPath = pagename.substring(0, pagename.lastIndexOf("/")+1)
				}
				file = parentPath + file;
			}
			if (parent.GAPage)
				parent.GAPage.trackDownload(pagename, file);
			else
				GAPage.trackDownload(pagename, file);
		}
	});

	//On click of link, throw google page track
	$('.rightRailDownloadFile').live("click", function() {
		if (parent.GAPage || GAPage) {
			//var file = $(this).attr('href');
			var file = $(this).get(0).href.replace(window.location.protocol+"//"+window.location.hostname,"");

			var pagename = window.location.pathname;
			var parentPath = ""
			if (file.indexOf("/") != 0) {
				parentPath = pagename;
				if (!/\/$/.test(pagename)) {
					parentPath = pagename.substring(0, pagename.lastIndexOf("/")+1)
				}
				file = parentPath + file;
			}
			if (parent.GAPage)
				parent.GAPage.trackDownload(pagename+"/RightRail/", file);
			else
				GAPage.trackDownload(pagename+"/RightRail/", file);
		}
	});
}

function initializeMailto() {
	/***********************************************************************
	Mailto Links
	***********************************************************************/
	//On click of link, throw google page track
	$('.gaMailTo').live("click", function() {
		if (GAPage) {
			var file = $(this).get(0).href;
			file = file.replace(/mailto:/ig, "");
			GAPage.trackEvent("Mailto Links", "Mailto", file);
		}
	});
}

function initializeExitLinks() {
    /***********************************************************************
    Exit Links
    ***********************************************************************/
    //On click of link, throw google page track
    $('.leavingSite').live("click", function () {
        if (GAPage) {
            var exitURL = $(this).get(0).href;
            GAPage.trackEvent("Exit Links", "Exit", exitURL);
        }
    });
}

var trackPrint = function () {
    if (GAPage) {
        var page = window.location.pathname;
        GAPage.trackEvent("Print Links", "Print", GAPage.normalizeURL(page));
    }
}

