//Added by Alex
path_fix=window.path_fix||'/js';

//Added by Alex
my_addEvent=function(event,el,func){
	if (el.addEventListener) {
		el.addEventListener (event,func,false);
	} else if (el.attachEvent) {
		el.attachEvent ("on"+event,func);
	} else {
		eval("el.on"+event) = func;
	} 
};

/***
 *	@version 	2.1.5
 *	@date		2007/11/05
 *	@author 	Tsvetomir Velichkov
 **/

var pageLoaded 			= false;
//var helper				= new helper();
//Changed by Alex
var helper;
my_addEvent('load',window,function(){helper=new helper();});
//Disabled by Alex - not found to use here, but we have function with same name.
//var popup				= '';
var dimmer				= null;
var popupZIndex 		= 900;
var dimmerZIndex 		= 899;
var movePopupWithMouse 	= false;
var currentPopup		= null;
var currentPopupVar		= null;
var attachedPopup		= null;
var attachedX			= 0;
var attachedY			= 0;
var debugPopup			= false;
var onloadStack			= new Array();
var onscrollStack		= new Array();
var bOpera=false; var bNS=false; var bIE=false; var bIE6=false; var bIE7=false;

//Detect the browser
if (window.opera) {
	bOpera 	= true;
}else if ((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) { //IE 7
	bIE 	= true;
	bIE7 	= true;
}else if ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) { //IE 6
	bIE	 	= true;
	bIE6 	= true;
}else {
	bNS 	= true;
}
//alert("NS:"+bNS+"\nOpera: "+bOpera+"\nIE6: "+bIE6+"\nIE7: "+bIE7);

//For proper usage the popup requires onmousemove, onload, onmouseup and onscroll handlers
//window.onload = function (e) {
//Changed by Alex
window_onload = function (e) {
	pageLoaded = true;

	//Init a new dimmer
	dimmer = new dimmer_init();

	//Parses the onload stack
	if (typeof onloadStack != 'undefined' && onloadStack.length > 0) {
		for(i=0;i<onloadStack.length;i++) {
			eval(onloadStack[i]+".display()");
		}
	}

	//Created and set here by Alex
	my_addEvent('scroll',window,window_onscroll);
	my_addEvent('mousemove',document,document_onmousemove);
	my_addEvent('mouseup',document,document_onmouseup);
}
my_addEvent('load',window,window_onload);

//window.onscroll = function (e) {
//Changed by Alex
window_onscroll = function (e) {
	if (!e) e = window.event;
	helper.refresh();

	//Reposition fixed popups
	if (bIE) {
		for (i=0; i<onscrollStack.length; i++) {
			eval(onscrollStack[i]+".rePositionFixed([helper.windowScrollX, helper.windowScrollY])");
		}
	}
}
//my_addEvent('scroll',window,window_onscroll);

//document.onmousemove = function (e) {
//Changed by Alex
document_onmousemove = function (e) {
	if (!e) e = window.event;

	helper.refresh();

	if (e.pageX || e.pageY) 	{
		helper.updateMouse(e.pageX, e.pageY, (e.pageX - helper.windowScrollX), (e.pageY - helper.windowScrollY));
	}else if (e.clientX || e.clientY) 	{
		helper.updateMouse((e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft), (e.clientY + document.body.scrollTop + document.documentElement.scrollTop), e.clientX, e.clientY);
	}

	if (currentPopup != null && movePopupWithMouse == true) {
		eval(currentPopupVar+'.absolute('+(helper.mouseX - (parseInt($(currentPopup).clientWidth / 2)))+','+(helper.mouseY-10)+')');
	}

	//Move mouse attached popup
	if (attachedPopup != null) {
		//Adjust proper position
		adjustY = 0;
		adjustX = 0;
		popupHeight = parseInt(eval(attachedPopup+'.height'));
		popupWidth = parseInt(eval(attachedPopup+'.width'));
		//Added by Alex
		//var _position_fix=document.all?5:0;
		var _position_fix=document.all?8:6;
		var space_not_enough_x=false,space_not_enough_y=false;

		//Abjust horizontal position
		if ((helper.mouseX2 + popupWidth + attachedX) > parseInt(helper.windowWidth)) {
			//adjustX = helper.mouseX2 + popupWidth + attachedX - parseInt(helper.windowWidth);
			//Changed by Alex
			adjustX=popupWidth + attachedX+_position_fix;
			//if(helper.mouseX2<popupWidth + attachedX){
			if(helper.mouseX2<popupWidth + attachedX){
				adjustX=helper.mouseX2+attachedX;
				space_not_enough_x=true;
			}
		}

		//Adjust vertical position
		if ((helper.mouseY2 + popupHeight + attachedY) > parseInt(helper.windowHeight)) {
			//adjustY = helper.mouseY2 + popupHeight + attachedY - parseInt(helper.windowHeight);
			//Changed by Alex
			adjustY=popupHeight + attachedY+_position_fix;
			if(helper.mouseY2<popupHeight + attachedY){
				adjustY=helper.mouseY2+attachedY;
				space_not_enough_y=true;
			}
		}
		if(space_not_enough_x&&space_not_enough_y){adjustX=0;adjustY=0;}

		eval(attachedPopup+'.absolute('+(helper.mouseX+attachedX-adjustX)+','+(helper.mouseY+attachedY-adjustY)+')')
	}

	//Show debug popup information
	if (debugPopup) {
		eval(debugPopup+'.refreshDebug()');
	}
}
//my_addEvent('mousemove',document,document_onmousemove);

