(function( $ ){

	var defaults = {
		'id':		'',
		'height':	450,
		'width':	600
	};

	var methods = {

		init: function(options) {
			return this.each(function(){

				var $this = $(this);

				if(typeof options == 'undefined') {
					options = defaults;
					options.id = $this.attr('id');
				}
				$this.options = $.extend(defaults, options)

				//Fade in Background
				$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
				$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies

				methods.open($this.options);

				//Set up closing callback
				$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...

					// handle any necessary on close events (e.g. stop video if playing) - see your resource partial.phtml and js for details
					$(this).parent('.poplight-block').trigger('popupOnClose').removeClass('poplight-open')

					$('#fade , .poplight-block').fadeOut('fast', function() {
						$('#fade, a.close').remove();  //fade them both out
					});

					return false;
				});
			});
		},

		open: function(options) {

			var popup = $('#' + options.id);  // save from having to run lots of jqueries

			// call on open callback if exists - this has to be the poplight-block class (the opened item) - TODO this needs to be more generic
			popup.trigger('popupOnOpen');

			//Fade in the Popup and add close button
			popup.css({ 'width': Number(options.width), 'height': Number(options.height) }).prepend('<a href="#" class="close"><img src="/icons/close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
			//popup.fadeIn().css({ 'width': Number(options.width), 'height': Number(options.height) }).prepend('<a href="#" class="close"><img src="/icons/close.png" class="btn_close" title="Close Window" alt="Close" /></a>');

			//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
			var popMargTop = (popup.height() + 80) / 2;
			var popMargLeft = (popup.width() + 80) / 2;

			//Apply margin to popup and fade in
			popup.css({
				'margin-top' : -popMargTop,
				'margin-left' : -popMargLeft
			}).fadeIn().addClass('poplight-open');
		},

		replace: function(options) {
			return this.each(function(){
				var $this = $(this);

				if(typeof options == 'undefined') {
					options = defaults;
					options.id = $this.attr('id');
				}
				$this.options = $.extend(defaults, options);

				$('.poplight-open').trigger('popupOnClose').fadeOut('fast').removeClass('poplight-open');

				methods.open($this.options);
			});
		},
		destroy: function( ) {  }
	};

	$.fn.poplight = function(method) {
		if(methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if(typeof method === 'object' || ! method ) {
			return methods.init.apply(this, arguments);
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.poplight' );
		}
	};
})( jQuery );



$(document).ready(function() {
	//When you click on a link with class of poplight and the href starts with a #
	$('a.poplight[href^=#]').live('click', function() {

		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size

		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value
		if(dim[1]) {
			var popHeight = dim[1].split('=')[1]; //Gets the first query string value
		}

		$(this).poplight({
			'id': popID,
			'height': popHeight,
			'width': popWidth
		});

		return false;
	});
});  // end document ready

