var ajax_url = "http://www.exoin.com/ajax.php";
//var ajax_url = "http://localhost/exoin/ajax.php";

function validateFrmAddUser() {
	if ($("#login").val() == "") {
	   alert(validate_user_empty_username);
	   document.frm_add.login.select();	
	   return false;
	}
	if ($("#password").val() == "") {
	   alert(validate_user_empty_password);
	   document.frm_add.password.select();	
	   return false;
	}	
	if ($("#password").val() != $("#repass").val()) {
	   alert(validate_not_match_password);
	   document.frm_add.password.focus();	
	   return false;
	}		
	return true;
}

function verifyExistingUser() {	
	$("#loader").css({display:"block"});	
	$("#target").css({display:"block"});		
	$.ajax({
		url: ajax_url + "?action=verify&login="+$("#login").val(),
		cache: false,
		success: function(html){
			$("#target").html(html);
			$("#loader").css({display:"none"});
			$("#verify_message").fadeOut(4000,function(){
				$("#verify_message").css({display:"none"});
			});
		}
	});
	return false;
}

function exit(el, path) {					
	el=document.getElementById(el);		
	$(el).css({display:"block"});
	$(el).html("Processing request...");
	$.ajax({
		url: path,
		cache: false,
		success: function(html){
			$(el).html(html);
			window.location='signin';			
		}
	});
	return false;
}

function selectFeedType(feed_url,username){
		if( $('#source').val() != 'Rss'){
			$('#import_as').hide();
			$('#url_label').text($('#source').val() + username) ;
		}else{
			$('#import_as').show();
			$('#url_label').text(feed_url);
		}
}

function followUser(user_id, display_id) {
	$("#loader").css({display:"block"});	
	$.ajax({
		url: ajax_url + "?action=followUser&user_id="+user_id,
		cache: false,
		success: function(html){
			$("#loader").css({display:"none"});			
			$("#follow_"+display_id).css({display:"none"});
			$("#remove_"+display_id).css({display:"block"});
		}
	});
	return;
}

function removeFollowUser(user_id, display_id) {
	$("#loader").css({display:"block"});	
	$.ajax({
		url: ajax_url + "?action=removeFollowUser&user_id="+user_id,
		cache: false,
		success: function(html){
			$("#loader").css({display:"none"});			
			$("#follow_"+display_id).css({display:"block"});
			$("#remove_"+display_id).css({display:"none"});
		}
	});
	return;
}


function followPost(post_id, display_id) {
	$("#loader").css({display:"block"});	
	$.ajax({
		url: ajax_url + "?action=followPost&post_id="+post_id,
		cache: false,
		success: function(html){
			$("#loader").css({display:"none"});		
			$("#follow_post_bar_"+display_id).css({display:"none"});
			$("#remove_post_bar_"+display_id).css({display:"block"});	
		}
	});
	return;
}

function removeFollowPost(post_id, display_id) {
	$("#loader").css({display:"block"});	
	$.ajax({
		url: ajax_url + "?action=removeFollowPost&post_id="+post_id,
		cache: false,
		success: function(html){
			$("#loader").css({display:"none"});
			$("#follow_post_bar_"+display_id).css({display:"block"});
			$("#remove_post_bar_"+display_id).css({display:"none"});
		}
	});
	return;
}


/******** REPORT **********/
var lastReportID;
var lastReportType;
var lastReportTarget;
var lastReportText;
function openReportPanel(id, type, event) {
	var e = event ? event : window.event;
	
	var target;
	if(e.target) {
		target = e.target
	} else {
		target = e.srcElement;
	}
	lastReportTarget = target;	
	
	var posx = 0;
	var posy = 0;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	} else {	
	
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		
	}
	
	if(type == "USER") {
		posx -= 150;
	}
	posy += 15;
	
	lastReportID = id;
	lastReportType = type;
	
	//Set visible
	$("#report_panel_type_USER").css({display:"none"});
	$("#report_panel_type_POST").css({display:"none"});
	$("#report_panel_type_COMMENT").css({display:"none"});
	$("#report_panel_type_"+type).css({display:"block"});
	
	var selType = document.getElementById('report_panel_type_'+type);
	selType.selectedIndex = 0;
	
	$("#report_panel").css({display:"block",top:posy,left:posx});
	
	return false;
}

function closeReportPanel() {
	$("#report_panel").css({display:"none"});
	return false;
}

