var popup = {

	init:function () {
		window.onscroll = popup.updateSize;
		this.divLayer = $("disableScreen");
		this.windowInside = $("windowInside");
		this.windowContent = $("warnDialogueContent");
		this.content = $('dialogueC');
		//------------ Truong update -------------------------//
		this.queuePopup = new Array();
		this.size = new Object();
		this.size.height = '';
		this.size.width = '';
		//------------ End update -------------------------//
	},

	show:function () {
		this.hideCombo();
	},

	hideCombo: function () {
		if(typeof document.getElementById("page") == "undefined") return;
		if($$("select")){
			var combos = $$("select");
			for(var i=0; i<combos.length; i++) {
				combos[i].hide();
			}
		}
		/*if($$("embed")){
			var embed = $$("embed");
			for(var i=0; i<embed.length; i++) {
				embed[i].hide();
			}
		}*/
	},

	showCombo: function () {
		if(typeof document.getElementById("page") == "undefined") return;
		if(document.getElementsByTagName("select")){
			var combos = document.getElementsByTagName("select");
			for(var i=0; i<combos.length; i++) {
				combos[i].show();
			}
		}
		/*if(document.getElementsByTagName("embed")){
			var embed = document.getElementsByTagName("embed");
			for(var i=0; i<embed.length; i++) {
				embed[i].show();
			}
		}*/
	},

	setDimension: function (popWidth, popHeight) {
		var browserSize = util.getBrowserSize();

		var ie5=document.all && !window.opera;
		var ns6=document.getElementById;
		var iecompattest = (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body;
		var dsocleft = ie5? iecompattest.scrollLeft : pageXOffset;
		var dsoctop = ie5? iecompattest.scrollTop : pageYOffset;
		this.grayOut(true);

		//------------ Truong update -------------------------//
		this.size.height = popHeight;
		this.size.width = popWidth;
		//------------ End update --------------------------//
		
		if (popWidth!=null) {
			$('warnDialogueContent').setStyle({'width':popWidth + 'px'});
			var popWidth2 = popWidth + 20;
			$('warnDialogue').setStyle({'width':popWidth2 + 'px'});
			popWidth2 = popWidth + 52;
			$('windowInside').setStyle({'width':popWidth2 + 'px'});
		} else {$('windowInside').setStyle({'width':453 + 'px'});}

		if (popHeight!=null) {			
			$('warnDialogueContent').setStyle({'height':popHeight + 'px'});
			var popHeight1 = popHeight + 20;
			$('warnDialogue').setStyle({'height':popHeight1 + 'px'});
			var popHeight2 = popHeight1 + 40;
			$('windowInside').setStyle({'height':popHeight2 + 'px'});
			
			var dialogueCHeight = popHeight - 20;
			$("dialogueC").setStyle({'height': dialogueCHeight + 'px'});
		} else {$('windowInside').setStyle({'height':100 + 'px'});}

		// handling windows
		this.windowInside.style.display = "block";
		new Rico.Effect.FadeTo(this.windowInside, 1.0, 180, 1);

		this.windowInside.style.top = ((browserSize.height/2 - parseInt(this.windowInside.style.height)/2) + dsoctop) + "px";
		this.windowInside.style.left = ((browserSize.width/2 - parseInt(this.windowInside.style.width)/2) + dsocleft) + "px";

		popup.windowContent.setStyle({'opacity':1,'-moz-opacity':1,'filter':'alpha(opacity=100)'});

		//alert($('warnDialogueContent').style.width + " - " + $('warnDialogueContent').style.height);
	},

	popItUp:function (url, option, width, height, DOM_Focus_ID, clickOKCallback ) { //element to focus
		this.resetHeight();
		// Show pop up
		this.show();
		//alert('test');
		// add by Yen.Truong
		if(DOM_Focus_ID) {
		    var temp = DOM_Focus_ID.indexOf('.');
		    var flag = false;
		    var el = '';
		    var page = '';
		    if(temp != -1) {
		       flag = true;
		       //p = DOM_Focus_ID.substr(0,temp);
		       el = DOM_Focus_ID.substr(temp+1);
		    }
		}

		// Handling alert pop up
		if((url == null || url == "") && typeof option != "object") {
			var str = 	'<div align="center" style="padding:10px; margin-bottom:5px;"><div>' + option + '</div><div><input type="button" value="Close" class="inputButton" id="butOk"/>'
						+ '</div></div>';			
			this.clickOK = function() {
			    popup.hide();
			    // add by Yen.Truong
			    if( clickOKCallback){
					clickOKCallback();
				}else if(flag){
		            $(el).focus();
		        }
			}
			hideCloseButton();
			this.content.innerHTML = str;
			this.windowInside.style.display = "block";
			height = setHeightForPopup();
			this.setDimension(width, height);
			this.windowContent.setStyle({'opacity':1,'-moz-opacity':1,'filter':'alpha(opacity=100)'});
			$("butOk").onmouseup = popup.clickOK;
			$("butOk").focus();
			$("butOk").onkeyup = function(evt){
			  var crossEvent = (evt)?evt:event;
			  var keyCode = crossEvent.keyCode;
			  if(keyCode == 13){
			    popup.clickOK();
			  }
			}
			return;
		}
		// Handling confirm pop up
		else if(url == null || url == "") {
			// Popup content
			var str = new String();
			if(option.html==true)
				str = option.confirmStr;
			else {
				str = 	'<div align="center" style="padding:10px;"><div>' + option.confirmStr
						+ '</div><div><input onclick="javascript:popup.clickYes(); popup.hide(); popup.notifyResult();" type="button" value="Yes" id="butYes" class="inputButton"/>'
						+ '<input onclick="popup.clickNo(); popup.hide()" type="button" value="No" id="butNo" class="inputButton" /></div></div>';
			}

			this.clickYes = option.clickYes;
			this.clickNo = option.clickNo;
			if(typeof(option.notifyResult) != 'undefined' )
				this.notifyResult = option.notifyResult ; //Add more allow function call back from click Yes button
			else
				this.notifyResult = function(){};
				
			hideCloseButton();
			
			this.content.innerHTML = str;
			this.windowInside.style.display = "block";
			height = setHeightForPopup();
			this.setDimension(width, height);
			this.windowContent.setStyle({'opacity':1,'-moz-opacity':1,'filter':'alpha(opacity=100)'});			
			//if(option.confirmStr.indexOf("delete") == -1)
				//$("butYes").focus();
			return;
		}
		else {
			// Handling ajax pop up					
			var dialogueCHeight = height - 40;
			$("dialogueC").setStyle({'height': dialogueCHeight + 'px'});
			showCloseButton();
			var loadingHtml = '<div style="text-align:center;padding-top:'+ (dialogueCHeight - 66)/2  +'px"><img src="' + IRAVE_URL_IMAGES_COMMON + 'circleLoading_bg_blue_large.gif"/></div>';
			popup.content.innerHTML = loadingHtml;
			popup.setDimension(width, height);
			
			popup.windowInside.style.display = "block";
			popup.windowContent.setStyle({'opacity':1,'-moz-opacity':1,'filter':'alpha(opacity=100)'});	
			
			//----------- Truong update (Put more method and parameters when call ajax ----------------//	
			var optionAjax = new Object();			
			
			// If option is defined
			if (option != null && typeof option == "object") {				
				optionAjax = option;
			}
			
			if (typeof optionAjax.method == 'undefined') {
				optionAjax.method = "get";
			}		
			if (typeof optionAjax.parameters == 'undefined') {
				optionAjax.parameters = "";
			}	
			//----------- End Update ----------------//			

			new Ajax.Request (url, {
				method: optionAjax.method,
				parameters: optionAjax.parameters,
				onSuccess: function(transport) {				
					var response = transport.responseText.strip();
					popup.content.innerHTML = response;				
					irave.initInviteAtHeader();
					
					if ((typeof(DOM_Focus_ID) != "undefined") && (DOM_Focus_ID != null) && (flag==false)){
						$(DOM_Focus_ID).focus();
						var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
						if(isIE)
							$(DOM_Focus_ID).focus(); //focus again if IE browser
					}else if(flag == true){
					   $(el).focus();
					   $(el).focus();//focus again if IE browser
					}
					
					if (typeof(option) == object && typeof(option.autoSize)!='undefined') {
						var height = parseInt($(option.idDialog).style.height) + 40;
						var width = parseInt($(option.idDialog).style.width);
						
						// Set dialogueC height again
						var dialogueCHeight = height - 40;
						$("dialogueC").setStyle({'height': dialogueCHeight + 'px'});		
						
						// Set dimension				
						popup.setDimension(width, height);
						
					}
					
					if (typeof(option.callBack)!= 'undefined' ) {
						option.callBack();
					}
					
				},
				onFailure: function(transport) {
					if (option.callBackError != undefined) {
						option.callBackError();
					}
				}
			});
		}
	},
/*
	popItUpHTML:function (strHTML, option, width, height, DOM_Focus_ID) {
		// Show pop up
		this.show();
		//alert('test');
		// add by Yen.Truong
		if(DOM_Focus_ID) {
		    var temp = DOM_Focus_ID.indexOf('.');
		    var flag = false;
		    var el = '';
		    var page = '';
		    if(temp != -1) {
		       flag = true;
		       //p = DOM_Focus_ID.substr(0,temp);
		       el = DOM_Focus_ID.substr(temp+1);
		    }
		}

		// Handling after delete successful.
		popup.setDimension(width, height);
		popup.content.innerHTML = strHTML;
		popup.windowInside.style.display = "block";
		popup.windowContent.setStyle({'opacity':1,'-moz-opacity':1,'filter':'alpha(opacity=100)'});
		if ((DOM_Focus_ID != "undefined") && (DOM_Focus_ID != null) && (flag==false)){
		   $(DOM_Focus_ID).focus();
		   var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
		   if(isIE)
		   $(DOM_Focus_ID).focus(); //focus again if IE browser
		}
        if (option.callBack!='undefined') {
            option.callBack();
        }

	},
*/
	
	hide:function () {

		var browserSize = util.getBrowserSize();
		// handling disable screen
		setTimeout("popup.grayOut(false);",1);
		// handling windows
		new Rico.Effect.FadeTo(this.windowInside, 0.0, 180, 1);
		setTimeout("popup.windowInside.style.display = 'none'; popup.showCombo();", 100);//200
		this.resetHeight();
		this.resetContent();
	},

	//--------------- Truong update ----------------------//
	pushQueue:function () {
		
		var popupObj = new Object();
		popupObj.size = new Object();
		popupObj.content = $('dialogueC').innerHTML;
		popupObj.contentValue = new Array();
		
		var elms = $('dialogueC').getElementsByTagName("*");
		for(var i = 0, maxI = elms.length; i < maxI; ++i) {
	    	var elm = elms[i];
		    switch(elm.type) {
		    	case "text":
		    	case "textarea":
		    	case "file":
		    	case "password":
		    	case "radio":
		    	//case "checkbox":
		    	//case "select-one":
		    	//case "select-multiple":
		        	popupObj.contentValue[i] = new Object();
		        	popupObj.contentValue[i].type = elm.type;
		        	popupObj.contentValue[i].name = elm.name;
		        	popupObj.contentValue[i].id = elm.id;
		        	popupObj.contentValue[i].value = elm.value;		        	
		    }
	  	}
	
		popupObj.size.width = this.size.width;
		popupObj.size.height = this.size.height;
		this.queuePopup.push(popupObj);		
	},
	showQueue:function () {
		var popupObj = this.queuePopup.pop();
		this.content.innerHTML = popupObj.content;
		this.windowInside.style.display = "block";		
		this.setDimension(popupObj.size.width, popupObj.size.height);
		this.windowContent.setStyle({'opacity':1,'-moz-opacity':1,'filter':'alpha(opacity=100)'});	
		
		var elms = $('dialogueC').getElementsByTagName("*");
		for(var i = 0, maxI = elms.length; i < maxI; ++i) {
	    	var elm = elms[i];
		    switch(elm.type) {
		    	case "text":
		    	case "textarea":
		    	case "file":
		    	case "password":
		    	case "radio":
		    	//case "checkbox":
		    	//case "select-one":
		    	//case "select-multiple":		    		
		        	elm.value = popupObj.contentValue[i].value;		        	
		    }
	  	}
	  			
		//if(option.confirmStr.indexOf("delete") == -1)
			//$("butYes").focus();
		return;
	},
	
	//--------------- end update --------------------------------//
	resetHeight:function () {
		this.content.setStyle({'height': ''});;		
	},
	
	resetContent:function () {
		this.content.innerHTML = "";		
	},	

	updateSize:function () {
		if(popup.divLayer.style.display == "none") return;
		var browserSize = util.getBrowserSize();

		// Get current top and left
		var ie5=document.all && !window.opera;
		var ns6=document.getElementById;
		var iecompattest = (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body;
		var dsocleft = ie5? iecompattest.scrollLeft : pageXOffset;
		var dsoctop = ie5? iecompattest.scrollTop : pageYOffset;

		var moveToX = ((browserSize.width/2 - parseInt(popup.windowInside.style.width)/2) + dsocleft);
		var moveToY = ((browserSize.height/2 - parseInt(popup.windowInside.style.height)/2) + dsoctop);

		new Rico.Effect.Position(popup.windowInside,moveToX,moveToY,200,10);
	},

	grayOut: function (vis, options) {
		// Pass true to gray out screen, false to ungray
		// options are optional.  This is a JSON object with the following (optional) properties
		// opacity:0-100
		// Lower number = less grayout higher = more of a blackout
		// zindex: #
		// HTML elements with a higher zindex appear on top of the gray out
		// bgcolor: (#xxxxxx)
		// Standard RGB Hex color code
		// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
		// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
		// in any order.  Pass only the properties you need to set.
		var options = options || {};
		var zindex = options.zindex || 50;
		var opacity = options.opacity || 70;
		var opaque = (opacity / 100);
		var bgcolor = options.bgcolor || '#fff';
		var dark=document.getElementById('darkenScreenObject');
		if (!dark) {
			// The dark layer doesn't exist, it's never been created.  So we'll
			// create it here and apply some basic styles.
			// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
			var tbody = document.getElementsByTagName("body")[0];
			var tnode = document.createElement('div');
			// Create the layer.
			tnode.style.position='absolute';
			// Position absolutely
			tnode.style.top='0px';
			// In the top
			tnode.style.left='0px';
			// Left corner of the page
			tnode.style.overflow='hidden';
			// Try to avoid making scroll bars
			tnode.style.display='none';
			// Start out Hidden
			tnode.id='darkenScreenObject';
			// Name it so we can find it later
			tbody.appendChild(tnode);
			// Add it to the web page
			dark=document.getElementById('darkenScreenObject');
			// Get the object.
		}
		if (vis) {
			// Calculate the page width and height
			if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
				var pageWidth = document.body.scrollWidth+'px';
				var pageHeight = document.body.scrollHeight+'px';
			} else if( document.body.offsetWidth ) {
				var pageWidth = document.body.offsetWidth+'px';
				var pageHeight = document.body.offsetHeight+'px';
			} else {
				var pageWidth='100%';
				var pageHeight='100%';
			}
			var browserSize = util.getBrowserSize();
			//alert(pageHeight + " - " + browserSize.height);
			if (parseInt(pageHeight)<browserSize.height) pageHeight = browserSize.height + "px";


			//set the shader to cover the entire page and make it visible.
			dark.style.zIndex=0;
			dark.style.backgroundColor=bgcolor;
			dark.style.width= pageWidth;
			dark.style.height= pageHeight;
			dark.style.display='block';
			new Rico.Effect.FadeTo(dark, 0.6, 200, 1);

		} else {
			new Rico.Effect.FadeTo(dark, 0.0, 200, 1);
			dark.style.display='none';
		}
	}
}

function setHeightForPopup() {
	return $("dialogueC").getHeight() + 40;
}

function hideCloseButton() {
	$("closeButton").hide();
}

function showCloseButton() {
	$("closeButton").show();
}


