/* Global Domready Event
------------------------------------------------------------------------------------*/
window.addEvent('domready', function(){
	modal = new ModalHandler();
});


/* sIFR
------------------------------------------------------------------------------------*/
var ChaletParis60 = {
  src: '/swf/ChaletParis1960.swf'
};
sIFR.activate(ChaletParis60);

var ChaletLondon60 = {
  src: '/swf/ChaletLondon1960.swf'
};
sIFR.activate(ChaletLondon60);

var ChaletNY60 = {
  src: '/swf/ChaletNewYork1960.swf'
};
sIFR.activate(ChaletNY60);

sIFR.replace(ChaletParis60, {
  selector: '#lgbt-hdr-area h1',
  wmode: 'transparent',
  css: [
      '.sIFR-root { font-size: 16px; color: #8ddae0; letter-spacing:1; height: 20px; leading: 8px; }'
  ],
  preventWrap: false,
  fitExactly: true,
  tuneHeight: -2
});
sIFR.replace(ChaletParis60, {
  selector: '#lgbt-hdr-area2 h1',
  wmode: 'transparent',
  css: [
      '.sIFR-root { font-size: 16px; color: #8ddae0; letter-spacing:1; }'
  ],
  preventWrap: false,
  fitExactly: true,
  tuneHeight: -2
});
sIFR.replace(ChaletParis60, {
  selector: '.lgbt-out-row h3',
  wmode: 'transparent',
  css: [
      '.sIFR-root { font-size: 24px; color: #ffffff; letter-spacing:2; }'
  ],
  preventWrap: false,
  fitExactly: true,
  tuneHeight: -2
});
sIFR.replace(ChaletLondon60, {
  selector: '.lgbt-fabulous-rowhdr h2',
  wmode: 'transparent',
  css: [
      '.sIFR-root { font-size: 15px; color: #8ddae0; }'
  ],
  preventWrap: false,
  fitExactly: true,
  tuneHeight: -4
});
sIFR.replace(ChaletNY60, {
  selector: '.community-org-name',
  wmode: 'transparent',
  css: [
      '.sIFR-root { font-size: 12px; color: #8ddae0; text-transform:uppercase; }'
  ],
  preventWrap: false,
  fitExactly: true,
  tuneHeight: -8
});
sIFR.replace(ChaletNY60, {
  selector: '.bars-name',
  wmode: 'transparent',
  css: [
      '.sIFR-root { font-size: 11px; color: #8ddae0; text-transform:uppercase; }'
  ],
  preventWrap: false,
  fitExactly: true,
  tuneHeight: -7
});
sIFR.replace(ChaletNY60, {
	selector: '.event-date .event span.event-title',
	wmode: 'transparent',
	css: [								  
		'.sIFR-root { font-size: 18px; color: #8ddae0; leading:4px; }'
	],
	preventWrap: false,
	fitExactly: true,
	tuneHeight: -2
});
sIFR.replace(ChaletLondon60, {
	selector: '.lgbt-calendar-month',
	wmode: 'transparent',
	css: [
		'.sIFR-root { font-size: 18px; color: #ffffff; text-transform:uppercase; }'
	],
	preventWrap: false,
	fitExactly: true,
	tuneHeight: -6
});
sIFR.replace(ChaletNY60, {
	selector: '.lgbt-calendar-event h5',
	wmode: 'transparent',
	css: [								  
		'.sIFR-root { font-size: 18px; color: #8ddae0; leading:7px; }'
	],
	preventWrap: false,
	fitExactly: true,
	tuneHeight: -4
});


