//Last changes: 18.03.2008 //Author: Pavel Pirogov

var pageX;	// object left-top corner position at mousedown moment
var pageY;
var pageX2; // mouse position on object
var pageY2;
var xmouseX;	// prevouse mouse position
var xmouseY; 
var mouseX;	// current mouse position
var mouseY; 
var down=false; // mousebutton are down
var move=false;	// moving object
var element;	// current object
var menu;		//content menu
var iframex;	// current iframe
var isIE=false;	// browser is IE
var URLs=new Array();	// URL's collection
var scale;	// scaling object
var typeE;	//type of current scaling object
var last; // last used object
var submiter; // submiter
var objects=new Array(); // array of objects
var maxIndex=1; //zIndex of last window
var offset=new Array(); // offset from page corner


function clickx(event) { // FOR Firefox show and hide // FOR IE hide
	if (event.target) targ = event.target;
	else if (event.srcElement) targ = event.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
   		targ = targ.parentNode;
   		
	if(event.button==2){ //mouse2
		if(targ.tagName=="HTML" || targ.tagName=="BODY"){
			locate_menu("menu");
		}
	}
	if(event.button==0){ //mouse1
		if(targ.tagName=="HTML" || targ.tagName=="BODY"){
			hidex_menu("menu");
		}
	}
}
function clickx2(event) { // FOR IE show
	if (event.target) targ = event.target;
	else if (event.srcElement) targ = event.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
   		targ = targ.parentNode;
   		
	if(targ.tagName=="HTML" || targ.tagName=="BODY"){
		locate_menu("menu");
	}
}

//content_menu events

function test_menu(){
	clear();
}

function testx(val){
	//echo(val);
}

//END content_menu events

var browserName=navigator.appName;


if (browserName=="Firefox"){ 
	document.captureEvents(Event.MOUSEMOVE);
}else { 
	if (browserName=="Microsoft Internet Explorer"){
  		isIE=true;
	}else{
    	
   	}
}
	document.onmousemove =
  	function (e) {
  	  	if(isIE){
	    	mouseX=event.clientX +document.documentElement.scrollLeft +document.body.scrollLeft;
	    	mouseY=event.clientY + document.documentElement.scrollTop +document.body.scrollTop;
	    }else{
	    	mouseX=e.pageX;
	    	mouseY=e.pageY;
	    }
	    // move Form
	    if(down && move){
	    	if(mouseX>=0 && mouseY>=0){
	    		//echo(".");
	    		element.style.left=mouseX-pageX2;
	    		element.style.top=mouseY-pageY2;
	    	}
	    }
	    //scale Form
	    if(down && scale){
	    	x=mouseX-widthE+pageX;
	    	y=mouseY-heightE+pageY;
	    	if(mouseX>=0 && mouseY>=0){
	    		
	    		if(typeE=="wysiwyg"){
	    			if(x>editorWidth){
		    			iframex.style.width=x;
		    		}
		    		if(y>editorHeight){
		    			iframex.style.height=y;
		    		}
	    		}else{
		    		if(x>350){
		    			iframex.width=x;
		    		}
		    		if(y>0){
		    			iframex.height=y;
		    		}
	    		}
	    	}
	    }
	    
	    return;
  	};
  	document.onmouseup = 
  	function (e) {
  	  	down=false;
  	  	move=false;
  	  	scale=false;
	    return;
  	};

function load(){
	clear();
	//echo("Session started.\n");
	objects = indexObjects();
}

