﻿//	Example usage:
//	var omniture = new OmnitureProto();
//	omniture.logZoomIn("control");
//	*Note* that Constructing an OmnitureProto object automatically
//	kicks off the 15 second timer log

function OmnitureProto(description)
{
	var startTime = new Date();
	var me = this;
	this.firstInteractionAlreadyLogged = false;
	this.description = description;

	//	Gets called every 15 seconds from time object was
	//	constructed and invokes the 15 sec event
	setInterval(function()
	{
		var totalSeconds = (new Date().getTime() - startTime.getTime()) / 60000;
		me.logEvent("event6", { "eVar1": Math.round(totalSeconds * 100) / 100 });
	}, 15000);
	
	this.setupSocialMediaClicks();
	
	$(window).unload(function () {
	    me.logSocialMosaic("unload");
	});
}

OmnitureProto.prototype.setupSocialMediaClicks = function()
{
	var me = this;
	$("a[class$='addthis_button_twitter']", "#SocialLinks").click(function() { me.logMosaicShared("Twitter"); });
	$("a[class$='addthis_button_facebook']", "#SocialLinks").click(function() { me.logMosaicShared("Facebook"); });
	$("a[class$='addthis_button_myspace']", "#SocialLinks").click(function() { me.logMosaicShared("MySpace"); });
	$("a[class$='addthis_button_bebo']", "#SocialLinks").click(function() { me.logMosaicShared("Bebo"); });
	$("a[class$='addthis_button_stumbleupon']", "#SocialLinks").click(function() { me.logMosaicShared("StumbleUpon"); });
}

OmnitureProto.prototype.logEvent = function(eventCode, eVars)
{
	if (arguments.length == 0 || eventCode == null)
		throw new Error("Omniture eventCode argument expected");

	var s = s_gi(s_account);
	s.linkTrackVars = 'events';
	s.linkTrackEvents = eventCode;
	s.events = eventCode;

	if (arguments.length > 1 && eVars != null)
	{
		for (eVarName in eVars)
		{
			s.linkTrackVars += "," + eVarName;
			s[eVarName] = eVars[eVarName];
		}
	}

	//	Since the majority of our calls aren't directly in response to an <a> click
	//	using true to not send the event source and to prevent the log delay
	s.tl(true, 'o', this.description);
}

//	Eexpected values for searchType argument (From Tagging Variables Spreadsheet):
//	"Country", "Favorite Shoe", "Top Rated", "Photo Contest Winner", "Keyword".
OmnitureProto.prototype.logSearch = function(searchType, searchTerm, resultCount) {

    if (searchType == "Country")
    {
        this.logSocialMosaic("country");
    }
    else if (searchType == "Top Rated")
    {
        this.logSocialMosaic("top rated");
    }
    else if (searchType == "Favorite Shoe")
    {
        this.logSocialMosaic("favorite shoe");
    }
    
	this.logEvent("event7",
	{
		"eVar2": searchType + ':' + searchTerm,
		"eVar3": searchTerm,
		"eVar4": resultCount + ':' + searchTerm
	});
}

//	searchType should match what was used in logSearch. We need clarification
//	on when this should actually be called
OmnitureProto.prototype.logSearchNextResult = function(searchType)
{
	this.logEvent("event17", { "eVar2": searchType });
}

OmnitureProto.prototype.logFirstInteraction = function()
{
	//	The way this method works is that it silently does nothing if this method
	//	has already been called. If you want it to let you know instead comment
	//	out the throw
	if (this.firstInteractionAlreadyLogged)
	{
		//	throw new Error("logFirstInteraction has already been called");
		return;
	}

	this.firstInteractionAlreadyLogged = true;
	this.logEvent("event3");
}

//	Expected values for zoomType argument (From Tagging Variables Spreadsheet):
//	"click", "scroll", "control"
OmnitureProto.prototype.logZoomIn = function(zoomType)
{
	this.logEvent("event4", { "eVar6": zoomType + " zoom" });
}

//	Expected values for zoomType argument (From Tagging Variables Spreadsheet):
//	"click", "scroll", "control"
OmnitureProto.prototype.logZoomOut = function(zoomType)
{
	this.logEvent("event5", { "eVar6": zoomType + " zoom" });
}

//	No expected values have been defined for shareType. I would venture to
//	guess: "Facebook", "Email", etc.
OmnitureProto.prototype.logMosaicShared = function(shareType)
{
	this.logEvent("event8", { "eVar5": shareType });
}

//	No expected values have been defined for shareType. I would venture to
//	guess: "Facebook", "Email", etc.
OmnitureProto.prototype.logPhotoShared = function(shareType)
{
	this.logEvent("event9", { "eVar5": shareType });
}

//	photoSubmission is any JavaScript object that has the following properties:
//	PhotoSubmissionID, Name, City, Country, PostalCode, FavoriteShoe, Rating. 
//	Country and Favorite Shoes should be Names, not IDs
OmnitureProto.prototype.logPhotoViewed = function(photoSubmission) {

    this.logSocialMosaic('photo detail');

	this.logEvent("event18",
	{
		"eVar16": photoSubmission.PhotoSubmissionID,
		"eVar17": !(photoSubmission.City) ? '' : photoSubmission.City,
		"eVar18": !(photoSubmission.PostalCode) ? '' : photoSubmission.PostalCode,
		"eVar19": !(photoSubmission.Country) ? '' : photoSubmission.Country,
		"eVar20": !(photoSubmission.FavoriteShoe) ? '' : photoSubmission.FavoriteShoe,
		"eVar22": Math.round(photoSubmission.Rating)
	});
}

OmnitureProto.prototype.logRegistrationInitiated = function()
{
	this.logEvent("event19");
}

OmnitureProto.prototype.logSocialMosaic = function(pageName)
{
	var s = s_gi(s_account);
	s.pageName = 'social mosaic:' + pageName;
	s.t();
}

function SimpleOmniture(){}

SimpleOmniture.prototype.logEvent = function(sender, eventCode)
{
	var s = s_gi(s_account);
	s.linkTrackVars = 'events';
	s.linkTrackEvents = eventCode;
	s.events = eventCode;
	
	s.tl(sender, "o");
}

SimpleOmniture.prototype.logSilverlightDownloadLinkClicked = function(sender, isIE6)
{
	this.logEvent(sender, isIE6 ? "event22" : "event20");
}

SimpleOmniture.prototype.logSilverlightDownloadReturnToMosaic = function(sender, isIE6)
{
	this.logEvent(sender, isIE6 ? "event23" : "event21");
}