// on page load
	//var NTPT_PGEXTRA = "group=product";
	var pageLoadTime = new Date();
	var imUnica = {};
	imUnica.eventsParams = {}
	imUnica.pageParams = {};
	imUnica.pageParams.path = window.location.pathname;
	imUnica.pageParams.domain = document.domain;
	imUnica.pageParams.browser = flattenObject(jQuery.browser);
	imUnica.pageParams.firstVisit = null;
	imUnica.pageParams.upn2 = null;
	imUnica.pageParams.pageName = null;
	imUnica.pageParams.templateName = null;
	imUnica.pageParams.pageLanguage = null;
	imUnica.eventsParams.clickElapsedTimeInSeconds = null;
	imUnica.eventsParams.scope = null;
	imUnica.eventsParams.linkType = null;
	imUnica.eventsParams.unicaId = null;
	addCookiesStats();
	


	$(document).ready(function(){
		
		imUnica.pageParams.pageName = $("body").attr("pageName");
		imUnica.pageParams.templateName = $("body").attr("templateName");
		imUnica.pageParams.pageLanguage = $("body").attr("language");
		
		// detect slider navigation links
		$(".slider-nav").find("a").each(function(index){
			$(this).attr("unicaId","slider-link-"+(index+1));
		});
		// detect all links in the slider content
		$(".slider-content").find("a").each(function(index){
			$(this).attr("unicaId","slider-content-link-"+(index+1));
		});
		// detect landing page and product page main area links
		$(".lp-main").find("a").each(function(index){
			$(this).attr("unicaId","main-link-"+(index+1));
		});
		
		// detect all links inside homepage 3 boxes
		$('*[data-value^="text-page-content"]').find("a").each(function(index){
			$(this).attr("unicaId","main-link-"+(index+1));
		});

		// detect all links inside homepage 3 boxes
		$('*[data-value^="hp-box"]').find("a").each(function(index){
			$(this).attr("unicaId","hp-box-link-"+(index+1));
		});

		// detect all links that lead to home
		$('a[href$="home.aspx"]').each(function(index){
			$(this).attr("unicaId","back-to-home-link");
		});

		// detect all links that leads to mail client
		$('a[href^="mailto"]').each(function(index){
			$(this).attr("unicaId","mail-link-"+(index+1));
		});
		
		
		/*
		$("a").live("mouseover",function(){
			var start = new Date();
			jQuery.data($(this),"unicaMouseOver",start.getTime())
			//$(this).data("unicaMouseOver",exitTime.getTime());
			
		});
		
		$("a").live("mouseout",function(){
			var endTime = new Date();
			jQuery.data($(this),"unicaMouseOut",endTime.getTime())
			//$(this).data("unicaMouseOut",endTime.getTime());
			
		});
		*/

		// all links on page
		$("a").live("click",function(){
		 	var SecondsPast = null;
		 	var $currentLink = $(this);
		 	var unicaIdAttribute = $currentLink.attr("unicaId");
		 	eventTriggerTime = new Date();

			
			// calculate time past from page load
			SecondsPast = (eventTriggerTime.getTime() - pageLoadTime.getTime()) / 1000; 
			imUnica.eventsParams.clickElapsedTimeInSeconds = SecondsPast;
			
			// find link scope
			imUnica.eventsParams.scope = $(this).parents('*[data-type=scope]').attr("data-value");
			
			// detect installer links
			if($currentLink.hasClass("drCleanup")){
				imUnica.eventsParams.linkType="installer";
			} else {
				imUnica.eventsParams.linkType="link";
			}

			// get unicaId if exists
			if(unicaIdAttribute !="" && unicaIdAttribute!='undefined'){
				imUnica.eventsParams.unicaId = unicaIdAttribute;
			} else{
				imUnica.eventsParams.unicaId = "";
			}
			
			// merge the parameters 
			imUnica.allParams = merge(imUnica.eventsParams)+"&"+merge(imUnica.pageParams);
		
			// report to Unica
			ntptEventTag("ev=click&"+imUnica.allParams);
		
			//return false;
		});
	});

	function merge(obj){
		var merged = "",
		counter = 0;
		for (var i in obj){
			if(counter>0){
				merged +="&";
			}
			merged +=i+'='+encodeURI(obj[i]);
			counter++;
		}
		return merged;
	}

	function flattenObject(obj){
		var merged = "",
		counter = 0;
		for (var i in obj){
			merged +=';'+i+';'+obj[i];
			counter++;
		}
		return merged;
	}

	function addCookiesStats(){
		if(!$.cookie("firstVisit")){
		imUnica.pageParams.firstVisit = "true";
		$.cookie("firstVisit","false",{path:'/'});
		} else {
			imUnica.pageParams.firstVisit = "false";
		}
		if($.cookie("upn2")){
			imUnica.pageParams.upn2 = $.cookie("upn2");
		} else {
			imUnica.pageParams.upn2 = "false";
		}
		
	}
