var sp_key = '85te1behty0wo0cs';

var News = new News();
var Country = new Country();
var Region = new Region();
var Misc = new Misc();

var Friends = new Friends();

function Friends() {
	this.add = function(id,login){
		jQuery.get("/ajax/friends/add/"+id);
		
		$("#add_friend").html('');
		
		alert(login+" added as a friend");
		
		Popup.close();
	};
}

function News(){	
	this.get = function(path,no,max) {
		no--;
		jQuery.getJSON("/ajax/news/get/"+no, function(json) {
			$("#news_title").html(json.date + " - " + json.title);
			$("#news_text").html(json.text);
			
			if(no <= max && no > 1){
				$("#news_prev").html(' <a href="#" onclick="News.get(\''+path+'\','+no+', '+max+');return false;">Previous ></a>');
			}else{
				$("#news_prev").html(" ");
			}
			
			var next = no+2;
			if(next <= max+1){
				$("#news_next").html('<a href="#" onclick="News.get(\''+path+'\','+next+', '+max+');return false;">< Next</a> | ');
			}else{
				$("#news_next").html(" ");
			}
		});
	};
}

function Country() {
	this.add = function() {
		var name = document.getElementById("name").value;
		
		jQuery.get("/ajax/country/add/"+name);
		
		jQuery.getJSON("/ajax/countries/get/",function(json) {
			var html = '';
			html+='<option value=""></option>';
			for(var i=0;i<json.length;i++) {
				html+='<option value="'+json[i].id+'">'+json[i].name+'</option>';
			}
			
			$("#country").html(html);
		});
		
		Popup.close();
	};
}

function Region() {
	this.add = function(country) {
		var name = document.getElementById("name").value;
		
		jQuery.get("/ajax/region/add/"+name+"/"+country);
		
		Misc.setRegion(country);
		
		Popup.close();
	}
}

