// Dressup script by Yohanes Aristianto (ariscool.com@gmail.com)
// Licensed for use at http://ariscool.com
// Please do not modify/host at another website















/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 * http://www.dynamicdrive.com/dynamicindex11/domdrag/index.htm
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};












// Current Page Reference
// copyright Stephen Chapman, 1st Jan 2005
// you may copy this function but please keep the copyright notice with it
function getURL(uri) {
uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
uri.file = uri.page;
if (uri.ext != '') uri.file += '.' + uri.ext;
if (uri.file == '') uri.page = 'index';
uri.args = location.search.substr(1).split("?");
return uri;
}








function set_html(id, cont) {
  if(document.getElementById) {
    document.getElementById(id).innerHTML = cont;
  }
  else {
    if(document.layers) document.id.innerHTML = cont; else document.all.id.innerHTML = cont;
  }
}

function set_el(id, dstyle) {
  if(document.getElementById) {
    if(document.getElementById(id)!=null) document.getElementById(id).style.display = dstyle;
  }
  else {
    if(document.layers) document.id.style.display = dstyle; else document.all.id.style.display = dstyle;
  }
}




var uri = new Object();
getURL(uri);


//No hotlinking please
if(uri.dom.toLowerCase()!='localhost' && uri.dom.toLowerCase()!='ariscool.com') alert('This script is hosted at http://ariscool.com');





// Dressup script by Yohanes Aristianto (ariscool.com@gmail.com)
// Licensed for use at http://ariscool.com
// Please do not modify/host at another website


var i,j,k;
var magic_line=0;
var h_imglist='';

var elapsed_loading_img=0;
var load_timeout = 15000;

var coor_x = new Array();
var coor_y = new Array();




var pas=true;
function animPA() {
  if(elapsed_loading_img<load_timeout) {
    if(pas) {
      set_html('loadingbox', '<div style="padding:8px;background-color:#FFC;color:#12A;"><b>Loading images.</b> Please wait...</div>');
      
      setTimeout("animPA()",800);
    }
    else {
      set_html('loadingbox', '<div style="padding:8px;background-color:#FFC;">&nbsp;</div>');
      
      setTimeout("animPA()",300);
    }
    pas=!pas;
  }
  else {
    set_el('loadingbox','none');
  }
}


function arrangeExistingImages() {
  if(uri.args[0].length<2) return;
  
  var myname = 'anonymous person';
  
  var qs = uri.args[0];
  var qst = qs.split('&');
  var tok;
  var imgi;
  
  for(var i=0; i<qst.length; i++) {
    if(qst[i].length<3) continue;
    
    tok = qst[i].split('=');
    if(tok.length!=2) continue;
    
    if(tok[0].toLowerCase()=='n') {
      myname=decodeURIComponent(tok[1]);
    }
    else if(tok[0].substr(0,1).toLowerCase()=='x') {
      imgi = parseInt(tok[0].substr(1));
      if(imgi<0) continue;
      
      coor_x[imgi] = parseInt(tok[1]);
      document.getElementById("mimg_"+imgi).style.left = coor_x[imgi] + 'px';
    }
    else if(tok[0].substr(0,1).toLowerCase()=='y') {
      imgi = parseInt(tok[0].substr(1));
      if(imgi<0) continue;
      
      coor_y[imgi] = parseInt(tok[1]);
      document.getElementById("mimg_"+imgi).style.top = coor_y[imgi] + 'px';
    }
  }
  
  set_html('hellobox', '<div style="padding-top:4px;padding-bottom:6px;background-color:#FFD;border-top:1px dashed #F66;border-bottom:1px dashed #F66;"><b>' + myname + '</b> &nbsp; &nbsp; &nbsp; <span style="background-color:#FF3;color:#F00;">&raquo;&raquo;</span> <span style="background-color:#FF9;color:#F00;">create your own:</span> <a href="#" style="color:#00F;" onclick="resetScene(); return false;">RESET THIS SCENE</a> <span style="background-color:#FF3;color:#F00;">&laquo;&laquo;</span></div>');
  set_el('scenetips','');
}


function launchArrangeNotUsedImages() {
  for(var i=0;i<img_list.length;i++) {
    if(img_list[i]!='END') {
      
      if(document.getElementById("oimg"+i).complete==false) {
        if(elapsed_loading_img<load_timeout) {
          elapsed_loading_img = elapsed_loading_img + 500;
          setTimeout("launchArrangeNotUsedImages()",500);
        }
        else {
          arrangeNotUsedImages();
        }
        
        return;
      }
      
    }
  }
  
  arrangeNotUsedImages();
}

function arrangeNotUsedImages() {
  var tmp;
  var def_left, def_top;
  var templ=0, temp=0, temptmax=0;
  var img_width, img_height;
  
  
  elapsed_loading_img = load_timeout+1;
  
  
  templ = area_width+20;
  tempt = 25;
  
  for(i=0;i<img_list.length;i++) {
    if(img_list[i]!='END') {
      tmp = document.getElementById("mimg_"+i);
      
      
      //Drag.init(tmp, null, 24, 400, 140, 515);
      Drag.init(tmp);
      
      tmp.onDragEnd = function(x, y) {
        var tmp2 = this.id.split('_');
        coor_x[tmp2[1]] = x;
        coor_y[tmp2[1]] = y;
        
        set_el('linkcode1','none');
        set_el('linknote','none');
      }
      
      
      if(coor_x[i]!=null && coor_y[i]!=null) continue;
      
      
      img_width = document.getElementById("oimg"+i).width;
      img_height = document.getElementById("oimg"+i).height;
      
      if(img_width==0) img_width=32;
      if(img_height==0) img_height=32;
      
      if(img_width>100 || img_height>100) {
        def_left = magic_line;
        def_top = 25;
      }
      else {
        if(temptmax==0) temptmax = 25+img_height;
        if(tempt+img_height>temptmax) temptmax=tempt+img_height;
        
        if(templ==0) templ=magic_line;
        if(tempt==0) tempt=25;
        
        if(templ+img_width>area_width+410) {
          templ = magic_line;
          tempt = temptmax+4;
        }
        
        def_left = templ;
        def_top = tempt;
        
        templ = templ+img_width+4;
      }
      
      
      tmp.style.left =  def_left + 'px';
      tmp.style.top = def_top + 'px';
      
      coor_x[i] = def_left;
      coor_y[i] = def_top;
    }
  }
}


