document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
}; 

function RemoveBad(strTemp) { 
    strTemp = strTemp.replace(/\+|\//g,""); 
    return strTemp;

} 



function checknamei(name) {

  pat = 'tmp/'+name+'.png';


if(!document.getElementById(pat)) { return(name); }


return checknamei(name+' ');

}



function checkname(name) {

if(document.getElementById(name)) return false;
else return true;

}

function setcolor(co) {
document.textform.color.value=co;
document.getElementById('selectcolor').style.background=co;
}


function setsize(s) {
document.textform.font_size.value=s.options[s.selectedIndex].value;
}

function setfont(f) {
document.textform.font.value=f.options[f.selectedIndex].value;
}


function saveScrollPositions() { 
if(document.pointform) { 
var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop; 
var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft; 
} 

window.scrollTo(scrollx, scrolly); 

} 



function remove_drag(id) {

	window.addEvent('domready', function(){
			$$('#draggables div').each(function(drag){
             	drag.addEvent('load', function(){this.remove(); });		
				drag.addEvent('emptydrop', function(){
				//	this.setStyle('background-color', '#faec8f');
    if(this.id==id) this.remove();     
                   
				});
			});		
					
		}); 

}


function load_drag() {
	window.addEvent('domready', function(){
			var fx = [];
			
			$$('#draggables div').each(function(drag){

				new Drag.Move(drag, {
				//	droppables: $$('#droppables div')
				});
				
				drag.addEvent('emptydrop', function(){
				//	this.setStyle('background-color', '#faec8f');
    this.focus();      
    //document.pointform.img_x.value = this.offsetTop;
    //document.pointform.img_y.value = this.offsetLeft;
                    
				});
			});		
					
	}); 
}

			

function editElement(obj) {
  
  var ni = document.getElementById('draggables');
  var isjpg = obj.id+'isjpg';
  var cname =obj.id+'c';
  //var cname= escape(c);
  var dname=obj.id+'d';
  var ext ='';

if(document.getElementById(isjpg)) ext='.jpg'; 
else ext='.gif';

if(checkname(cname+ext)) {

if(il = document.itemform.list.length) { il2 = il + 1;}
else { il2=2; }


addElement2(cname,cname+ext);

  var newdiv = document.createElement('div');
  newdiv.setAttribute('class','drag');
  newdiv.setAttribute('className','drag');
  newdiv.setAttribute('id',cname+ext);
  newdiv.setAttribute('style','z-index:'+il2);
  newdiv.innerHTML = '<img class="it" src="'+cname+ext+'" onmousedown="point(\''+ cname+ext +'\',\''+ cname +'\' );" title="'+cname+'">';

  ni.appendChild(newdiv);
load_drag();
//load_bin();
document.getElementById(cname+ext).style.zIndex=il2;

point(cname+ext,cname);
setpointer(cname+ext);


}
}



function addElement2(c,e) {


  var ni = document.getElementById('itemlist');
  var newdiv = document.createElement('div');
  var divIdName = 'my'+c;
  newdiv.setAttribute('class','list');
  newdiv.setAttribute('className', 'list');
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = '<input type="hidden" value="'+c+'" name="list"> '+unescape(c)+'  <img src="images/btn_close.gif" style="cursor:pointer;" title="Delete" onclick=\'removeElement("'+divIdName+'","'+e+'")\'> <img src="images/btn_up.gif" style="cursor:pointer;" title="Bring to Front" onclick=\'toFront("'+e+'","front")\'> <img src="images/btn_down.gif" style="cursor:pointer;" title="Send to Back" onclick=\'toFront("'+e+'","back")\'>';

  ni.appendChild(newdiv);
	
}


function toFront(fid,dr) {

var cindex = eval(document.getElementById(fid).style.zIndex);
if(dr=='front') var nindex = cindex + 1 ;
if(dr=='back')  var nindex = cindex - 1 ;

var x = document.images;
for (var i=0;i<x.length;i++)
{
	var id = x[i].id; 

if(x[i].className=='it' && x[i].parentNode.style.zIndex==nindex) {

x[i].parentNode.style.zIndex = cindex;


document.getElementById(fid).style.zIndex = nindex; 

}
} 

saveScrollPositions();
}



function removeElement(divNum,im) {

var dindex=document.getElementById(im).style.zIndex;
var aindex = document.itemform.list.length;

for(var i=dindex;i<aindex;i++){
toFront(im,'front');
}

  var d = document.getElementById('itemlist');  

  var olddiv = document.getElementById(divNum);
 
  d.removeChild(olddiv);


 var e = document.getElementById('draggables');
  var oldimg = document.getElementById(im); 
  e.removeChild(oldimg);

}



	window.addEvent('domready', function(){
			var accordion = new Accordion('h3.atStart', 'div.atStart', {
				opacity: false,
				onActive: function(toggler, element){
					toggler.setStyle('color', '#ff3300');   
  
				},
				
				onBackground: function(toggler, element){
					toggler.setStyle('color', '#222');
				}
			}, $('accordion'));
			
	
		}); 



	window.addEvent('domready', function(){
			var drop = $('draggables');

			var dropFx = drop.effect('background-color', {wait: false});  
			
			$$('.item').each(function(item){
			
				item.addEvent('mousedown', function(e) {
					e = new Event(e).stop();
			
					var clone = this.clone()
						.setStyles(this.getCoordinates()) 
						.setStyles({'opacity': 0.7, 'position': 'absolute'})
						.addEvent('emptydrop', function() {
							this.remove();
							drop.removeEvents();
						}).inject(document.body);
			
					drop.addEvents({
						'drop': function() {
							drop.removeEvents();
							clone.remove();
				//	item.clone().inject(drop);
							dropFx.start('7389AE').chain(dropFx.start.pass('B7E1FF', dropFx));
       if(item.id=='gtext') { edittext(); }
       else { editElement(item);}

    
						},
						'over': function() {
							dropFx.start('98B5C1');
						},
						'leave': function() {
							dropFx.start('B7E1FF');
						}
					});
			
					var drag = clone.makeDraggable({
						droppables: [drop]
					}); // this returns the dragged element
			
					drag.start(e); // start the event manual
				});
			
			});
		}); 









function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent || obj.offsetParent.id=='draggables')
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent || obj.offsetParent.id=='draggables')
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function moveup(id,mov){

   
   // up = document.pointform.img_y.value;
    obj = document.getElementById(id);
    up = obj.offsetTop;

     postup= eval(up) - eval(mov);
     obj=document.getElementById(id);
     obj.style.top=postup +'px';
    document.pointform.img_y.value = postup;
}

