$(document).ready(function(){

// menu

LAST_MENU_SELECTED = readCookie("lastmenu");

createCookie("lastmenu", MENU_SELECTED, 7);

if( LAST_MENU_SELECTED != MENU_SELECTED ){
	$("ul#menu li a.menu_"+LAST_MENU_SELECTED).next("ul").show().slideUp( 500 );
	$("ul#menu li a.menu_"+MENU_SELECTED).next("ul").slideDown( 500 );
}else{
	$("ul#menu li a.menu_"+MENU_SELECTED).next("ul").show();
}




// gallery

image_no = 1;
image_total = $("div#image_gallery").find("img").length + 1;

fading = false;

images = new Array();

function update_position(){
	$("div#gallery_position").html( image_no+"/"+image_total );
}

if( image_total > 0 ){
	$("div#image_gallery").find("img").each(function(){
		images[images.length] = $(this).attr("src");
	});
	
	$("div#image_gallery_show").html( '<div class="specs" style="position: absolute;">'+$("div.specs").html()+'</div>' );
	
	//$("div#image_gallery_show").html('<img src="'+images[0]+'" alt="" />');
	//$("div#image_gallery_show img").css("z-index", "3");
	
	update_position();
	
	$("div#gallery_nav").show();
}

$("a.next").click(function(){
	if( image_no < image_total && fading==false ){
		fading = true;
		image_no = image_no + 1;
		
		if( image_no == 2 ){
			$("div#image_gallery_show div.specs").css("z-index", "4");
			$("div#image_gallery_show").append('<img src="'+images[image_no-2]+'" alt="" style="z-index: 3;" />');
			$("div#image_gallery_show div.specs").fadeOut(500, function(){ $(this).remove(); fading=false; });
		}else{
			$("div#image_gallery_show img").css("z-index", "4");
			$("div#image_gallery_show").append('<img src="'+images[image_no-2]+'" alt="" style="z-index: 3;" />');
			$("div#image_gallery_show img:first").fadeOut(500, function(){ $(this).remove(); fading=false; });
		}
		update_position();
	}
	return false;
});

$("a.back").click(function(){
	if( image_no > 1 && fading==false ){
		fading = true;
		image_no = image_no - 1;
		
		if( image_no == 1 ){
			$("div#image_gallery_show img").css("z-index", "4");
			$("div#image_gallery_show").append('<div class="specs" style="position: absolute; z-index: 3;">'+$("div.specs").html()+'</div>');
			$("div#image_gallery_show img:first").fadeOut(500, function(){ $(this).remove(); fading=false; });
		}else{
			$("div#image_gallery_show img").css("z-index", "4");
			$("div#image_gallery_show").append('<img src="'+images[image_no-2]+'" alt="" style="z-index: 3;" />');
			$("div#image_gallery_show img:first").fadeOut(500, function(){ $(this).remove(); fading=false; });
		}
		update_position();
	}
	return false;
});

});



// from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

