/* ------------------------------------------------

Theme Name:   FocalPhoto
Theme URL:    http://codecanyon.net/user/Sitebase?ref=Sitebase
Description:  Photo gallery theme
Author:       Sitebase
Author URI:   http://codecanyon.net/user/Sitebase?ref=Sitebase

--------------------------------------------------- */

// Global vars
var meta = [];
var current_state = 'gallery';
var gallery;

jQuery(document).ready(function($) {
    
	// Read meta data
	read_meta();
	
	// Prepare website
	init();
	
	// Bind mouseover effects
	bind_events();

	// Load gallery images and init gallery
	load_gallery('', meta['default_category']);
	
}); 

function init(){

	// Hide category overview
	//$('#content').hide();

	// Fade out boxes
	$('#top').css('opacity', meta['category_inactive_opacity']);
	$('#description').css('opacity', 0.8);
	
	// Remove links for disabled javascript
	$('.next,.prev').attr('href', 'javascript:void(0);');
	$.each($('#top a'), function(index, value) {
		$(this).attr('href', "javascript:load_category('" + $(this).html() + "');");
	});

}

function bind_events(){
	$("#top").mouseenter(function() {
		$(this).stop(true,true).animate({opacity: meta['category_active_opacity']}, 400);
	}).mouseleave(function() {
		$(this).stop(true,true).animate({opacity: meta['category_inactive_opacity']}, 400);
	});
	
	$(".thumb").mouseenter(function() {
		$(this).stop(true,true).animate({opacity: meta['thumb_active_opacity']}, 400);
	}).mouseleave(function() {
		$(this).stop(true,true).animate({opacity: meta['thumb_inactive_opacity']}, 400);
	});
}

function state(value){
	if(current_state != value){
		$('#description-wrapper').html('');
		if(value == 'category'){
			$("#gallery,.next,.prev").fadeOut(500);
			$("#content").fadeIn(500);
		}else{
			$("#content").fadeOut(500);
			$("#gallery").fadeIn(500);
			
			// Only show navigation if there are more than 1 image in the category
			if($("#content > a").size() > 1){
				$(".next,.prev").fadeIn(500);
			}
		}
		current_state = value;
	}
}

function load_category(category){
	gallery.stop();
	state('category');
	$("#content").html('');
	$("#content").addClass("loader");
	$("#description-wrapper").fadeOut(500);
	$("#content").fadeOut(500, 
		function(){
			$("#description-wrapper").load("ajax.php", 'action=description&cat=' + category,
				function(){
					$("#description-wrapper").fadeIn(500);
				}
			);
			$("#content").load("ajax.php", 'action=category&cat=' + category,
				function(){
					$("#content").removeClass("loader");
					$('.thumb').css('opacity', 0.4);
					bind_events();
					$("#content").fadeIn(500);
				}
			);
		}
	);
}

function load_gallery(image, category){
	state('gallery');
	$("#content").html('');
	$("#gallery").load("ajax.php", 'action=gallery&img=' + image + '&cat=' + category,
		function(){
			// Init gallery
			gallery = $("#gallery").featuredbox({width: 958,
			height: 617,
			slidesAnimation: meta['gallery_animation'],
			slidesSpeed: 800,
			descriptionAnimation: "fade",
			descriptionSpeed: "slow",
			rotateInterval: meta['gallery_interval'],
			next: '.next',
			previous: '.prev'});
		}
	);
}

function read_meta(){
	var metas = document.getElementsByTagName('META');
	var i, key;
	for (i = 0; i < metas.length; i++){
		key = metas[i].getAttribute('NAME')
		meta[key] = metas[i].getAttribute('CONTENT');
	}
}