function movedown(id,mov){

     //down = parseFloat(document.pointform.img_y.value);
    obj = document.getElementById(id);
    down = obj.offsetTop;

     posdown= eval(down) + eval(mov);
     obj=document.getElementById(id);
     obj.style.top=posdown +'px';

    document.pointform.img_y.value = posdown;
}


function moveleft(id,mov){

  //  xpos = document.pointform.img_x.value;
    obj = document.getElementById(id);
    xpos = obj.offsetLeft;


     posleft= eval(xpos)-eval(mov);
     obj=document.getElementById(id);
     obj.style.left=posleft +'px';

    document.pointform.img_x.value = posleft;
}

function moveright(id,mov){
     //xpos = parseFloat(document.pointform.img_x.value);
    obj = document.getElementById(id);
    xpos = obj.offsetLeft;

     posright= eval(xpos) + eval(mov);
     obj=document.getElementById(id);
     obj.style.left=posright + 'px';

    document.pointform.img_x.value = posright;

}


function setpointer(id){  
      document.pointform.pointer.value = id;      
}



function point(id,h){

 

document.pointform.pointer.value = id;
ig = document.getElementById(id);

    document.pointform.img_x.value = findPosX(ig);
    document.pointform.img_y.value = findPosY(ig);


hh="my"+h;


var x = document.getElementsByClassName('list');

for (var i=0;i<x.length;i++)
{
x[i].style.background="";
 }




document.getElementById(hh).style.background ="#f9ca59";

}

