//really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 600;
defaultHeight = 400;

//kinda important
var AutoClose = true;

function popImage(imageURL,imageTitle){
	var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
	if( !imgWin ) { return true; } //popup blockers should not cause errors
	imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
		'function resizeWinTo() {\n'+
		'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
		'var oH = document.images[0].height, oW = document.images[0].width;\n'+
		'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
		'window.doneAlready = true;\n'+ //for Safari and Opera
		'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
		'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
		'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
		'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
		'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
		'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
		'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
		'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
		'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
		'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
		'}\n'+
		'<\/script>'+
		'<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
		(document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
		'<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
		(document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
	imgWin.document.close();
	if( imgWin.focus ) { imgWin.focus(); }
	return false;
}


function ShowPicture(url,new_opacity)
{
	if (screen.availWidth) 
	{
	    var body = document.getElementsByTagName("body")[0];	var div = document.createElement("div");
	    var closemark = document.createElement("div");	var image = document.createElement("img");
	    opacity = new_opacity== undefined?0.85:new_opacity;    div.style.position = "absolute";
	    var scW = screen.availWidth ? screen.availWidth : screen.width;	var scH = screen.availHeight ? screen.availHeight : screen.height;
	    div.style.margin = "-50px 0 0 -170px";	div.style.border = "3px double #999999";	div.style.padding = "20px";	div.style.opacity = opacity;	div.style.filter = "Alpha(Opacity=" + opacity*100 + ")"
	    div.style.backgroundColor = "#CCCCCC";	div.style.fontSize = "10px";	div.style.lineHeight = "10px";	div.style.textAlign = "right";
	    //make it dragable
	    div.style.cursor = 'move';	var dragable1 = DragHandler.attach(div);	closemark.innerHTML='close X'
	    closemark.style.border = "3px #999999";	closemark.style.width = "40px";
	    closemark.style.backgroundColor = "#CCCCFF";	closemark.style.margin = "0 0 0 0";
	    closemark.style.cursor = 'hand';	closemark.style.cursor = 'pointer';
	    closemark.style.align = 'right';	image.src=url	
	    body.appendChild(div);
	    div.appendChild(closemark);
	    div.appendChild(image);
	    div.style.top = (scH/2)-(image.height/2); //e.clientY;
	    //alert(scH - josselin test back - n'eo ket aes);
	    div.style.left = (scW/2)-(image.width/2); // e.clientX;
	    image.focus();
	    image.onblur = function(e)
	    {
		    opacityDown(this.parentNode);
	    }
	    closemark.onclick = function(e)
	    {
		    opacityDown(this.parentNode);
	    }
	 }
	 else
	 {
	   popImage(url,'image')
	 }
};

opacity = 0.85
function opacityDown(theElement)
{
	if (opacity < 0.08)
	{
		theElement.parentNode.removeChild(theElement);
        opacity = 0.85
	}
	else
	{
		opacity -= 0.07;
	    //alert(scH - josselin test back - n'eo ket aes);
		theElement.style.filter = "Alpha(Opacity=" + opacity*100 + ")"; //for IE            
		theElement.style.opacity = opacity; //for FF        
		setTimeout(function(){opacityDown(theElement);}, 75);
	}
	return true;
};

function ShowPopUpFrame(url,new_opacity)
{	
	if (screen.availWidth) 
	{
	    var body = document.getElementsByTagName("body")[0];
	    var div = document.createElement("div");
	    var closemark = document.createElement("div");
	    var iframe = document.createElement("iframe");
	    opacity = new_opacity== undefined?0.85:new_opacity;

        div.style.position = "absolute";
	    //alert(scH - josselin test back - n'eo ket aes);
	    var scW = screen.availWidth ? screen.availWidth : screen.width;
	    var scH = screen.availHeight ? screen.availHeight : screen.height;
	    div.style.margin = "-50px 0 0 -170px";
	    div.style.border = "3px double #999999";
	    div.style.padding = "20px";
	    div.style.opacity = opacity;
	    div.style.filter = "Alpha(Opacity=" + opacity*100 + ")"
	    div.style.backgroundColor = "#CCCCCC";
	    div.style.fontSize = "10px";
	    div.style.lineHeight = "10px";
	    div.style.textAlign = "right";
	    div.style.align = 'right';
	    //make it dragable
	    div.style.cursor = 'move';
	    var dragable1 = DragHandler.attach(div);
    	
	    closemark.innerHTML='close X'
	    closemark.style.border = "3px #999999";
	    closemark.style.width = "40px";
	    closemark.style.backgroundColor = "#CCCCFF";
	    closemark.style.margin = "0 0 0 0";
	    closemark.style.cursor = 'hand';
	    closemark.style.cursor = 'pointer';
	    closemark.style.align = 'right';
    	
	    iframe.src=url;
	    iframe.style.width = "750";
	    iframe.style.height = "550";    	
	    body.appendChild(div);
	    div.appendChild(closemark);
	    div.appendChild(iframe);
	    div.style.top = 200;//(scH/2)-(iframe.height/2); //e.clientY;
	    div.style.left = 200; //(scW/2)-(iframe.width/2); // e.clientX;
	    iframe.focus();
	    //alert(scH - josselin test back - n'eo ket aes);
	    iframe.onkeypress = function(e)
	    {
	    }
	    closemark.onclick = function(e)
	    {
		    opacityDown(this.parentNode);
	    }
	  }
	  else
	  {
	     window.open(url,'', 'resizable=yes,scrollbars=yes,status=no,toolbar=no');
	  }
	   
};

function ShowPopUpWindow(url)
{	
	     win = window.open(url,'', 'resizable=yes,scrollbars=yes,status=no,toolbar=no');
}


/**
*
*  Drag Drop
*
**/

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;
	}

}
