/*
Simple Modal

var modal_settings = {
	content	: 'iframe',
	iframe_url : 'listing_category_modalAE.php?id='+cat_id,
	width	: 600,
	height	: 600
}
modal(modal_settings);
*/
function modal(settings) {
	var defaults = {
		content	: 'inline',
		straight_content : false,
		add_close : false,
		width	: null,
		height	: null,
		iframe_url : null,
		fade	: true,
		modal	: false,
		title_text : '',
		title	: false
	}
	
	var settings =  $.extend(defaults, settings);//sample_content
		
	try {
		if(document.getElementById("modal_overlay") === null) {

			var title_html = '';
			if (settings.title==true) title_html = '<div class="modal_title">'+settings.title_text+'<a href="javascript:void(0)" onclick="modal_close('+settings.fade+')" class="modal_close"></a></div>';
			// Append overlay/window to body
			
			switch (settings.content) {
				case 'straight':
					mod_content = settings.straight_content;
					// add close button
					if (settings.add_close == true) {
						content += '<div style="text-align:center"><button type="button" onclick="modal_close(false)">Close</button></div>';
					}
					break;
				case 'inline':
					mod_content = getEle(settings.inline_id).innerHTML;
					break;
				case 'iframe':
					mod_content = '<iframe frameborder="0" hspace="0" src="'+(settings.iframe_url)+'" id="modal_iframeContent" name="modal_iframeContent'+(Math.round(Math.random()*1000))+'" width="100%" height="100%"> </iframe>';
					break;
			}

			var modal_close = (settings.modal==false) ? ' onclick="modal_close(true)"' : '';
			$("body").append('<div id="modal_overlay" style="display:none" '+modal_close+'></div><div id="modal_window" style="display:block; display:none">'+title_html+'<div id="modal_content">'+mod_content+'</div></div>');
			
			// set styles
			//if (modal_detectMacXFF()) {
			//	$("#modal_overlay").addClass("modal_overlayMacFFBGHack");//use png overlay so hide flash
			//} else {
				$("#modal_overlay").addClass("modal_overlayBG");//use background and opacity
			//}
			
			$("#modal_window").css('margin-left',	'-'+((settings.width+20)/2)+'px');
			$("#modal_window").css('width',			(settings.width+20)+'px');
			$("#modal_window").css('margin-top',	'-'+((settings.height + 28) / 2)+'px');
			
			$("#modal_content").css('width', settings.width+'px');
			$("#modal_content").css('height', settings.height+'px');
			
			// show or fade (fade does not work if < IE8
			if (settings.fade == true && !isIE()) {
				$("#modal_overlay").fadeIn("fast", function(){ $("#modal_window").fadeIn("fast") });
			} else {
				$("#modal_overlay").css('display' , '');
				$("#modal_window").css('display' , 'block');
			}
		}
	} catch(e) {
		//nothing here
		txt="There was an error on this page.\n\n";
		txt+="Error description: " + e.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
	}
}


function modal_close(fade) {
	if(typeof fade == "undefined" || fade == null) var fade = true;
	if (fade==true && !isIE()) {
		$("#modal_overlay").fadeOut("fast",function(){$('#modal_window,#modal_overlay').trigger("unload").unbind().remove();});
	} else {
		$("#modal_window,#modal_overlay").remove();
	}
}

function modal_detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}
function isIE() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('msie') != -1) {
		return true;
	}
	return false;

}
