//document.domain = 'easyfav.com';

function initEvents()
{
	MozUserSelect = document.body.style.MozUserSelect;
	document.body.onmousemove = moveDragableElement;
	document.body.onmouseup = stop_dragDropElement;
	document.body.onselectstart = cancelSelectionEvent;

	document.body.ondragstart = cancelEvent;
	//window.onscroll	=	onScrollFunction;
	window.onresize =	onResizeFunction;
	if (window.event || window.navigator.appName == "Microsoft Internet Explorer") // IE
	{
		documentHeight = document.body.offsetHeight;
	}
	else
	{
		documentHeight = document.documentElement.clientHeight;
	}
	if (document.getElementById('menuCom'))
		document.getElementById('menuCom').onmouseup = initFromHashDelayed;
	if (document.getElementById('menuSettings'))
		document.getElementById('menuSettings').onmouseup = initFromHashDelayed;
	if (document.getElementById('homeId'))
		document.getElementById('homeId').onmouseup = initFromHashDelayed;
	if (document.getElementById('menulinkLeft'))
		document.getElementById('menulinkLeft').onmouseup = initFromHashDelayed;
	window.addEventListener('hashchange', initFromHash, false);
}

function initFromHashDelayed()
{
	//location.hash = "";
	//initFromHash();
}
function initFromHash(e)
{
	//window.removeEventListener('hashchange', initFromHash, false);	
	if (location.hash.length == 0)
	{	
		location.hash ="#home";
	}
	var hashValue = location.hash.split('/');
	Delayed_HideCard();
	if (hashValue.length != 0)
	{
		if (!document.getElementById("myeasyfavtab"))
		{
			if (hashValue[0] == "#p" && hashValue.length == 2)
			{
				document.getElementById('profilNickname').value = hashValue[1];
				document.getElementById('formLoadPage').submit();
			}
			else if (hashValue[0] == "#p" && hashValue.length == 3)
			{
				document.getElementById('profilNickname').value = hashValue[1];
				if (hashValue[2].length != 0)
					document.getElementById('profilUniverse').value = hashValue[2];
				document.getElementById('formLoadPage').submit();
			}
			else
			{
				if (hashValue[0] == "#com" || hashValue[0] == "#settings")
					AW_Show("Need to be logged", true);				
				else
					AW_Show("Invalid request", true);				
			}
			return;
		}
		if (hashValue[0] == "#com")
		{
			if (hashValue.length != 3)
				AJAX_SendRequest('./ajax/communication.php?com');
			else
				AJAX_SendRequest('./ajax/communication.php?com&nickname=' + hashValue[2]);
				
			//location.hash = "#com/notif";
		}
		else if (hashValue[0] == "#settings")
		{
			AJAX_SendRequest('./ajax/settings.php?settings');
		}
		else if (hashValue[0] == "#p" && hashValue.length == 2)
		{
			AJAX_SendRequest("./ajax/users.php?p=" + hashValue[1]);
		}
		else if (hashValue[0] == "#p" && hashValue.length == 3)
		{
			if (hashValue[2].length != 0)
				AJAX_SendRequest("./ajax/users.php?u=" + hashValue[2] + "&p=" + hashValue[1]);
			else
				AJAX_SendRequest("./ajax/users.php?p=" + hashValue[1]);				
		}
		else if (hashValue[0] == "#home" && hashValue.length == 1)
		{
			AJAX_SendRequest("./ajax/users.php?home");
		}
		else if (hashValue[0] == "#home")
		{
			AJAX_SendRequest("./ajax/users.php?u=" + hashValue[1] + "&home");
		}
	}
}

function TAB_OnClick(str)
{
	window.removeEventListener('hashchange', initFromHash, false);	
	window.location.hash = str;
	setTimeout("window.addEventListener('hashchange', initFromHash, false);", 500);
}

function initNodeHashLinks(node)
{
	var aList = node.getElementsByTagName("a");
	for (i = 0; i < aList.length; i++)
	{
		if (aList[i].getAttribute('rel') == 'useHashValue')
			aList[i].onmouseup = initFromHashDelayed; 
	}
}

function COMMON_StopPropagation(e)
{
	if (window.event) // IE
	{
		if (window.navigator.appName == "Microsoft Internet Explorer")
			window.event.cancelBubble = true;
		else
		{
			e.stopPropagation();
		}
	}
	else
	{
		e.stopPropagation();
	}
}

