﻿var s_helper = {
	GetSCAccount : function() {
		var s_account="rrvideodev"
		loc=document.location.toString();
		if(loc.indexOf("videostore.rr.com") != -1) 
			s_account="rrvideoprod";	
		return s_account;
	},

	GetPageName : function() {
		var pagename = this._toReadable(this._getPage());

		var productcategory = this._getQueryParam('category');
		if(pagename.toLowerCase().indexOf('browse') != -1)
		{
			pagename = this._categoryToPageName(productcategory);
			
			var page = this._getQueryParam('page');
			if (page != "undefined")
				pagename += ":" + "page" + page;
			else
				pagename += ":home";
		}		
			
		var productid=this._getProductID();
		
		if (productid != "undefined")
		{
			if (productcategory != "undefined")
				pagename = this._categoryToPageName(productcategory);
			
			var producttitle = this.GetTitle() + "";
			
			if(producttitle != "undefined")
				pagename += ":" + producttitle;
		}
			
		return pagename.toLowerCase();
	},

	GetTitle : function() {
		var x = document.getElementsByTagName("a");
		for(i=0;i < x.length; i++) {
			var aclass = x[i].className.toLowerCase();
			if(aclass == "current") {
				var title = x[i].innerHTML;
				title = title.replace(/:/g,'-');
				return title; 
			}
		}
	},

	GetProductsVar : function(link, shoppingCartData) {
		if (link == null)
			return shoppingCartData.collect(function(d) { return ";" + d.split(/,/)[2]; }).join(',');
		else
			return ";" + this._getQueryParam('productID', link.href);
	},

	TrackAddtoCart : function(link, shoppingCartData) {
		var s = s_gi(this.GetSCAccount());
		s.linkTrackVars = 'products,events';
		s.linkTrackEvents = 'scAdd';
		s.events = 'scAdd';
		s.products = this.GetProductsVar(link, shoppingCartData);
		s.tl(this.href,'o', 'Add to Cart: '+ this.GetProductsVar(link, shoppingCartData));
		
		s.products = s.events = "";
	},
	
	TrackVideoEvent : function(eventType, seconds) {
		var productID = this.GetProductID();
		
		if (productID == "")
			return;
			
		var title = this.GetTitle();
		
		if (eventType == "onPlay" && seconds == 0)
		{
			// starting video
			s.prop3=s.eVar3 = title; 
			s.prop4=s.eVar4 = productID; 
			s.events = "event11";
			s.linktrackVars = "prop3,prop4,eVar3,eVar4,events"; 
			s.linktrackEvents = "event11"; 
			s.tl(this.href, 'o', title); 

			s.Media.open(title, seconds, "trailer player"); 
			s.Media.play(title, seconds);
		}
		else if (eventType == "onPlay" || eventType == "onScrubFinish")
		{
			// playing video
			s.Media.play(title, seconds);
		}
		else if (eventType == "onPause" || eventType == "onScrubStart")
		{
			// pausing video
			s.Media.stop(title, seconds);
		}
	},
	
	IsErrorPage : function() {
		var pagename = this.GetPageName();

		if(pagename.match(/(error|404)/i))
			return "errorPage";
		return "";
	},
	
	GetHeir1 : function() {
		var pageName = this.GetPageName();
		var prop = pageName.replace(/:(home|page[\d]*)$/, "");
		
		return prop == "" ? "home" : prop;
	},

	GetProp1 : function() {
		var heir1 = this.GetHeir1();
				
		return heir1.split(/:/)[0];
	},
	
	GetProp2: function() {
		var heir1 = this.GetHeir1();
		var split = heir1.split(/:/);
			
		return split[0] + (split.length > 1 ? (":" + split[1]) : "");
	},
	
	GetProductID: function() {
		var productID = this._getProductID();
		
		return productID == "undefined" ? "" : productID;
	},
	
	GetContentType: function() {
		var page = this._getPage();

		if (page.match(/detail$/i))
			return this._toReadable(page);
			
		if (page.match(/^browse$/i))
		{
			var primary;
			var category = this._getQueryParam('category');
			
			if (category == "justIn" || category == "newReleaseMovies" || category == "allMovies" ||
				category == "movieRentals" || category == "moviePurchases")
				primary = "section";
			else
				primary = "sub section";
				
			var page = this._getQueryParam('page');
			
			if (page == "undefined")
				return primary + ":home";
			else
				return primary + ":page" + page;
		}
		
		if (page.match(/^search$/i))
			return "search:results";
			
		return this._toReadable(page);
	},
	
	GetLoginEvent: function() {
		if (document.referrer.match(/login$/i))
			return "event4";
		else
			return "";
	},
	
	_getQueryParam : function(variable, url) {
		var query;

		if (typeof(url) == "undefined")
		{
			// use current url
			url = window.location.search;
			query = url.substring(1);
		}
		else
		{
			// use passed in url
			url = url.split(/\?/);
			query = url[1];
		}
			
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0].toLowerCase() == variable.toLowerCase()) {
				var qsValue = pair[1];
				break;
			}
		} 
		return qsValue + "";
	},
	
	_categoryToPageName : function(category) {
		category = this._toReadable(category);
		category = category.replace(/\/([^\/]*)$/, ":$1");
		category = category.replace(/\//g, " ");
		
		return category;
	},
	
	_toReadable: function(text) {
		return text.replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase();
	},
	
	_getPage: function() {
		var loc = location.pathname;
		
		if (loc.indexOf(".") < 0)
			return "Home";

		return loc.substring(loc.lastIndexOf("/") + 1,loc.indexOf("."));
	},
	
	_getProductID: function() {
		var productid = this._getQueryParam('contentID');
		if(productid == "undefined")
			productid = this._getQueryParam('externalID');
			
		return productid;
	},
		
	Log : function(s) {
//		for (var i in s)
//		{
//			if (i.match(/^prop/) ||
//				i == "pageName" ||
//				i == "heir1" ||
//				i == "events" ||
//				i == "products" ||
//				i == "pageType" ||
//				i == "purchaseID" ||
//				i == "state" ||
//				i == "zip")

//				console.log("s." + i + " = " + unescape(s[i]));
//		}
	}
};

var TrailerPlayerCallback = {
	onVideoEvent: function(eventType, seconds) {
		s_helper.TrackVideoEvent(eventType, seconds);
	}
};