///document.onmouseup = function (e) {
//Changed by Alex
document_onmouseup = function (e) {
	//Stop popup drag
	if (currentPopup != null && movePopupWithMouse == true) {
		$(currentPopup).style.zIndex = eval(currentPopupVar+'.table.style.zIndex = '+currentPopupVar+'.oldZIndex');
		currentPopup = null;
		movePopupWithMouse = false;
	}
}
//my_addEvent('mouseup',document,document_onmouseup);

/**
 * JS Popup Class
 **/

function popup_init(instanceName, attributes) {
	if (typeof attributes == 'undefined') {
		attributes = new Object();
	}
	//Set attributes
	this.keyId				= rand_str(4);
	this.popupId			= 'popup_'+this.keyId;
	this.instanceName 		= instanceName;

	//Define basic methods
	this.initHTML 			= popup_init_basic_html;
	this.setTitle			= popup_set_title;
	this.setText			= popup_set_text;
	this.display			= popup_show;
	this.hide				= popup_hide;
	this.hideDelayed		= popup_hide_delayed;
	this.position			= popup_position;
	this.absolute			= popup_position_absolute;
	this.fixed				= popup_position_fixed;
	this.rePositionFixed 	= popup_position_reposition_fixed;
	this.draggable			= popup_set_draggable;
	this.fadeOut			= popup_fade_out;
	this.fadeIn				= popup_fade_in;
	this.mouseAttach		= popop_attach_to_mouse;
	this.mouseDetach 		= popup_detach;
	this.debug				= popup_debug;
	this.refreshDebug 		= popup_debug_refresh;
	this.setStyle			= popup_set_style;
	this.moveUnder			= popup_place_under;
	this.reCenter			= popup_recenter;
	this.minimize			= popup_minimize;
	this.maximize			= popup_maximize;
	this.restore			= popup_maximize;
	this.minimizeRestore 	= popup_minimize_restore;
	this.log			 	= popup_write_2_log;

	//Define properties
	this.attributes			= attributes;
	this.attached			= false;
	this.visible			= false;
	this.allowDrag			= false;
	this.allowMinimize		= (attributes['allowMinimize']||false);
	this.closeBtn			= (attributes['closeBtn']||false);
	this.posLeft			= 0;
	this.posTop				= 0;
	this.width				= 0;
	this.height				= 0;
	this.opacity			= 100;
	this.minimized			= false;
	this.imagePath			= (attributes['imagesPath'] || imagesPath || path_fix+'/jspopup/images/');
	this.logDiv				= false;

	//Check if prototype is loaded...
	if (typeof Prototype.Version == 'undefined') {
		alert("Prototype not loaded! Script operation canceled.");
		return false;
	}

	if (attributes['logTo']) this.logDiv = attributes['logTo'];
	this.log("Created a new instance. KeyId: "+this.keyId, attributes);
	this.initHTML();

	if (typeof attributes == 'object') {
		if (attributes['title']) this.setTitle(attributes['title']);
		if (attributes['text']) this.setText(attributes['text']);
		if (attributes['draggable']) this.draggable(attributes['draggable']);
		if (attributes['position']) this.position(attributes['position']);
		if (attributes['absolute']) this.absolute(attributes['absolute']);
		if (attributes['fixed']) this.fixed(attributes['fixed']);
		if (attributes['style']) this.setStyle(attributes['style']);
	}
	//Init a new iframe object
	if (document.all && !window.opera) this.iframe 	= new iframe_init();
}