function mdown(object)
{
	focusx(object);
	
	down=true;
	move=true;
	pageX=parseInt(element.style.left.split("px"));
	pageY=parseInt(element.style.top.split("px"));
	pageX2=mouseX-pageX;
	pageY2=mouseY-pageY;
	return;
}
function mdownx(object,event)
{
	focusx(object);
	
	down=true;
	move=true;
	//alert("fire = "+event.layerX+" Client = "+event.clientX+" IE = "+event.offsetX+" offset: x="+offset['x']+" y="+offset['y']);
	pageX=parseInt(element.style.left.split("px"));
	if(!pageX){
		offset['x']=(document.body.offsetWidth-900)/2-9;
		offset['y']=145;
  		//alert(window.innerHeight);
  		if(isIE){
			element.style.left=event.clientX-offset['x']-event.offsetX;//-event.offsetX;
			element.style.top=event.clientY-offset['y']-event.offsetY;//-event.offsetY;
		}else{
			element.style.left=event.clientX-event.layerX;//-event.offsetX;
			element.style.top=event.clientY-event.layerY;//-event.offsetY;
		}
	}else{
		pageY=parseInt(element.style.top.split("px"));
	}
	mdown(object);
	return;
}

function locate(object){
	//showy(object);
	element.style.left=mouseX;
	element.style.top=mouseY;
}

function locate_menu(object){
	menu=document.getElementById(object);
	if(object!=last){
		maxIndex=updateIndex();
		menu.style.zIndex=maxIndex;
	}
	last=object;
	//echo(maxIndex);
	menu.style.visibility="visible";
	menu.style.left=mouseX;
	menu.style.top=mouseY;
	
}

function scalex(object,typex)
{
	typeE=typex;
	element=document.getElementById(object);
	
	focusx(object);
	
	pageX=parseInt(element.style.left.split("px"));
	pageY=parseInt(element.style.top.split("px"));
	
	if(typeE=="wysiwyg"){
		typez='editor';
		//alert(typez);
		//alert(object);
		iframex=document.getElementById(typez+object+"Panel");
		widthE=mouseX-parseInt(iframex.style.width.split("px"))+pageX;
		heightE=mouseY-parseInt(iframex.style.height.split("px"))+pageY;
		//echo(heightE);
	}else{
		iframex=document.getElementById(object+"Panel");
		widthE=mouseX-iframex.width+pageX;
		heightE=mouseY-iframex.height+pageY;
	}
	down=true;
	scale=true;
}

function submitx(url){
	submiter=document.getElementById("submiter");
	submiter.src=url;
	//echo(submiter);
	return submiter;
}

function fullscreenx(object,typex){
	if(element.tag!=1){
		element=document.getElementById(object);
		typeE=typex;
		
		focusx(object);
		
		element.tag=1;
		element.x=parseInt(element.style.left.split("px"));
		element.y=parseInt(element.style.top.split("px"));
		
		element.style.left=0;
		element.style.top=0;
		
		if(typeE=="wysiwyg"){
			var typez="editor";
			iframex=document.getElementById(typez+object+"Panel");
			
			element.w=iframex.style.width;
			element.h=iframex.style.height;
			
			iframex.style.width=900;
			iframex.style.height=(screen.availHeight-230);
		}else{
			iframex=document.getElementById(object+"Panel");
			
			element.w=iframex.width;
			element.h=iframex.height;
			
			iframex.width=900;
			iframex.height=(screen.availHeight-230);
		}
	}
	else
	{
		element.tag=0;
		element.style.left=element.x;
		element.style.top=element.y;
		if(typeE=="wysiwyg"){
			var typez="editor";
			iframex=document.getElementById(typez+object+"Panel");
			
			iframex.style.width=element.w;
			iframex.style.height=element.h;
		}else{
			iframex=document.getElementById(object+"Panel");
			
			iframex.width=element.w;
			iframex.height=element.h;
		}
	}
}

function openx(object){
	focusx(object);
	element.style.visibility="visible";
}

function focusx(object){//XXX
	if(object!=last){
		//alert(2);
		element=document.getElementById(object);
		maxIndex=updateIndex();
		//alert(3);
		//alert(object);
		element.style.zIndex=maxIndex;
		//alert(4);
		if(document.getElementById("type"+object).className=="window"){
			//alert(5);
			document.getElementById(object+"left").style.background="url(images/window/editor_top_left_active.png)";
			document.getElementById(object+"top").style.background="url(images/window/editor_top_active.png)";
			document.getElementById(object+"right").style.background="url(images/window/editor_top_right_active.png)";
		}
	}
	last=object;
}

