/******* Gallery functions *********/
secret_key = "34sd43j4reaERfjfj39xj4o9ws9";

// View size options for specified picture
function view_size_options(picture_id) {
	$('#gallery-thumb').toggle();
	$('#gallery-details').toggle();
	document.gallery_details_form.picture_id.value = picture_id;
	$.getJSON("processor.php", {action: "sizes", key: secret_key}, function(data){
		for (var key in data) {
			current_field = data[key];
			document.gallery_details_form[current_field].value = "0";
		}
	});
}

// Load full image
function load_full(image_path,image_id,base_url) {
//	$("#gallery-full-image").fadeOut('slow');
	$("#gallery-full-image").css({'opacity' : '0' ,'filter' : 'alpha(opacity=0)'});
//	$("#gallery-full-image").fadeIn('slow');
	$("#gallery-full-image").attr('src', image_path).fadeIn('slow');

	$("#purchase-link").attr('onClick', 'view_size_options('+image_id+');');
}

// Load range of thumbnails
function load_thumbs(start,album_id) {
	$.getJSON("processor.php",{ action: "gallery", key: secret_key, start: start, album_id: album_id },
		function(data){
			if (data.previous!="-1") {
				$("#previous-link").attr("onClick", "load_thumbs("+data.previous+"," +album_id+");");
				$("#previous-link").css({'display' : 'block'});
			} else {
				$("#previous-link").css({'display' : 'none'});
			}
			if (data.next!="-1") {
				$("#next-link").attr("onClick", "load_thumbs("+data.next+"," +album_id+");");
				$("#next-link").css({'display' : 'block'});
			} else {
				$("#next-link").css({'display' : 'none'});
			}
			$("#gallery-thumb-list").html(data.output);
	 	}
	);
}

function slideSwitch() {
    var $active = $('#gallery-thumb-list div.active');

    if ( $active.length == 0 ) $active = $('#gallery-thumb-list div:last');
	
    var $next =  $active.next().length ? $active.next()
        : $('#gallery-thumb-list div:first');
	
    $active.addClass('last-active');
	
    $next.css({opacity: 0.5})
        .addClass('active')
        .animate({opacity: 1.0}, 0, function() {
            $active.removeClass('active last-active');
        });
	
	path = $next.find('img').attr('src');
	if (path != undefined) {

		if (window.location.href == "http://www.bellababyphotography.com/gallery" || window.location.href == "http://www.bellababyphotography.com/gallery#" || window.location.href == "http://www.bellababyphotography.com/gallery/") {
			path = path.replace(/thumb/, "full_watermark");
		}
		else {
			path = path.replace(/thumb/, "full_resized");
		}
	
		image_id = $('#gallery-thumb-list div.active input').attr('id');
		load_full(path, image_id, "http://www.bella-life.com/");
	}
	else {
		path = "";
	}
	
}

$(document).ready(function() {
	if (window.location.href != "http://www.bella-life.com/cart") {
    	var	t = setInterval( "slideSwitch()", 4000 );
	}
	$('.play').bind('click', function() {
		t = setInterval("slideSwitch()", 4000);
		$('.stop').addClass('active');
		$(this).removeClass('active');
	});	

	$('.stop').bind('click', function() {
		clearTimeout(t);
		$(this).removeClass('active');
		$('.play').addClass('active');
	})
	
	$('div.image').bind('click', function() {
		$(this).addClass('active');
		var active = $('#gallery-thumb-list div.active');
		active.removeClass('active');
		$(this).addClass('active');
	})
});