/* Global Classes
------------------------------------------------------------------------------------*/
var Modal = new Class({
	Implements: [Events, Options],
	options: {
		overlayId: 'modal-overlay',
		modalId: 'modal-window',
		hasClose: true,
		closeClass: 'close',
		closeHtml: '',
		clickToClose: true,
		transitionDuration: 250,
		onShowComplete: Class.empty,
		onHideComplete: Class.empty,
		onUpdateComplate: Class.empty
	},
	initialize: function(options){
		this.setOptions(options);
		
		// create overlay element
		this.overlay = new Element('div').setProperty('id', this.options.overlayId).inject(document.body);
		if(this.options.clickToClose){
			// add a pointer on the overlay if click to close is set
			this.overlay.setStyle('cursor', 'pointer');
		}
		
		// create modal window
		this.modal = new Element('div').setProperty('id', this.options.modalId).injectAfter(this.overlay);
		if(this.options.hasClose){
			// create close button if option set
			this.modalClose = new Element('div').addClass(this.options.closeClass).set('html', this.options.closeHtml).inject(this.modal);
		}
		this.modalContent = new Element('div').addClass('content').inject(this.modal);
		
		// create effects objects
		this.modal.fx = new Fx.Tween(this.modal, {
			property: 'opacity',
			duration: this.options.transitionDuration
		});
		this.overlay.fx = new Fx.Tween(this.overlay, {
			property: 'opacity',
			duration: this.options.transitionDuration
		});
		this.overlay.fx.set(0);
		
		this.isOpen = false;
		
		this.attach();
	},
	attach: function(){
		var self = this;
		
		if(this.options.clickToClose){
			this.overlay.addEvent('click', function(){
				self.hide();
			});
		}
		
		window.addEvent('resize', function(){
			if(self.isOpen){
				self.positionOverlay();
				self.positionModal();
			}
		});
		
		// attach event if option set for close
		if(this.options.hasClose){
			this.modalClose.addEvent('click', function(){
				self.hide();
			});
		}
	},
	show: function(content){
		// modal is not open yet
		if(!this.isOpen){
			this.isOpen = true;
			
			this.modal.fx.set(0);
			this.modal.setStyle('display', 'block');
			// inject content in to modal window
			content.inject(this.modalContent);
			
			// fix for ie6 select boxes showing through and safari 2 flash showing through
			if(window.ie6)
				$$('select').setStyle('visibility', 'hidden');
			else if(window.webkit419)
				$$('embed').setStyle('visibility', 'hidden');
			
			// show/position and fade in overlay
			this.overlay.setStyle('display', 'block');
			this.positionOverlay();
			this.overlay.fx.start(0.6).chain(function(){
				this.positionModal();
				this.modal.fx.start(1).chain(function(){
					this.fireEvent('onShowComplete');
				}.bind(this));
			}.bind(this));
		}
		// modal is already open so we just need to update and reposition
		else{
			this.modal.fx.start(0).chain(function(){
				this.modalContent.empty();
				content.inject(this.modalContent);
				this.positionModal();
				this.modal.fx.start(1).chain(function(){
					this.fireEvent('onShowComplete');
					this.fireEvent('onUpdateComplete');
				}.bind(this));
			}.bind(this));
		}
	},
	hide: function(){
		if(this.isOpen){
			this.isOpen = false;
			this.modal.fx.start(0).chain(function(){
				this.modal.setStyles('display', 'none');
				this.overlay.fx.start(0).chain(function(){
					// fix for ie6 select boxes showing through and safari 2 flash showing through
					if(window.ie6)
						$$('select').setStyle('visibility', 'visible');
					else if(window.webkit419)
						$$('embed').setStyle('visibility', 'visible');
					this.overlay.setStyle('display', 'none');
					this.modalContent.empty();
					this.fireEvent('onHideComplete');
				}.bind(this));
			}.bind(this));
		}
	},
	positionOverlay: function(){
		this.overlay.setStyles({
			'height': window.getScrollHeight(),
			'width': window.getWidth()
		});
	},
	positionModal: function(){
		var modalSize = this.modal.getSize();
		var left = (window.getScrollLeft() + (window.getWidth() - modalSize.x)/2);
		var top = (window.getScrollTop() + (window.getHeight() - modalSize.y)/2);
		if(top < 0)
			top = 0;
		this.modal.setStyles({
			'top': top,
			'left': left
		});
	}
});

var ModalHandler = new Class({
	Implements: [Events, Options],
	options: {
		downloadUrl: '/vegas/features/gay-travel/download.jsp'
	},
	initialize: function(options){
		this.setOptions(options);
		
		this.modal = new Modal();
		
		this.attach();
	},
	attach: function(){
		// event delegation for opener links
		$(document.body).addEvent('click', function(e){
			var target = $(e.target);
			if(target.get('tag') == 'a'){				
				if(target.get('id') == 'lnk-download'){
					e.preventDefault();
					this.showDownload();
				}
			}
		}.bind(this));
	},	
	showDownload: function(){
		// pull in the news list
		var request = new Request.HTML({
			onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript){
				// show in modal window
				this.modal.show(responseElements[0]);				
			}.bind(this)
		});
		request.get(this.options.downloadUrl);
	}
});
/* End Modal window */

/*
 * This is a patch for Mootools Request.HTML
 *
 * If you use Request.HTML to load some data containing any HTML special chars
 * like &nbsp; or &laquo; it won't work with Webkit based browsers.
 */
Request.HTML.implement({
	processHTML: function(text){
		var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
		text = (match) ? match[1] : text;
		
		var container = new Element('div');
		
		return $try(function(){
			var root = '<root>' + text + '</root>', doc;
			if(Browser.Engine.trident){
				doc = new ActiveXObject('Microsoft.XMLDOM');
				doc.async = false;
				doc.loadXML(root);
			}else{
				doc = new DOMParser().parseFromString(root, 'text/html');
			}
			root = doc.getElementsByTagName('root')[0];
			for(var i = 0, k = root.childNodes.length; i < k; i++){
				var child = Element.clone(root.childNodes[i], true, true);
				if(child) container.grab(child);
			}
			return container;
		}) || container.set('html', text);
	}
});