// Basisdaten
var STR_class_wrapper = 'div.groupWrapper';
var STR_class_item = 'groupItem';
var STR_class_item_content = 'itemContent';
var STR_class_item_options = 'itemOptions';
var STR_class_handler = 'div.itemHeader';



// Sortable bei Seitenaufruf starten
$(document).ready(
	function () {startSortable();}
	
);



// Sortable starten
function startSortable()
{
	$(STR_class_wrapper).Sortable(
		{
			accept: 	 STR_class_item,
			helperclass: 'sortHelper',
			activeclass: 'sortableactive',
			hoverclass:  'sortablehover',
			handle: 	 STR_class_handler,
			tolerance: 	 'pointer',
			fit:		 false,
			opacity:	 0.8,
			onChange : function(ser)
					   {
							//serialize();
							updNGCOrder('sort');
					   },
			onStart : function()
					  {
							//$.iAutoscroller.start(this, document.getElementsByTagName('body'));
					  },
			onStop : function()
					 {
						//$.iAutoscroller.stop();
					 }
		}	
	);
	
} // end function 



// Sortierung serialisieren
function serialize(s)
{
	serial = $.SortSerialize(s);
	alert(serial.hash);
}



// Anzahl der Spalten anpassen
function changeAmountCols(INT_amount, STR_idstartswith)
{
	for (INT_col = 1; INT_col <= 3; INT_col ++)
	{
		var RES_col = document.getElementById(STR_idstartswith + INT_col);
		//alert(STR_idstartswith + INT_col);
		if (INT_amount == 1)
			RES_col.style.cssText = 'width: 98%';
		else if (INT_amount == 2)
			RES_col.style.cssText = 'width: 48.5%';
		else if (INT_amount == 3)
			RES_col.style.cssText = 'width: 32%';
		else
			RES_col.style.cssText = 'width: ' + (100/INT_amount-2) + '%';
			
		if (INT_col > INT_amount)
		{					
			// Boxen auslesen
			var ARR_boxes = RES_col.childNodes;
			
			// Boxen durchlaufen	
			for (INT_box = 0; INT_box < ARR_boxes.length; INT_box ++)
			{
				if(ARR_boxes[INT_box].id != '')
				{
				// Box umhaengen
				var RES_box_orig = document.getElementById(ARR_boxes[INT_box].id);

				if (RES_box_orig)
				{				
					// Box klonen
					var RES_box_clone = $('#' + ARR_boxes[INT_box].id).clone();
					
					// Alte Box entfernen
					RES_box_orig.id = ARR_boxes[INT_box].id + '_tmp';
					deleteBox(RES_box_orig.id, 1);
					
					// Box anhaengen
					RES_box_clone.appendTo('#' + STR_idstartswith + INT_amount).fadeIn('slow');
													
				} // end if
				}
				
			} // end for	
			
			// Spalte ausblenden
			$('#' + STR_idstartswith + INT_col).hide();		
		}
		else
		{
			// Spalte einblenden
			$('#' + STR_idstartswith + INT_col).show();
		}
		
	} // end for

	// save cols
		
	changeAmountColsImg(INT_amount, 'cols');
	updNGCOrder('sort');
	
	
	$(STR_class_wrapper).SortableDestroy();
	startSortable();
	
} // end function changeAmountCols



// Box entfernen
function deleteBox(STR_box_id,bNot)
{
	// Box ausblenden und anschliessend entfernen
	$('#' + STR_box_id).hide('fast', function() 
			{
				var RES_item = document.getElementById(STR_box_id);
				var RES_parent_item = RES_item.parentNode;	
				RES_parent_item.removeChild(RES_item);

				//db del
				var INT_nc = 0;

				if(typeof INT_glob_ngc != "undefined")
					INT_nc = INT_glob_ngc;

				if(!bNot)
				{
					$.get("/_ajax/mysite/myprofile/account/_nc_del.cfm", { title: STR_box_id, nc: INT_nc} );	 
				}
			}
		);
	
} // end function deleteBox



// Inhalt einer Box verstecken
function foldBox(STR_box_id)
{
	// Inhalt ausblenden
	$('#' + STR_box_id + ' > .' + STR_class_item_content).slideUp('fast');
	$('#' + STR_box_id + ' a.minimize').hide();
	$('#' + STR_box_id + ' a.maximize').show();
	
	$('#' + STR_box_id + ' > .' + STR_class_item_options).hide();
	$('#' + STR_box_id + ' a.optionsmin').show();
	$('#' + STR_box_id + ' a.optionsmax').hide();
	
	//db del
	var INT_nc = 0;

	if(typeof INT_glob_ngc != "undefined")
		INT_nc = INT_glob_ngc;
	
	$.get("/_ajax/mysite/myprofile/account/_nc_minimize.cfm", { title: STR_box_id, nc: INT_nc } );
	
} // end function foldBox


