var fbpanelopen = true;
function fbpanelcontrol(){
	if(fbpanelopen == true){
		$("#fbpanel").slideUp(1000);
		fbpanelopen = false;
		createCookie("fbpanelopen", "0", 10);
	} else {
		$("#fbpanel").fadeIn(1000);
		fbpanelopen = true;
		createCookie("fbpanelopen", "1", 10);
	}
	
}


function fbaddfavobject(val){
	$("#fbaddfavlink_"+val).html("Sparad");
		$.ajax({
		  url: "fb.addfavobject.php",
		  type: "GET",
		  data: { val : val },
		  cache: false,
		  success: function(html){
		  }
		});
	
	
}
function fbremfavobject(val){

	if(confirm("Vill du verkligen radera denna favorit?")){
	
		$.ajax({
		  url: "fb.fbremfavobject.php",
		  type: "GET",
		  data: { val : val },
		  cache: false,
		  success: function(html){
		  	if(html != "NO"){
				fbfavlistopen = false;
			  	fbgetuserfavlist();
				$("#fbuserfavobjectlink").html('<a href="#" onclick="if('+html+' > 0){ fbgetuserfavlist(); return false; } else { return false; }">Stjärnmärkta objekt ('+html+')</a>');
			}
		  }
		});
		
	
	} 
	return false;
}


var fbfavlistopen = false;
function fbgetuserfavlist(){
	if(fbfavlistopen == false){
		fbfavlistopen = true;
		$.ajax({
		  url: "fb.getuserfavlist.php",
		  type: "GET",
		  data: { },
		  cache: false,
		  success: function(html){
		  	if(html != "NO")
			  	$("#fbuserfavlist").html(html).show();
			else
				$("#fbuserfavlist").hide();
		  }
		});
	} else {
		fbfavlistopen = false;
		$("#fbuserfavlist").slideUp();
	}
}
function fbcommentlogin (val){
	FB.Connect.requireSession(function(){
		fbgetlogin();
		getobjectcommentbox(val);
	});
}
function fbcommentblurinput(val){
	var msg = $("#fbcommentinput_"+val).val();
	var l = msg.length;
	
	if(l == 0){
		$("#commentbox_"+val).html('<input class="fbcommentboxinput" onclick="getobjectcommentbox('+val+');" type="Text" value="Kommentera...">');
	}
}

function fbupdatesearchoption (t, val){
	$.ajax({
	  url: "fb.updatesearchoption.php",
	  type: "GET",
	  data: { t : t, val : val},
	  cache: false,
	  success: function(html){
	  	
	  }
	});
}

function fbgetobjectcomments(val){
	$.ajax({
	  url: "fb.getobjectcomments.php",
	  type: "GET",
	  data: { val : val},
	  cache: false,
	  success: function(html){
	  	if(html != "NO"){
			$("#commentlist_"+val).html(html);
		}
	  }
	});
}

function fbcommentcompose(val){
	var elname = "fbcommentinput_"+val;
	var msg = $("#"+elname).val();
	
	if(msg.length > 0){
		$.ajax({
		  url: "fb.objectcommentcompose.php",
		  type: "GET",
		  data: { val : val, msg : msg},
		  cache: false,
		  success: function(html){
		  	fbgetobjectcomments(val);
			$("#"+elname).val("");
			fbcommentblurinput(val);
		  }
		});
		
	}
}	
function fbgetlogin(page){
		if(Get_Cookie("fbpanelopen") == "1"){
			fbpanelopen = true;
		} else {
			fbpanelopen = false;
		}

	$.ajax({
	  url: "fb.getlogin.php",
	  type: "GET",
	  data: { fblp : page},
	  cache: false,
	  success: function(html){
	  	
		$("#fblogin").html(html);
		initFB();
		
	  }
	});

}

function fbsetstatus(){

	var val = $("#selectfbstatus").val();

	$.ajax({
	  url: "fb.setstatus.php",
	  type: "GET",
	  data: { val : val},
	  cache: false,
	  success: function(html){
	  	$("#fbstatus").html(html);
	  }
	});
	

}
function fbsetgender(){

	var val = $("#selectfbgender").val();

	$.ajax({
	  url: "fb.setgender.php",
	  type: "GET",
	  data: { val : val},
	  cache: false,
	  success: function(html){
	  	$("#fbgender").html(html);
	  }
	});
	

}
function fbsetlocation(){

	var val = $("#selectfblocation").val();

	$.ajax({
	  url: "fb.setlocation.php",
	  type: "GET",
	  data: { val : val},
	  cache: false,
	  success: function(html){
	  	$("#fbcity").html(html);
	  }
	});
	

}

