/* Show the login form on the header */
var showLoginFrom = false;
function showLoginForm() {
	if ($("#hdrLogin").is(":hidden") ) {
		$("#hdrLogin").show("slow");
		if ($("#hdrLogin form[name=frmHdrLogin] input[name=email]").val().length > 0) {
			$("#hdrLogin form[name=frmHdrLogin] input[name=password]").focus();
		} else {
			$("#hdrLogin form[name=frmHdrLogin] input[name=email]").focus();
		}
		$("#hdrLogin").data("show", true);
	} else {
		$("#hdrLogin").hide("slow");
		$("#hdrLogin").data("show", false);
	}
}
var retryTryLoginCount = 0;
function relogin() {	
	if ($("#hdrLogin").data("success") != "1") {		
		//Relogin		
		if (retryTryLoginCount < 3) {
			retryTryLoginCount++;
			dologin($("#hdrLogin").data("formName"));
		} else {
			retryTryLoginCount = 0;
			$("#hdrLogin").html(html).show();
			$("#hdrTool").hide();
			$("#hdrLoginProgress").hide();
		}
	}
}
function doLogin(formName) {
	var email = $("#hdrLogin form[name=" + formName + "] input[name=email]").val();
	var password = $("#hdrLogin form[name=" + formName + "] input[name=password]").val();
	$("#hdrLogin").hide();
	
	$("#hdrLoginProgress").show().html('<img src="/images/loading1.gif" with=51 height=19 align=absmiddle> Đang đăng nhập');
	
	
	$.ajax({
	  url: "/ajax/ajax_login.php",
	  cache: false,
	  type: "POST",
      data: "email=" + email + "&password=" + password,
	  timeout: 30000,
	  success: function(html){
		$("#hdrTool").html(html);
		//document.location.href = "./store.php?action=myroom";
	  },
	  error: function() {
		 alert('Lỗi không đăng nhập được, bạn hãy thử lại sau vài giây.');
		 $("#hdrLogin").show();
		 $("#hdrTool").hide();
		 $("#hdrLoginProgress").hide();		
	  }
	});
}

function logout() {
	$("#hdrLoginProgress").show().html('<img src="/images/loading1.gif" with=51 height=19 align=absmiddle> Đang thoát&nbsp;');
	$.ajax({
	  url: "/ajax/ajax_logout.php",
	  cache: false,
	  type: "GET",
	  timeout: 30000,
	  success: function(html){
		$("#hdrLogin").html(html).show();
		$("#hdrTool").hide();
		$("#hdrLoginProgress").hide();
	  },
	  error: function() {
		 alert('Lỗi không thoát được, bạn hãy thử lại sau vài giây.');
	  }
	});
}

function insertHTML(value, id)
{	
	// Get the editor instance that we want to interact with.
	var oEditor = eval('CKEDITOR.instances.' + id);
	

	// Check the active editing mode.
	if ( oEditor.mode == 'wysiwyg' )
	{
		// Insert HTML code.
		// http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#insertHtml
		oEditor.insertHtml( value );
	}
	else
		alert( 'You must be in WYSIWYG mode!' );
}
function focusYoutubeField(fieldId) {
	if ($('#' + fieldId).val() == 'Nhập đường dẫn hoặc mã nhúng của clip...') {
		$('#' + fieldId).val('');
	}	
}
function blurYoutubeField(fieldId) {
	if ($('#' + fieldId).val() == '') {
		$('#' + fieldId).val('Nhập đường dẫn hoặc mã nhúng của clip...');
	}	
}
function insertYoutubeClip(fieldId, editorId) {
	var code = $('#' + fieldId).val();
	var data = "code=" + code;
	$.ajax({
	  type: "POST",
	  url: "/ajax/store/ajax_insert_youtube_clip.php",
	  data: data,
	  processData: false,
	  success: function(html) {
		
			//alert(data.html);
			//alert(data.html.replace("\\",""));
			if (html.indexOf("ERROR", 0) == 0) {
				alert("Có lỗi: " + html);
			} else if (html.indexOf("EMBED_DETECTED", 0) == 0) {
				insertHTML(code, editorId);
			} else {
				insertHTML(html, editorId);
			}
		
	  },
	  error: function(XMLHttpRequest, textStatus, errorThrown) {
		 alert('Xin lỗi, chức năng chèn phim không hoạt động vào lúc này.' + textStatus + " errorThrown=" + errorThrown);
		 
	  }
	});
}
function showSearchArea() {	
	$("#searchArea").toggle();
}