function popup_init_basic_html() {
	if (typeof webPath == 'undefined') {
		webPath		= path_fix+'/jspopup/';
	}

	this.table = document.createElement('table');
	this.table.style.background 	= 'transparent';
	this.table.style.borderCollapse = 'collapse';
	this.table.style.visibility 	= 'hidden';
	this.table.style.position		= 'absolute';
	this.zIndex						= popupZIndex;
	this.table.style.zIndex			= popupZIndex; popupZIndex++;
	this.tbody 						= document.createElement('tbody'); //Create tbody element for compatibility with IE
	this.table.id 					= this.popupId;
	this.table.varName 				= this.instanceName;

	//We need 3x3 table grid
	for (i=0;i<3;i++) {
		var tr = document.createElement('tr');
		//Create row columns
		for (j=0;j<3;j++) {
			var td = document.createElement('td');
			if (i==0 && j==0) { //top left
				td.style.width 			= '18px';
				td.style.height 		= '18px';
				td.style.background 	= 'url('+this.imagePath+'top_left.png)';
			}else if (i==0 && j==1) { //top center
				td.style.background 	= 'url('+this.imagePath+'top.png)';
				td.id					= 'drag_'+this.keyId;
				td.style.MozUserFocus 	= 'ignore';
				td.style.MozUserInput 	= 'disabled';
				td.style.MozUserSelect 	= 'none';
				td.style.fontSize		= '10px';
				td.style.fontFamily		= 'Verdana';
				td.innerHTML			= "<div style=\"position: relative\"><div style=\"position: absolute; left: 0; top: 3px;color:white\" id=\"title_"+this.keyId+"\"> </div></div>";
				td.onmousedown 			= popup_start_drag;
				td.unselectable			= true;
			}else if (i==0 && j==2) { //top right
				td.style.width 			= '18px';
				td.style.height 		= '18px';
				td.style.verticalAlign  = 'top';
				td.style.background 	= 'url('+this.imagePath+'top_right.png)';
				if (this.closeBtn)
				//td.innerHTML 			= "<div style=\"position: relative\"><div style=\"position: absolute; left: -7px; top: 10px; width: 20px\"><a href='javascript:;' onclick='"+this.instanceName+".hide({hideDimmer:true})' style='position:relative'><img src='"+this.imagePath+"close.png' alt='' border='0' /></div></div>";
				td.innerHTML 			= "<div style=\"position: relative\"><div style=\"position: absolute; right: 12px; top: 12px; width: 40px; font-size:1px; line-height: 1px; text-align:right\">"+
				(this.allowMinimize ? "<a href='javascript:;' onclick='"+this.instanceName+".minimizeRestore()' style='position:relative'><img src='"+this.imagePath+"minimize.gif' id='minico_"+this.keyId+"' alt='' border='0' />&nbsp;&nbsp;" : null)+
				"<a href='javascript:;' onclick='"+this.instanceName+".hide({hideDimmer:true})' style='position:relative'><img src='"+this.imagePath+"x.gif' alt='' border='0' /></div></div>";

			}else if (i==1 && j==0) { //center left
				td.style.width 			= '18px';
				td.style.background 	= 'url('+this.imagePath+'left.png)';
				td.id 					= 'left_'+this.keyId;
			}else if (i==1 && j==1) { //center middle
				td.style.background 	= 'url('+this.imagePath+'bg.png)';
				td.style.padding		= '6px 0 6px 0';
				td.style.display		= 'block';
				td.id 					= 'holder_'+this.keyId;
				td.className			= 'popupContainer';
				tr.id					= 'midrow_'+this.keyId;
			}else if (i==1 && j==2) { //center right
				td.style.width 			= '18px';
				td.style.background 	= 'url('+this.imagePath+'right.png)';
				td.id 					= 'right_'+this.keyId;
			}else if (i==2 && j==0) { //bottom left
				td.style.width 			= '18px';
				td.style.height 		= '18px';
				td.style.background 	= 'url('+this.imagePath+'bottom_left.png)';
			}else if (i==2 && j==1) { //bottom center
				td.style.background 	= 'url('+this.imagePath+'bottom.png)';
			}else if (i==2 && j==2) { //bottom right
				td.style.width 			= '18px';
				td.style.height 		= '18px';
				td.style.background 	= 'url('+this.imagePath+'bottom_right.png)';
			}

			if (bIE6) td.style.behavior	= 'url('+webPath+'js/iepngfix.htc)';

			tr.appendChild(td);
		}
		this.tbody.appendChild(tr);
	}

	this.table.appendChild(this.tbody);
	document.body.appendChild(this.table);

	this.log("Basic HTML initialized. Images Path: "+this.imagePath);
}

