// JQuery Extensions:
jQuery.extend(jQuery.easing, {
  easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
  }
});
 
// All page initialization goes inside here:
var lightbox;
 
// Lightbox
function Lightbox ()
{
	this.settings = {
		height: 220,
		width: 265,
		defaults: {
			height: 220,
			width: 265
		},
		hasOverlay: true,
		lbID: "lightbox",
		lbContainerID: "lightbox-container",
		lbContentID: "lightbox-content",
		lbOverlayID: "lightbox-overlay",
		overlayHTML: "\
		<div id=\"lightbox-overlay\"></div> \
		",
		html: "\
		<div id=\"lightbox\"> \
			<div id=\"lightbox-container\"> \
				<div id=\"lightbox-content\"> </div> \
			</div> \
		</div> \
		",
		initialized: false,
		movementAmount: 0,
		easing: "easeOutCubic"
	};
	this.prevDimensions = {};
	this.current = {};
	this._instance = null;
}

Lightbox.prototype._create = function (text, options)
{
	this._init();
	$("#"+this.settings.lbContentID).html(text);
	this.open(options);
};
Lightbox.prototype.load = function (url, options)
{
	var mylb = this;
	
	if (typeof this.settings.loading != "undefined")
	{
		this.settings.loading(url, options);
	}
	
	$.get(url, function(data) {
    	mylb._create(data, options);
    });
    
};
Lightbox.prototype._init = function ()
{
	$("#lightbox").stop();
		
	if (!this.settings.initialized)
	{
		this.initialize();
	}
};
Lightbox.prototype.initialize = function ()
{
	if (this.settings.hasOverlay) {
		$("body").append(this.settings.overlayHTML);	
	} 

	$("body").append(this.settings.html);

	// Set all the default settings.
	$("#lightbox").css({
		display: "none",
		position: "absolute",
		left: 0,
		top: 0,
		width: (this.settings.height) ? this.settings.height : this.settings.defaults.height,
		height: (this.settings.width) ? this.settings.width : this.settings.defaults.height
	});
	
	this.center();
	this.onComplete();
	if (this.settings.hasOverlay) {
		$(window).resize(this.centerAll);
		$(window).scroll(this.centerAll);
	} else {
		$(window).resize(this.center);
		$(window).scroll(this.center);
	}
	
	this.settings.initialized = true;
};

Lightbox.prototype.visible = function ()
{
	return !this._hidden();
};
Lightbox.prototype._hidden = function ()
{
	return $("#lightbox").css("display") == "none" || $("#lightbox").css("opacity") == 0;
};
Lightbox.prototype.open = function (options)
{
	this._init();
	if (options)
		options = this._parseOptions(options);
	else if (this.settings.width && this.settings.height) 
		options = this._parseOptions({width: this.settings.width, height: this.settings.height});
	else 
		options = this._parseOptions({width: this.settings.defaults.width, height: this.settings.defaults.height});
	
	var movedOptions = {};
	// If the lightbox isn't visible, reset to the appropriate size.
	if (!this.visible()) 
	{
		movedOptions = {
			width: options.width - this.settings.movementAmount,
			height: options.height - this.settings.movementAmount
		};
		
		$("#lightbox").css({
			width: movedOptions.width+"px",
			height: movedOptions.height+"px"
		});
		
		this.center();
		
		$("#lightbox .filler").css({opacity:0});
	}
	
	this.show(options);
};
Lightbox.prototype.show = function(options)
{
	options = this._parseOptions(options);
	$("#campaign-spot").hide();
	options.opacity = 1;
	this.reposition(options);
	if (this.settings.hasOverlay) {
		$("#lightbox-overlay").show();
		$("#lightbox-overlay").click(function(){
			lightbox.close();
			$("#lightbox").hide();
			$("#xi-container").remove();
		});
	}
};
Lightbox.prototype.hide = function(options)
{
	$("#campaign-spot").show();
	options = this._parseOptions(options);
	options.opacity = 0;
	this.reposition(options);
	
	if (this.settings.hasOverlay)
		$("#lightbox-overlay").hide();
};
Lightbox.prototype.close = function ()
{
	this._init();
	
	this.prevDimensions = {
		width: this.current.width,
		height: this.current.height
	};
	
	this.hide({
		width: this.current.width - this.settings.movementAmount,
		height: this.current.height - this.settings.movementAmount
	});
	
};
Lightbox.prototype.onComplete = function()
{
	/*
	if (typeof this.prevDimensions != "undefined")
	{
		$("#lightbox").css({
			width: this.prevDimensions.width+"px",
			height: this.prevDimensions.height+"px"
		});
		this.center();
		this.prevDimensions = undefined;
	}
	*/
	this.current = {
		width: $("#lightbox").width(),
		height: $("#lightbox").height()
	};
	
	if ($("#lightbox").css("display") == "block" && $("#lightbox").css("opacity") == 0)
	{
		$("#lightbox").css("display", "none");
	}
	
	// Nudge IE's memory... of course.
	if ($.browser.msie)
	{
		$("#lightbox"+" .filler").css("opacity", "hide");
		$("#lightbox"+" .filler").css("opacity", "show");
	}
};

