$(document).ready(function() {
	//div_sort();
});
function div_sort() {
	$('a.closeEl').bind('click', toggleContent);
//	$("div[sort1=Sortable]").remove();
//	$("div[sort2=Sortable]").remove();
//	$("div[sort3=Sortable]").remove();
//	$("div[sort4=Sortable]").remove();
//	$("div[sort5=Sortable]").remove();
//	$("div[sort6=Sortable]").remove();
//	$("div[sort7=Sortable]").remove();
//	$("div[sort8=Sortable]").remove();
//	$('div.groupWrapper').unbind('Sortable');
	$('div.groupWrapper').Sortable({
			accept: 'groupItem',
			helperclass: 'sortHelper',
			activeclass : 	'sortableactive',
			hoverclass : 	'sortablehover',
			handle: 'div.itemHeader',
			tolerance: 'pointer',
			onChange : function(ser) {}, onStart : function() {$.iAutoscroller.start(this, document.getElementsByTagName('body'));},
			onStop : function()	{
				$.iAutoscroller.stop();
				if(this.parentNode.id=="sort1"|this.parentNode.id=="sort2"|this.parentNode.id=="sort3"|this.parentNode.id=="sort4") {
					var name1="down"+this.id;
					document.getElementById(name1).innerHTML="↓";
				} else {	
					hideDiv(this.id);								
				}
				
				change(this.id,this.parentNode.id);
			}
	});	
}
var toggleContent = function(e)
{
	var action="";
	var divid=this.parentNode.parentNode.id;
	var targetContent = $('div.itemContent', this.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		action='open';
		$(this).html('[-]');
	} else {
		targetContent.slideUp(300);
		action='close';
		$(this).html('[+]');
	}
	$.ajax({
	   type: "GET",
	   url: "ajax/change.php",
	   cache: true,
	   data: "action="+action+"&divid="+divid,
	   success: function(msg){
		   //alert(msg)
		   //$("#google_counter").html(msg);
	   }
	});
	
	return false;
};
function serialize(s)
{
	serial = $.SortSerialize(s);
	alert(serial.hash);
};
function getCount(name) {
	var j=0; var child;
	if(child = document.getElementById(name).childNodes)
	for(var i = 0; i < child.length; i++) {
		var t=child.item(i).id;
		if(t) {j++;}
	}	
	return j; 
}

function down(name) {
	var t=0;
	var parent=document.getElementById(name).parentNode.id;
	if(parent=='sort1'||parent=='sort2'||parent=='sort3'||parent=='sort4') {t=1;} else {t=2;}
	
	if(t==2) {
		var conf=confirm("ნამდვილად გინდათ ამ კატეგორიის ამოწევა?")
		if(conf) {
			showDiv1(name);
			var sortT=new Array('sort1','sort2','sort3','sort4');
			var sortCount=new Array();
			var minCount=getCount(sortT[0]); var curCount=0;
			for(var i=0; i<4; i++) {
				sortCount[i]=getCount(sortT[i]);
				if(minCount>sortCount[i]) {minCount=sortCount[i]; curCount=i;}
			}
			$('div#'+name).remove().appendTo('div#'+sortT[curCount]); 
			change(name,sortT[curCount]);
		}
	}
	
	if(t==1) {
		var conf=confirm("ნამდვილად გინდათ ამ კატეგორიის წაშლილებში გადატანა?")
		if(conf) {
			hideDiv1(name);
			var sortT=new Array('sort5','sort6','sort7','sort8');
			var sortCount=new Array();
			var minCount=getCount(sortT[0]); var curCount=0;
			for(var i=0; i<4; i++) {
				sortCount[i]=getCount(sortT[i]);
				if(minCount>sortCount[i]) {minCount=sortCount[i]; curCount=i;}
			}
			$('div#'+name).remove().appendTo('div#'+sortT[curCount]); 
			change(name,sortT[curCount]);
		}
	}
}

function showDiv1(name) {
	var targetContent = $('div.itemContent', document.getElementById(name));
	if (targetContent.css('display') == 'none') {
		targetContent.css('display','block');
		var name2="show"+name;
		document.getElementById(name2).innerHTML="[-]";
	}
	var name1="down"+name;
	document.getElementById(name1).innerHTML="↓";
}

function hideDiv1(name) {
	var targetContent = $('div.itemContent', document.getElementById(name));
	if (targetContent.css('display') != 'none') {
		targetContent.css('display','none');
		var name2="show"+name;
		document.getElementById(name2).innerHTML="[+]";
	}
	var name1="down"+name;
	document.getElementById(name1).innerHTML="↑";
}