function editor_button_over(object,btnType,btnType2){
	d=object.getElementsByTagName("TD");
	for(var i=0;i<d.length;i++){
		try{
			if(d[i].className==btnType+"ButtonLeft")
				d[i].className=btnType2+"ButtonLeft";
			if(d[i].className==btnType+"Button")
				d[i].className=btnType2+"Button";
			if(d[i].className==btnType+"ButtonRight")
				d[i].className=btnType2+"ButtonRight";
		}catch(e){}
	}
}

function savex(object){
	URLs[URLs.length]=document.getElementById(object).src;
}
function backx(object){
	document.getElementById(object).src=URLs[(URLs.length-1)];
	URLs[(URLs.length-1)]=null;
}

function updateIndex(){
	//clear();
	d = document.getElementsByTagName('div');
	j=parseInt(d[0].style.zIndex)||0;
	x=parseInt(d[0].style.zIndex)||0;
	cnt=0;
	//alert("x="+x);
	for(i=0;i<d.length;i++){
		if(parseInt(d[i].style.zIndex)<x){
			x=parseInt(d[i].style.zIndex);
		}
		if(parseInt(d[i].style.zIndex)>j){
			j=parseInt(d[i].style.zIndex);
		}
		cnt++;
	}
	//alert("cnt="+cnt);
	//echo("(x="+x+";j="+j+")");
	
	if(j<cnt)j++;
	
	//alert("j="+j);
	
	for(i=0;i<d.length;i++){
		if(parseInt(d[i].style.zIndex) > 1){
			try{
				if(document.getElementById("type"+d[i].id).className=="window"){
					//alert(d[i].style.zIndex);
					d[i].style.zIndex=parseInt(d[i].style.zIndex)-1;
					//alert(d[i].style.zIndex);
					document.getElementById(d[i].id+"left").style.background="url(images/window/editor_top_left.png)";
					document.getElementById(d[i].id+"top").style.background="url(images/window/editor_top.png)";
					document.getElementById(d[i].id+"right").style.background="url(images/window/editor_top_right.png)";
				}
			}
			catch(err){
			}
			//alert("hola2");
		}
	}
	//alert("j=="+j);
	return j;
}

function indexObjects(){
	//clear();
	//echo("ok");
	d = document.getElementsByTagName('div');
	for(i=0;i<d.length;i++){
		//echo("1");
		objects[d[i].id]=new Array();
		objects[d[i].id][0]=0;
		d[i].tag=0;
		//echo(d[i].tag);
	}
	//echo(d[0].tag)
}

function closex(object){
	//clear();
	//echo(screen.availWidth+"/"+screen.availHeight);
	element=document.getElementById(object);
	element.style.visibility="hidden";
	return;
}

function hidex_menu(object){
	menu=document.getElementById(object);
	menu.style.visibility="hidden";
	return;
}

/*function echo(S){
	document.getElementById("consolePanel").value+=S;
}*/
function clear(){
	document.getElementById("consolePanel").value="";
}

