//$ = jQuery.noConflict();

var detectMacXFF2 = function () {
  var userAgent = navigator.userAgent.toLowerCase();
  if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
    var ffversion = new Number(RegExp.$1);
    if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
      return true;
    }
  }
}

var detectIE6 = function () {
  var userAgent = navigator.userAgent.toLowerCase();
  if (/msie 6/.test(userAgent)) {
    return true;
  }
}

/*SS: The "modal-overlay" does not work in FF2 mac, hence the workaroung to use a transparent png (CSS class: modal-overlay-ffmacx).
But for now we'll just use the same class everywhere */

var overlayCls = "modal-overlay";
//var overlayCls = "modal-overlay-ffmacx";

//SS: IE6 fix to handle png transparency
if (detectIE6()){
	overlayCls = "modal-overlay";
}

/** SS: We'll skip this check since we'll just use the png image class for all
if (detectMacXFF2()) {
	overlayCls = "modal-overlay-ffmacx";
}**/

var modalWindow = {
	/* these were breaking Ext windows
	parent:String = "body",
	windowId:String = null,
	content:String = null,*/
	parent:'body',
	windowId:null,
	content:null,
	width:Number,
	height:Number,
	close:function(){
		$(".modal-window").remove();
		$("."+overlayCls).remove();
	},
	open:function(){
		var modal = "";
		// Adjust modal placement depending on window size (especially when the popup window height is greater than window height)
		winHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.documentElement.clientHeight);
		//alert(winHeight);		
		
		if (this.height > winHeight){
			marginTop = 50;
			topOffset = "0%";
			position = "absolute";	// we want the popup to scroll with the main content
		}
		else {
			marginTop = -(this.height / 2);
			topOffset = "50%";
			position = "fixed";		// we want to keep the popup centered and not scroll
		}
		modal += "<div class=\""+overlayCls+"\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:" + marginTop + "px; margin-left:-" + (this.width / 2) + "px; top:" + topOffset + ";position:"+position+"\">";
		modal += this.content;
		modal += "</div>";

		$(this.parent).append(modal);

		$(".modal-window").append("<a class=\"close-window\"></a>");
		$(".close-window").click(function(){modalWindow.close();});
		//$("."+overlayCls).click(function(){modalWindow.close();});
	}
};

var openMyModal = function(id,url,width,height){
	modalWindow.windowId = id;
	modalWindow.width = width;
	modalWindow.height = height;
	modalWindow.content = "<iframe width='"+width+"' height='"+height+"' frameborder='0' scrolling='no' allowtransparency='true' src='" + url + "'></iframe>";
	modalWindow.open();
	return modalWindow;
};

var openMyScrollingModal = function(id,url,width,height){
	modalWindow.windowId = id;
	modalWindow.width = width;
	modalWindow.height = height;
	modalWindow.content = "<iframe width='"+width+"' height='"+height+"' frameborder='0' scrolling='auto' allowtransparency='true' src='" + url + "'></iframe>";
	modalWindow.open();
	return modalWindow;
};


