/*
	(C) 2003-2009 AllMyFavorites.net
*/
var httpreq = null;
var _modaldialog = null;

function PopupMenu() {

	this.items = new Array;
	this.images = new Array;

	// Functions
	this.AddMenuItem = PopupMenu_AddMenuItem;
	this.ShowMenu = PopupMenu_ShowMenu;
}

function PopupMenu_AddMenuItem( txt,action,image) {

	this.items[ txt] = action;
	this.images[ txt] = image;
}


function PopupMenu_ShowMenu( x,y) {

	var htmls = document.getElementsByTagName( "html");
	var html = htmls[ 0];
	
	// Offset if page is scrolled
	x = x +  html.scrollLeft;
	if ( x + 200 > html.scrollLeft + html.clientWidth)	x = html.scrollLeft + html.clientWidth - 220;
	y = y + html.scrollTop;
	if ( y + 200 > html.scrollTop + html.clientHeight) y = html.scrollTop + html.clientHeight - 220;
	
	MakeMask( closeMenu);

	if ( ! IsIE()) {
		var shadow = document.createElement( "div");
		shadow.id = "_menu_shadow";
		shadow.style.visibility = "visible";
		shadow.style.position = "absolute"; 
		shadow.style.left = x + 5 + "px";
		shadow.style.top = y + 5 + "px";
		shadow.style.backgroundColor = "black"; 
		shadow.style.filter = "alpha( opacity=75)";
	
		document.body.appendChild( shadow);
	}
	
	var menu = document.createElement( "div");
	menu.id = "_menu_";
	menu.style.position = "absolute"; 
	menu.style.left = x + "px";
	menu.style.top = y + "px";
	menu.style.backgroundColor = "#FFFFFF"; 
	menu.style.backgroundImage = "url( /images/menubackground.jpg)"; 
	menu.style.backgroundRepeat = "repeat-y";
	menu.style.border = "1px solid #808080";
	if ( IsIE()) {
		menu.style.visibility = "hidden";
		menu.style.filter = "blendTrans(duration=0.2); progid:DXImageTransform.Microsoft.Shadow(color='black', Direction=135, Strength=5)";
	}


//	menu.style.paddingLeft = "25px";
//	menu.style.paddingRight = "10px";
	document.body.appendChild( menu);

	for ( var i in this.items) {
		if ( i == "-") {
			var sep = document.createElement( "div");
			var imghtml = "<img src='/images/transparent.gif' width=16 height=2 border=0 hspace=6 vspace=0>"
			imghtml += "<img src='/images/menuseparator.png' width='100' height='2' border=0 vspace=3>"
			sep.innerHTML = imghtml;
			menu.appendChild( sep);
		} 
		else {
			var menuitem = document.createElement( "a");
	
			var imghtml = "";
			if ( this.images[ i] != "") 
				imghtml = "<img src='"+this.images[ i]+"' width=16 height=16 border=0 hspace=3 vspace=0 style='padding-right:10px'>"
			else
				imghtml = "<img src='/images/transparent.gif' width=16 height=16 border=0 hspace=3 vspace=0 style='padding-right:10px'>"
		
			menuitem.innerHTML = imghtml + i;
			menuitem.className = "popupmenuitem";
			menuitem.href = this.items[ i];
	
	//		menuitem.style.fontFamily = "sans-serif";
	//		menuitem.style.fontSize = "10pt";
	//		menuitem.style.color = "black";
		
			menu.appendChild( menuitem);
		}			
	}
		
	var width = GetObjWidth( menu) + 20;
	var height = GetObjHeight( menu);
	menu.style.width = width + "px";
	if ( ! IsIE()) {
		shadow.style.width = width + "px";
		shadow.style.height = height + "px";
	}
	
	var imgs = menu.getElementsByTagName("img");
	for (var i = 0; i < imgs.length; i++) {
		var img = imgs[ i];
		if ( img.width == 100) {
			img.width = width - 30;
		}			
	}
	if ( IsIE()) {
		menu.filters.blendTrans.stop();
		menu.filters.blendTrans.apply();
		menu.style.visibility = "visible";
		menu.filters.blendTrans.play();    
	}
}

