var swiffy;

function openContact() {
	hideh1s();
	var box = new MooBox();
	box.setOptions({	
		template: 'contact'
	});
	box.show();
}
		

function closeBox() {
	showh1s();
	if(swiffy) {
		swiffy.box.setStyles({ border: ''	});
		swiffy.box.innerHTML = "";
		swiffy.box = null;
		swiffy = null;
	}
	new MooBox().close();
}
/**
 * Function converts class into a Singleton
 *
 * Usage: MooBox.toSingleton(); // where MooBox is my class to be used as a singleton
 *
 * Based http://forum.mootools.net/viewtopic.php?id=6409 about Singleton Classes in Mootools way
 * Thank you nwhite, http://www.nwhite.net/
 */
Class.prototype.toSingleton = function(){
	var p = this.prototype;
	var instance = undefined;
 
	if($defined(p.initialize) && $type(p.initialize) == 'function') var init = p.initialize;
 
	p.initialize = function(){
		if(!$defined(instance)){
			if($defined(init) && $type(init) == 'function') init.apply(this,arguments);
			instance = this;
		}
		return instance;	
	}
};

/**
 * Moobox class
 * 
 * Events: onComplete
 */
var MooBox = new Class({
	Implements: [Options, Events],
	timerCenter: null,
	elemFilter: null,
	elemBox: null,
	options: {
		debug: true,
		template: '',
		params: ''
	},
	initialize: function(options) {

		this.log('Initialising the box');
		this.setOptions(options);
	},
	show: function() {		
		this.log('Showing AdvantageUpload');
		// hide invalid elements
		var elems = $$("OBJECT");
		for(i=0; i<elems.length; i++) {
			try {
				if($(elems[i].id)) $(elems[i]).addClass('hide_under_moobox');
			} catch (e) {
				// ignore : Hack for IE causing problems with Swiff objects...
			}			
		}
		
		// Load content and show
		this.create();
		this.load();
		this.centerBox();
		this.test = 10;
		this.timerCenter = setInterval(this.centerBox.bind(this),1000);
	},
	create: function() {
		this.log('Creating the box');
		this.filter = $('cool_filter');
		this.filter.setStyle('top','0px');
		this.filter.setStyle('height',document.body.offsetWidth+"px");
		var ef_one = new Fx.Morph(this.filter, {duration: 300, transition: Fx.Transitions.Sine.easeIn});
 		ef_one.start({'opacity': [0, .9] });

		this.elemBox = $('cool_box');
		this.elemBox .innerHTML = "";
		this.elemBox.setStyle('top','0px');
		this.elemBox.setStyle('height','auto');
		var ef_two = new Fx.Morph(this.elemBox, { duration: 600, transition: Fx.Transitions.Sine.easeIn});
 		ef_two.start({'opacity': [0, 1] });
	
 		var url = "templates/" + this.options.template + ".php";

 		if(this.options.params) url = url + "?" + this.options.params;
	
	},
	onStartLoad: function() {
		this.elemBox.innerHTML = "<img src='images/ajax_working.gif' />";
	},
	onGetCompleted: function() {
		this.fireEvent("loaded");
	},
	centerBox: function(){
		// center box horizontally		
		var halfboxWidth = this.elemBox.getStyle('width').toInt() / 2;
		var browserWidth = document.body.offsetWidth;
		var targetWidth = (browserWidth / 2) - halfboxWidth;
		var ef_three = new Fx.Morph(this.elemBox, { duration: 100, transition: Fx.Transitions.Quad.easeOut});
 		ef_three.start({'left': targetWidth + "px"  });
	},
	load: function(options) {
		this.setOptions(options);
		this.log('Loading box content');
		
		// create url and amend params
		var url = "templates/" + this.options.template + ".php";
 		if(this.options.params) url = url + "?" + this.options.params + "&date" + Date();
	
		var req = new Request.HTML({  
			method: "get",   
			url: url,  
			data: null,	
			update: this.elemBox,
			onComplete: this.onGetCompleted.bind(this)
		});		
		//req.addEvent('complete', this.onGetCompleted.bind(this));
		req.addEvent('request', this.onStartLoad.bind(this));
		req.send();
	},
	clear: function() {
		var elems = $$("OBJECT");
		for(i=0; i<elems.length; i++) {
			try {
				$(elems[i].id).removeClass('hide_under_moobox');					
			} catch (e) {
				// ignore
			}				
		}
		this.elemBox.innerHTML = "";
		
	},
	close: function(){
		this.log('Closing box');		
		// show elements
		// close
		clearInterval(this.timerCenter);
		
		setTimeout(  this.clear.bind(this), 900);
		var ef_one = new Fx.Morph(this.filter, {duration: 600, transition: Fx.Transitions.Sine.easeIn});
		ef_one.start({'opacity': [.9, 0] });
		
		var ef_two = new Fx.Morph(this.elemBox, { duration: 300, transition: Fx.Transitions.Sine.easeIn});
		ef_two.start({'opacity': [1, 0] });
	},	
	postForm: function(frmID){
		this.log('Posting form');		
		var elemForm = $(frmID)
		var postString = "";

		for(i=0; i<elemForm.elements.length; i++){
			if(elemForm.elements[i].type == "checkbox") {
				if(elemForm.elements[i].checked) postString += (elemForm.elements[i].id + "=" + elemForm.elements[i].value + "&");			
			} else {
				if(elemForm.elements[i].type == "textarea") {
					postString += (elemForm.elements[i].id + "=" + URLEncode(elemForm.elements[i].value) + "&");
				} else {
					postString += (elemForm.elements[i].id + "=" + URLEncode(elemForm.elements[i].value) + "&");
				}
				

			}
		}
		this.log('Post ajax ' + postString);
		var req = new Request.HTML({  
			method: "post",  
			url: elemForm.action,  
			data: postString,
			update: this.elemBox
			}).send();  
		//req.addEvent('complete', this.onGetCompleted.bind(this));
		//req.addEvent('request', this.onStartLoad.bind(this));	
	},
	log: function(text, args) {
		if (this.options.debug && window.console) console.log(text.substitute(args || {}));
	}	
});

// Use the helping function to convert this call into a Singleton
MooBox.toSingleton();