function replaceDIV(id1,id2,hide){
	if(hide){
		hideGalleryActions();
		//alert(id1.split("_")[2]+" "+id2.split("_")[2]);
	}
	obj1=document.getElementById(id1);
	obj2=document.getElementById(id2);
	tmp=obj1.innerHTML;
	obj1.innerHTML=obj2.innerHTML;
	obj2.innerHTML=tmp;
}
function hideGalleryActions(){
	d = document.getElementsByTagName('span');
	for(i=0;i<d.length;i++){
		if(d[i].className=="actions"){
			if(d[i].style.visibility=='visible')
				d[i].style.visibility='hidden';
		}
	}
}
function cutGalleryImage(id){
	d = document.getElementsByTagName('span');
	for(i=0;i<d.length;i++){
		if(d[i].className=="cut"){
			d[i].style.visibility='hidden';
		}
	}
	d2 = document.getElementsByTagName('span');
	for(i=0;i<d2.length;i++){
		if(d2[i].className=="paste"){
			d2[i].style.visibility='visible';
		}
	}
	obj=document.getElementById(id);
	obj.style.visibility='hidden';
}
function replaceValues(id1,id2){
	obj1=document.getElementById(id1);
	obj2=document.getElementById(id2);
	tmp=obj1.value;
	obj1.value=obj2.value;
	obj2.value=tmp;
}
function replaceImage(loaded){
	document.getElementById('zoomImg').src=loaded.src;
}
function zoomImage(img,width){
	if(img != null){
		document.getElementById('zoomImg').src=img;
		document.getElementById('load_image').src=img+"&width="+width;
	}
}
function getImage(id){
	from=document.getElementById(id);
	d = from.getElementsByTagName('img');
	if(d.length>0)
		return d[0].src;
	else
		return null;
}
function moveGaleryImage(cut,cutl,cutn,paste,pastel,pasten){
	hideGalleryActions();
	d = document.getElementsByTagName('span');
	for(i=0;i<d.length;i++){
		if(d[i].className=="cut"){
			d[i].style.visibility='visible';
		}
	}
	d2 = document.getElementsByTagName('span');
	for(i=0;i<d2.length;i++){
		if(d2[i].className=="paste"){
			d2[i].style.visibility='hidden';
		}
	}
	id=document.getElementById('gallery_cut_id').value;
	document.getElementById("item_"+id+"_opacity").className="trans100";
	document.getElementById("buttons_"+id).style.visibility="visible";
	//alert("move "+cut+" to "+paste);
	
	
	tmpx=cut.split("_")[2];
	tmpx2=paste.split("_")[2];
	second=false;
	first=false;
	for(i=0;i<gallery.length;i++){
		//gallery[0];
		if(gallery[i]==tmpx && !second)
			first=true;
		if(gallery[i]==tmpx2 && !first)
			second=true;
	}
	if(first){
		tmpx3=cutn.split("_")[2];
		moveStep(tmpx,tmpx2,0);
	
	}else if(second){
		tmpx3=cutl.split("_")[2];
		moveStep(tmpx,tmpx2,1);
	}
	//alert(tmpx+" ? "+tmpx2);
}
function moveStep(cut,fin,index){
	first=-1;
	second=-1;
	for(i=0;i<gallery.length;i++){
		//alert(cut+" "+fin);
		if(gallery[i]==cut)
			first=i;
		if(gallery[i]==fin)
			second=i;
	}
	if(index==0){
		start=first;
		end=second;
		for(i=start;i<end;i++){
			//alert("gallery_item_"+gallery[i] +"_cell " + " gallery_item_"+gallery[i+1] +"_cell",false);
			try{
				replaceDIV("gallery_item_"+gallery[i] +"_cell","gallery_item_"+gallery[i+1] +"_cell",false);
				replaceValues("gallery_item_pos_"+gallery[i],"gallery_item_pos_"+gallery[i+1]);
			}catch(e){alert('error code:404');}
		}
	}else{
		start=second;
		end=first;
		//alert("start="+start+" end="+end);
		for(i=end;i>start;i--){
			//alert("gallery_item_"+gallery[i] +"_cell " + " gallery_item_"+gallery[i+1] +"_cell",false);
			try{
				replaceDIV("gallery_item_"+gallery[i] +"_cell","gallery_item_"+gallery[i-1] +"_cell",false);
				replaceValues("gallery_item_pos_"+gallery[i],"gallery_item_pos_"+gallery[i-1]);
			}catch(e){alert('error code:404');}
		}
	}
}