function Misc() {
	this.isIE6 = function(path) {
		var browser=navigator.appName;
		var b_version=navigator.appVersion;
		var start = b_version.indexOf("MSIE");
		var version=b_version.substring(start+5,start+6);
		
		if(browser=="Microsoft Internet Explorer" && version<=6) {
			jQuery.get("/ajax/ie6/true");
			return true;
		} else {
			jQuery.get("/ajax/ie6/false");
			return false;
		}
	};
	
	this.switchHome = function(which) {
		var whatHeight = $("#what").height() + 55;
		
		if(which == 'why') {
			$("#what").animate({marginTop:"-"+whatHeight+"px"}, 800);
			$("#title_home").html("<h1>Why use tenniscontact?</h1>");
		} else {
			$("#what").animate({marginTop:"0px"}, 800);
			$("#title_home").html("<h1>What is tenniscontact?</h1>");
		}
	};
	
	this.setCheckbox = function(checkbox) {
		var value = $("#"+checbox).value;
		
		if(value=='1')
			$("#"+checbox).value = '';
		else
			$("#"+checbox).value = '1';
		
	};
	
	this.forecast = function(location) {
		if(location!="")
		{
			jQuery.getJSON("/ajax/forecast/"+location+"/", function(json) {
				var description = json.rss.channel.item.description;
				
				$("#forecast").html(description);
			});
		}
	};
	
	this.refresh = function() {
		location.reload();
	};
	
	this.logout = function(domain) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+7);
		document.cookie = 'tenniscontact=;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;domain='+domain;
		
		location.href="/logout";
	};
	
	this.viewTopic = function(id) {
		jQuery.get("/ajax/viewTopic/"+id+"/");
	};
	
	this.answers = function(nbanswers) {
		var html = '';
		
		for(i=1;i<=nbanswers;i++){
			html+='<div>Answer '+i+'</div>';
			html+='<div><input type="text" name="answer'+i+'" style="width:300px;" /></div><br/>';
		}
		
		html+='<div class="or_btn"><input type="submit" value="Create" class="oran_btn" /></div>';
		
		$("#answers").html(html);
	};
	
	this.setPrivacy = function() {
		var emails = document.getElementById("emails").value;
		var password = document.getElementById("password").value;
		
		if(password==""){
			password = "null";
		}
		
		if(emails==""){
			emails = "null";
		}
		
		jQuery.post("/ajax/privacy/"+encodeURIComponent(emails)+"/"+password, function(json) {
			document.getElementById("idPrivacy").value = json;
			
			Popup.close();
		});
	};
	
	this.removePicture = function(id,path) {
		jQuery.get("/ajax/photos/remove/"+id);
		
		Misc.refresh();
	};
	
	this.forgotPassword = function(path) {
		var email = document.getElementById("email").value;
		
		jQuery.get("/ajax/forgot_password/"+encodeURIComponent(email), function() {
			alert("An email with a link to change your password has been sent to you");
		});
		
		Popup.close();
	};
	
	this.balloon = function(value, path) {
		jQuery.get("/ajax/balloon/"+value, function(json) {
			if(json==1 && document.getElementById("balloon")!=null){
				document.getElementById("balloon").style.display='none';
			}
		});
	};
	
	this.feedbackMenu = function() {
		if ($("#categories:last").is(":hidden")) {
			var pos = $("#Div1").position();
			var top = pos.top + 23;
			$("#categories").css({ marginTop: 0, top: top, left: pos.left });
			
			$("#categories").slideDown(200);
		} else {
			$("#categories").hide();
		}
	};
	
	this.participate = function(idEvent) {
		var phone = document.getElementById("phone").value;
		if(phone==""){phone="empty";}
		jQuery.post("/ajax/participate/"+idEvent+"/"+phone+"/");
		
		location.href="/events/event/"+idEvent;
	};
	
	this.confirm = function() {
		var fRet;
		fRet = confirm("Are you sure you want to delete this event?");
		
		return fRet;
	};
	
	this.setRegion = function(country,option) {
		jQuery.getJSON("/ajax/region/get/"+country,function(json) {
			var html = '';
			html+='<option value=""></option>';
			for(var i=0;i<json.length;i++) {
				html+='<option value="'+json[i].id+'">'+json[i].name+'</option>';
			}
			
			$("#region").html(html);
		});
		
		if(!option) {
			$("#link_region").html('<a href="#" onclick="Popup.region();return false;">Don\'t find it?</a>');
		}
		document.getElementById("region").disabled=false;
	};
	
	this.setStatus = function(pseudo) {
		document.getElementById("status").value = "";
		
		var obj = FB.Facebook.apiClient.get_session();
		var uid;
		var session;
		if(obj!=null){
			uid = obj.uid;
			session = obj.session_key;
		}
		
		var html = '<div class="or_btn"><input type="submit" value="OK" class="oran_btn" /></div>';
		if(session!=null)
			html+='<input type="hidden" name="session" value="'+session+'" />';
			
		$("#submit_status").html(html);
	};
	
	this.findUser = function(value, path) {
		if(value.length>2) {
			document.getElementById("users").style.visibility = "visible";
			
			jQuery.getJSON("/ajax/user/find/"+value,function(json) {
				var html='';
				for(var i=0;i<json.length;i++) {
					html+='<div onclick="Misc.setReceiver(\''+json[i].id+'\',\''+json[i].login+'\');">'+json[i].login+'</div>';
				}
				
				$("#users").html(html);
			});
		}
	};
	
	this.setReceiver = function(id,login) {
		document.getElementById("idReceiver").value=id;
		document.getElementById("to").value=login;
		document.getElementById("users").style.visibility="hidden";
	};
	
	this.hide = function() {
		if(document.getElementById("users")){
			document.getElementById("users").style.visibility="hidden";
		}
		
		if(document.getElementById("categories")){
			document.getElementById("categories").style.display = "none";
		}
	};
	
	this.addSmiley = function(id) {
		switch(id) {
			case 1:
				document.getElementById('message').value += ' (a) ';
				break;
			case 2:
				document.getElementById('message').value += ' :@ ';
				break;
			case 3:
				document.getElementById('message').value += ' (h) ';
				break;
			case 4:
				document.getElementById('message').value += ' :\'( ';
				break;
			case 5:
				document.getElementById('message').value += ' :$ ';
				break;
			case 6:
				document.getElementById('message').value += ' :k ';
				break;
			case 7:
				document.getElementById('message').value += ' :D ';
				break;
			case 8:
				document.getElementById('message').value += ' :) ';
				break;
			case 9:
				document.getElementById('message').value += ' ;) ';
				break;
			case 10:
				document.getElementById('message').value += ' :s ';
				break;
		}
	};
}