var lastbg='';

function getHook(id){
	if (document.getElementById(id)) return document.getElementById(id);
	else return false;
}

function rowOverEffect(object){
	lastbg=object.className;
	if (object.className == 'users_table_row') object.className = 'usersTableRowOver';
}
function rowOutEffect(object){
	if (object.className == 'usersTableRowOver') object.className = 'users_table_row';
}

function toggle_close(object){
	if (object.className =='close') object.className = 'close_on';
	else object.className='close';
}

function rowSelectUnselect(object){
	if (object.className == 'dataTableRowOver') object.className = 'dataTableRowSelected';
	else if (object.className == 'dataTableRowSelected') object.className = 'dataTableRowOver';
}

function popUp(URL){
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, \'" + id + "\', \'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 340,top = 112\');");
}


function show(id){
 if(document.getElementById(id).style.display=='none')
 	document.getElementById(id).style.display='block';
 else document.getElementById(id).style.display='none';
}
function disp(id){
	document.getElementById(id).style.display='block';
}

function hide(id){
	document.getElementById(id).style.display='none';
}

function popup(mylink, windowname){
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, windowname, 'width=269px,height=354px,scrollbars=no,status=no');
	return false;
}

function profile(mylink, windowname){
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, windowname, 'width=600px,height=760px,scrollbars=no,status=no');
return false;
}

function reset(id){
	document.getElementById(id).innerHTML ='<img src="images/indicator.gif" alt=""/>';
	document.getElementById(id).style.display='none';
}

function check_name(){
	namestr=document.getElementById("username").value;
	if (namestr.length >1){
		create_overlay('actionfield');	
		x_data_check_username(namestr,do_check);
	}
	else document.getElementById("helper_div").innerHTML='';
}

function do_check(result){
   if (result==0) {
   	document.getElementById("helper_div").style.display='none';
   	document.form_adduser.submit();
   }
	else{
		document.getElementById("helper_div").innerHTML='<img style="border:none;margin:0;" src="images/exclamation.png" alt="">Username already taken, please choose a different one.';
		document.getElementById("helper_div").style.display='block';
		destroy_overlay();
	}      
}

function add_proj(id){
	create_overlay('addproj');	
	arr=0+';'+id+';'+document.getElementById('new_p_name').value+';'+document.getElementById('new_p_start').value+';'+document.getElementById('new_p_for').value;
	x_data_save_project(arr,show_me2);  
}

function save_proj(){
	create_overlay('project_info');	
	arr= document.getElementById("pid").value+';'+document.getElementById("box_id").value+';'+document.getElementById("pname").value+';'+document.getElementById("comm").value+';'+document.getElementById("t_for").value;
	x_data_save_project(arr,show_me2);
}


function show_browse(){
	document.getElementById('button_container').innerHTML='<input id=\"button_image\" type=\"hidden\" value=\"\"/>';
	document.getElementById('banner_container').innerHTML='<input id=\"banner_image\" type=\"hidden\" value=\"\"/>';
	document.getElementById('popup_container').innerHTML='<input id=\"popup_image\" type=\"hidden\" value=\"\"/>';
	e=document.getElementById('art_type').options[document.getElementById('art_type').selectedIndex].value;
	if (e=='a' ||e=='b'|| e=='c'){
		document.getElementById('r_btn').style.display='';
		hide('r_bnr');
		if(e=='b') {
			document.getElementById('r_pop').style.display='';
		}else {
			document.getElementById('r_link').style.display='';
			hide('r_pop');
		}
		if(e=='c')document.getElementById('r_prof_link').style.display='';
		else hide('r_prof_link');
	}else{
		hide('r_btn');
		document.getElementById('r_bnr').style.display='';
		if(e=='e') {
			document.getElementById('r_pop').style.display='';
		}else{
			hide('r_pop');
			document.getElementById('r_link').style.display='';
		}		
		if(e=='f')document.getElementById('r_prof_link').style.display='';
		else hide('r_prof_link');
		
	}
}	

