
var panels=Array();
var slot = Array();
var ver = '1.0';
var domain="media-aude.pl";

var conf_array=Array(Array('mainContentLeft'),Array('mainContentRight'));
function initM(){
	for (var panel_res_id in conf_array) {
		panel_id = conf_array[panel_res_id][0];
		panels[panel_res_id] = Array();
		panels[panel_res_id].box = Array();
		panels[panel_res_id].slots = Array();
		panels[panel_res_id].node = document.getElementById(panel_id);
		slot[panel_res_id] = Array();
		
		bi = 0;
		i = -1;
		while ((i = getNextSlot(i, panel_res_id)) !== false) {
			box = getFirstNodeWithClass(panels[panel_res_id].node.childNodes[i], 'TabbedPanelsAllBorder');
			box.id = 'box_' + panel_res_id + '_' + bi;
			panels[panel_res_id].box[bi] = Array();
			panels[panel_res_id].box[bi].id = box.id;
			panels[panel_res_id].box[bi].links = getLinks(box);
			panels[panel_res_id].box[bi].links.up.onclick = new Function('click2up(' + panel_res_id + ',\'' + bi + '\');return false;');
			panels[panel_res_id].box[bi].links.down.onclick = new Function('click2down(' + panel_res_id + ',\'' + bi + '\');return false;');
			panels[panel_res_id].slots[bi] = panels[panel_res_id].node.childNodes[i];
			slot[panel_res_id][bi] = bi;
			bi++;
		}
	}
	if (document.cookie) 
		if (readCookie('ver') == ver)
 			slot = eval(readCookie('slots'));
	setOrder();
	
}

function getNextSlot(index,panel_res_id){
	if (panels[panel_res_id].node.childNodes.length<=index+1)
		return false;
	for (i = index + 1; i < panels[panel_res_id].node.childNodes.length; i++) 
		if (panels[panel_res_id].node.childNodes[i].tagName == 'DIV' && panels[panel_res_id].node.childNodes[i].className == 'Slot') {
			return i;
		}
	return false;
}
function getLinks(slot_node){
	pall_nodegL = getFirstNodeWithClass(getFirstNodeWithClass(getFirstNodeWithClass(slot_node,'TabbedPanelsTitle00'),'TabbedPanelsTitle01'),'positionAll');
	magL = Array();
	magL.up = getFirstATag(getFirstNodeWithClass(pall_nodegL,'positionUp'));
	magL.down = getFirstATag(getFirstNodeWithClass(pall_nodegL,'positionDown'));
	return magL;
	
}
function swap(panel_res_id,si_up,si_down){
	tmpSI=slot[panel_res_id][si_up];
	slot[panel_res_id][si_up]=slot[panel_res_id][si_down];
	slot[panel_res_id][si_down]=tmpSI;
	setOrder();
}
function setOrder(){
	for (var panel_res_id in slot) {
		for (var slot_id in slot[panel_res_id]) {
			box_id=slot[panel_res_id][slot_id];
			box_name=box_name='box_'+panel_res_id+'_'+box_id;
			box=document.getElementById(box_name);
			panels[panel_res_id].slots[slot_id].appendChild(box);

			if (slot_id != 0) {
				panels[panel_res_id].box[box_id].links.up.style.display="block";

			}else{
				panels[panel_res_id].box[box_id].links.up.style.display="none";
			}
			if (slot_id != (slot[panel_res_id].length-1)){
				panels[panel_res_id].box[box_id].links.down.style.display="block";
			}else{
				panels[panel_res_id].box[box_id].links.down.style.display="none";
			}
		}
	}
	setCookie();
} 
function click2up(panel_res_id, box_index){
	si_down=getBoxSlot(panel_res_id,box_index);
	si_up=(si_down*1-1);
	if (si_down > 0 && si_down < slot[panel_res_id].length){
		swap(panel_res_id,si_up, si_down);
	}
}
function click2down(panel_res_id,box_index){
	si_up=getBoxSlot(panel_res_id,box_index);
	si_down=(si_up*1+1);
	if (si_up >= 0 && si_up < slot[panel_res_id].length-1){
		swap(panel_res_id,si_up, si_down);
	}
}

function getFirstNodeWithClass(parent_node, class_name){
	for (igFNWC = 0; igFNWC < parent_node.childNodes.length; igFNWC++) 
		if (parent_node.childNodes[igFNWC].className == class_name) 
			return parent_node.childNodes[igFNWC];
	return false;	
}	
function getFirstATag(parent_node){
	for (igFAT = 0; igFAT < parent_node.childNodes.length; igFAT++) 
		if (parent_node.childNodes[igFAT].tagName == "A") 
			return parent_node.childNodes[igFAT];
	return false;	
}	
function getBoxSlot(panel_res_id,box_index){
	for (igBS=0;igBS<slot[panel_res_id].length;igBS++)
		if (slot[panel_res_id][igBS]==box_index)
			return igBS;
	return false;
}
function setCookie(){
	s='['
	for (var panel_res_id in slot) {
		s += '[';
		for (var slot_id in slot[panel_res_id]) {
			s+=slot[panel_res_id][slot_id];
			if (slot_id<(slot[panel_res_id].length-1))
				s+=',';
		}
		s += ']';
		if (panel_res_id<(slot.length-1))
			s+=','
	}
	s+=']';
	var exp=new Date();
	exp.setTime(exp.getTime()+1000*60*60*24*365);
	document.cookie="slots="+escape(s)+"; domain=."+domain+"; expires="+ exp.toGMTString();
	document.cookie="ver="+escape(ver)+"; domain=."+domain+"; expires="+ exp.toGMTString();
}
function readCookie(name){
	name+="=";
	startCookie=document.cookie.indexOf(name);
	if (startCookie==-1) 
		return "";
	startCookie+=name.length;
	if (document.cookie.indexOf(";",startCookie)==-1){
		endCookie=document.cookie.length;
	}else{
		endCookie=document.cookie.indexOf(";",startCookie);
	}

	textCookie=document.cookie.substring(startCookie,endCookie);
	textCookie=unescape(textCookie);
	return textCookie;
}