function popup_minimize() {
	this.minimized = true;
	$('midrow_'+this.keyId).style.display = 'none';
	$('minico_'+this.keyId).src = this.imagePath + 'restore.gif';
	$('drag_'+this.keyId).style.width = (parseInt(this.width)-42)+'px';

	if (typeof this.iframe == 'object') this.iframe.resizeTo(this.width, $('popup_'+this.keyId).clientHeight);

	this.log("State changed to: minimized");
}

function popup_maximize() {
	this.minimized = false;
	$('minico_'+this.keyId).src = this.imagePath + 'minimize.gif';
	if (!bIE) $('midrow_'+this.keyId).style.display = 'table-row';
	else $('midrow_'+this.keyId).style.display = 'block';

	if (typeof this.iframe == 'object') this.iframe.resizeTo(this.width, $('popup_'+this.keyId).clientHeight+20);

	this.log("State changed to: restored");
}

function popup_minimize_restore() {
	if (this.minimized) this.maximize();
	else this.minimize();
}

function popup_set_title(title) {
	$('title_'+this.keyId).innerHTML = title;

	this.log("Title changed to \""+title+"\"");
}

/**
 *	Sets the popup draggable
 *	@param bool state
 **/
function popup_set_draggable(state) {
	if (state==true) {
		$('drag_'+this.keyId).style.cursor = 'move';
		this.allowDrag = true;
	}else {
		$('drag_'+this.keyId).style.cursor = 'normal';
		this.allowDrag = false;
	}

	this.log("Drag state changed to: "+(state?"true":"false"));
}

/**
 *	Sets the popup content
 *	@param string text
 **/
function popup_set_text(text) {
	$('holder_'+this.keyId).innerHTML = text;
	//We need to set popup dimensions;
	this.width = $(this.popupId).clientWidth;
	this.height = $(this.popupId).clientHeight;

	if (typeof this.iframe == 'object') this.iframe.resizeTo(this.width, this.height);

	this.log("Text changed");
}

/**
 *	Show the popup
 **/
function popup_show(attributes) {
	if (typeof attributes == 'object') {
		if (typeof attributes['dimmer'] != 'undefined' && attributes['dimmer']==true) dimmer.show();
		if (typeof attributes['centered'] != 'undefined' && attributes['centered']==true) eval(this.instanceName+'.position('+(helper.windowWidth/2-this.width/2)+','+(helper.windowHeight/2-this.height/2)+');');
		if (typeof attributes['onload'] != 'undefined' && attributes['onload']==true) {
			if (!pageLoaded) {
				onloadStack[onloadStack.length] = this.instanceName;
				return;
			}
		}

		if (attributes['minstate']) this.minimizeRestore();
	}

	//Update width and height when rendering the popup
	this.width = $(this.popupId).clientWidth;
	this.height = $(this.popupId).clientHeight;

	this.visible = true;
	$(this.popupId).style.visibility = 'visible';
	$(this.popupId).style.opacity = '1';
	//$(this.popupId).style.filter = 'alpha(opacity=100)';
	this.opacity = 100;

	if (typeof this.iframe == 'object') this.iframe.display();
	this.log("Visibilite changed to: visible");
}

/**
 *	Hide the popup
 **/
