// JavaScript Document
var Lightbox = new Class({

	initialize: function(id,aClassName,defaultScKey,defaultTitle) {
		this.id = id;
		this.lb = $(id);
		this.aClassName = $pick(aClassName,'lb-anchor');
		this.defaultScKey = $pick(defaultScKey,'bundleMyServices');
		this.defaultTitle = $pick(defaultTitle,'View Deals in Your Area');
		
		// make sure lightbox has absolute positioning for IE6, and fixed for all other browsers
		lbPosition = (window.ie6) ? 'absolute' : 'fixed';
		this.lb.setStyles({
			position : lbPosition,
			opacity: 0
		});
		
		$each($$('.' + aClassName), function(el){
			el.onclick = this.setLightBox.pass(el,this);
		}, this);
		
		myLinks = this.lb.getElementsByTagName('a');
		$each(myLinks, function(el) { 
			if (el.rel == "hide") {
				el.onclick = this.hideLightBox.pass(el,this);
			}
		}, this);
		//create overlay, set up onclick event to hide lightbox
		this.idOverlay = "lbOverlay-" + this.id;
		this.overlay = new Element('div', {'id': this.idOverlay, 'styles' : {'position': 'fixed','z-index': '999','left': '0','width': '100%','background-color': '#000','cursor': 'pointer','opacity': '0','display': 'none','top': '0','height': '100%'}}).injectInside(document.body);
		if (window.ie6) {
			this.overlay.setStyles({
				position: 'absolute',
				width: window.getWidth().toInt() + 'px',
				height: window.getHeight().toInt() + 'px'
			});
		}

		this.overlay.onclick = this.hideLightBox.pass(this.overlay,this);
		
		// effects for overlay and lightbox
		this.fxOverlay = new Fx.Style(this.overlay, 'opacity', {duration:1000});
		this.fxLB = new Fx.Style(this.lb, 'opacity', {duration:1000});
	},
	
	setLightBox: function(el) {
		// setup form, and position and show the lightbox
		this.setLBForm(el);
		this.showLightBox();				
		return false;
	},
	
	showLightBox: function() {
		this.positionLightBox();
		this.lb.setStyle('display','block');
		this.fxLB.start(0,1);
		
		//position and show the overlay (only need to position for IE6)
		if (window.ie6) this.overlay.setStyle('top',window.getScrollTop() + 'px');
		this.overlay.setStyle('display','block');
		this.fxOverlay.start(0,.8);	
	},
	
	setLBForm: function(el) {
		lbVars = this.getLBVars(el);
		
		lbTitle = ("title" in lbVars) ? lbVars['title'] : this.defaultTitle;
		
		ids = '#' + this.id + ' .lb-title';
		el = $$(ids);
		if (el.length > 0) {
			el[0].innerHTML = lbTitle;
		}

		// find a form, inside the lightbox - form has class name lb-form
		ids = '#' + this.id + ' .lb-form';
		// make sure there's a form in the lightbox
		if (this.lbForm = $$(ids)[0]) {
			// Update hidden form elements.  There should be an scKey along with either a
			// scpId/package ID or spId/keyword pair.  
			
			//Refresh all related hidden inputs before updating with new values.
			if ($(this.id + '-scKey')) $(this.id + '-scKey').remove();
			if ($(this.id + '-scpId')) $(this.id + '-scpId').remove();
			if ($(this.id + '-packageID')) $(this.id + '-packageID').remove();
			if ($(this.id + '-keyword')) $(this.id + '-keyword').remove();
			if ($(this.id + '-spId')) $(this.id + '-spId').remove();

			// if scKey is found in href, use it, otherwise use default scKey
			lbScKey = ("scKey" in lbVars) ? lbVars['scKey'] : this.defaultScKey;
			this.scKeyInput = new Element('input', {
				'name': 'scKey',
				'type': 'hidden',
				'value': lbScKey,
				'id': this.id + '-scKey'
			}).injectInside(this.lbForm);

			if ("scpId" in lbVars) {
				this.scpIdInput = new Element('input', {
					'name': 'scpId',
					'type': 'hidden',
					'value': lbVars['scpId'],
					'id': this.id + '-scpId'
				}).injectInside(this.lbForm);	
			}
			if ("packageID" in lbVars) {
				this.packageIDInput = new Element('input', {
					'name': 'packageID',
					'type': 'hidden',
					'value': lbVars['packageID'],
					'id': this.id + '-packageID'
				}).injectInside(this.lbForm);	
			}
			if ("keyword" in lbVars) {
				this.keywordInput = new Element('input', {
					'name': 'keyword',
					'type': 'hidden',
					'value': lbVars['keyword'],
					'id': this.id + '-keyword'
				}).injectInside(this.lbForm);	
			}
			if ("spId" in lbVars) {
				this.spIdInput = new Element('input', {
					'name': 'spId',
					'type': 'hidden',
					'value': lbVars['spId'],
					'id': this.id + '-spId'
				}).injectInside(this.lbForm);	
			}
		}	
	},
	
	positionLightBox: function() {
		// position the lightbox, if no value found for lightbox height, use 250 as a default
		var lbHeight = (this.lb.getStyle('height').toInt() > 0) ? this.lb.getStyle('height').toInt() : 350;
		// set lbTop differently for IE6, which uses position:absolute instead of position:fixed
		lbTop = (window.ie6) ? window.getScrollTop() + ((window.getHeight() - lbHeight)/2) : (window.getHeight() - lbHeight)/2;
		this.lb.setStyle('top',lbTop.toInt() + "px");
		
		// assume that ligthbox has a given width
		var lbLeft = window.getScrollLeft() + ((window.getWidth() - this.lb.getStyle('width').toInt())/2);
		this.lb.setStyle('left',lbLeft.toInt() + "px");
	},
	
	hideLightBox: function() {
		this.fxLB.start(1,0);
		this.fxOverlay.start(.8,0);
		var displayNoneLB = function() {this.lb.setStyle('display','none'); }
		displayNoneLB.delay(1000,this);
		var displayNoneOverlay = function() {this.overlay.setStyle('display','none');}
		displayNoneOverlay.delay(1000,this);
		return false;
	},
	
	getLBVars: function(el) {
		lbVars = new Array();
		
		// check to make sure the url has a ?, and therefore has variables
		if (/\?/.test(el.href)) {
			varString = el.href;
			varString = varString.replace(/^[^\?]*\?/,'');
			allVars = varString.split('&');

			for (i=0; i<allVars.length; i++) {
				thisVar = allVars[i].split('=');
				k = thisVar[0];
				v = thisVar[1];
				v = this.urldecode(v);
				lbVars[k] = v;
			}
		}
		return lbVars;
	},
    
    urldecode : function ( str ) {
        // http://kevin.vanzonneveld.net
        // +   original by: Philip Peterson
        // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
        // *     example 1: urldecode('Kevin+van+Zonneveld%21');
        // *     returns 1: 'Kevin van Zonneveld!'
        
        var ret = str;
           
        ret = ret.replace(/\+/g, '%20');
        ret = decodeURIComponent(ret);
        ret = ret.toString();
     
        return ret;
    }
 
    
});