function setpoint(id){
     

ig = document.getElementById(id);

ig.style.top='0px';
ig.style.left='0px';


}


function checkBoxValidate(cb) {
for (j = 0; j < document.itemform.list.length; j++) {
if (eval("document.itemform.list[" + j + "].checked") == true) {
document.itemform.list[j].checked = false;
if (j == cb) {
document.itemform.list[j].checked = true;
         }
      }
   }
}


function clearbox() {
for (j = 0; j < document.itemform.list.length; j++) {
if (eval("document.itemform.list[" + j + "].checked") == true) {
document.itemform.list[j].checked = false;
      }
   }
}


function checkKey(e){


var Key = 0;
var pointer = document.pointform.pointer.value;

if(pointer!='leave'){
if(window.event) // IE
  {
  Key = e.keyCode;

  }
else if(e.which) // Netscape/Firefox/Opera
  {
  Key = e.which;
  }




 mov = document.pointform.speed.value;
    

    if (Key != 0){
   switch(Key){
     case 97: moveleft(pointer,mov);break;
     case 100:moveright(pointer,mov);break;
     case 119:moveup(pointer,mov);break;
     case 115:movedown(pointer,mov);break;
   } 


}


} 

}
 


function initial(type){


var x = document.getElementsByTagName('div');

if(type=='caricature') var link = 'merge-caricature.php?';
else var link = 'merge.php?';


var arr = new Array();
var j=0;

for (var i=0;i<x.length;i++)
{
	var id = x[i].id;
 



if(x[i].className=='drag' ) {


 g = document.getElementById(id);
cc = g.style.zIndex;


 xx = findPosX(g);
 yy = findPosY(g);
 

link2 ='img'+cc+'='+id+'&x'+cc+'='+xx+'&y'+cc+'='+yy+'&';

arr[j] = [link2,i];
j++;


}

}



Array.prototype.multiSort = function(index){
for(var i=0; i<this.length; i++){
var temp = this[i].splice(index,1);
this[i].unshift(temp);
} return this.sort();
}

arr.multiSort(1); 


for(var y=0; y<arr.length; y++){

link +=arr[y][1];

}



link +='img50=watermark.gif&x50=172&y50=307&';
link +='length='+x.length;
link +='&type='+type;
document.write('');
location.href=(link);



}

function edittext() {


if(document.textform.gtext.value!=''){
  var test = ' '+document.textform.gtext.value+' ';
  var testing2 = escape(test);
  var testing  = RemoveBad(testing2);

  testing = checknamei(testing);
  pat = 'tmp/'+testing+'.png';

if(checkname(pat)) {

  var color = document.textform.color.value;
var size = document.textform.font_size.value;
var font = document.textform.font.value;



   var ni = document.getElementById("draggables");



if(il = document.itemform.list.length) { il2 = il + 1;}
else { il2=2; }

addElement2(testing,pat);
 var newdiv = document.createElement('div');
  newdiv.setAttribute('class','drag');
  newdiv.setAttribute('className','drag');
  newdiv.setAttribute('id',pat);
  newdiv.setAttribute('style','z-index:'+il2);
newdiv.innerHTML = '<img class="it" src="/test.php?text='+testing+'&size='+size+'&color='+color+'&font='+font+'" onclick="point(\''+ pat +'\',\''+ testing +'\' );" >';

  ni.appendChild(newdiv);
load_drag();

document.getElementById(pat).style.zIndex=il2;
point(pat,testing);
setpointer(pat);
document.textform.gtext.blur();



}


}
}