function popup_hide(attributes) {
	if (typeof attributes == 'object') {
		if (typeof attributes['delay'] != 'undefined' && attributes['delay'] > 0) {
			eval(this.instanceName+'.hideDelayed('+(attributes['delay']*1000)+')');
			return true;
		}

		if (attributes['hideDimmer'] && attributes['hideDimmer'] == true) dimmer.hide();
	}

	this.visible = false;
	$('popup_'+this.keyId).style.visibility = 'hidden';

	if (typeof this.iframe == 'object') this.iframe.hide();
	this.log("Visibilite changed to: hidden");
}

/**
 *	Hide the popup after a given delay
 *	@param int delay
 **/
function popup_hide_delayed(delay) {
	setTimeout(this.instanceName+'.hide()', delay);
}

/**
 *	Fade in the popup
 *	@param int step
 *	@param bool cnt
 **/
function popup_fade_in(step, cnt) {
	$(this.popupId).style.visibility = 'visible';
	eval(this.instanceName+'.visible = true');

	if (this.opacity < 100) {
		this.opacity += step;
		$(this.popupId).style.opacity = this.opacity/100;
		//$(this.popupId).style.filter = 'alpha(opacity='+this.opacity+')';

		setTimeout(this.instanceName+'.fadeIn('+step+', 1)', 50);
	}else if (typeof cnt == 'undefined') {
		this.opacity = 0;
		$(this.popupId).style.opacity = 0;

		eval(this.instanceName+'.fadeIn('+step+', 1)');
	}else{
		eval(this.instanceName+'.display()');
	}
}

/**
 *	Fade out the popup
 *	@param int step
 *	@param bool cnt
 **/
function popup_fade_out(step) {
	this.opacity -= step;
	if (this.opacity > 0) {
		$(this.popupId).style.opacity = this.opacity/100;
		//$(this.popupId).style.filter = 'alpha(opacity='+this.opacity+')';

		setTimeout(this.instanceName+'.fadeOut('+step+')', 50);
	}else{
		eval(this.instanceName+'.hide()');
	}
}

/**
 *	Moves the popup to position based on the current scroll
 *	@param integer left
 *	@param integer top
 */
function popup_position(left, top) {
	if (typeof left == 'object') {
		top 	= left[1];
		left 	= left[0];
	}

	this.posLeft 	= parseInt(left) + parseInt(helper.windowScrollX) + 'px';
	this.posTop		= parseInt(top)	 + parseInt(helper.windowScrollY) + 'px';
	$('popup_'+this.keyId).style.position 	= 'absolute';
	$('popup_'+this.keyId).style.left = this.posLeft;
	$('popup_'+this.keyId).style.top = 	this.posTop;

	this.log("Position changed to: (relative) " + left + 'px ' + top + 'px ');
	if (typeof this.iframe == 'object') this.iframe.position(left, top);
}


/**
 *	Moves the popup to fixed position
 *	@param integer left
 *	@param integer top
 */
function popup_position_fixed(left, top) {
	if (typeof left == 'object') {
		top 	= left[1];
		left 	= left[0];
	}

	this.posLeft 	= parseInt(left) + 'px';
	this.posTop		= parseInt(top)	 + 'px';
	if(!bIE) 	$('popup_'+this.keyId).style.position = 'fixed';
	else 		$('popup_'+this.keyId).style.position = 'absolute';
	$('popup_'+this.keyId).style.left 		= this.posLeft;
	$('popup_'+this.keyId).style.top 		= this.posTop;

	//Add to onscroll stack
	if (bIE) {
		onscrollStack[onscrollStack.length] = this.instanceName;
	}

	//TEMPORARY: Cannot drag fixed popups
	this.draggable(false);


	this.log("Position changed to: (fixed) " + left + 'px ' + top + 'px ');
	if (typeof this.iframe == 'object') this.iframe.position(left, top);
}

function popup_position_reposition_fixed(offsetLeft, offsetTop) {
	if (typeof offsetLeft == 'object') {
		offsetTop 	= offsetLeft[1];
		offsetLeft 	= offsetLeft[0];
	}

	$('popup_'+this.keyId).style.left 		= parseInt(this.posLeft) + offsetLeft;
	$('popup_'+this.keyId).style.top 		= parseInt(this.posTop) + offsetTop;
}