function fbsetbirthdate(){
	var y = $("#selectfbbirthyear").val();
	var m = $("#selectfbbirthmonth").val();
	var d = $("#selectfbbirthday").val();

	$.ajax({
	  url: "fb.setbirthdate.php",
	  type: "GET",
	  data: { y : y, m : m, d : d},
	  cache: false,
	  success: function(html){
	  	$("#fbbirthdate").html(html);
	  }
	});
}
function fbsetmoveperiod(){
	var s = $("#selectfbmoveperiodstart").val();
	var e = $("#selectfbmoveperiodend").val();

	$.ajax({
	  url: "fb.setmoveperiod.php",
	  type: "GET",
	  data: { s : s, e : e },
	  cache: false,
	  success: function(html){
	  	$("#fbmoveperiod").html(html);
	  }
	});

}
function fbchangemoveperiod(current, y){

	var s = current.split(",");
	var manader = new Array();
	manader[1] = "Jan";
	manader[2] = "Feb";
	manader[3] = "Mar";
	manader[4] = "Apr";
	manader[5] = "May";
	manader[6] = "Jun";
	manader[7] = "Jul";
	manader[8] = "Aug";
	manader[9] = "Sep";
	manader[10] = "Oct";
	manader[11] = "Nov";
	manader[12] = "Dec";
	var start = s[0];
	var stop = s[1];
	
	
	var res = '';
	res += '<select id="selectfbmoveperiodstart">';
	
	for(i = y; i <= y+1; i++){
		for(o = 1; o <= 12; o++){
			on = o < 10 ? '0'+o : o;
			t = ''+i+'-'+on;
			n = manader[o]+' '+i;
			res += '<option value="'+t+'"'+(start == t ? ' selected' : '')+'>'+n;
		}
	}
	
	res += '</select>';

	res += '<select id="selectfbmoveperiodend">';
	
	for(i = y; i <= y+1; i++){
		for(o = 1; o <= 12; o++){
			on = o < 10 ? '0'+o : o;
			t = ''+i+'-'+on;
			n = manader[o]+' '+i;
			res += '<option value="'+t+'"'+(stop == t ? ' selected' : '')+'>'+n;
		}
	}
	
	res += '</select><input type="button" value="OK" class="button" onclick="fbsetmoveperiod();">';

	$("#fbmoveperiod").html(res);

}

function fbchangebirthdate (current){
	var c = current.split("-");
	var res = "";
	res += '<select class="selectfbbirthdate" id="selectfbbirthyear">';
	for(i = 10; i <= 99; i++){
		var ins = "19"+i;
		res += '<option value="'+ins+'"'+(ins == c[0] ? " selected" : "")+'>'+i;
	}
	for(i = 0; i <= 5; i++){
		var ins = "200"+i;
		res += '<option value="'+ins+'"'+(ins == c[0] ? " selected" : "")+'>'+i;
	}
	res += "</select>";
	res += '<select class="selectfbbirthdate" id="selectfbbirthmonth">';
	for(i = 1; i <= 12; i++){
		var ins = i < 10 ? "0"+i+"" : i;
		res += '<option value="'+ins+'"'+(ins == c[1] ? " selected" : "")+'>'+ins;
	}

	res += "</select>";
	res += '<select class="selectfbbirthdate" id="selectfbbirthday">';
	for(i = 1; i <= 31; i++){
		var ins = i < 10 ? "0"+i+"" : i;
		res += '<option value="'+ins+'"'+(ins == c[2] ? " selected" : "")+'>'+ins;
	}

	res += '</select><input type="button" value="OK" class="button" onclick="fbsetbirthdate();">';
	
	$("#fbbirthdate").html(res);
}

function fbchangestatus (current){

	var status = new Array("Pensionär", "Mellan två jobb", "Arbetande", "Student");

	var t='<select id="selectfbstatus">';
	
	for(var i in status){
		t += '<option value="'+status[i]+'"'+(current == status[i] ? " selected" : "")+'>'+status[i];
	}
	
	t += '</select><input type="button" value="OK" class="button" onclick="fbsetstatus();">';
	$("#fbstatus").html(t);
	
}
function fbchangelocation(current_id){

	$.ajax({
	  url: "fb.changelocation.php",
	  type: "GET",
	  data: { val : current_id},
	  cache: false,
	  success: function(html){
	  	$("#fbcity").html(html);
	  }
	});

	
}
function fbchangegender(current){

	var status = new Array("Man", "Kvinna");

	var t='<select id="selectfbgender">';
	
	for(var i in status){
		t += '<option value="'+status[i]+'"'+(current == status[i] ? " selected" : "")+'>'+status[i];
	}
	
	t += '</select><input type="button" value="OK" class="button" onclick="fbsetgender();">';
	$("#fbgender").html(t);
}