function closeMenu() {

	if ( ! IsIE()) {
		var shadow = document.getElementById( "_menu_shadow");
		if ( shadow) {
			document.body.removeChild( shadow);
			delete shadow;
		}
	}
	DoCloseMask();
	var menu = document.getElementById( "_menu_");
	if ( menu){ 
		document.body.removeChild( menu);
		delete menu;
	}
	return false;
}

function MakeMask( fnOnClick) {
	
	DoCloseMask();

	var htmls = document.getElementsByTagName( "html");
	var html = htmls[ 0];
	var width = html.scrollWidth;
	var height = html.scrollHeight;
	if ( width < html.clientWidth)
		width = html.clientWidth;
	if ( height < html.clientHeight)
		height = html.clientHeight;
	var mask = document.createElement( "span");
	mask.id = "_mask_";
	mask.style.position = "absolute";
	mask.style.left = "0px";
	mask.style.top = "0px";
	mask.style.height = height + "px";
	mask.style.width = width  + "px";
	mask.onclick = fnOnClick;
	if ( IsIE()) {
		mask.style.background = "black";
		mask.style.filter = "alpha( opacity=15); blendTrans(duration=0.2)";
		mask.style.visibility = "hidden";
	}
	else {
		mask.style.backgroundImage = "url( /images/25black.png)"; 
		mask.style.backgroundRepeat = "repeat";
	}		

	document.body.appendChild( mask);

	if ( IsIE()) {
		mask.filters.blendTrans.stop();
		mask.filters.blendTrans.apply();
		mask.style.visibility = "visible";
		mask.filters.blendTrans.play();    
	}
}

function DoCloseMask() {

	var mask = document.getElementById( "_mask_");
	if ( mask != null) {
		document.body.removeChild( mask);
		delete mask;
	}
}

function CloseMask() {
	
	var mask = document.getElementById( "_mask_");
	if ( ! mask) return;
	if ( IsIE()) {
		mask.filters.blendTrans.stop();
		mask.filters.blendTrans.apply();
		mask.style.visibility = "hidden";
		mask.filters.blendTrans.play(); 
		mask.onfilterchange=function() {
			if (this.filters.blendTrans.status == 0) {
				DoCloseMask();
			}
 		}
		return;
	}
	DoCloseMask();
}

function ModalDialog( txt,image) {

	_modaldialog = this;
	this.txt = txt;
	if ( typeof( image) != "undefined")
		this.image = image;
	else		
		this.image = "";
	this.title = "AllMyFavorites.net";
	this.x = 100;
	this.y = 100;
	this.txtOk = "Ok";
	this.txtCancel = "Cancel";
	
	this.OnCancel = Nothing;
	this.OnOk = Nothing;
	this.Show = ModalDialog_Show;
	this.YesNo = ModalDialogYesNo;
}

function Nothing() {}

function ModalDialog_Show() {

	var htmls = document.getElementsByTagName( "html");
	var html = htmls[ 0];
	
	var x = this.x + html.scrollLeft;
	var y = this.y + html.scrollTop;
	
	MakeMask( Nothing);
	
	var dlg = document.createElement( "div");
	dlg.id = "_ModalDlg";
	dlg.style.position = "absolute";
	dlg.style.left = x + "px";
	dlg.style.top = y + "px";
	dlg.style.backgroundColor = "#FFFFFF";
	dlg.style.border = "solid 1px black";
	var html = "<table border='0' cellpadding='0' cellspacing='0'>";
	html += "<tr >";
	html += "<td id='dlgtitle' colspan='5' align='center'>";
	html += this.title;	
	html += "</td></tr>";

	html += "<tr>";
	if ( this.image != "") {
		html += "<td style='padding:10px'><img src='"+this.image+"'>";
	}	
	else {
		html += "<td width='0'>";
	}
	html += "</td><td colspan='4' style='padding:10px'>";
	html += this.txt;
	html += "</tr>";
	
	html += "<tr><td></td>";
	if ( this.OnCancel == null) {
		html += "<td></td>";
		html += "<td align='center' style='padding:10px' colspan='1'>";
		html += "<input type='button' value='"+this.txtOk+"' onClick='ModalDialog_Ok()' style='width:80px; height:25px'></td>";
		html += "<td></td>";
	}
	else {
		html += "<td align='center' style='padding:10px'>";
		html += "<input type='button' value='"+this.txtOk+"' onClick='ModalDialog_Ok()' style='width:80px; height:25px'></td>";
		html += "<td></td>";
		html += "<td align='center' style='padding:10px'>";
		html += "<input type='button' value='"+this.txtCancel+"' onClick='ModalDialog_Cancel()' style='width:80px; height:25px'></td>";
	}
	html += "<td></td></tr></table>";		
	dlg.innerHTML = html;
	if ( IsIE()) {
		dlg.style.visibility = "hidden";
		dlg.style.filter = "blendTrans(duration=0.2); progid:DXImageTransform.Microsoft.Shadow(color='black', Direction=135, Strength=5)";
	}
	document.body.appendChild( dlg);
	if ( IsIE()) {
		dlg.filters.blendTrans.stop();
		dlg.filters.blendTrans.apply();
		dlg.style.visibility = "visible";
		dlg.filters.blendTrans.play();    
	}
	document.onkeydown = OnDlgKeyDown;
	Drag.init( dlg, null, 0, 0, null, null);
}