/**
 *	Moves the popup to position based on the top left corner
 *	@param integer left
 *	@param integer top
 */
function popup_position_absolute(left, top) {
	if (typeof left == 'object') {
		top 	= left[1];
		left 	= left[0];
	}

	this.posLeft 	= left + 'px';
	this.posTop		= top + 'px';
	$('popup_'+this.keyId).style.position 	= 'absolute';
	$('popup_'+this.keyId).style.left = this.posLeft;
	$('popup_'+this.keyId).style.top = 	this.posTop;

	this.log("Position changed to: (absolute) " + left + 'px ' + top + 'px ');
	if (typeof this.iframe == 'object') this.iframe.absolute(left, top);
}

function popup_start_drag(e) {
	var targ;
	if (!e) e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3)	targ = targ.parentNode;

	currentPopup = targ.parentNode.parentNode.parentNode.id;
	currentPopupVar = targ.parentNode.parentNode.parentNode.varName;
	if (currentPopupVar) {
		if (eval(currentPopupVar+'.allowDrag==true')) {
			movePopupWithMouse = true;
			eval(currentPopupVar+'.oldZIndex = '+currentPopupVar+'.table.style.zIndex');
			$(currentPopup).style.zIndex = 999;
		}
	}

	return false;
}

function popop_attach_to_mouse(attributes) {
	if (typeof attributes != 'object') {
		attributes = {right: 5, top: 5};
	}

	this.attached	= true;
	attachedX		= attributes['right'];
	attachedY		= attributes['top'];
	attachedPopup 	= this.instanceName;

	this.log("Attached to the mouse");
}

function popup_detach() {
	attachedPopup = null;
	this.attached = false;

	this.log("Detached from the mouse");
}

function popup_debug(layer_id) {
	debugPopup = this.instanceName;

	this.debug_container = layer_id;

	$(layer_id).innerHTML = '';
	for(method in this) {
		$(layer_id).innerHTML += method + "=>" + this[method] + "<br />";
	}
}

function popup_debug_refresh() {

	$(this.debug_container).innerHTML = '';
	for(method in this) {
		if (eval('typeof '+this.instanceName+'.'+method+' == \'function\'')) {
			continue;
		}
		$(this.debug_container).innerHTML += method + "=>" + this[method] + "<br />";
	}
}

function popup_set_style(style) {
	if (typeof style == 'object') {
		for (attrib in style) {
			eval('$(\'holder_'+this.keyId+'\').style.'+attrib+' = "'+style[attrib]+'"');
		}
	}

	this.log("Style set", style);
}

function popup_place_under(target, offset) {
	var target_xy 	= get_xy(target);
	var leftOffset 	= 0;
	var topOffset  	= 0;
	if (typeof offset == 'object') {
		if (offset['left']) leftOffset 	= offset['left'];
		if (offset['top']) 	topOffset 	= offset['top'];
	}

	this.log("Placed under element");
	this.absolute(target_xy[0]+leftOffset, target_xy[1]+topOffset);
}

function popup_recenter() {
	this.width = $(this.popupId).clientWidth;
	this.height = $(this.popupId).clientHeight;

	this.position( (helper.windowWidth/2-this.width/2), (helper.windowHeight/2-this.height/2) );

	if (typeof this.iframe == 'object') {
		this.iframe.position( (helper.windowWidth/2-this.width/2), (helper.windowHeight/2-this.height/2) );
		this.iframe.resizeTo(this.width, this.height);
	}

	this.log("Position recentered");
}

function popup_write_2_log(action, attributes) {
	var attribs = '';
	if (typeof attributes != 'undefined') {
		attribs += '{';
		for (atr in attributes) {
			atrib = attributes[atr];

			if (typeof attributes[atr] == 'boolean') {
				attribs += atr + ': '+atrib;
			}else if (typeof attributes[atr] == 'string') {

				atrib = atrib.replace(/</, "&lt;");
				atrib = atrib.replace(/>/, "&gt;");

				attribs += atr + ': \''+atrib+'\'';
			}else{
				attribs += atr + ': '+atrib;
			}

			attribs += ', ';
		}
		attribs = attribs.substring(0, attribs.length-2);
		attribs += '}';
	}
	//$(this.logDiv).style.fontSize = '10px';
	//$(this.logDiv).style.fontFamily = 'Verdana';
	if (this.logDiv != false) {
		$(this.logDiv).innerHTML += '<strong>' + this.instanceName+':</strong> ' + action + " " + attribs + "<br />";
	}

}

