var cpArr = new Array(), offset_obj = null, cpExpiryDate = new Date(), cpHelp = null;
function initialiseControlPanel(id,y) {
	if (!document.getElementById(id)){
		return false;
	}
	if (cpArr.length == 0) {
		FixCookieDate(cpExpiryDate)
		cpExpiryDate.setFullYear(parseInt(cpExpiryDate.getFullYear()) + 1)
	}
	if (cpArr.length == 1) {
		cpHelp = new api_obj("cpHelp");
	}
	if (offset_obj == null) offset_obj = document.getElementById("mainPage")
		cpArr.push(new api_obj(id))
	var i = cpArr.length - 1
		cpArr[i].obj_dragDrop()
	var cpBody = cpArr[i].obj.getElementsByTagName("DD")
	var cpHide = document.getElementById(id + "hide")
	if (cpHide){
		cpHide.title = "Hide panel"
		cpHide.onclick = function() {
			cpArr[i].hidePanel()
		}
	}
	var bnHelp = document.getElementById(id + "help")
	if (bnHelp){		
		bnHelp.title = "Display Help for this Panel";
		bnHelp.onclick = function() {
			cpArr[i].csh = cpArr[i].csh ? false : true;
			cpArr[i].help();
		}		
	}
	var cpReset = document.getElementById(id + "reset")
	cpReset.title = "Reset panel position"
	cpReset.onclick = function() {
		cpArr[i].resetPanelPosition()
	}
	cpBody[0].onmouseover = function() {
		if (dd_obj == 0) cpArr[i].obj_noDragDrop()
	}
	cpBody[0].onmouseout = function() {
		if (dd_obj == 0) cpArr[i].obj_dragDrop()
	}
	cpArr[i].getPanelCookie = function() {
		this.cookie = GetCookie("cp" + this.obj.id)
		if (this.cookie != null) {
			this.absPos = true
			this.cookieArr = this.cookie.split("&")
			for (var j = 0; j < this.cookieArr.length; j++) this.cookieArr[j] = parseInt(this.cookieArr[j].slice(2,this.cookieArr[j].length))
		} else this.absPos = false
	}
	cpArr[i].setPanelPosition = function(reset) {
		this.getPanelCookie()
			// dock should check for collision?
		//this.dock = { x : (get_window_x() / 2) + 338, y : (i > 0) ? 7 + parseInt(cpArr[i - 1].obj.offsetHeight) + cpArr[i - 1].dock.y : y, w : this.obj_w, h : this.obj.offsetHeight }
		//this.dock = { x : 0, y : (i > 0) ? 0 + parseInt(cpArr[i - 1].obj.offsetHeight) + cpArr[i - 1].dock.y : y, w : this.obj_w, h : this.obj.offsetHeight }
		this.dock = { x : (get_window_x() / 2) + 338, y : (i > 0) ? 0 + parseInt(cpArr[i - 1].obj.offsetHeight) + cpArr[i - 1].dock.y : y, w : this.obj_w, h : this.obj.offsetHeight }
		if (this.cookie != null) {
			this.obj_zIndex(this.cookieArr[2])
			if (dd_z < this.cookieArr[2]) dd_z = this.cookieArr[2]
			this.obj_moveTo(this.cookieArr[0] + parseInt(offset_obj.offsetLeft),this.cookieArr[1])
		} else { 
			if (id == "controlPanelMarketPlace") this.dock = { x : (get_window_x() / 2) - 510, y : 145 }; //y : 105
			this.obj_moveTo(this.dock.x,this.dock.y)				
			/* // not quite right yet
			if (i > 0) {
				if ((reset && !cpArr[i - 1].absPos) || !reset) this.obj_moveTo(this.dock.x,this.dock.y)
			} else this.obj_moveTo(this.dock.x,this.dock.y)
			*/
		}
		this.cpY = this.obj_y
		this.obj_offsetLeft = this.obj_x - parseInt(offset_obj.offsetLeft)
	}
	cpArr[i].setPanelPosition()
	cpArr[i].resetPanelPosition = function() {
		DeleteCookie("cp" + this.obj.id,"/")
		this.setPanelPosition(true)
	}
	cpArr[i].show = function() {
		if ((bw.ie6 || bw.ie55) && bw.win) {
			this.obj.filters[0].apply()
			this.obj.filters[0].play()
		}
		this.obj_visibility("visible")
	}
	cpArr[i].hide = function() {			
		this.obj_visibility("hidden")
	}
	cpArr[i].hidePanel = function() {
		SetCookie("cp" + this.obj.id,false,null,"/")
		this.hide()
	}
	cpArr[i].help = function() {			
		var aItms = this.obj.getElementsByTagName("a");
		var cp = this;
		cp.cshPanel = false;	
		
		if (cp.csh){
			bnHelp.title = "Hide Help for this Panel";				
			
			for (var i=0; i<aItms.length; i++)
				if (aItms[i].id) {
					var itm = new api_obj(aItms[i].id)
					itm.css.cursor = "help";
					
					itm.obj.onmouseover = function(){						
						cpHelp.obj_moveTo(cp.dock.x -230, cp.dock.y);
						cpHelp.css.width = 200;
						cpHelp.obj_display("block");
						cpHelp.obj_write_ajax("/exec/help/cpHelp.aspx", "id=" + this.id);
					}					
					itm.obj.onmouseout = function(){						
						cpHelp.obj_display('none');						
					}					
				}			
		}
		else {				
			bnHelp.title = "Display Help for this Panel";
			
			for (var i=0; i<aItms.length; i++){
				aItms[i].style.cursor = "pointer";
				aItms[i].onmouseover = null;
				cpHelp.obj_display("none");
			}
		}
	}
	cpArr[i].show()
	cpArr[i].csh = false;
	return cpArr[i]
}

