/*
* common.js
* ScreenToaster
* Michael Bagur
*
* common javascript for every pages
*
*******************************************/

// Jquery no conflict
jQuery.noConflict(); 

// Document ready
jQuery(function($) {
	try {
/*		// Unbold sous safari
		if ($.browser.safari) {
			for(i=1;i<7;i++) {
				$('h'+i).css({fontWeight: 'normal'});
				$('.unbold').css({fontWeight: 'normal'});
				$('.details a').css({fontWeight: 'normal'});
			}
		}*/
	
		// Cookies activated
		if (document.cookie != "" && document.cookie != null) {
			// Get cookies
			var phpSessId = $.cookie('PHPSESSID')
			var lastPhpSessId = $.cookie('LASTPHPSESSID');
			var location = ""+document.location;
			
			// Trace first visits
			if ((lastPhpSessId == null || lastPhpSessId != phpSessId) && phpSessId != null) {
				// Set cookie
				$.cookie('LASTPHPSESSID',phpSessId,{expires: 365});
				
				// Is it the first visit ?
				var firstVisit = 'first';
				if (lastPhpSessId != null) firstVisit = 'return';
				
				// Get referer
				var referer = "";
				if (document.referrer != "") referer = unescape(decodeURI(document.referrer));
				
				// Get origin webiste
				var origin = getGetParameter('origin');
					
				// post event
				postEvent('First_Visit',[firstVisit, origin, referer]);
			}
		
			// Get origin webiste
			var origin = getGetParameter('origin');
			if (origin != null && origin != "") {
				var lastOrigin = $.cookie('LASTORIGIN');
				if (lastOrigin == null || lastOrigin != origin) {
					$.cookie('LASTORIGIN',origin,{expires: 365});
					
					// post event
					postEvent('Visit_Origin',[origin,location]);
				}
			}
			
			// Trace watch page
			if (location != "" && g_watchUrl && location.indexOf(g_watchUrl) != -1) {
				var hashcode = location.substring(location.indexOf(g_watchUrl)+g_watchUrl.length, location.length);
				postEvent('Watch_Video',[hashcode]);
			}
			
			// Trace search queries
			if (location != "" && g_searchUrl && location.indexOf(g_searchUrl) != -1) {
				var query = getGetParameter('query_search');
				postEvent('Search_Query',[query]);
			}
		}		
	// Catch errors
	} catch (e) {
		logger.error("common.js > "+e);
	}
});

/**
* This function searches for a parameter in the url
*/
function getGetParameter(name) {
	var ret = "";
	
	// Get the parameter part of the url
	var location = document.location.search;
	
	// Search for the parameter
	var deb = location.indexOf(name+'=');
	if (deb != -1) {
		// Search for the end of the parameter
		fin = location.indexOf('&',deb+name.length+1);
		if (fin != -1) {
			ret  = location.substr(deb+name.length+1, fin);
		} else {
			ret = location.substr(deb+name.length+1, location.length);
		}
		ret = ret.replace(/\+/g, ' ');	
		ret = unescape(decodeURI(ret));	
	}
	
	return ret;
}

/**
* This function post an event for statistics
*/
function postEvent(event_type,messsages) {
	try {
		// Prepare message
		var message = "";
		for (var i=0; i<messsages.length; i++) {
			if (i != 0) message += "|";
			message += messsages[i].replace(/[|]/gi,'_');
		}
		
		var url = '';
		if (document.g_statsUrl) url = document.g_statsUrl;
		else if (g_statsUrl) url = g_statsUrl;
		
		// Get values
		var date = new Date().getTime();
		var session = jQuery.cookie('PHPSESSID');
		
		var user = '';
		if (document.g_userLogin) user = document.g_userLogin;
		else if (g_userLogin) user = g_userLogin;
		
		// Encode for url transmission
		event_type = encodeURI(event_type);
		message = encodeURI(message);
		session = encodeURI(session);
		user = encodeURI(user);
		
		// Add virtual image which make the request
		jQuery('body').append('<img style="position: absolute; top: -5px; left: -5px;" width="1" height="1" border="0" src="'+url+'?time='+date+'&status='+event_type+'&message='+message+'&session='+session+'&user='+user+'" />');
	// Catch errors
	} catch (e) {
		logger.error("common.js > "+e);
	}
}