function ModalDialog_Ok() {
	
	ModalDialog_Exit( _modaldialog.OnOk);
}

function ModalDialog_Exit( fn) {

	fn();
	CloseMask();
	var dlg = document.getElementById( "_ModalDlg");
	if ( IsIE()) {
		dlg.filters.blendTrans.stop();
		dlg.filters.blendTrans.apply();
		dlg.style.visibility = "hidden";
		dlg.filters.blendTrans.play(); 
		dlg.onfilterchange=function() {
			if (this.filters.blendTrans.status == 0) {
				ModalDialog_DoExit();
			}
 		}
		return;
	}
	ModalDialog_DoExit();
}

function ModalDialog_DoExit() {

	document.onkeydown = null;
	
	var dlg = document.getElementById( "_ModalDlg");
	if ( dlg != null) {
		document.body.removeChild( dlg);
		delete dlg;
	}
	if ( _modaldialog != null) {
		delete _modaldialog;
		_modaldialog = null;
	}
}

function ModalDialog_Cancel() {

	ModalDialog_Exit( _modaldialog.OnCancel);
}

function ModalDialogYesNo() {
	
	this.txtOk = "Yes";
	this.txtCancel = "No";
}

function LoadDialog( dlgurl,data) {

	var mask = document.getElementById( "_mask_");
	if ( mask == null) 
		MakeMask( Nothing);
	else
		mask.onclick = Nothing;

	var html = document.getElementById( "html");
	var dlg = document.createElement( "div");
	dlg.id = "_ModalDlg";
	dlg.style.position = "absolute";
	dlg.style.left = 50 + html.scrollLeft + "px";
	dlg.style.top = 50 + html.scrollTop + "px";
	dlg.style.backgroundColor = "#FFFFFF";
	dlg.style.border = "solid 1px black";
	dlg.style.visibility = "hidden";
	document.body.appendChild( dlg);

	window.status="Loading dialog - please wait...";
	if ( ! httpreq)
		httpreq = CreateHttpReq();
	else
		httpreq.abort();
		
	if ( data == null || data == "")		
		httpreq.open("GET", dlgurl,true);
	else		
		httpreq.open("POST", dlgurl,true);
	httpreq.onreadystatechange=function() {
		if (httpreq.readyState==4) {
			window.status = "";
			var dlg = document.getElementById( "_ModalDlg");
			dlg.innerHTML = httpreq.responseText;
			dlg.style.visibility = "visible";
			var input = dlg.firstChild;
			while ( input.tagName != "INPUT") {
				if ( input.firstChild != null) {
					input = input.firstChild;
					continue;
				}
				if ( input.nextSibling) {
					input = input.nextSibling;
					continue;
				}
				input = input.parentNode;
				while ( input.nextSibling == null) 
					input = input.parentNode;
				input = input.nextSibling;
			}
			input.focus();
		}
	}
	if ( data != "") {
		httpreq.setRequestHeader( 'Content-Type','application/x-www-form-urlencoded');
		httpreq.setRequestHeader( 'Content-length', data.length); 
		httpreq.send( data);
	}
	else {
		httpreq.send( null);
	}
	
	Drag.init( dlg);
	dlg.bBubble = true;
	document.onkeydown = OnDlgKeyDown;
}

function OnDlgKeyDown( e) {
	if (typeof e == 'undefined') e = window.event;
	if ( e.keyCode == 27) {
		httpreq.abort();
		DoCloseMask();		
		ModalDialog_DoExit();
	}
}