function COMMON_GetButtonClicked(e)
{
	if (window.event && window.navigator.appName == "Microsoft Internet Explorer") // IE
	{
		return window.event.button;
	}
	else
	{
		return e.which;
	}
}

function COMMON_GetTarget(e)
{
	if (window.event && window.navigator.appName == "Microsoft Internet Explorer") // IE
	{
		return event.srcElement;
	}
	else
	{
		return e.target;
	}	
}

function COMMON_GetRelatedTarget(e)
{
	{
		return e.relatedTarget;
	}	
}

function COMMON_GetTargetToElement(e)
{
	if (window.event && window.navigator.appName == "Microsoft Internet Explorer") // IE
	{
		return event.toElement;
	}
	else
	{
		return e.target;
	}	
}

function COMMON_HasAttributes(obj)
{
	if (window.event || window.navigator.appName == "Microsoft Internet Explorer") // IE
	{
		return false;
	}
	else
	{
		return obj.hasAttributes();
	}
}

function COMMON_GetClass(obj)
{
	if (!obj.getAttribute)
		return;
	if (window.event)
	{
		if (window.navigator.appName == "Microsoft Internet Explorer") // IE
			return obj.getAttribute("className");
		else
			return obj.getAttribute("class");
	}
	else	if (window.navigator.appName == "Microsoft Internet Explorer") // IE
	{
		return obj.getAttribute("className");		
	}
	else
	{
		return obj.getAttribute("class");
	}
}

function COMMON_SetClass(obj, value)
{
	if (window.event) // IE
	{
		if (window.navigator.appName == "Microsoft Internet Explorer") // IE
		{
			return obj.setAttribute("className", value);			
		}
		else
			return obj.setAttribute("class", value);
	}
	else if (window.navigator.appName == "Microsoft Internet Explorer")
	{
		return obj.setAttribute("className", value);		
	}
	else
	{
		return obj.setAttribute("class", value);
	}
}

function COMMON_GetTopPos(obj)
{
	var top = obj.offsetTop/1;
	while((obj = obj.offsetParent) != null){
		if(obj.tagName!='HTML')top += obj.offsetTop;
	}
	if(document.all)top = top/1 + 13; else top = top/1 + 4;
	return top;
}

function COMMON_GetLeftPos (obj)
{
	var left = obj.offsetLeft/1 + 1;
	while((obj = obj.offsetParent) != null){
		if(obj.tagName!='HTML')left += obj.offsetLeft;
	}
	if(document.all)left = left/1 - 2;
	return left;
}

function COMMON_GetClientWidth()
{
	if (window.event)
	{
		if (window.navigator.appName == "Microsoft Internet Explorer") // IE
		{
			value = document.body.clientWidth;
		}
		else
		{
			// Opera entre autres
			value = document.body.clientWidth;
			//value = document.documentElement.clientWidth;
		}
	}
	else
	{
		// FF / Chrome
		value = document.documentElement.clientWidth;
	}
	return value;
}

function COMMON_GetClientHeight()
{
	if (window.event)
	{
		if (window.navigator.appName == "Microsoft Internet Explorer") // IE
		{
			value = document.body.clientHeight;
		}
		else
		{
			// Opera entre autres
			value = document.body.clientHeight;
			//value = document.documentElement.clientHeight;
		}
	}
	else
	{
		// FF / Chrome
		value = document.documentElement.clientHeight;
	}
	return value;
}

function COMMON_MouseLeftClickValue()
{
	if (window.event && window.navigator.appName == "Microsoft Internet Explorer") // IE
	{
		if (window.event.type == "mouseup")
			return 1;
		else if (window.event.type == "click")
			return 0;
		return window.event.button;
	}
	else
	{
		return 1;
	}
}

function COMMON_GetValueFromStyle(heightOrWidth)
{
	return heightOrWidth.toString().replace("px", "");
}

function COMMON_clearSelection ()
{
	if (window.getSelection)
		window.getSelection().removeAllRanges();
	else if (document.selection && document.selection.empty)
		document.selection.empty();
}

function COMMON_show_AddFormRequest(requestFile, callback, callbackOnClose)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = requestFile;
	ajaxObjects[ajaxIndex].onCompletion = function(){ COMMON_responseShowAddForm(ajaxObjects[ajaxIndex].response, callback, callbackOnClose);};	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