function resetScene() {
  var tmp;
  var def_left, def_top;
  var templ=0, temp=0, temptmax=0;
  var img_width, img_height;
  
  templ = area_width+20;
  tempt = 25;
  
  for(i=0;i<img_list.length;i++) {
    if(img_list[i]!='END') {
      tmp = document.getElementById("mimg_"+i);
      
      
      img_width = document.getElementById("oimg"+i).width;
      img_height = document.getElementById("oimg"+i).height;
      
      if(img_width==0) img_width=32;
      if(img_height==0) img_height=32;
      
      if(img_width>100 || img_height>100) {
        def_left = magic_line;
        def_top = 25;
      }
      else {
        if(temptmax==0) temptmax = 25+img_height;
        if(tempt+img_height>temptmax) temptmax=tempt+img_height;
        
        if(templ==0) templ=magic_line;
        if(tempt==0) tempt=25;
        
        if(templ+img_width>area_width+410) {
          templ = magic_line;
          tempt = temptmax+4;
        }
        
        def_left = templ;
        def_top = tempt;
        
        templ = templ+img_width+4;
      }
      
      
      tmp.style.left =  def_left + 'px';
      tmp.style.top = def_top + 'px';
      
      coor_x[i] = def_left;
      coor_y[i] = def_top;
    }
  }
}

function initImages() {
  var tzorder, turl;
  
  
  if(magic_line>0) return;
  
  set_el('doloadimages','none');
  
  
  area_width = parseInt(area_width);
  area_height = parseInt(area_height);
  
  if(area_width<=0) area_width=375;
  if(area_height<=0) area_height=375;
  
  magic_line = area_width + 20;
  
  document.getElementById("dragnote").style.left = magic_line + 'px';
  document.getElementById("sharelink").style.top = (area_height+160) + 'px';
  
  document.getElementById("myarea").style.width = area_width + 'px';
  document.getElementById("myarea").style.height = area_height + 'px';
  
  
  background_url = background_url.replace(/^\s*|\s*$/g,"");
  if(background_url!='') document.getElementById("myarea").style.backgroundImage = "url('" + background_url + "')";
  
  
  document.gf.li_text.value = default_link_text;
  
  for(i=0;i<img_list.length;i++) {
    if(img_list[i]!='END') {
      tzorder = img_list[i].substr(0,img_list[i].indexOf('!'));
      turl = img_list[i].substr(img_list[i].indexOf('!')+1);
      
      tzorder = parseInt(tzorder.replace(/^\s*|\s*$/g,""));
      turl = turl.replace(/^\s*|\s*$/g,"");
      
      if(isNaN(tzorder)) tzorder=1;
      
      h_imglist = h_imglist + '<div id="mimg_' + i + '" style="position:absolute; z-index: ' + tzorder +'"><img id="oimg' + i + '" src="' + turl + '"></div>';
    }
  }
  
  
  set_html('myarea', h_imglist);
  
  arrangeExistingImages();
  
  animPA();
  
  if(document.getElementById("oimg0").complete==false || document.getElementById("oimg0").complete==true) {
    launchArrangeNotUsedImages();
  }
  else {
    setTimeout("arrangeNotUsedImages()",5000);
  }
}


function generateCode() {
  var html_code='';
  var imgcode='';
  var img_width;
  
  var myname = document.gf.li_name.value;
	myname = myname.replace(/^\s*|\s*$/g,"");
	if(myname.length<2) {
	  alert("Your name must be at least 2 characters long.");
	  document.gf.li_name.focus();
	  return false;
  }
  else {
    myname = myname.replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;');
  }
  
  var mylinktext = document.gf.li_text.value;
	mylinktext = mylinktext.replace(/^\s*|\s*$/g,"");
	if(mylinktext.length<1) {
	  mylinktext = 'My Dressup Game';
  }
  else {
    mylinktext = mylinktext.replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;');
  }
  
  
  for(i=0;i<img_list.length;i++) {
    if(img_list[i]!='END') {
      img_width = document.getElementById("oimg"+i).width;
      
      if(coor_x[i]+img_width<=magic_line+15) {
        if(imgcode!='') imgcode = imgcode + '&';
        imgcode = imgcode + 'x' + i + '=' + coor_x[i] + '&y' + i + '=' + coor_y[i];
      }
    }
  }
  
  if(uri.page.toLowerCase()=='index')
    html_code = 'http://'+uri.dom+'/'+uri.path+'?';
   else
    html_code = 'http://'+uri.dom+'/'+uri.path+uri.page+'?';
  
  html_code = html_code + imgcode;
  html_code = html_code + '&n=' + encodeURIComponent(myname);
  
  
  html_code = '<a href="' + html_code + '" target="_blank">' + mylinktext + '</a>';
  
  
  document.gf.linkcode1.value = html_code;
  set_html('linkpreview',html_code);
  
  
  set_el('linkcode1','');
  set_el('linknote','');
}