cX = new Array();
cY = new Array();

Array.prototype.in_array = function(needle) {
	for(var i=0; i < this.length; i++) if(this[ i] === needle) return true;
	return false;
}

function encodeHEX(n) {
	window.location="\x6D\x61\x69"+"\x6C\x74\x6F\x3A"+n+"\x40"+"\x73\x65\x62\x61\x73\x74\x69\x61\x6e\x73\x69\x65\x62\x65\x72"+'\x2E'+'\x63\x68';
}

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return (new Array(myWidth,myHeight));
}

function relocate(w,h,id) {
	win = new Array();
	win = windowSize();	
	x = Math.round(Math.random()*(win[0]-w-200));
	y = Math.round(Math.random()*(win[1]-h-100));
	count = 0;
	while (cX.in_array(Math.round(x/80))&&count<20) {
		x = Math.round(Math.random()*(win[0]-w-200));
		count++;
	}
	count = 0;
	while (cY.in_array(Math.round(y/80))&&count<20) {
		y = Math.round(Math.random()*(win[1]-h-100));
		count++;
	}	
	cX.push(Math.round(x/80));	
	cY.push(Math.round(y/80));	
	$(id).setStyle("left", x+"px");
	$(id).setStyle("top", y+"px");
}


var lastE;
var lastZ;
var dt = 0;

function swapDepth (id) {
	if (lastE) $(lastE).setStyle("z-index", lastZ);	
	lastE = id;
	lastZ = $(id).getStyle("z-index");	
	$(id).setStyle("z-index", 1000);	
	$(id).pop = 1; 
	$(id).makeDraggable({
		onStart: function(){
			$(id).pop = 0;
	    },
	    onComplete: function(){
			$(id).pop = 0;
	    }
	});
}

function loadPop (url,cap,xw,yw,id,thmb,dot) {
	if ($(thmb).pop==0) {
		$(thmb).pop = 1;
		return;
	}
	if (dot==1) {
		var myHTMLRequest = new Request.HTML().get('/dot/'+id);
	}
	win = new Array();
	win = windowSize();	
	x = (win[0]/2)-(xw/2);
	y = (win[1]/2)-(yw/2);
	$("pop").setStyle("left", x+"px");
	$("pop").setStyle("top", y+"px");
	$("pop").set('html',"<div class='popImg' onclick='closePop();' id='popContainer'><img src='"+url+"' width='"+xw+"' height='"+yw+"'  id='popImg'><p class='caption' id='cap'></p></div>");
	
  	$("cap").set('html',cap); 
	$("pop").setStyle("visibility", "visible");	
	$("popBack").setStyle("visibility", "visible");	
	if (dot==1) {
		var dot_el  = new Element('div', {'id': 'dot_'+dt, 'class': 'palette_marked', 'html': '<img src="/img/reddot.png">'});
		$(dot_el).inject($(thmb));
		
		$('dot_'+dt).setStyle("left", Math.round($('dot_'+dt).getPosition($(thmb)).x+(Math.random()*20)-10)+"px");
		$('dot_'+dt).setStyle("top", Math.round($('dot_'+dt).getPosition($(thmb)).y+(Math.random()*20)-10)+"px");
		dt++;
	}
}

function closePop() {
	$("pop").setStyle("visibility", "hidden");	
	$("pop").set("html","");
	$("popBack").setStyle("visibility", "hidden");	
}




