$(document).ready(function() {
	$("#photo_gallery_thumbnails a").click(function(){
		$.ajax({
			type:'GET',
			url: $(this).attr('href'),
			success:function(data) {
				$.openWindow(700, 500, "Photo", data);
				$("#float_box").hide();
				$("#float_box .big_img img").load(function(){
				  $.screenResize($("#float_box"));
				});
				navigation();
			}
		});
		return false;
	});
	$("#galleries_menu li:first").hoverIntent(down,up);
});

function navigation(){
	var loader = $("#loader");
	$("#close").click(function(){
	  closeWindowScreen();
	  return false;
	});
	$(".arrows").click(function() {
		stop_slideshow();
		var id = $(".big_img").find("img").attr("class").replace("detail_", "");
		var method = $(this).attr('href').replace("#", "");
		eval(method + "_photo("+id+")");
		return false;
	});

	document.onkeydown = function(e){
		keycode = (e==null) ? event.keyCode : e.which;
		var id = $(".big_img").find("img").attr("class").replace("detail_", "");
		if(keycode == 37){
			prev_photo(id);
		}else if(keycode == 39){
			next_photo(id);
		}else if(keycode == 27){
			closeWindowScreen();
		}
	};
}

function down(){ $("#photo_galleries").animate({height: 'toggle', opacity: 'toggle' },200);} 
function up(){ $("#photo_galleries").animate({height: 'toggle', opacity: 'toggle' },300);}

function closeWindowScreen (){
	jQuery.closeWindow();
	document.onkeydown=function(){};
}

function prev_photo(id){
	var index = get_index(id);
	index--;
	if (index < 0) {
		index = (photos.length-1);
	}
	show_photo(index);
}

function next_photo(id){
	var index = get_index(id);
	index++;
	if (index > (photos.length-1)) {
		index = 0;
	}
	show_photo(index);
}

function show_photo(i){
	$("#float_box").hide();
	var obj = photos[i];
	$(".big_img").find("img").removeClass().addClass("detail_"+obj.id).attr("src", obj.src);
	$("#description h4").text(obj.name);
	$("#description #photo_body").html(obj.body);
}

function get_index(id){
	for (var i=0, obj; obj = photos[i]; i++) {
		if (obj.id == id) {
			return i
		}
	};
}
