//
//title=main js;version=3.3.0.1;date=2009-03-13
$(document).ready(function(){
	$("legend span.openhide").click(function(){
		$(this).parent().parent().find(".openhideblock").toggle('fast');
		$(this).find("strong").toggleClass("active");
	});
	
	//SEO	
	var seoarray = new Array();
	
	$("#seorefreshdiv #refresh").click(function(){
		
		seo_update_pb ("disabled");
		
		var module = $(this).attr("class");
		var page = $(this).attr("rel");
		
		
		$.ajaxSetup({
	  		url: '../ee_ajax.php',      
	  		data: 'operation=seoprep&module=' + module + '&page=' + page + "&langid=" + gloaballangid,      
	  		type: 'GET',
	  		dataType : 'xml',
	  		ifModified: true,
	 		success: function(data){
	 			$(data).find('el').each(function(){
					seoarray[$(this).find('key').text()] = new Array(
								$(this).find('v1').text(), 
								$(this).find('v2').text(), 
								$(this).find('v3').text(), 
								$(this).find('v4').text(), 
								$(this).find('v5').text()
								);
	 			});
      		},
     		cache: false
   		}); 
   		
   		$.ajax({ 
   				complete: function(){
   					seowork (seoarray);
    			},
    			error: function() {
    				seo_update_pb ("");
    			}
   			});
   		return false;
	});
	
	function seowork (seoarray)
	{
		var ee_seo_from_title = "";
        var ee_seo_from_text = "";
        var ee_seo_from_seourl = "";
        var ee_seo_from_imgalt = "";
        var seo_resault = new Array();
        if (seoarray["id"].length)
        {
        	var ft = "";
        	var ftt = "";
        	var fs = "";
        	var fi = "";
	        for (var i=0; i<5; i++)
	        {
	        	ft = "";
	        	ft = $("[name=" + seoarray["fromtitle"][i] + "]").val();
	        	if (ft != "on" && ft != "---") {
	        		ee_seo_from_title += " " + ft;
	        		}
	        	//innovaedit patch
	        	ftt = ""; 
	        	ftt = $("iframe#idContentedit_"+seoarray["fromtext"][i]).contents().find("body").text();
	        	if (seoarray["fromtext"][i]) {
	        		if(ftt && ftt != "---") {
	        			ee_seo_from_text += " " + ftt;
	        		}
	        		else {
	        			ftt = $("[name=" + seoarray["fromtext"][i] + "]").val();
	        			if (ftt != "on" && ftt != "---") {
	        				ee_seo_from_text += " " + ftt;
	        			}
	        		}
	        	}	
	        	fs = "";
	        	fs = $("[name=" + seoarray["fromseourl"][i] + "]").val();
	        	if (fs != "on" && fs != "---") {
	        		ee_seo_from_seourl += " " + fs;
	        		}
	        	fi = "";
	        	fi = $("[name=" + seoarray["fromimgalt"][i] + "]").val();
	        	if (fi != "on" && fi != "---") {
	        		ee_seo_from_imgalt += " " + fi;
	        		}
	        }
	         
	        var module = $("#seorefreshdiv #refresh").attr("class");
			var page = $("#seorefreshdiv #refresh").attr("rel");
			
	        $.ajaxSetup({
		  		url: '../ee_ajax.php',
		  		data: 'operation=seowork&module=' + module + '&page=' + page + '&langid=' + gloaballangid + '&fti=' + ee_seo_from_title + '&fte=' + ee_seo_from_text + '&fsu=' + ee_seo_from_seourl + '&fia=' + ee_seo_from_imgalt,
		  		type: "POST",
		  		dataType : 'xml',
		 		success: function(msg){
		 			
		 			$(msg).find('el').each(function(){
						seo_resault[$(this).find('key').text()] = $(this).find('val').text();
		 			});
	      		},
	     		cache: false
	   		}); 
	        
	        $.ajax({
	        	complete: function(){
				       update_seo (seoarray, seo_resault);
	    			},
    			error: function() {
//    				alert();
    				seo_update_pb ("");
    			}			        	
	        });
        }
        else
        {
        	seo_update_pb ("");
        }
	}
	
	function update_seo (seoarray, seo_resault)
	{
		var ch = $("#refreshallcheckbox").attr("checked");
		
		if (ch || $("[name="+seoarray["meta"][0]+"]").val() == "") {
			$("[name="+seoarray["meta"][0]+"]").val(seo_resault["meta_description"]);
		}
		
		if (ch || $("[name="+seoarray["title"][0]+"]").val() == "") {
			$("[name="+seoarray["title"][0]+"]").val(seo_resault["meta_title"]);
		}
		
		if (ch || $("[name="+seoarray["title"][0]+"]").val() == "") {
			$("[name="+seoarray["tags"][0]+"]").val(seo_resault["meta_tags"]);
		}
		
		if (ch || $("[name="+seoarray["imgalt"][0]+"]").val() == "") {
			$("[name="+seoarray["imgalt"][0]+"]").val(seo_resault["file_alt"]);
		}
		
		if (ch || $("[name="+seoarray["seourl"][0]+"]").val() == "") {
			$("[name="+seoarray["seourl"][0]+"]").val(seo_resault["seo_url"]);
		}

		seo_update_pb ("");
//		alert("Complete!");
	}
	
	function seo_update_pb (dis)
	{
		$("#eeseotable input").attr("disabled", dis);
		$("#eeseotable textarea").attr("disabled", dis);
	}
	
	$("#loglist strong").css("cursor", "pointer");
	$("#loglist strong").css("border-bottom", "1px dashed gray");
	$("#loglist strong").css("padding-bottom", "1px");
	$("#loglist strong").click(function(){
		var cid = $(this).attr("class");
		$("#loglist div#"+cid).toggle("fast");
	});
	
	//multichecbox
	$("div.category").click(function(){
		var id = $(this).attr("id");
		$(this).find("span").toggleClass("active");
		$("div#it" + id).toggle("show");
	});
	
	$("div.categoryitems label input").click(function(){
		ee_multicheckbox_count ($(this).parent().parent().attr("id"));
		$(this).parent().toggleClass("thischeckboxisselected");
	});
	
	$("div.multicheckboxblocktable label input").click(function(){
		$(this).parent().toggleClass("thischeckboxisselected");
	});
	
	$("div.multicheckboxblock label input").click(function(){
		$(this).parent().toggleClass("thischeckboxisselected");
	});
	
	$("div.categoryitems").each(function(){
		ee_multicheckbox_count ($(this).attr("id"));
	});
	
	function ee_multicheckbox_count (id)
	{
		var count = $("div#" + id + " label input:checked").length;
		if(count > 0){
			$("strong." + id).text(" [" + count + "]");
		}
		else {
			$("strong." + id).text("");
		}
	}
	
});
function ocmenu(item, tid, t1, t2) {
	var id = document.getElementById(item);
	var textid = document.getElementById(tid);
	if (t1 == null) 
	{
		t1 = "+";
	}
	if (t2 == null) 
	{
		t2 = "-";
	}
	if (id.style.display == "block") 
	{
		id.style.display = "none";
		textid.innerText = t1;
	}
	else 
	{
		id.style.display = "block";
		textid.innerText = t2;
	}
}

