/*
 * @author Luis Merino
 * @copyright author
 * 
 * @created Sat Feb 20 01:50:45 CET 2010
 */

(function(){
	
	var instance = null;
		
	var Zenbox = this.Zenbox = new Class({
	
		Implements: [Options, Events],
		
		options: {
			show_tab: true,
			tab: ''
		},
		
		initialize: function(options){
			if (instance) return instance;
			instance = this;
			this.setOptions($merge(Zenbox.params, options));
			this.build();
		},
		
		build: function(){
			this.client_url = this.options.url;
			this.container = new Element('div', {'id': 'zenbox-overlay', 'styles': {'display': 'none'}}).inject(document.body);
			if (this.options.show_tab) {
				new Element('a', {
					'href': '#',
					'id': 'zenbox-tab',
					'events': {
						'click': this.render.bindWithEvent(this)
					},
					'styles': {
						'background-image': 'url("http://assets0.zendesk.com/external/zenbox/images/tab_'+this.options.tab_id+'.png")',
						'background-color': this.options.tab_color,
						'border-color': this.options.tab_color,
						'background-position': '-10px',
						'opacity' : 0.8
					}
				}).inject(document.body);
			} else {
				document.id(this.options.tab).addEvent('click', this.render.bindWithEvent(this));
			}
			this.fireEvent('onBuild');
		},
		
		render: function(event){
			event.stop();
			// temp fix
			return document.location.href = 'http://support.vadingo.com/anonymous_requests/new';
			this.fireEvent('onRender');
			
			if (!this.iframeUrl) {
				if ($(this.options.loggedout)) {
					this.options.tags.push('non-member');
				} else if ($(this.options.loggedin)) {
					var permission = document.id(this.options.loggedin).get('rel') || 'non-member';
					this.options.tags.push(permission);
				}
				this.options.tags.push(MooTools.lang.getCurrentLanguage());

				this.iframeUrl = /*URI.base.valueOf() + 'zenbox?';*/'http://' + this.client_url + '/external/zenbox/index?x=5';
				this.options.tags && (this.iframeUrl += '&set_tags=' + this.options.tags.join(','));
				this.options.email && (this.iframeUrl += '&set_email=' + this.options.email);
				this.options.subject && (this.iframeUrl += '&set_subject=' + this.options.subject);
				this.options.subject_header && (this.iframeUrl += '&set_subject_header=' + this.options.subject_header);
				this.options.email_header && (this.iframeUrl += '&email_header=' + this.options.email_header);
				location.href && (this.iframeUrl += '&page=' + location.href);
				encodeURI(this.iframeUrl);
			}
			SqueezeBox.open(this.iframeUrl, {handler: 'iframe', size: {x:590, y:400}});
		}
		
	});

	$extend(Zenbox, {
		params: {
			tab_id: 'support',
			tab_color: '#000',
			title: 'The vadingo helpdesk',
			text: 'How may we help you? Please fill in details below, and we\'ll get back to you as soon as possible.',
			tags: ['website'],
			url: 'vadingo.zendesk.com',
			loggedout: 'login',
			loggedin: 'profile'
		}
	});
	
})();