var oShowHideTimeOut = null;
var fValue = 0;
var func_callbackOnClose = null;
function COMMON_responseShowAddForm(html, callback, callbackOnClose)
{
	openTopPanels(true);
	COMMON_callAddFormCloseCallback();
	var div = document.getElementById('addDiv');
	div.style.opacity = 0;
	div.innerHTML = html;
	div.style.display = "block";
	if (callback)
		callback();
	func_callbackOnClose = callbackOnClose;
	fValue = 0;
	if (oShowHideTimeOut)
		clearTimeout(oShowHideTimeOut);
	oShowHideTimeOut = setTimeout(COMMON_showAddForm, 50);
}

function COMMON_showAddForm()
{
	var div = document.getElementById('addDiv');
	if (div.style.opacity < 1)
	{
		fValue = fValue + 0.1;
		div.style.opacity = fValue;
		oShowHideTimeOut = setTimeout(COMMON_showAddForm, 50);
	}
	else
	{
		if (oShowHideTimeOut)
			clearTimeout(oShowHideTimeOut);
	}
}

function COMMON_hideAddForm()
{
	var div = document.getElementById('addDiv');
	if (div.style.opacity > 0)
	{
		fValue = fValue - 0.1;
		div.style.opacity = fValue;
		div.style.display = "none";
		oShowHideTimeOut = setTimeout(COMMON_hideAddForm, 50);
	}
	else
	{
		if (oShowHideTimeOut)
			clearTimeout(oShowHideTimeOut);
	}
	COMMON_callAddFormCloseCallback();
}

function COMMON_callAddFormCloseCallback()
{
	if (func_callbackOnClose)
		func_callbackOnClose();
	functioncallbackOnClose = null;	
}

function COMMON_GetHashFromURL(url)
{
	value = url.split("#");
	return value[1];
}

function COMMON_GetHashFromName(name)
{
	name = name.replace(" ", "_");
	name = encodeURI(name);
	return "#" + name;
}

function COMMON_IsChildOf(obj, objParent)
{
	var oNodeList = objParent.childNodes;
	if (!oNodeList)
		return false;
	for (var i = 0; i < oNodeList.length; i++)
	{
		if (oNodeList[i] == obj)
			return true;
		if (COMMON_IsChildOf(obj, oNodeList[i]))
			return true;
	}
	return false;
}
function COMMON_GetSizeFromPx(sPxValue)
{
	return parseInt(sPxValue.substr(0, sPxValue.length - 2));
}

// OS Detection
var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				   string: navigator.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};
	BrowserDetect.init();
	
// SHADOW BOX LOAD
/*if (typeof(Shadowbox) != 'undefined')
{
	baseurl = "../";
	Shadowbox.loadSkin('classic', baseurl + 'js/shadowbox/skin');
	Shadowbox.loadLanguage('fr', baseurl + 'js/shadowbox/lang');
	Shadowbox.loadPlayer(['iframe','html'], baseurl + 'js/shadowbox/player');
	var flow = new Array();
	var reload = false;
	
	$(document).ready(function(){
	    Shadowbox.init({
	        'onClose': function(){
	            if (flow.length > 0){
	                parent.checkPopup = true;
	                $(document).one('mouseover',function(){
	                    if (flow.length > 0 && checkPopup == true){
	                        page = flow[flow.length - 1];
	                        Shadowbox.open({
	                            "content": page.url,
	                            "player": "iframe",
	                            "width": page.width,
	                            "height": page.height
	                        });                
	                        flow.pop();
	                    }
	                }); //trap this later on the main window to open another lightbox
	            }else{
	                if (reload == true){
	                    if (!$("#justForReload").get(0))
	                        $("BODY").append('<form id="justForReload" method="post"></form>');
	                    $("#justForReload").get(0).submit();
	                }
	            }
	        }
	    });
	    
	    $(window).mouseover(function(){
	    })
	
	    $("#closeShadowbox").click(function(){
	        parent.Shadowbox.close();
	    });
	});
}*/
	
	function onChangeLanguage()
	{
		var t = document.getElementById("user_language");
		var optionList = t.getElementsByTagName("option");
		for (var i = 0; i < optionList.length; i++)
		{
			if (optionList[i].selected)
			{
				var ajaxIndex = ajaxObjects.length;
				ajaxObjects[ajaxIndex] = new sack();
				ajaxObjects[ajaxIndex].requestFile = "./common/change_language.php?change_lang=" + optionList[i].value;;
				ajaxObjects[ajaxIndex].onCompletion = function(){
					window.location.reload();
				};
				ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
				return;
			}
		}
	}