/**
 * JS Helper Class
 **/

function helper() {
	this.updateMouse 	= helper_mouse;
	this.refresh	 	= helper_refresh;
	this.mouseX 		= 0;
	this.mouseY 		= 0;
	this.mouseX2 		= 0;
	this.mouseY2 		= 0;
	this.windowWidth 	= 0;
	this.windowHeight 	= 0;
	this.windowScrollX 	= 0;
	this.windowScrollY 	= 0;

	this.refresh();
}

function helper_refresh() {
	if (!document.all) { //All browsers except IE
		this.windowWidth 	= window.innerWidth-20;
		this.windowHeight 	= window.innerHeight;
		this.windowScrollX 	= window.pageXOffset;
		this.windowScrollY 	= window.pageYOffset;
	}else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE in DTD, Opera9
		this.windowWidth 	= document.documentElement.clientWidth-20;
		this.windowHeight 	= document.documentElement.clientHeight;
		this.windowScrollX 	= document.documentElement.scrollLeft;
		this.windowScrollY 	= document.documentElement.scrollTop;
	}else{ //Normal IE
		this.windowWidth 	= document.body.clientWidth-20;
		this.windowHeight 	= document.body.clientHeight;
		this.windowScrollX 	= document.body.scrollLeft;
		this.windowScrollY 	= document.body.scrollTop;
	}
}

function helper_mouse(posx1, posy1, posx2, posy2) {
	this.mouseX = posx1;
	this.mouseY = posy1;
	this.mouseX2 = posx2;
	this.mouseY2 = posy2;
}

/**
 * Screen dimmer
 */
function dimmer_init(attributes) {
	//Default properties
	this.opacity 	= 80;
	this.enableClose 	= true;
	this.logDiv			= '';

	//Default methods
	this.show 		= dimmer_show;
	this.hide 		= dimmer_hide;
	this.setOpacity = dimmer_set_opacity;
	this.log		= dimmer_write_2_log;

	//Default statuses
	this.visible	= false;

	this.dimmer					= document.createElement('div');
	this.dimmer.id				= 'dimmer_div';
	this.dimmer.onclick 		= function (e) { if (dimmer.enableClose) dimmer.hide(); }
	this.dimmer.style.visibility= 'hidden';
	this.dimmer.style.position = 'absolute';
	this.dimmer.style.left		= 0;
	this.dimmer.style.top		= 0;
	this.dimmer.style.background= 'black';
	this.dimmer.style.cursor	= 'pointer';
	this.dimmer.style.zIndex	= dimmerZIndex; //Under all popups

	this.setOpacity(this.opacity);

	document.body.appendChild(this.dimmer);

	if (bIE6) { //Detect if browser is IE6
		this.iframe 	= new iframe_init();
		if (typeof this.iframe == 'object') this.iframe.resizeTo(helper.windowWidth, helper.windowHeight);
	}

	if (typeof attributes != 'undefined') {
		if (attributes['logTo']) this.logDiv = attributes['logTo'];
		this.log('Dimmer initialized');
	}
}

function dimmer_write_2_log(action, attributes) {
	var attribs = '';
	if (typeof attributes != 'undefined') {
		attribs += '{';
		for (atr in attributes) {
			atrib = attributes[atr];

			if (typeof attributes[atr] == 'boolean') {
				attribs += atr + ': '+atrib;
			}else if (typeof attributes[atr] == 'string') {

				atrib = atrib.replace(/</, "&lt;");
				atrib = atrib.replace(/>/, "&gt;");

				attribs += atr + ': \''+atrib+'\'';
			}else{
				attribs += atr + ': '+atrib;
			}

			attribs += ', ';
		}
		attribs = attribs.substring(0, attribs.length-2);
		attribs += '}';
	}
	//$(this.logDiv).style.fontSize = '10px';
	//$(this.logDiv).style.fontFamily = 'Verdana';
	if (this.logDiv != false) {
		$(this.logDiv).innerHTML += '<strong>dimmer:</strong> ' + action + " " + attribs + "<br />";
	}

}