Lightbox.prototype.reposition = function(options, force)
{
	$("#lightbox").stop();
	
	if (this.settings.hasOverlay) {
		lightbox.centerOverlay();
	}
	
	// should not use lightbox variable
	var center = lightbox.centerOptions(options);
	
	var newOptions = {
		"top": center.top+"px",
		"left": center.left+"px",
		width: options.width+"px",
		height: options.height+"px"
	};
	
	var mylb = this;
	
	if (options.opacity >= 0)
		newOptions.opacity = options.opacity;
		
	if (force)
	{
		$("#lightbox").css(newOptions);
		this.onComplete();
	}
	else 
	{
		if (this.visible())
		{
			if (newOptions.opacity == 0)
			{
				$("#lightbox .filler").animate({opacity: 0}, 150, mylb.settings.easing, function()
				{
					$("#lightbox").animate(newOptions, 200, mylb.settings.easing, mylb.onComplete);
				
				});
			}
			else
			{
				$("#lightbox").animate(newOptions, 200, mylb.settings.easing, mylb.onComplete);
				
			}
		}
		else
		{
			$("#lightbox").animate(newOptions, 200, mylb.settings.easing, function()
			{
				$("#lightbox .filler").animate({opacity: 1}, 150, mylb.settings.easing, mylb.onComplete);
			
			});
		}
	}
		
};
Lightbox.prototype.centerAll = function () 
{
	lightbox.center();
	lightbox.centerOverlay();
}
Lightbox.prototype.centerOverlay = function()
{
	var opts = lightbox.centerOverlayOptions();
	$("#lightbox-overlay").css({
		"top":opts.top+"px",
		"left":"0px"
	});
};
Lightbox.prototype.centerOverlayOptions = function()
{
	var opts = {};
	//var width = $("lightbox-overlay").width();
	//var height = $("lightbox-overlay").height();
	
	opts.top = $(window).scrollTop();
	opts.left = 0;
	opts.width = $(window).width();
	opts.height = $(window).height();
	
	return opts;
};
Lightbox.prototype.center = function (options, instance)
{
	// should not use lightbox variable
	var opts = lightbox.centerOptions(options);
	$("#lightbox").css({
	 	'top':opts.top+"px",
		"left":opts.left+"px"
	});
}
Lightbox.prototype.centerOptions = function (options)
{
	var opts = {};
	var width = $("#lightbox").width();
	var height = $("#lightbox").height();
	
	if (options && options.width && options.height)
	{
		width = options.width;
		height = options.height;
	}
	
	opts.top = Math.floor($(window).height()/2 - height/2 + $(window).scrollTop());
	opts.left = Math.floor($(window).width()/2 - width/2);
	
	return opts;
};
Lightbox.prototype._parseOptions = function (options)
{
	if (options == {} || options == undefined)
	{
		options = {};
		options.width = this.current.width;
		options.height = this.current.height;
	}
	else 
	{
		if (options.width == "same" || options.width == undefined) 
			options.width = this.current.width;
			
		if (options.height == "same" || options.height == undefined)
			options.height = this.current.height;
			
		if (options.width < 0)
			options.width = 0;
			
		if (options.height < 0)
			options.height = 0;
	}
	
	return options;
};

var qs;

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

$(document).ready(function()  
{
	lightbox = new Lightbox();
	lightbox._instance = lightbox;
	
	qs = new Querystring();
	if (qs.contains("autoplay")) {
		if (qs.get("autoplay","0") == "1") {
			lightbox.load('/common/forms/xi/xi-video.html', { width: 901, height: 572 });
		}
	}
});	
