/**********************************************************/
/*        Thematic maps modal panel                       */
/*        Requires: Yui librabries						  */
/*		  Wisdom 2008	                                  */
/**********************************************************/

YAHOO.bosatlas.lightboxPanel = { 
	lightboxPanel		: null,
	lightboxTitle		: null,
	lightboxModal		: false,
	panelName			: "",
	tabId				: null,
	lightboxBody		: "",
	lightboxWidth		: "",
	lightboxContentUrl	: "",
	lightboxClose		: false,
	ligthboxDraggable	: false,
	lightboxEffects		: null,
	lightboxHeight		: "auto",
	ajaxLoaderClass		: "showAjaxLightboxLoader",

	init : function(lightboxProperties) {
		if(Dom.get(this.panelName)){
			// lightbox already open, return false
			return false;
		}
		this.lightboxContentUrl = lightboxProperties.url;
		this.lightboxWidth = lightboxProperties.width;
		this.lightboxHeight = lightboxProperties.height;
		this.lightboxTitle = lightboxProperties.title;
		this.lightboxClose = lightboxProperties.close;
		this.ligthboxDraggable = lightboxProperties.draggable;
		this.lightboxEffects = lightboxProperties.effects;
		this.lightboxModal = lightboxProperties.modal;
		this.lightboxFixedcenter = lightboxProperties.fixedcenter;
		this.panelName = lightboxProperties.panelName;
		this.tabId = lightboxProperties.tabId;
	
		this.draw();
		this.setAjaxLoader(1);
		this.getXHRContent();
	},
	
	draw : function() {
	
		this.lightboxPanel = new YAHOO.widget.Panel(this.panelName, { width:this.lightboxWidth, height:this.lightboxHeight, modal:this.lightboxModal, visible:true,underlay:"shadow",draggable:false, close:this.lightboxClose, fixedcenter: this.lightboxFixedcenter} ); //effect:this.lightboxEffects
		if(this.lightboxTitle) {
			this.lightboxPanel.setHeader(this.lightboxTitle);
		}
		
		
		this.lightboxPanel.render(document.body);
	
		// using timed clearing to avoid timing problems
		var timedClearing = function(dialog) {
			var tC = window.setTimeout(function(){
				dialog.destroy();
				YAHOO.bosatlas.tooltip.init();
				},50);
		}

		this.lightboxPanel.hideEvent.subscribe(function(){timedClearing(this)});

	},
	
	runAfterEffects : function() {
		
		// lightbox content related javascript init functions
		
		// tooltip init
		YAHOO.bosatlas.tooltip.init();
		
		// form validation
		YAHOO.bosatlas.thematicalForm.init();
		
		// append tooltips to lightbox
		
		var moveTooltips = function() {
			var allTooltips = Dom.getElementsByClassName("yui-tt", "div");
			for(var i=0; i<allTooltips.length; i++) {
				Dom.get("lightboxPanel_c").appendChild(allTooltips[i]); 	
			}
		}
		
		var timedCheck = window.setTimeout(function(){
			// move tooltips
			moveTooltips();
			
			// format print preview box
			YAHOO.bosatlas.SVGMap.formatPrintPreview(); 
			
			// datatable init 
			YAHOO.bosatlas.dataTable.formatTable();
		}, 500);
		
		
		var timedCheck2 = window.setTimeout(function(){
			// set tabname if export form
			var exportMapPlacholder = Dom.get("saveMapName");
			if(exportMapPlacholder) {
				YAHOO.bosatlas.tabHandler.exportTabName(exportMapPlacholder)
			}
			// disable forms default behaviours
			YAHOO.bosatlas.thematicalForm.disableForm(YAHOO.bosatlas.lightboxPanel.panelName);
			
			// format theme combobox
			YAHOO.bosatlas.thematicalForm.formatCombobox(Dom.get(YAHOO.bosatlas.thematicalForm.settings.themeComboboxId));

		}, 100);
		
		this.setAjaxLoader(0);

	},
	formatContent : function() {
		
		var lightboxFrame = document.createElement("div");
		lightboxFrame.innerHTML = this.lightboxBody;
		this.lightboxBody = lightboxFrame;
		this.appendContent();
	},

	appendContent : function() {

		this.lightboxPanel.setBody(this.lightboxBody);
		this.lightboxPanel.render();
		
		// run afterEffects
		this.runAfterEffects();
	},
	getXHRContent : function() {
		
		var selectedTab = YAHOO.bosatlas.tabHandler.getActiveTab();
		var tabId = selectedTab.getAttributeConfig('element').value.id.split("_")[1];
		
		var callback =  {   
			
			cache:false,

			success: function(o) {
				// set content in lightbox panel object
				if(trim(o.responseText)) {
					
					HTMLContent = o.responseText;
				}
				else {
					var HTMLContent = '<div class="genLightboxErrorMsg">'+ LANG_GENERAL_AJAX_NODATA_ERRORMSG + '</div>';
				}
				
				o.argument.lightboxPanel.lightboxBody = HTMLContent;
				o.argument.lightboxPanel.formatContent();
				
			},
				
			failure: function(o) { 
				if(parseInt(o.status) == 403) {
					drawSessionErrorNotification(null,LANG_SESSION_ERROR,"canvas", true)
				}
				
				if(parseInt(o.status) == 500 || parseInt(o.status) == 404) {
					drawGeneralErrorNotification(o.argument.tabId,LANG_GENERAL_ERROR,"appContent", true)
				}
			}, 
			
			argument : {lightboxPanel: this, tabId:tabId}
		}
		var transaction = YAHOO.util.Connect.asyncRequest('GET', this.lightboxContentUrl, callback, null);  
		
	},
	setAjaxLoader : function(state) {
		var ajaxLoadRegion = Dom.get(this.panelName);
		if(state == 1) {
			Dom.addClass(ajaxLoadRegion, this.ajaxLoaderClass);
		} else {
			Dom.removeClass(ajaxLoadRegion, this.ajaxLoaderClass);	
		}
	},

	close : function() {
    	this.lightboxPanel.hide();
   
    }
}

function drawLightbox(title,url,width,height) {

	YAHOO.bosatlas.lightboxPanel.init({
		url: url,
		title: title,
		width: width,
		height: height,
		close: true,
		fixedcenter: true,
		draggable: false,
		modal: true,
		panelName: "lightboxPanel",
		effects: {effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}}
	);
}