function add_artwork(id,am,pid){
	create_overlay('actionfield');
	e=document.getElementById('art_type').options[document.getElementById('art_type').selectedIndex].value;
	link=document.getElementById('alink').value;
	proflink=document.getElementById('aprof_link').value;
	if(e=='a'||e=='b'||e=='c')image=document.getElementById('button_image').value;
	else image=document.getElementById('banner_image').value;
	image_width=document.getElementById('image_width').value;
	image_height=document.getElementById('image_height').value;
	popup=document.getElementById('popup_image').value;
	arr=pid+';'+e+';'+image+';'+image_width+';'+image_height+';'+link+';'+proflink+';'+popup+';'+document.getElementById('l_source').value+';'+id+';'+am;
 	x_data_save_artwork(arr,show_me2);		
}

function del_artwork(id){
	create_overlay('actionfield');
	x_data_del_artwork(id,show_me2);
}
function update_status(id,status){
	x_data_save_adm_proof(id+';'+status,show_me2);
}
function show(data){
	document.getElementById('actionfield').innerHTML=data;
}
function notify(pid){
	create_overlay('actionfield');
	x_data_send_notification(pid,show_me2);
}	
function fill(data){
	document.getElementById('l_source').value+=data;
}

function show_me2(date_server) {
	destroy_overlay();
	if(date_server !='OK'){document.getElementById('actionfield').innerHTML=date_server;}
	else{
		if(getHook('actionfield')) disp('actionfield');
		p=window.location.href.indexOf("&act");
		a=window.location.href.substr(0,p);
		if(a=='')window.location.reload( false ); 
		else window.location.href =window.location.href.substr(0,p);
	}
}

function change_status(obj){
	create_overlay('datatable');
	id=obj.id.substr(2,obj.length);
	a=obj.options[obj.selectedIndex].value;
	x_data_change_status(id+';'+a,show_me2);
}

function sticker_done(server_date){
	document.getElementById('message').innerHTML=server_date;
}

function transform(){
	a=document.getElementById('message').innerHTML;
	document.getElementById('message').innerHTML='<textarea id="mes" rows="6" cols="13">'+a+'</textarea><div style="width:30px;z-index:90;" onclick="savesticker();">Save</div>';
	document.getElementById('mes').focus();
}

var DragHandler = {

	// private property.
	_oElem : null,

	// public method. Attach drag handler to an element.
	attach : function(oElem) {
		oElem.onmousedown = DragHandler._dragBegin;
		// callbacks
		oElem.dragBegin = new Function();
		oElem.drag = new Function();
		oElem.dragEnd = new Function();

		return oElem;
	},

	// private method. Begin drag process.
	_dragBegin : function(e) {
		var oElem = DragHandler._oElem = this;

		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.dragBegin(oElem, x, y);

		document.onmousemove = DragHandler._drag;
		document.onmouseup = DragHandler._dragEnd;
		return false;
	},

	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';

		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.drag(oElem, x, y);

		return false;
	},

	// private method. Stop drag process.
	_dragEnd : function() {
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		oElem.dragEnd(oElem, x, y);

		document.onmousemove = null;
		document.onmouseup = null;
		DragHandler._oElem = null;
	}

}
function destroy_overlay(){
    var divOverlay=getHook('frame');
    divOverlay.parentNode.removeChild(divOverlay);
}

function create_overlay(divID)
{
    var divPopup=getHook(divID);
    var iFrame = document.createElement("IFRAME");
    iFrame.setAttribute("src", "modules/overlay.php");
	iFrame.setAttribute("frameborder","0");
	iFrame.setAttribute("id","frame");
    //Match IFrame position with divPopup
    iFrame.style.left=divPopup.offsetLeft + 'px';
	iFrame.style.top =divPopup.offsetTop + 'px';
	//Match Iframe size with divPopup
    iFrame.style.width =divPopup.offsetWidth + 'px';
    iFrame.style.height =divPopup.offsetHeight + 'px';
	iFrame.style.position ='absolute';
	
	container=divPopup.parentNode;
	container.appendChild(iFrame);
}