/// <reference path="jquery/jquery.intellisense.js" />

function Popup()
{
	this.container = document.getElementById("popup");
	this.title = document.getElementById("popup-title");
	this.subtitle = document.getElementById("popup-subtitle");
	this.content = document.getElementById("popup-content");
	this.overlay = document.getElementById("dimmedOverlay");
	this.defaults = {
		cssClass: null,
		title: null,
		subtitle: null,
		content: null,
		showSubtitle: true,
		showNavigation: false
	};
	
	this.navigation = $(".nav", this.container);
	if (this.navigation.length > 0)
	{
		this.position = 0;
		this.count = 1;
		this.callbackObj = null;
		
		this.elemIndex = $(".index", this.navigation);
		this.elemCounter = $(".counter", this.navigation);

		$(".popup-prev", this.navigation).bind("click", this.onPreviousItem);
		$(".popup-next", this.navigation).bind("click", this.onNextItem);
	}
	
	$(".popup-close", this.container).each(function() {
		$(this).bind("click", Popup.close);
	});
}

/*
 * Event handler for when the previous item should be displayed.
 */
Popup.prototype.onPreviousItem = function()
{
	var popup = Popup.instance;
	
	popup.position--;
	if (popup.position < 0)
		popup.position = popup.count -1;

	popup.updateNavigation(true);
	return false;
}

/*
 * Event handler for when the next item should be displayed.
 */
Popup.prototype.onNextItem = function()
{
	var popup = Popup.instance;
	
	popup.position++;
	if (popup.position >= popup.count)
		popup.position = 0;

	popup.updateNavigation(true);
	return false;
}

/*
 * Updates the navigation. Displays the current item index and the total number of items.
 * @allowCallback: specifies if the callbackObj (if present) should be invoked.
 */
Popup.prototype.updateNavigation = function(allowCallback)
{
	var index = this.position;

	this.elemIndex.html(index +1);
	this.elemCounter.html(this.count);
	
	if (this.callbackObj && allowCallback === true)
		this.callbackObj.show(index);
}

/*
 * Opens the popup.
 * @title: specifies the title of the popup.
 * @content: specifies the content of the popup.
 * @showNavigation: specifies if the navigation should be displayed.
 */
Popup.open = function(options)
{
	var popup = Popup.instance;
	var settings = jQuery.extend({}, popup.defaults, options);
	
	if (settings.title)
		$(popup.title).html(settings.title);
	if (settings.subtitle)
		$(popup.subtitle).html(settings.subtitle);
	if (settings.content)
		$(popup.content).html(settings.content);
	
	var cssClass = "popup dBlock73 " + settings.cssClass;
	$(popup.container).attr("class", cssClass);
	
	$(popup.container).removeClass("hide");
	$(popup.overlay).removeClass("hide");

	if (settings.showSubtitle !== true)
		$(popup.subtitle).addClass("hide");
	else
		$(popup.subtitle).removeClass("hide");

	if (settings.showNavigation !== true)
		popup.navigation.addClass("hide");
	else
	{
		popup.navigation.removeClass("hide");
		popup.updateNavigation();
	}

	return false;
}

/*
 * Set the navigation properties for the popup.
 * @position: specifies the zero-based index of the current item.
 * @count: specifies the total number of items.
 * @callbackObj: specifies the object that is called to update the popup when the current item changes.
 */
Popup.setNavigation = function(position, count, callbackObj)
{
	var popup = Popup.instance;
	popup.position = position;
	popup.count = count;

	if (callbackObj)
		popup.callbackObj = callbackObj;
	else
		popup.callbackObj = null;
}

/*
 * Hides the popup.
 */
Popup.close = function()
{
	var popup = Popup.instance;
	$(popup.container).addClass("hide");
	$(popup.overlay).addClass("hide");
	return false;
}

Popup.instance = null;
Popup.setup = function() {
	if (Popup.instance == null)
		Popup.instance = new Popup();
}

$(document).ready(Popup.setup);