
var photogallery_thumbs_start = 0;
var photogallery_content_id = 0;
var photogallery_active_thumb_id = 0;
var photogallery_active_image_id = 0;

function photogallery_teaser_mouseover(elm) {
	if ( $(elm).children(".photogallery_teaser_over").length ) {
		$(elm).children(".photogallery_teaser_over").css("display", "block");
	}
}

function photogallery_teaser_mouseout(elm) {
	if ( $(elm).children(".photogallery_teaser_over").length ) {
		$(elm).children(".photogallery_teaser_over").css("display", "none");
	}
}

function photogallery_open(content_id) {
	photogallery_content_id = content_id;
	
	$("#photogallery_overlay_div").load("listener_content.php", { template: "photogallery", photogallery_action: "load_overlay"}, 
		function(){photogallery_open_step2();});
}

function photogallery_open_step2() {
	content_id = photogallery_content_id;

	doc_height = $(document).height();
	doc_width = $(document).width();
	
	win_height = $(window).height();
	win_width = $(window).width();
	
	win_scroll_top = $(window).scrollTop();
	win_scroll_left = $(window).scrollLeft();
	
	$("#photogallery_overlay").show();
	
	$("#photogallery_overlay").css("height", doc_height);
	$("#photogallery_overlay").css("width", doc_width);

	$("#photogallery_player").show();
	
	player_height = $("#photogallery_player").height();
	player_width = $("#photogallery_player").width();
	
	player_top = ( (win_height - player_height) / 2) + win_scroll_top;
	player_left = ( (win_width - player_width) / 2) + win_scroll_left;
	
	$("#photogallery_player").css("top", player_top);
	$("#photogallery_player").css("left", player_left);
	
	$("#photogallery_player_thumbs_images_div").load("listener_content.php", { template: "photogallery", photogallery_action: "load_thumbs", content_id: content_id, first_load: true});

	$("#photogallery_player_image").load("listener_content.php", { template: "photogallery", photogallery_action: "load_image", content_id: content_id},
		function(){photogallery_active_image_id = $("#photogallery_image_id").val();});

	$("#photogallery_player_caption_div").load("listener_content.php", { template: "photogallery", photogallery_action: "load_caption", content_id: content_id});

	$.post("listener_content.php", { template: "photogallery", photogallery_action: "load_num_images", content_id: content_id}, 
		function(data){photogallery_num_images(data);});

	

	photogallery_active_thumb_id = 1;
}


function photogallery_num_images(data) {
	if (data.length > 0) {
		photogallery_photos = JSON.parse(data);
		
		if (photogallery_photos.count > 3) {
			$("#photogallery_player_thumb_arrow_right").show();
		}
		
	}
		
}

function photogallery_close() {
	$("#photogallery_overlay").hide();
	$("#photogallery_player").hide();

	photogallery_thumbs_start = 0;
	photogallery_content_id = 0;
	
	$("#photogallery_player_thumbs_images_div").html("");
	$("#photogallery_player_image").html("");
	$("#photogallery_player_caption_div").html("");
	$("#photogallery_player_thumb_arrow_left").hide();
	$("#photogallery_player_thumb_arrow_right").hide();
}

function photogallery_thumbs_next() {
	photogallery_thumbs_start = photogallery_thumbs_start + 5;
	
	if ((photogallery_thumbs_start + 5) >= photogallery_photos.count) {
		$("#photogallery_player_thumb_arrow_right").hide();
	}
	
	if (photogallery_thumbs_start > 0) {
		$("#photogallery_player_thumb_arrow_left").show();
	}

	$("#photogallery_player_thumbs_images_div").load("listener_content.php", { template: "photogallery", photogallery_action: "load_thumbs", content_id: photogallery_content_id, thumbs_start: photogallery_thumbs_start, active_image: photogallery_active_image_id});
	
}

function photogallery_thumbs_prev() {
	photogallery_thumbs_start = photogallery_thumbs_start - 5;

	if (photogallery_thumbs_start==0) {
		$("#photogallery_player_thumb_arrow_left").hide();
	}

	if ((photogallery_thumbs_start + 5) < photogallery_photos.count) {
		$("#photogallery_player_thumb_arrow_right").show();
	}

	$("#photogallery_player_thumbs_images_div").load("listener_content.php", { template: "photogallery", photogallery_action: "load_thumbs", content_id: photogallery_content_id, thumbs_start: photogallery_thumbs_start, active_image: photogallery_active_image_id});

}

function photogallery_thumb_click(image_id, div_id) {
	$("#photogallery_player_caption_div").html("");

	$("#photogallery_player_image").load("listener_content.php", { template: "photogallery", photogallery_action: "load_image", content_id: photogallery_content_id, image_id: image_id},
		function(){photogallery_active_image_id = $("#photogallery_image_id").val();});
		
	$("#photogallery_player_caption_div").load("listener_content.php", { template: "photogallery", photogallery_action: "load_caption", content_id: photogallery_content_id, image_id: image_id});

	$(".photogallery_player_thumb_mask_active_wide").remove();
	$(".photogallery_player_thumb_mask_active_tall").remove();

	photogallery_active_thumb_id = div_id;

	if ( $("#photogallery_player_thumb_image"+photogallery_active_thumb_id).children(".photogallery_player_thumb_image_wide").length ) {
		$("#photogallery_player_thumb_image"+photogallery_active_thumb_id).append("<div class=\"photogallery_player_thumb_mask_active_wide\"></div>");

	} else {
		$("#photogallery_player_thumb_image"+photogallery_active_thumb_id).append("<div class=\"photogallery_player_thumb_mask_active_tall\"></div>");

	}
	
}

function photogallery_thumb_over(elm) {
	if ($(elm).siblings(".photogallery_player_thumb_mask_inactive_wide").length) {
		$(elm).siblings(".photogallery_player_thumb_mask_inactive_wide").css("background-image","url(images/photogallery_player_thumb_mask_over_wide.gif)");
		
	} else if ($(elm).siblings(".photogallery_player_thumb_mask_inactive_tall").length) {
		$(elm).siblings(".photogallery_player_thumb_mask_inactive_tall").css("background-image","url(images/photogallery_player_thumb_mask_over_tall.gif)");
	}
}

function photogallery_thumb_out(elm) {
	if ($(elm).siblings(".photogallery_player_thumb_mask_inactive_wide").length) {
		$(elm).siblings(".photogallery_player_thumb_mask_inactive_wide").css("background-image","url(images/photogallery_player_thumb_mask_inactive_wide.gif)");
		
	} else if ($(elm).siblings(".photogallery_player_thumb_mask_inactive_tall").length) {
		$(elm).siblings(".photogallery_player_thumb_mask_inactive_tall").css("background-image","url(images/photogallery_player_thumb_mask_inactive_tall.gif)");
	}

}