﻿/*
	Unobtrusive application sources based on jQuery plugin structure
*/
var hi = {};
(function($)
{	
	hi.plugins = function()
	{
		function init()
		{
			/*
				Set "navigation-header" NavMethod for nav header links
			*/
			$('ul#nav > li > a').click(function(e)
			{
				s.c_w('navMethod','navigation-header',0);
			});
			/*
				Set "navigation-menu" NavMethod for drop menu links
			*/
			$('#nav .drop a').click(function(e)
			{
				s.c_w('navMethod','navigation-menu',0);
			});
			/*
				Set "footer" NavMethod for footer links
			*/
			$('#footer a').click(function(e)
			{
				s.c_w('navMethod','footer',0);
			});
			
			/*
				Track link binding - all links with or within class hi-bh-tl 
			*/
			$('a.hi-bh-tl, .hi-bh-tl a').click(function(e)
			{
				var name = $(this).attr("title");
				if (name) hi.plugins.omniture.tracklink("link:" + name.toLowerCase().replace(/\s/g,'_'));
			});
			
			/*
				Dimmed layer binding - all links with class hi-bh-dl, fancybox, iframe or hiyoungiframe
			*/
			$('a.hi-bh-dl, a.fancybox, a.iframe, a.hiyoungiframe').click(function(e)
			{
				var name = $(this).attr("title");
				if (name) hi.plugins.omniture.layer(name.replace(/\s/g,'_'), "dimmed layer");
			});
			
			/*
				Tab binding - all tab links will be tracked
			*/
			$('a.tab').click(function(e)
			{
				var name = $(this).text().trim().toLowerCase().replace(/\s/g,'_');
				if (name) hi.plugins.omniture.tracklink(name + "-tb");
			});
			
			/*
				Initialise omniture library 
			*/
			hi.plugins.omniture.init();
		};	

		/*
			Make hi.plugins.init() globaly available 
		*/
		return{ init:init }
	
	}();


	/*
		HI reporting in Omniture
	*/
	hi.plugins.omniture =
	{
		notrack : false,
		
		/* Init - Get all c_ variables from the page and track the page */
		init : function ()
		{
			/* Reset account based on Report suite designation c_rsd and Extention c_rsx */
			if (typeof c_rsd == 'undefined' || c_rsd == null) c_rsd = 'hi';
			if (typeof c_rsx == 'undefined' || c_rsx == null) {
				if (location.href.indexOf("www.hi.nl") != -1) {
					c_rsx = 'prod';
				} else if (location.href.indexOf("hi.nl.kpnis.nl") != -1) {
					c_rsx = 'test';
				} else {
					c_rsx = 'dev';
				}
			}
			s.sa("kpncom" + c_rsd + c_rsx);
			
			/* Map page variables to an object */
			var c_vars = {},
				vars = ['pagename','rsd','rsx','funnelname','funnelstep','campaignpage','lmd','pid','contenttype','searchterm','searchcount','resultid','searchclick','firstclick','events','products','productcombination','comparedproducts','purchaseID','filter','info','login']
			for(var i=0;i<vars.length;i++) {
				varName = 'c_'+vars[i];
				if (typeof window[varName] != 'undefined') {c_vars[vars[i]] = window[varName];}
			}
			
			/* Insert cookie results (login) in page variables */
			var login = s.c_r('omniture_inlog');
			if (login != 'null') {
				c_vars['login'] = login;
				s.c_w('omniture_inlog','',-1);
			}
			
			/* Do not track in page when c_notrack is set to true,
			   in this case the page should call the trackpage function. */
			if (typeof c_notrack != "undefined" && ""+c_notrack == "true") {
				hi.plugins.omniture.notrack = true;
			}
			
			/* Track the page with the page variables, 
			   make sure trackpage is only executed automaticaly for one time */
			if (!hi.plugins.omniture.notrack) {
				hi.plugins.omniture.trackpage(c_vars);
				hi.plugins.omniture.notrack = true;
			}
		} ,
		
		/* trackpage - track the page based on the passed tracking variables */
		trackpage : function (c)
		{
			/* Default rsd is "hi" */
			if (typeof c.rsd == "undefined") {
				c.rsd = "hi";
			}
			
			/* Default contenttype is "content" */
			if (typeof c.contenttype == "undefined") {
				c.contenttype = "content";
			}

			/* c.pagename from canonical */
			if (typeof c.pagename == "undefined") {
				var canonical = $('link[rel="canonical"]').attr('href');
				if (canonical != null && canonical!="" ){
					c.pagename = canonical;
				}
			}

			/* c.pagename from location */
			if (typeof c.pagename == "undefined") {
				c.pagename = window.location.pathname;
			}

			// store the path in reversed order in an array.
			// if the path is /mobiel/abonnementen.htm
			// the array is ["abonnementen","mobiel"]
			c.pagename = c.pagename.toLowerCase().replace(/^http[^\/]*\/\/[^/]*/, "").replace(/:/g, "/").replace(/^\//, "").replace(/\..*/, "");
			_path = c.pagename.split('/').reverse();
			
			// Add funnelname and funnelstep to the path
			if (typeof c.funnelname != "undefined" && typeof c.funnelstep != "undefined") {
				if (_path.length > 2) {
					_path.shift();  // Remove filename on 3 levels and more
				}
				_path = [ c.funnelstep.toLowerCase(), c.funnelname.toLowerCase() ].concat(_path);
			}
			
			// channel example "mobiel"
			s.channel = _path.pop();
			
			// add cmp to the path if it is a campaignpage
			if (typeof c.campaignpage != 'undefined' && c.campaignpage == "true")
			{

				if (_path.length == 0)
				{
					_path.push(s.channel);
					_path.push('cmp');
					s.channel = 'cmp';
				} else
				{
					_path.push('cmp');
					_path.push(s.channel);
				}
			} else
			{
				_path.push(s.channel);
			}

			// If a funnelname is defined reset the channel to be the funnelname
			if (typeof c.funnelname != "undefined") {
				s.eVar6 = c.funnelname.toLowerCase();
			}
			// pagename example "abonnementen:mobiel"
			s.pageName = _path.join(':');
			//alert("s.pageName: " + s.pageName);

			// Trim the pagename value to 100 characters if the pagename exceeds more than 100 characters.
			if (s.pageName.length > 100) {
				s.pageName = s.pageName.substring(0,100);
			}

			// prop2 example "abonnementen:mobiel 20100101", path trimmen op (100 - versieinfo)
			var s_versioninfo = "";
			if (typeof c.lmd != "undefined" && c.lmd != "") s_versioninfo += " " + c.lmd;
			if (typeof c.lmd != "undefined" && c.pid != "") s_versioninfo += " " + c.pid;
			s.prop2 =  s.pageName.substring(0, 100 - s_versioninfo.length) + s_versioninfo.toLowerCase();

			// hier1 example "mobiel/abonnementen"
			_path.reverse();
			s.hier1 = _path.join('/');
			
			s.pageType = c.contenttype.toLowerCase();
			s.linkName = '';
			s.eVar2 = s.channel;
			
			// eVar3 contains the first two levels of the path
			for (i=_path.length; i > 2; i--) _path.pop();
			
			s.eVar3 = _path.join('/');
			s.eVar15 = '';
			s.eVar16 = '';        	
			s.products = '';	        	
			s.prop1 = s.pageType;
			s.eVar29 = s.pageType;
			s.prop3 = '';
			s.siteref = c.rsd;
			s.events = '';

			// eVar17 merchandising section
			
			if (s.pageType !== 'content' && s.pageType !== 'product detail' && s.pageType !== 'product detail shop' && s.pageType != 'funnel')
			{
				s.eVar17 = s.hier1;
			};

			// for example: c.login = "customer"
			if (typeof c.login != 'undefined' && c.login)
			{
				s.eVar24 = c.login;
				s.events = s.apl(s.events,"event24",",",1);
			}

			// eVar18 Internal banner Promotions, eVar12 other internal links
			var ic = s.getQueryParam('omn_ic');
			if (ic && ic.indexOf('banner:') > -1)
			{
				s.eVar18 = ic;
				s.eVar15 = "internal campaign";
			} else if (ic)
			{
				s.eVar12 = ic;
			}
			
			// Add events to s object
			if (typeof c.events != "undefined") {
				for (i=0; i < c.events.length; i++) {
					if (typeof c.events[i].event != "undefined") {
						s.events = s.apl(s.events,c.events[i].event,",",1);
					}
				}
			}

			// Page & Content
			if (c.contenttype != 'error')
			{
				s.eVar1 = s.pageName;
				s.eVar2 = s.channel;
				s.events = s.apl(s.events,"event1",",",1);
				s.prop3 = s.getPreviousValue(s.pageName, 's_gpv_pn');
				if (!s.prop3) s.eVar28 = s.pageName;
				if (s.events.indexOf('prodView') > -1 )
				{
					s.events = s.apl(s.events,"event4",",",1);
				}
			};

			// Add Products to s object
			if (typeof c.products == "object") {
				for (i=0; i < c.products.length; i++) {
					if (typeof c.products[i].id != "undefined") {
						var prod = ';' + c.products[i].id;
						if (typeof c.products[i].qty != "undefined") {
							prod = prod + ";" + c.products[i].qty;
							if (typeof c.products[i].price != "undefined") {
								prod = prod + ";" + c.products[i].price;
							}
						}
						s.products = s.apl(s.products,prod,",",1);
					}
				}
			}
			
			// Set eVar27 with the productcombination ( standaard | niet standaard )
			if (typeof c.productcombination != "undefined") {
				s.eVar27 = c.productcombination;
			}
			
			// Add comparedproducts to eVar14
			if (typeof c.comparedproducts == "object") {
				for (i=0; i < c.comparedproducts.length; i++) {
					if (typeof c.comparedproducts[i].id != "undefined") {
						// Only add compared products that are not viewed here
						var inProducts = false;
						if (typeof c.products == "object") {
							for (j=0; j < c.products.length; j++) {
								if (typeof c.products[j].id != "undefined" && c.comparedproducts[i].id == c.products[j].id) {
									inProducts = true;
								}
							}
						}	
						if (!inProducts) {
							s.eVar14 = s.apl(s.eVar14,c.comparedproducts[i].id,":",1);
							s.eVar15 = 'vergelijken';
						}
					}
				}
			}
			
			// Add purchaseID
			if (typeof c.purchaseID != "undefined") {
				s.purchaseID = c.purchaseID;
			}
			
			// External Campaigns
			if (!s.campaign) s.campaign = s.getQueryParam('campaignid').toLowerCase();
			if (!s.campaign) s.campaign = s.getQueryParam('M5Referer').toLowerCase();
			if (!s.campaign) s.campaign = s.getQueryParam('cid').toLowerCase();
			if (s.campaign)
			{	
				s.campaign = s.getValOnce(s.campaign,"s_v0_"+c.rsd,0);
			}
			
			/* Search results in the page */
			if(typeof c.searchterm != "undefined")
			{
				s.eVar16 = c.searchterm.toLowerCase();
				s.prop8 = s.eVar16;
				s.events = s.apl(s.events,"event2",",",1);
				s.eVar15 = "search";
				if(typeof c.searchcount != "undefined")
				{
					s.evar25 = c.searchcount;
					if (c.searchcount == 0)
					{
						s.events = s.apl(s.events,"event3",",",1);
					}
				}
				if(typeof c.resultid != "undefined")
				{
					hi.plugins.omniture.searchtrack(c.resultid);
				}
				/* Reset products on the search result page*/
				if (s.products = "") s.products = ";";
			};

			// Filter mechanisme
			if (typeof c.filter != "undefined") {
				//s.prop1 = "filter";
				s.prop13 = c.filter;
				s.eVar13 = c.filter;
				s.eVar15 = "filter";
			}

			// Watch info eVar
			if(typeof c.info != "undefined")
			{
					s.eVar19 = c.info;
					s.events = s.apl(s.events,"event14",",",1);
			};

			eVar15Val = s.c_r('navMethod');
			s.c_w('navMethod','',-1);
			if(eVar15Val != "null" && eVar15Val != "" && s.eVar15!='search' && s.eVar15!='filter' && s.eVar15!='vergelijken' && typeof isDimLayer == 'undefined')
			{
				s.eVar15 = eVar15Val;
			};
			navMethodVal = s.getQueryParam('navMethod');
			if(navMethodVal != "")
			{
				s.eVar15 = navMethodVal;
			};
			if (s.eVar15)
			{
				if(s.eVar15 != 'filter')
				{
					s.eVar13 = 'non-filter';
				}
				if(s.eVar15 != 'search')
				{
					s.eVar16 = 'non-search';
				}
				if(s.eVar15 != 'browse' && s.eVar15 != 'navigation-header' && s.eVar15 != 'navigation-menu')
				{
					s.eVar17 = 'non-browse';
				} 
				if(s.eVar15 != 'internal campaign')
				{
					s.eVar18 = 'non-internal campaign';
				}
			};

			// Visitor reporting
			s.prop4 = s.getNewRepeat("","s_c4_gnr");
			s.eVar4 = s.prop4;

			// Service
			s.setupFormAnalysis();
			s.eVar23 = s.prop9;
			s.runOnce = true;

			// Execute the call
			var s_code=s.t();
		},
		
		/*
			Track searchresult
			searchterm - The querystring submitted bythe user
			searchcount - The number of results
			resultid - Wrapper identifer for resultset, all links in this wrapper get their click tracked as a search click
			for example: hi.plugins.omniture.searchresult(q,results,'#searchresult');
		*/
		searchresult : function (searchterm, searchcount, resultid)
		{
			s.linkTrackVars = "eVar1,eVar2,eVar3,eVar13,eVar15,eVar16,eVar17,eVar18,eVar25,prop3,prop8,events,products";
			s.linkTrackEvents = "event2,event3";
	
			s.eVar16 = searchterm.toLowerCase();
			s.prop8 = s.eVar16;
			s.events = "event2";
			s.eVar15 = "search";
			if (typeof searchcount != "undefined")
			{
				s.evar25 = searchcount;
				if (searchcount == 0)
				{
					s.events = s.apl(s.events,"event3",",",1);
				}
			}
			s.products = ';';
			s.eVar13 = 'non-filter';
			s.eVar17 = 'non-browse';
			s.eVar18 = 'non-internal campaign';
			s.eVar25 = searchcount;
			
			s.tl(true, 'o', 'searchresult:'+ s.pageName);
			
			if (resultid) hi.plugins.omniture.searchtrack(resultid);
		},
		
		/* Add event handlers for searchresult click events
			resultid - area in which results are stored. 
		*/
		searchtrack : function (resultid)
		{
			$(resultid).find('a').click(function() {
				s.linkTrackVars = "eVar26,events";
				s.linkTrackEvents = "event23,event25";
				
				// Use the title to report to omniture
				var resultname = $(this).attr("title");
				
				// Set event25 for search clicks
				s.events = "event25";
				
				// Only set event23 on the first click for the same query
				// query is stored in eVar16  TODO: are we sure this works also after a back
				var prev = s.getPreviousValue(s.eVar16,"s_v0_searchquery",0);
				if (s.eVar16 != prev)
				{
					s.events = s.apl(s.events,"event23",",",1);
				}

				s.eVar26 = resultname;
				
				s.tl(true, 'o', 'searchclick:'+ s.pageName);
			});
		},

		/*
			Track links
			name - name of the link to track (xx:yy:zz)?
		*/
		tracklink : function (name)
		{
			s.linkTrackVars = "eVar12";
			s.eVar12 = name + ':'+ s.pageName;
			s.tl(true, 'o', s.eVar12);
		},
		
		/*
			Track layer as a page event
			layername - name of the layer
			contenttype - now only "dimmed layer" is supported
		*/
		layer : function (layername, contenttype)
		{	
			// save values
			var pageName = s.pageName;
			var prop1 = s.prop1;
			var prop2 = s.prop2;
			var hier1 = s.hier1;
			var eVar1 = s.eVar1;
			var eVar3 = s.eVar3;
			var events = s.events;
			var products = s.poducts;

			s.pageName = layername.toLowerCase() + '-dl:' + s.pageName;
			s.prop1 = contenttype;
			s.prop2 = s.prop2;
			s.hier1 = s.hier1 + '/' + layername.toLowerCase() + '-dl';
			s.events = "event1";
			s.products = "";
			
			// Execute the call
			var s_code=s.t();						
			
			// reset values
			s.pageName = pageName;
			s.prop1 = prop1;
			s.prop2 = prop2;
			s.hier1 = hier1;
			s.eVar1 = eVar1;
			s.eVar3 = eVar3;
			s.events = events;
			s.products = products;
		}
	}
})(jQuery);

/*
	Initialize plugins on document ready
*/
$(document).ready(function() {
	hi.plugins.init();
}); 


