App.load = function(){
	App.startTime = (new Date()).getTime();


	// Check browser compatibility
	/*if (App.checkClient()) {
		dataHandler.loadUser();
	}*/
	if (!user.id){
		if(getCookie("user_id") && getCookie("session_id") && !getCookie("logout")){
			deleteCookie("user_id");
			dataHandler.loadUser();
		}else{
			if(getCookie("logout")){
				deleteCookie("logout");
			}
			App.showLogin();
		}
	}
}
App.loading = function(st){
	if(st==0){
		window.location.reload();
	}
}
App.reload = function(){
		window.location.reload();
}
App.logout = function(){
	setCookie("logout","1",36500,"");
	deleteCookie("user_id");
	dataHandler.logout();
}
App.showLoading = function(num){
	App.shadowing(num);
	if(num==0){
		var div=document.getElementById("loadingDiv");
		div.style.display="none";
	}
}
App.showLogin = function (args){
		var settingsElem = $("wwgSettings");
		if (settingsElem && settingsElem.visible()) {
			App.closeSettings();
		}
		
		if(this.popupPassRecovery){
			this.popupPassRecovery.state(0);
		}
		
		if(this.popupLogin){
			this.popupLogin.state(1);
			return;
		}
		var div=document.getElementById("loadingDiv");
		div.style.display="none";
		App.shadowing(1);
		
	var win = App.popupLogin = new popupWindow("login","SIGN_IN","<form id='formSignin'><div>"+
'<div style="padding-top:20px;padding-bottom:20px;padding-left:50px;padding-right:50px"><table style="width:100%">\
<tr><td><label for="settingsAccountLoginMail">EMAIL</label></td><td style="padding:5px; width:170px;"><input style="width:100%" type="text" id="settingsAccountLoginMail"/></td></tr>\
<tr><td><label for="settingsAccountLoginPassword">PASSWORD</label></td><td style="padding:5px; width:170px;"><input style="width:100%" type="password" id="settingsAccountLoginPassword"/></td></tr>\
</table></div><div style="text-align:right;padding-right:50px;"><input type="submit" value="SIGN_IN"/></div>'
+"</div></form>",400,200);	

	App.popupLogin.div.style.height="200px";
	
	App.translate($('formSignin'));
	
	this.popupLogin = win;
	 //Event.observe("formSignin","submit",
	 document.getElementById("formSignin").onsubmit = function(){// handling submit event via prototype break the callback return value...
		dataHandler.login(document.getElementById("settingsAccountLoginMail").value, 
		document.getElementById("settingsAccountLoginPassword").value);
		return false;
	 	};
}
App.discoAddRSSForm = function(){
	var url;
	feedOk=false;
	if($('feedurl')){
		url=$('feedurl').value;		
		url=url.replace(/ /g, "%20");
	//$("feedurlList").innerHTML="<img src='images/loading.gif' alt='wait...'/>";
		new Ajax.Request("../services/RSSdiscovery.php?lang="+user.language+"&url="+encodeURIComponent(url),{
			onComplete:function(xhr){
				var i,r,h="",m,t,rsss= eval("("+xhr.responseText+")");
				m=rsss.length;
				if (m && m>1){
					h='<select name="feedList" id="feedList"><option value="0">Select ...</option>';
					for(i=0;i<m;++i){
						r=rsss[i];
						t=String(r.title?r.title:r.href).escapeHTML();
						h+='<option value='+r.href+'>'+t+'</option>';
					}
					h+="</select><input type='button' onclick='App.discoAddRSSForm()' value='ok'>";
					$("feedDiv").innerHTML=h;
					sel=$("feedList");				
					Event.observe(sel,"change",App.convertListToText);
				}else if(m && m==1){		
					sel=$("feedurl");
					sel.value=rsss[0].href;
					document.getElementById("widgetName").value=rsss[0].title;
					document.getElementById("widgetDesc").value="";
					Event.observe(sel,"change",FeedSearch);
					livePreviewAuto();
				}else{
					$('feedurl').value="No feed found";
				}
			}
		});
		return false;
	}else{
		App.convertListToText();
	}
}
App.convertListToText = function(){
	var sel,url="",h="";
	sel=$("feedList");
	if(sel.selectedIndex=="0"){
		url=sel.options[1].value;
	}else{
		url=sel.options[sel.selectedIndex].value;
	}
	h='<input type="text" value="'+url+'" id="feedurl" size="40"/><input type="button" onclick="App.discoAddRSSForm()" value="ok">';
	$("feedDiv").innerHTML=h;
	sel=$("feedurl");
	Event.observe(sel,"change",FeedSearch);
	document.getElementById("widgetDesc").value="";
	document.getElementById("widgetName").value="";
	livePreviewAuto();
}