function hideDiv(name) {
	var targetContent = $('div.itemContent', document.getElementById(name));
	if (targetContent.css('display') != 'none') {
		targetContent.slideUp(300);
		var name2="show"+name;
		document.getElementById(name2).innerHTML="[+]";
	}
	var name1="down"+name;
	document.getElementById(name1).innerHTML="↑";
}

function showDiv(name) {
	var targetContent = $('div.itemContent', document.getElementById(name));
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		var name2="show"+name;
		document.getElementById(name2).innerHTML="[-]";
	}
}

var deleted=1;
function hideDeleted() {
	if(deleted%2==0) {
		document.getElementById("deleted").style.visibility="hidden";
		document.getElementById("showhide").src="img/show0.jpg";	
	} else {
		document.getElementById("deleted").style.visibility="visible";
		document.getElementById("showhide").src="img/show1.jpg";	
	}
	deleted++;
}
function hideDeleted1() {
	if(deleted%2==0) {
		document.getElementById("deleted").style.display="none";
		document.getElementById("showhide").src="img/show2.jpg";	
	} else {
		document.getElementById("deleted").style.display="block";
		document.getElementById("showhide").src="img/show3.jpg";	
	}
	deleted++;
}

function cl(id) {
	$('#div'+id).css("display","none");
	$.ajax({
	   type: "GET",
	   url: "ajax/change.php",
	   cache: true,
	   data: "close="+id,
	   success: function(msg){
		   //alert(msg)
	   }
	});	
}
function change(name1,name2) {
	var childNodeArray = document.getElementById(name2).childNodes;
	
	var userName=new Array(); var j=0;
	var child = document.getElementById(name2).childNodes;
	for(var i = 0; i < child.length; i++) {
		var t=child.item(i).id;
		if(t) {userName[j] =t; j++;}
	}
	
	var text=""; var text1="";
	for(var i=0; i<userName.length; i++) {
		text+="<br>"+(i+1)+" - "+userName[i];
		text1+=userName[i]+"-";
	}
	
	var name2_id=1;
	if(name2=="sort1") name2_id=1; if(name2=="sort2") name2_id=2; if(name2=="sort3") name2_id=3; if(name2=="sort4") name2_id=4; if(name2=="sort5") name2_id=5; if(name2=="sort6") name2_id=6; if(name2=="sort7") name2_id=7; if(name2=="sort8") name2_id=8; 
	
	changeSort(1,name2_id,text1,name1);
}



function changeSort(userId,groupId,sortText,name1) { 
	$.ajax({
	   type: "GET",
	   url: "ajax/change.php",
	   cache: true,
	   data: "user_id="+userId+"&group_id="+groupId+"&new="+sortText+"&name="+name1,
	   success: function(msg){
		   //alert(msg)
		   //div_sort();
	   }
	});
}

function box(cat,id,type) { 
	var name="#kt"+cat;
	if(type=="small"||type=="big"||type=="right") {
		name="#kt"+cat;
	} else {
		name="#fixed_kt"+cat;
	}	
	//if(type!='small_fixed')
	$(name).html('loading...');
	$.ajax({
	   type: "GET",
	   url: "ajax/box.php",
	   cache: true,
	   data: "cat="+cat+"&id="+id+"&type="+type,
	   success: function(msg){
		   $(name).html(msg);
	   }
	});
	
}

function addStyle() {
	var title = prompt("შეიყვანეთ დასახელება", "");
	if(title!=""&&title!=null) {
		$.ajax({
		   type: "GET",
		   url: "ajax/change_style.php",
		   cache: true,
		   data: "title="+title+"&action=add",
		   success: function(msg){
			   if(parseInt(msg)=="0") location.reload(true); else alert("დაემატება ვერ მოხერხდა.");
		   }
		});
	}
}
function saveStyle() {
	var id=$('#style').val();
	if(id!=0) {
		$.ajax({
		   type: "GET",
		   url: "ajax/change_style.php",
		   cache: true,
		   data: "id="+id+"&action=save",
		   success: function(msg){
			   if(parseInt(msg)=="0") alert("შეინახა წარმატებით!"); else alert("შენახვა ვერ მოხერხდა.");
		   }
		});
	}
}
function deleteStyle() {
	var id=$('#style').val();
	var conf=confirm('ნამდვილად გინდათ ამ სტილის წაშლა?');
	if(conf) {
		$.ajax({
		   type: "GET",
		   url: "ajax/change_style.php",
		   cache: true,
		   data: "id="+id+"&action=delete",
		   success: function(msg){
			   if(parseInt(msg)=="0") location.reload(true); else alert("წაშლა ვერ მოხერხდა.");
		   }
		});
	}
}