function selectall(formname)
{
	var num = document.forms[formname].length;
	if (document.forms[formname].elements["selectallch"].checked)
	{
		document.forms[formname].elements["selectallch"].checked = false;
	}
	else
	{
		document.forms[formname].elements["selectallch"].checked = true;
	}
	for ( var i=0; i<num; i++)
	{
		if (document.forms[formname].elements[i].type == "checkbox")
		{
			if (document.forms[formname].elements[i].checked)
			{
				document.forms[formname].elements[i].checked = false;
			}
			else
			{
				document.forms[formname].elements[i].checked = true;
			}
		}
	}
}

function count_process(elt, operation)
{
	var count = document.getElementById(elt);
	var valuethis = count.value;
	if ( valuethis == "NaN")
	{
		 valuethis = 1;
	}
	else
	{
		if (operation == "+")
		{
			valuethis++;
		}
		else
		{
			if (operation == "-")
			{
				valuethis--;
			}
		}
		if (valuethis < 0)
		{
			valuethis = 0;
		}
	}
	if ( valuethis == "NaN")
	{
		 valuethis = 1;
	}
	count.value = valuethis;
}

function eelogexpand(item, tid, t1, t2) {
	var id = document.getElementById(item);
	var textid = document.getElementById(tid);
	if (t1 == null) 
	{
		t1 = "+";
	}
	if (t2 == null) 
	{
		t2 = "—";
	}
	if (id.style.height != "auto") 
	{
		id.style.height = "auto";
		textid.innerText = t2;
	}
	else 
	{
		id.style.height = "30px";
		textid.innerText = t1;
	}
	return false;
}