// over ride obj_dragDrop() mouseup event handler function
function obj_ddUp(e) {
	if (dd_obj) {
		dd_obj.obj_offsetLeft = dd_obj.obj_x - parseInt(offset_obj.offsetLeft)
		SetCookie("cp" + dd_obj.obj.id,"x=" + dd_obj.obj_offsetLeft + "&y=" + dd_obj.obj_y + "&z=" + dd_obj.obj_z,cpExpiryDate,"/")
		dd_obj.absPos = true
	}
	dd_obj = 0
}


function Vote(obj)
{
	//cast vote using ajax
	var rad = document.fencPoll.pollOption;		
	for (var i=0; i < rad.length; i++)
		if (rad[i].checked)
			Resource.Vote(document.fencPoll.poll.value, rad[i].value, Ajax_Callback);			
}

function Ajax_Callback(res)
{
	if (res != null)
	{
		if ((res.value != null) && (typeof(res.value) == "object"))
		{
			var obj = res.value;
			
			if (obj.Message != "")
				alert(obj.Message);			
			
			if (obj.Reload)
				document.location.reload();			
		}
		else
			alert(res.error);		
	}
}

window.onresize = function() {
	for (i = 0; i < cpArr.length; i++) cpArr[i].obj_moveTo(parseInt(offset_obj.offsetLeft) + cpArr[i].obj_offsetLeft,cpArr[i].obj_y)
}

function newOptions(obj) {	
	var objValue = obj.value;
	obj.selectedIndex = 0;
	if (objValue != "0")
		eval(objValue);
}

/*
	// move panel if it's covered by another?
	if (i > 0) {
		if (cpArr[i - 1].absPos && !cpArr[i].absPos) {
			if ((((cpArr[i - 1].obj_x >= cpArr[i].dock.x) && (cpArr[i - 1].obj_x <= (cpArr[i].dock.x + cpArr[i].dock.w))) || ((cpArr[i - 1].obj_x < cpArr[i].dock.x) && ((cpArr[i - 1].obj_x + cpArr[i - 1].obj_w) > cpArr[i].dock.x))) && (((cpArr[i - 1].obj_y >= cpArr[i].dock.y) && (cpArr[i - 1].obj_y <= (cpArr[i].dock.y + cpArr[i].dock.h))) || ((cpArr[i - 1].obj_y < cpArr[i].dock.y) && ((cpArr[i - 1].obj_y + cpArr[i - 1].obj_h) > cpArr[i].dock.y)))) {
				// is partially covered by previous panel
			}
		}
	}
*/