// Inhalt einer Box zeigen
function unfoldBox(STR_box_id)
{
	// Inhalt ausblenden
	$('#' + STR_box_id + ' > .' + STR_class_item_content).slideDown('fast');
	$('#' + STR_box_id + '  a.minimize').show();
	$('#' + STR_box_id + '  a.maximize').hide();
	
	//db del
	var INT_nc = 0;

	if(typeof INT_glob_ngc != "undefined")
		INT_nc = INT_glob_ngc;
	
	$.get("/_ajax/mysite/myprofile/account/_nc_maximize.cfm", { title: STR_box_id, nc: INT_nc } );
	
} // end function unfoldBox	
	
	
// Inhalt einer Box bearbeiten (verstecken)
function foldBox2(STR_box_id)
{
	// admin container einblenden 
	$('#' + STR_box_id + ' > .' + STR_class_item_options).slideDown('normal');
	$('#' + STR_box_id + ' a.optionsmin').hide();
	$('#' + STR_box_id + ' a.optionsmax').show();
	
} // end function foldBox2


// Inhalt einer Box bearbeiten (zeigen)
function unfoldBox2(STR_box_id)
{
	// admin container ausblenden
	$('#' + STR_box_id + ' > .' + STR_class_item_options).slideUp('normal');
	$('#' + STR_box_id + '  a.optionsmin').show();
	$('#' + STR_box_id + '  a.optionsmax').hide();
	
} // end function unfoldBox2

// layer ein und ausblenden
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
// layer eiin und ausblenden ende

function toggleDiv(id,flagit) 
{
	if (flagit=="1")
		{
	if (document.layers) document.layers[''+id+''].visibility = "show"
	else if (document.all) document.all[''+id+''].style.visibility = "visible"
	else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
		}
	else
	if (flagit=="0")
		{
	if (document.layers) document.layers[''+id+''].visibility = "hide"
	else if (document.all) document.all[''+id+''].style.visibility = "hidden"
	else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
		}
}




function updNGCOrder(STR_idstartswith)
{
	var INT_nc = 0;

	if(typeof INT_glob_ngc != "undefined")
		INT_nc = INT_glob_ngc;

	for (INT_col = 1; INT_col <= 3; INT_col ++)
	{
		var up_col = document.getElementById(STR_idstartswith + INT_col);
		
		
			// Boxen auslesen
			var ARR_boxes = up_col.childNodes;
			
			var INT_box_cnt = 0;

			// Boxen durchlaufen	
			for (INT_box = 0; INT_box < ARR_boxes.length; INT_box ++)
			{
				if(ARR_boxes[INT_box].className=='groupItem')
				{
					INT_box_cnt++;

					//alert(ARR_boxes[INT_box].id);

					$.get("/_ajax/mysite/myprofile/account/_nc_order.cfm", { column: INT_col, box: INT_box_cnt, title: ARR_boxes[INT_box].id, nc: INT_nc} );				
				}
			} // end for			

		
	} // end for
	

	
} // end function updOrder


// Anzahl der Spalten anpassen
function changeAmountColsImg(INT_amount, STR_idstartswith)
{	
	var INT_nc = 0;

	if(typeof INT_glob_ngc != "undefined")
		INT_nc = INT_glob_ngc;
	
		
	for (INT_col = 1; INT_col <= 3; INT_col ++)
	{
		
		
		if (INT_col == INT_amount)
		{
			$('#' + STR_idstartswith + 'max' + INT_col).hide();
			$('#' + STR_idstartswith + 'min' + INT_col).show();
			
			$.get("/_ajax/mysite/myprofile/account/_nc_save_col.cfm", { column: INT_col, nc: INT_nc} );
		}
		else
		{			
			
			$('#' + STR_idstartswith + 'max' + INT_col).show();
			$('#' + STR_idstartswith + 'min' + INT_col).hide();
		}
	}	

}


// Anzahl der Spalten anpassen
function addNewsGroup(INT_ngc, INT_ng, STR_id)
{
	
	$.get("/_ajax/mysite/myprofile/account/_ngc_add.cfm", { ngc: INT_ngc, ng: INT_ng} );
	document.getElementById(STR_id).innerHTML=unescape('hinzugef%FCgt');
	//$('#' + STR_id).innerHTML='hinzugefügt';
}	



// Optionen speichern
function save_options(STR_newsgrouptypes, STR_newsgroups, STR_div)
{	
	try
	{
	   // Form-Optionen
	   var ARR_options = 
			{  
				type: 'GET',
				url: '/_ajax/newsgroupcategories/newsgroups/_options.cfm?pk=' + STR_newsgrouptypes + '&fk=' + STR_newsgroups + '&op=update',
				beforeSubmit: function(responseText) 
							  {	 
								$("#optionsform_" + STR_newsgroups).fadeTo("normal", 0.5);
							  },
				success: function(responseText) 
						 { 
							$("#" + STR_div + " > .itemContent").remove();
							$("#" + STR_div).append(responseText);
							$("#" + STR_div + " > .itemContent").slideDown("normal");
							$("#optionsform_" + STR_newsgroups).fadeTo("normal", 1);
						 }
			} 
   
	   // Submit durchfuehren
	   $("#optionsform_" + STR_newsgroups).ajaxSubmit(ARR_options);
	   
	 }
	 catch (e)
	 {
		 $("#optionsform_" + STR_newsgroups).fadeTo("normal", 1);
	 }
  
}