// Global variables.
var path;
var path2;
var imageArray = [];
var allowedElements;
var elementsArray;
var allowedLogouts;
var allowedLinks;
var linksArray;
var splittedName;

// No conflict.
jQuery.noConflict(); 
jQuery(document).ready(function()
{
	// Set allowed link elements.
	allowedElements = ["a#gallery_link", "a#gallery_login_link"];
	elementsArray = allowedElements .join(", ");
	
	jQuery(elementsArray).live('click', function(event)
	{	
		// Get page name.
		splittedName = jQuery(this).attr('href').split("?page=");
              
		// Set full path.
		path = jQuery(this).attr('href');
		
		// Load content.
		if(splittedName[1] =="gallerylogin")
		{
			jQuery("div#gallery_container").load(path, function() { activateLogin(); });
			jQuery("div#gallery_overlay").fadeTo("def", 0.6);
		}
		else
		{
			jQuery("div#gallery_container").load(path, function() { activateGallery(); });
		}
		return false;
	});
});

function activateLogin()
{
	// Fade gallery container in.
	jQuery("div#gallery_container").fadeTo("def", 1.0);

	// Login form.
	jQuery("#feu_login_container #feu_login").live('click', function(event)
	{
		// Stop link.
		event.preventDefault();
		jQuery("#feu_login_container form").ajaxSubmit({target: "#gallery_container", success: activateLogout});
	});	
	
	// Register form.
	jQuery("div.contact_form div.submit").live('click', function(event)
	{
		// Stop link.
		event.preventDefault();
		jQuery("#feu_register_container form").ajaxSubmit({target: "#gallery_container"});
	});
	
	// Close button.
	jQuery("div#gallery_container img#close_gallery").live('click', function(event)
	{
		disableGallery();
	});
}

function activateLogout()
{	
	// Logout link.
	jQuery("div#gallery_container a.logout").live('click', function(event)
	{	
		// Stop link.
		event.preventDefault();
		
		// Clear old links.
		jQuery("#gallery_links").remove();
		
		// Perform logout
		path2 = jQuery("a.logout").attr('href');
		jQuery("div#gallery_container").load(path2, function()
		{
			disableGallery2();
		});
		return false;
	});
}

function activateGallery()
{
	// Set allowed elements.
	allowedLinks = ["div#gallery_container #gallery_links a.first", "div#gallery_container #gallery_links a.second"];
	linksArray = allowedLinks .join(", ");
	
	// Store image information from links to array.
	jQuery(linksArray).each(function(index)
	{
		imageArray.push([this.href, this.title]);
	});
	
	// Initialize slimbox.
	jQuery.slimbox(imageArray, 0, {overlayOpacity: 0.6, overlayFadeDuration: 300, resizeDuration: 1, captionAnimationDuration: 1});
}

function activateGallery2()
{
	// Set allowed elements.
	allowedLinks = ["div#gallery_container #gallery_links a.first", "div#gallery_container #gallery_links a.second"];
	linksArray = allowedLinks .join(", ");
	
	// Store image information from links to array.
	jQuery(linksArray).each(function(index)
	{
		imageArray.push([this.href, this.title]);
	});
	
	// Initialize slimbox.
	jQuery.slimbox(imageArray, 0, {overlayOpacity: 0.0, overlayFadeDuration: 1, resizeDuration: 1, captionAnimationDuration: 1});
}

function disableGallery()
{	
	// Fade gallery out.
	jQuery("div#gallery_container").fadeTo("def", 0.0);
	jQuery("div#gallery_overlay").fadeTo("def", 0.0, function()
	{
		removeOverlay();
	});
}
function removeOverlay()
{
	// Remove gallery.
	jQuery("div#gallery_overlay").css("display", "none");
	jQuery("div#gallery_container").css("display", "none");
}

function disableGallery2()
{	
	// Remove gallery2 and refresh page.
	jQuery("div#gallery_links").remove();
	jQuery("div#slim_container").fadeTo("def", 0.0, function(){jQuery("div#slim_container").remove();});
	jQuery("div#gallery_container").fadeTo("def", 0.0, function(){jQuery("div#gallery_container").remove();});
	jQuery("div#gallery_overlay").fadeTo("def", 0.0, function()
	{
		jQuery("div#gallery_overlay").remove();
		jQuery(location).attr("href", window.location);
	});
}