function dimmer_set_opacity(opacity) {
	this.dimmer.style.opacity	= opacity/100;
	this.dimmer.style.filter	= 'alpha(opacity='+opacity+')';
	this.opacity = opacity;
}

function dimmer_show(attributes) {
	if (typeof attributes == 'object') {
		//Closes the popup after a given delay in seconds
		if (attributes['closeDelay'] && attributes['closeDelay'] > 0) setTimeout('dimmer.hide()', attributes['closeDelay']*1000);

		//Changes the popup opacity
		if (attributes['opacity'] && attributes['opacity'] > 0 && attributes['opacity'] <= 100) dimmer.setOpacity(attributes['opacity']);

		//Makes the popup stay until explicitly closed or page refreshed
		if (attributes['disableClose'] && attributes['disableClose']==true) dimmer.enableClose = false;
		else dimmer.enableClose = true;
	}

	helper.refresh();
	this.dimmer.style.left		= 0;
	this.dimmer.style.top		= 0;
	this.dimmer.style.width		= document.documentElement.scrollWidth+'px';
	this.dimmer.style.height	= document.documentElement.scrollHeight+'px';
	this.dimmer.style.visibility= 'visible';
	this.visible				= true;

	if (typeof this.iframe == 'object') this.iframe.display();
}

function dimmer_hide(attributes) {
	this.dimmer.style.visibility= 'hidden';
	this.visible				= false;

	//Close popups
	if (typeof attributes == 'object') {
		if (attributes['hidePopup']) {
			if (typeof attributes['hidePopup'] == 'object') {
				for (elem in attributes['hidePopup']) {
					if (typeof attributes['hidePopup'][elem] == 'object')
						attributes['hidePopup'][elem].hide();
				}
			}
		}
	}

	if (typeof this.iframe == 'object') this.iframe.hide();
}

/**
 * iFrame
 **/

function iframe_init() {
	this.keyId		= rand_str(4);
	this.iFrameId	= 'iframe_'+this.keyId;

	//Properties
	this.position	= iframe_position;
	this.absolute	= iframe_position_absolute;
	this.resizeTo	= iframe_resize;
	this.display	= iframe_display;
	this.hide		= iframe_hide;

	//Attributes
	this.visible	= false;
	this.posLeft	= 0;
	this.posTop		= 0;
	this.width		= 0;
	this.height		= 0;

	this.iframe		= document.createElement('iframe');
	this.iframe.id			= this.iFrameId;
	this.iframe.style.position		= 'absolute';
	this.iframe.style.display		= 'none';
	this.iframe.frameBorder 			= 0;
	this.iframe.style.left		= '0';
	this.iframe.style.top		= '0';
	this.iframe.style.width		= '0';
	this.iframe.style.height		= '0';
	this.iframe.style.background		= 'transparent';
	this.iframe.style.filter			= 'alpha(opacity=0)'; //Set the alpha transparency to make the iframe transparent in IE. "allowtransparent" wont work

	document.body.appendChild(this.iframe);
}

function iframe_position(left, top) {
	//alert(left);
	this.posLeft 	= (left+helper.windowScrollX) + 'px';
	this.posTop		= (top+helper.windowScrollY) + 'px';
	$(this.iFrameId).style.left = this.posLeft;
	$(this.iFrameId).style.top = this.posTop;
}

function iframe_position_absolute(left, top) {
	this.posLeft 	= left + 'px';
	this.posTop		= top + 'px';
	$(this.iFrameId).style.left = this.posLeft;
	$(this.iFrameId).style.top = this.posTop;
}

function iframe_resize(width, height) {
	this.width		= width + 'px';
	this.height		= height + 'px';
	$(this.iFrameId).style.width = this.width;
	$(this.iFrameId).style.height = this.height;
}

function iframe_display() {
	this.visible = true;
	this.iframe.style.display = 'block';
}

function iframe_hide() {
	this.visible = false;
	this.iframe.style.display = 'none';
}

/**
 * Basic helper function
 **/

function rand_str(length) {
	var randomstring = '';
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var randomstring = '';
	for (var i=0; i<length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}

	return randomstring
}

function get_xy(elem) {
	var curleft = 0;
	var curtop = 0;
	var obj = $(elem);
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}

	return [curleft,curtop];
}
