var hela_site = (hela_site = hela_site) || function()
{
	this.debug("new hela_site");
	this.init();
};
// constants
hela_site.EVENT_HASH_CHANGE = "hashchange";
hela_site.FADE_SPEED_SLOW = 400;
hela_site.FADE_SPEED_FAST = 200;
hela_site.FRAGMENTS_BASE_ROOT = "../";
hela_site.TRANSITION_FADE = "fade";
hela_site.INFO_PAGE = "";
// class-props
hela_site.debug = false;
hela_site.fadeSpeed = hela_site.FADE_SPEED_FAST;
hela_site.galleriaHeight = 650;
hela_site.galleriaWidth = 650;
hela_site.initialTransition = hela_site.TRANSITION_FADE;
hela_site.transition = hela_site.TRANSITION_FADE;
hela_site.fragmentsBaseRoot = hela_site.FRAGMENTS_BASE_ROOT;
hela_site.useZeroBasedIndexingOnly = false;
// instance props
hela_site.prototype.initialized = false;
hela_site.prototype.info = null;
hela_site.prototype.galleria = null;
hela_site.prototype.cache = null;

// methods
hela_site.prototype.init = function()
{
	this.debug("init");
	if(!this.initialized)
	{	
		this.info = $("#hela_site_info")[0] || null;
		this.galleria = $("#hela_site_galleria")[0] || null;
		this.cache = $("#hela_site_cache")[0] || null;
		var $this = this;
		$(window).bind(hela_site.EVENT_HASH_CHANGE, function(event)
		{
			$this.onHashChangeHandler(event);
		});
		$("ul.nav>li>a").bind("click", function(event)
		{
			$this.onClickHandler(event);
		});
		this.initialized = true;
		this.debug("initialized: "+this.initialized);
		$(window).trigger(hela_site.EVENT_HASH_CHANGE);
	};
};
hela_site.prototype.onClickHandler = function(event)
{
	event.preventDefault();
	$("ul.nav>li.active").removeClass("active");
	var li = $(event.target).parent();
	$(li).addClass("active");
	var hash = $(event.target).attr("href");
	this.pushState(hash);
};
//
hela_site.prototype.onHashChangeHandler = function(event)
{
	this.debug("onHashChangeHandler("+event.type+")");
	var fragment = event.fragment || "";
	var tokens = fragment.split("/");
	var page = tokens[0];
	var imageIndex = parseInt(tokens[1]);
	var defaultIndex = (hela_site.useZeroBasedIndexingOnly == true) ? 0 : 1;
	if(isNaN(imageIndex) || imageIndex < defaultIndex) 
	{
		imageIndex = defaultIndex;
	};
	// galleria is 0-based while hashes are 1 based
	// call image index via wrapper/converter
	this.switchToPage(page, this.hashToGalleriaIndex(imageIndex));
};
/**
*	switch site managed by this site
*	if page is empty - then show default element 
*	if page is not empty then check if page is already in cache
*	and if not load it via ajax fragments
*	if index is not -1 - then when galleria loads switch it or
*	if galleria is already here switch image index
*/
hela_site.prototype.switchToPage = function(pageName, imageIndex)
{
	this.debug("switchToPage("+pageName+", "+imageIndex+")");
	// navigation links
	$("ul.nav>li.active").removeClass("active");
	$("a[href$=\"#"+pageName+"\"]").parent().addClass("active");
	//
	if(pageName.toLowerCase() == hela_site.INFO_PAGE)
	{
		this.hideGalleria();
		this.showInfo();
		return;
	};
	// check if page is already cached
	var cachedPage = $(this.cache).data(pageName);
	if(!cachedPage)
	{
		var url = hela_site.fragmentsBaseRoot+pageName+".html";
		var $this = this;
		var newPage = $("<div class=\"item\"/>")
		.data("imageIndex", imageIndex)
		.data("loaded", false)
		.appendTo($(this.cache))
		.load(url, function(response, status)
		{
			if(status != "success")
			{
				$this.debug("ERROR LOADING CONTENT: "+status);
				return;
			};
			$this.updateGalleria(pageName, this);
			$this.showGalleria();
			$this.hideInfo();
		});
		$(this.cache).data(pageName, newPage);
	} else
	{
		$(cachedPage).data("imageIndex", imageIndex);
		this.updateGalleria(pageName, cachedPage);
		this.showGalleria();
		this.hideInfo();
	};
};
/**
*
*/
hela_site.prototype.updateGalleria = function(pageName, sourceElement)
{
	this.debug("updateGalleria("+pageName+", "+sourceElement+")");
	// galleria can be null or not
	var galleria = Galleria.get(0);
	if(!galleria)
	{
		galleria = this.createGalleria(sourceElement);
		$(this.galleria).data("pageName", pageName);
	} else
	{	
		var currentPageName = $(this.galleria).data("pageName");
		if(currentPageName && (currentPageName == pageName))
		{
			this.debug("pageName: "+pageName+" (already here)");
			var newImageIndex = $(sourceElement).data("imageIndex");
			var currentImageIndex = galleria.getIndex();
			this.debug("newImageIndex/currentImageIndex: "+newImageIndex+"/"+currentImageIndex);
			if(currentImageIndex != newImageIndex)
			{
				this.debug("galleria.show("+newImageIndex+")");
				galleria.show(newImageIndex);
			} else
			{
				this.debug("do not update index");
			};
		} else
		{
			galleria.load($(sourceElement));
			$(this.galleria).data("pageName", pageName);
		};
		
	};
};
/**
* creates new galleria instance using mix of options and initial 
* html-markup passed as argument
*/
hela_site.prototype.createGalleria = function(sourceElement)
{
	var galleria = Galleria.get(0);
	if(galleria)
	{
		return galleria;
	};
	var $this = this;
	var options = {
		dataSource: $(sourceElement),
		debug: hela_site.debug,
		extend: function()
		{
			this.bind("image", function(event)
			{
				$this.imageLoadedHandler(event);
			});
		},
		height: hela_site.galleriaHeight,
		initialTransition: hela_site.initialTransition,
		keepSource: true,
		transition: hela_site.transition,
		width: hela_site.galleriaWidth
	};
	galleria = $(this.galleria).galleria(options);
	return galleria;
};
hela_site.prototype.imageLoadedHandler = function(event)
{
	this.debug("event: "+event.type);
	var thisIndex = event.index;
	var thisPage = $(this.galleria).data("pageName");
	var thisDataSource = $(this.cache).data(thisPage);
	var oldIndex = $(thisDataSource).data("imageIndex");
	var loaded = $(thisDataSource).data("loaded");
	if(loaded == false)
	{
		$(thisDataSource).data("loaded", true);
		this.debug("galleria source updated");
		this.debug("image with index: "+thisIndex+" loaded");
		if(oldIndex != thisIndex)
		{
			this.debug("updating image index to "+oldIndex);
			Galleria.get(0).show(oldIndex);
		};
		return;
	};
	if(!isNaN(oldIndex))
	{
		if(thisIndex == oldIndex)
		{
			this.debug("nothing to update via hash change");
			return;
		};
	};
	// index in galleria is 0 based while in hash we want 1-based
	// call index vai wrapper
	var hash = "#"+thisPage+"/"+this.galleriaToHashIndex(thisIndex);
	this.debug("updating via new hash: "+hash);
	this.pushState(hash);
};
//
hela_site.prototype.showGalleria = function()
{
	$(".floatedThumbnails").fadeIn(hela_site.fadeSpeed);
	$(this.galleria).fadeIn(hela_site.fadeSpeed);
};
hela_site.prototype.hideGalleria = function()
{
	$(".floatedThumbnails"). fadeOut(hela_site.fadeSpeed);
	$(this.galleria).fadeOut(hela_site.fadeSpeed);
};
hela_site.prototype.showInfo = function()
{
	$(this.info).fadeIn(hela_site.fadeSpeed);
};
hela_site.prototype.hideInfo = function()
{
	$(this.info).fadeOut(hela_site.fadeSpeed);
};
/**
* @public
* this will effectively change state of browser navigation
* and will trigger onhashpagechange
*/
hela_site.prototype.pushState = function(state)
{
	if($.bbq) $.bbq.pushState(state);
};
//
hela_site.prototype.hashToGalleriaIndex = function(index)
{
	if(hela_site.useZeroBasedIndexingOnly) return index;
	var newIndex = index-1;
	return newIndex;
};
hela_site.prototype.galleriaToHashIndex = function(index)
{
	if(hela_site.useZeroBasedIndexingOnly) return index;
	var newIndex = index+1;
	return newIndex;
};
///
hela_site.prototype.debug = function(msg)
{
	if(!hela_site.debug) return;
	if(window.console)
	{
		var timeStr = new Date().toTimeString()
		window.console.log(timeStr+": "+msg)
	}
};
//