function reportIt() {

	var selType = document.getElementById('report_panel_type_'+lastReportType);
	
	var reportType = document.getElementById('report_panel_type_'+lastReportType).value;
	var reportText = document.getElementById('report_panel_text').value;
	
	if (reportType == "-1") {
		alert(report_select_reason)
		return false;
	} else if (reportText.length <= 0) {
		alert(report_type_reason)
		return false;
	}

	$("#loader").css({display:"block"});
	$.ajax({
		url: ajax_url + "?action=report&id="+lastReportID+"&type="+lastReportType+"&reportType="+reportType+"&reportText="+reportText,
		cache: false,
		success: function(html){
			$("#loader").css({display:"none"});
		}
	});
	
	//lastReportTarget.style.visibility = 'hidden';
	lastReportTarget.parentNode.parentNode.innerHTML = report_result;
	closeReportPanel();
	return false;
	
}

/****************************/



/**** Preview ****/

function previewPost() {

	var type,title,description,url;
	
	type = $("#type").val();
	title = $("#title").val();
	url = $("#url").val();
	obj = "";
	
	try {
		/* description = myNicEditor.nicInstances[0].getContent(); */
		
		var ed = tinyMCE.get('description');
		description = ed.getContent();
		description = description.replace(/&/g, "&amp;");
		
		obj = $("#object").val();
	} catch(e) {
		description = $("#description").val();
	}
	
	$("#loader_preview").css({display:"block"});	
	$("#preview_panel").css({display:"block"});		
	$.ajax({
		url: ajax_url + "?action=preview&type="+type+"&title="+encodeURIComponent(title)+"&description="+encodeURIComponent(description)+"&url="+escape(url)+"&object="+encodeURIComponent(obj),
		cache: false,
		success: function(html){
			var clearHTML = html;
			$("#preview_panel").html(clearHTML);
			$("#loader_preview").css({display:"none"});
			$("#preview_panel").css({display:"block"});
		}
	});
	return false;
}

/*****************/


/****  Message ***/

function openMessage(id, type) {

	$("#message_short_"+id).css({display:"none"});
	$("#message_"+id).css({display:"block"});
	
	if(type == 'inbox') {
	
		$("#loader").css({display:"block"});
		$.ajax({
			url: ajax_url + "?action=readMessage&id="+id,
			cache: false,
			success: function(html){
				$("#loader").css({display:"none"});
			}
		});
	
	}
	
	
	return false;
}

function deleteMesssage(id, type) {

	$("#message_div_"+id).css({display:"none"});
	
	$("#loader").css({display:"block"});
	$.ajax({
		url: ajax_url + "?action=deleteMessage&id="+id+"&type="+type,
		cache: false,
		success: function(html){
			$("#loader").css({display:"none"});
		}
	});
	
	return false;
	
}

/******************/



/**** Main Page Tab ****/

function getLatestByType(type) {
	$("#tab_loader").css({display:"block"});	
	$("#tab_panel").css({display:"block"});		
	$.ajax({
		url: ajax_url + "?action=getLatestByType&type="+type,
		cache: true,
		success: function(html){
			var clearHTML = html;
			$("#tab_panel").html(clearHTML);
			$("#tab_loader").css({display:"none"});
			$("#tab_panel").css({display:"block"});
			$(".tabs-nav li").removeClass("tabs-selected");
			$("#tab_"+type).addClass("tabs-selected");
		}
	});
	
	return false;
}

/*****************/



/******  Help *****/

function toogleVisible(display_str) {
	var display_id = document.getElementById(display_str);
	if(display_id.style.display == "none") {
		display_id.style.display = "block";
	} else {
		display_id.style.display = "none";
	}	
	return false;
}


var isSliding = false;
function slideLeft(display_str, value, visual_width) {
	if(isSliding) {
		return false;
	}
	var left = Number($("#"+display_str).css('left').substring(0, $("#"+display_str).css('left').length-2));
	var width = Number($("#"+display_str).css('width').substring(0, $("#"+display_str).css('width').length-2));
	//alert(left+ " " + width + " " + visual_width);
	if(left <= visual_width - width + 20) {
		return false;
	}
	$("#"+display_str).animate({"left": "-="+value+"px"}, "slow", "swing", endSlide);
	isSliding = true;
	return false;
}

function slideRight(display_str, value) {
	if(isSliding) {
		return false;
	}
	var left = Number($("#"+display_str).css('left').substring(0, $("#"+display_str).css('left').length-2));
	if(left >= 0) {
		return false;
	}
	$("#"+display_str).animate({"left": "+="+value+"px"},  "slow", "swing", endSlide);
	isSliding = true;
	return false;
}

function endSlide() {
	isSliding = false;
}

/******************/