/*
  Created By: Chris Campbell
  Website: http://particletree.com
  Date: 2/1/2006  
  Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
  Modified By: Arnaud Daret
*/

/*-----------------------------------------------------------------------------------------------*/

// Event.observe(window, 'load', initialize, false);
// Event.observe(window, 'load', getBrowserInfo, false);
// Event.observe(window, 'unload', Event.unloadCache, false);

var Dialog = Class.create();

Dialog.prototype = {
    
    loadTiny:false,
    callback:null,
    
    initialize: function(url,params) {
	if (params != null) {
	    this.loadTiny=$H(params).get('tiny')==true;
	    this.callback=$H(params).get('callback')!="undefined"?$H(params).get('callback'):null;	    
	}
	this.addEmptyBox();
	this.content = url;
	this.activate();	
    },
    
    initTiny: function() {
	if (this.loadTiny) {
	    $$("textarea").each(function(elt){
		    tinyMCE.execCommand('mceAddControl', false, elt.identify());		    
		});
	}	
    },
    
    unloadTiny: function() {
	if (this.loadTiny) {
	    $$("textarea").each(function(elt){
		    tinyMCE.execCommand('mceRemoveControl', false, elt.identify());
		});
	}	
    },
    
    // Add an empty box
    addEmptyBox: function(){
	var arrayPageSize = getPageSize();
	$('mainContent').appendChild(new Element("div",{id:'dialogOverlay'}));	
	$('dialogOverlay').setStyle({height: arrayPageSize[1]+'px',width: arrayPageSize[0]+'px'});
	$('dialogOverlay').setOpacity(0.7);
	$('mainContent').appendChild(new Element("div",{id:'dialog','class':'loading'}));
	if (Prototype.Browser.IE) $('dialog').setStyle({width: '100%'});
	new Ajax.Updater('dialog','../ui/templates/dialog.html', {asynchronous:false,
		onComplete: function() {
		    if (Prototype.Browser.IE) {
			$("ctl").replace(new Element("img",{src:'../ui/images/ctl.gif', alt:'ctl',style:'vertical-align:top'}));
			$("ctr").replace(new Element("img",{src:'../ui/images/ctr.gif', alt:'ctr',style:'vertical-align:top'}));
			$("cbl").replace(new Element("img",{src:'../ui/images/cbl.gif', alt:'cbl',style:'vertical-align:top'}));
			$("cbr").replace(new Element("img",{src:'../ui/images/cbr.gif', alt:'cbr',style:'vertical-align:top'}));
		    } else {
			var ctx = $("ctl").getContext("2d");
			ctx.fillStyle = "rgb(158,154,33)";
			ctx.beginPath();
			ctx.arc(6,6,6,Math.PI,0,false);
			ctx.fill();
			
			ctx = $("ctr").getContext("2d");
			ctx.fillStyle = "rgb(158,154,33)";
			ctx.beginPath();
			ctx.arc(0,6,6,0,Math.PI,true);
			ctx.fill();	    
			
			ctx = $("cbr").getContext("2d");
			ctx.fillStyle = "rgb(158,154,33)";
			ctx.beginPath();
			ctx.arc(0,0,6,0,Math.PI,false);
			ctx.fill();
			
			ctx = $("cbl").getContext("2d");
			ctx.fillStyle = "rgb(158,154,33)";
			ctx.beginPath();
			ctx.arc(6,0,6,Math.PI,0,true);
			ctx.fill();
		    }
		}
	});	
    },
    
    // Turn everything on - mainly the IE fixes
    activate: function(){	
	if (Prototype.Browser.IE) {
	    // this.getScroll();
	    // this.prepareIE('100%', 'hidden');
	    this.setScroll(0,0);
	    this.hideSelects('hidden');
	}
	this.displayDialog("block");
    },
    
    // Ie requires height to 100% and overflow hidden or else you can scroll down past the dialog
    prepareIE: function(height, overflow){
	bod = document.getElementsByTagName('body')[0];
	bod.style.height = height;
	bod.style.overflow = overflow;
	
	htm = document.getElementsByTagName('html')[0];
	htm.style.height = height;
	htm.style.overflow = overflow; 
    },
    
    // In IE, select elements hover on top of the dialog
    hideSelects: function(visibility){
	$$('select').each(function(elt){
		elt.style.visibility = visibility;
	    });
    },
        
    setScroll: function(x, y) { window.scrollTo(x, y); },
    
    centerBox: function() {	
	var dim = getPageSize();
	var diagdim = $('dialog').getDimensions();
	var x = diagdim.width; // console.log("x="+x);
	var y = diagdim.height; // console.log("y="+y);
	var toppos = eval((dim[3]-y)/2);
	if (toppos < 0) { 
	    toppos = 0;
	    $('dialog').setStyle({position:'absolute'});
	}
	var margin = eval((dim[2]-x)/2)+'px';
	// console.log("margin="+margin);
	$('dialog').setStyle({left:margin, right:margin, top:toppos+'px'});	
    },
    
    displayDialog: function(display){
	$('dialogOverlay').style.display = display;
	$('dialog').style.display = display;
	if(display != 'none') this.loadInfo();
    },
    
    // Begin Ajax request based off of the href of the clicked linked
    loadInfo: function() {
	if (this.content != null)
	new Ajax.Request(this.content, {method: 'post', parameters: "", 
					onComplete: this.processInfo.bindAsEventListener(this)
		    // if (this.callback != null) this.callback();
	
	    });
    },

    // update dialog content with new ajax url request
    update: function(url) {
	this.content = url;
	this.loadInfo();
    },

    // apply the callback
    callback: function() {
	if (this.callback != null) this.callback.call();
    },
    
    // Display Ajax response
    processInfo: function(response){
	this.setContent(response.responseText);
    },

    setContent: function(text){ 
	$('lbContent').update(text);	
	$('dialog').className = "done";
	this.actions();
	$$('.exit').each(function(elem) {
		Event.observe(elem, 'click', dialog.deactivate.bindAsEventListener(dialog), false);
	    });
	this.initTiny();
	this.centerBox();
	if (Prototype.Browser.IE) this.centerBox();
	this.callback();
    },
    
    // Search through new links within the dialog, and attach click event
    actions: function(){
	$$('.lbAction').each(function(elt) {
		Event.observe(elt,'click',this[elt.rel].bindAsEventListener(this), false);
		elt.onclick = function(){return false;};
	    });	
    },
	
    // Example of creating your own functionality once dialog is initiated
    insert: function(e){
	link = Event.element(e).parentNode;
	Element.remove($('lbContent'));	
	new Ajax.Request(link.href, {method: 'post', 
		    parameters: "", 
		    onComplete: this.processInfo.bindAsEventListener(this)});	
    },
	
    deactivate: function(){
	if (Prototype.Browser.IE) {
	    // this.setScroll(0,this.yPos);
	    this.prepareIE("auto", "auto");
	    this.hideSelects("visible");
	}
	this.unloadTiny();
	this.displayDialog("none");
	$('dialogOverlay').remove();
	$('dialog').remove();
    }
}