function getPosit(elem) {
    var pos = {x : elem.offsetLeft, y : elem.offsetTop};
    if (elem.offsetParent) {
        var tmp = getPosit(elem.offsetParent);
        pos.x += tmp.x;
        pos.y += tmp.y;
    } 
    return pos;
}

function zoomover(id)
{
	var a = document.getElementById(id);
	if (a.hasChildNodes)
	{
		var i = a.childNodes.length;
		for (ii=0; ii<i; ii++)
		{
			if (a.childNodes[ii].tagName == 'IMG')
			{
				if (a.childNodes[ii].offsetParent)
				{
					var left = getPosit(a.childNodes[ii]);
				}
			}
			if (a.childNodes[ii].tagName == 'SPAN')
			{
				a.childNodes[ii].style.display = "inline";
				a.childNodes[ii].style.left = left.x + "px";
				a.childNodes[ii].style.top = left.y + "px";
//				alert (a.childNodes[ii].offsetLeft);
			}
		}
	}
}

function zoomout(id)
{
	var a = document.getElementById(id);
	if (a.hasChildNodes)
	{
		var i = a.childNodes.length;
		for (ii=0; ii<i; ii++)
		{
			if (a.childNodes[ii].tagName == 'SPAN')
			{
				a.childNodes[ii].style.display = "none";
			}
		}
	}
}

function updown (id, operation)
{
	var num2 = document.getElementById(id);
	var num = parseInt(num2.value);
	if (isNaN(num))
	{
		num = 0;
	}
	if (operation == 'up')
	{
		num++;
	}
	else if (operation == 'down')
	{
		num--;
	}
	
	num2.value = num;
}

/* eEgnith multilang library */
function ml(formid, name)
{
	for (var i=1; i<=5; i++)
	{
		
		var temp = name+'0'+i;
		var tempflag = 'flag'+name+'0'+i;
		
		var fid = document.getElementById(temp);
		var fidflag = document.getElementById(tempflag);
		if (fid != null)
		{
			if (temp == formid)
			{
				fid.style.display = 'block';
				fidflag.style.backgroundColor = '#ececec';
				fidflag.style.paddingTop = '2';
				fidflag.style.paddingBottom = '6';
				fidflag.style.marginTop = '0';
			}
			else
			{
				fid.style.display = 'none';
				fidflag.style.backgroundColor = 'white';
				fidflag.style.paddingTop = '2';
				fidflag.style.paddingBottom = '2';
				fidflag.style.marginTop = '4';
			}
		}
	}
	
}

var cache = new Array();
var flags = new Array();
var gloaballangid = "01";

function mlall(id)
{
	gloaballangid = id;
	var len = cache.length;
	var thisid = '';
	
	for (var i=1; i<=5; i++)
	{
		var temp = '0'+i;
		var fidflag = document.getElementById(flags[i]);
		if (fidflag)
		{
			if (temp == id)
			{
				fidflag.style.backgroundColor = '#eff5e5';
				fidflag.style.paddingTop = '2';
				fidflag.style.paddingBottom = '10';
				fidflag.style.marginTop = '0';
				if (fidflag.hasChildNodes())
				{
					fidflag.childNodes(0).style.opacity = "0";
					fidflag.childNodes(0).style.filter = "alpha(opacity=99)";
				}
			}
			else
			{
				fidflag.style.backgroundColor = 'white';
				fidflag.style.paddingTop = '2';
				fidflag.style.paddingBottom = '2';
				fidflag.style.marginTop = '8';
				if (fidflag.hasChildNodes())
				{
					fidflag.childNodes(0).style.opacity = "0.5";
					fidflag.childNodes(0).style.filter = "alpha(opacity=50)";
				}
			}
		}
	}
	
	
	for (var i = 1; i <= len; i++)
	{
		if (cache[i])
		{
			thisid = cache[i].substring(3,5);
			var fid = document.getElementById(cache[i]);
			if (thisid == id)
			{
				
				fid.style.display = 'block';
			}
			else
			{
				fid.style.display = 'none';
			}
		}
	}
}

function ee_js_replace_value (from, to)
{
	var fromvalue = document.getElementById(from);
	var tovalue = document.getElementById(to);
	tovalue.value = fromvalue.value;
}

function ee_js_set_display (id, display)
{
	var el = document.getElementById(id);
	el.style.display = display;
}

function oc(item)
{
	var id = document.getElementById(item);
	if (id.style.display == "block") 
	{
		id.style.display = "none";
	}
	else 
	{
		id.style.display = "block";
	}
}