var pages = [];
var pages_index = 0;
var in_subnav = false;

$(document).ready(function(){
	
	$(window).resize(function() {
		fit_big_img();
	});

	// load json data
	if (display=='photo' || display=='info')
	{
		$("#p2 img").load(function() {
			$(this).removeAttr("width").removeAttr("height").css({ width: "", height: "" });
			fit_big_img();
			if (pages) update_submenu();
			$(this).css("display","block");
			if (!in_subnav) $("#subnav").delay(2000).fadeTo('slow', 0);
		});
		
		$("#p1 img, #p3 img").load(function() {
		    $(this).removeAttr("width").removeAttr("height").css({ width: "", height: "" });
			fit_big_img();
		});
		
		load_children(id_parent);

	}
	else
	{
		// thumbnails, homepage, text pages
		$("#p1 img, #p3 img").load(function() {
		    $(this).removeAttr("width").removeAttr("height").css({ width: "", height: "" });
		
			h = get_available_height();
			$("#wrapper").height( h );
			resize_p1_and_p3(h);
		});
	}
	
	// subnav hide/show
	$("#subnav").hover(
		function () {
			$("#subnav").clearQueue().fadeTo('slow', 1);
			in_subnav = true;
		}, 
		function () {
			$("#subnav").delay(1000).fadeTo('slow', 0);
			in_subnav = false;
		}
	);

	
});

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function get_available_height()
{
	return $(window).height() - 114;
}

function get_available_width()
{
	return $(window).width() - 250;
}

function update_submenu()
{
	$("#page_content").html(pages[pages_index].content);
	$("#pages_bullets a").removeClass('on');
	$("#pages_bullets #bull"+pages[pages_index].id).addClass('on');	
}

function swap(dir)
{
	pages_index += dir;
	if (pages_index>pages.length-1) pages_index = 0;
	if (pages_index<0) pages_index = pages.length-1;
	
	show_images();
}

function swap_by_id(id)
{
	id_page = id;
	set_pages_index_to_active_page();
	swap(0);
}

function show_images()
{
	$("#p1 img").attr("src", 'cache/nextprev/'+get_page_file(-1));
	$("#p2 img").css("display","none");
	$("#p2 img").attr("src", 'files/'+get_page_file(0));
	$("#p3 img").attr("src", 'cache/nextprev/'+get_page_file(1));	
}

function get_page_file(dir)
{
	i = pages_index;
	i += dir;
	if (i>pages.length-1) i = 0;
	if (i<0) i = pages.length-1;
	
	return pages[i].file;
}

function load_children(id)
{		
	$.getJSON("index.php?a=getjson&type=children&id="+id, function(result){ 
		
		pages = result;
		set_pages_index_to_active_page();
		
		var imgs = [];
		for(c=0; c<pages.length; c++)
			jQuery.preLoadImages('files/'+pages[c].file);

	});
}

function set_pages_index_to_active_page()
{
	for(c=0; c<pages.length; c++)
		if (pages[c].id==id_page)
			pages_index = c;
}

function fit_big_img()
{
	// available space
	w = get_available_width();
	h = get_available_height();
	
	// resize wrapper
	$("#wrapper").height( h );

	// resize img 2
	if (display=='photo' || display=='info')
	{
		imgw = $("#p2 img").width();
		imgh = $("#p2 img").height();
		if (imgw/w>imgh/h) {
			$("#p2 img").width(w);
			$("#p2 img").height( (imgh*w)/imgw );
		} else {
			$("#p2 img").width( (imgw*h)/imgh );
			$("#p2 img").height(h);	
		}
		new_h = $("#p2 img").height();
	}
	
	// position next prev btns
	if (display=='photo')
	{
		var top = 0;
		top = (new_h/2) - 35;
		top = get_available_height()/2 - 35;
		$("#btn_prev, #btn_next").css("top",top+"px");
		$("#btn_prev, #btn_next").css("display","block");
	}
	

	resize_p1_and_p3(new_h);
	$("#p2").height( new_h );
}

function resize_p1_and_p3( new_h )
{
	// resize img 1 & grid 1
	imgw = $("#p1 img").width();
	imgh = $("#p1 img").height();
	$("#p1 img").width((new_h*imgw)/imgh);
	$("#p1 img").height(new_h);
	$("#g1").height(new_h);
	
	// resize img 3 & grid 3
	imgw = $("#p3 img").width();
	imgh = $("#p3 img").height();
	$("#p3 img").width((new_h*imgw)/imgh);
	$("#p3 img").height(new_h);
	$("#g3").height(new_h);

	// resize pic boxes
	$("#p1").height( new_h );
	$("#p3").height( new_h );
}


