
var currentPage;
var currentImg;
function loadPage(ID){
	
	if ($numChildren = $('#pagesHolder').children().size()){
		
		
		$(".pageObj").animate({
			top: $("#pagesHolder").height()
		},500,function(){
			$("#pagesHolder").empty();
			$('#pagesHolder').css("z-index","-1");
			doAjax(ID);
		});
		
	}else{
		
		$("#pagesHolder").empty();
		doAjax(ID);
	}
	
	}

function doAjax(ID){

	
		
	$.ajax({ 
			url: "ajax.php?id="+ID, 
			context: $("#pagesHolder"), 
			success: function(data){
			
				currentImg=0;
				$(this).html(data);
				$(".pageObj").css("top", $("#pagesHolder").height());
				$('#pagesHolder').css("z-index","1002");
				$(".pageObj").animate({
					top: 20
				},
				500,
				function(){
					changeImg(1);
				});
				
				//set the currentpage for closing;
			
				/*
				if ($('.images'.length)){
					$numChildren = $('.images').children().size();
					$('.images').css("height","468px");
					
					$('.images').galleria({
							transition:"fade",
							image_margin:0,
							thumbnails:($numChildren>1),
							image_position:'top left',
							image_crop:true
						}
					);
					if ($numChildren==1) {
						$('.galleria-stage').css("bottom","0px");
						$('.images').css("height","408px");
						$('.images').css("margin-bottom","70px");
					}
				
				}
				*/
			}
			
		});
}

function closeWindow(){
	

	
	$(".pageObj").animate({
			top: $("#pagesHolder").height()
		},500,function(){
			$("#pagesHolder").empty();
			$('#pagesHolder').css("z-index","-1");
		});
}

$(document).ready(function() {
	$(document).keydown(function(ev){
		switch(ev.keyCode){
		
			case 27:
			closeWindow();
			break;
		}
	})
	
	var resizeTimer = null;

	$(window).bind('resize', function() {
	
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(resizePageHolder, 100);
	
	});
	
	resizePageHolder();

 });

function resizePageHolder(){
	
	_width  = ($("body").width()/2)+250;
	/* alert(_width); */
	$("#pagesHolder").css("width",_width+"px");

}


function changeImg(n){

	if ($("#galleryImg"+n).length > 0){
	
		if (currentImg!=n){
		
			if (currentImg!="undefined"){
				$("#galleryImg"+currentImg).fadeOut();
				$("#galleryBT"+currentImg).removeClass("selected");
			}
			$("#galleryImg"+n).fadeIn();
			$("#galleryBT"+n).addClass("selected");
			currentImg = n;
		}
		
	}
		
}

function nextImg(){changeImg(currentImg+1)}
function prevImg(){changeImg(currentImg-1)}

//====================================================== SWF

function SWF( swfID ){
	if(navigator.appName.indexOf("Microsoft") != -1){
		s = window[swfID];
	}else{
		s = document[swfID];
	}
	return s;
}

var lastHash = '';

function pollHash() {
	if(lastHash !== location.hash) {
		lastHash = location.hash;
		callFlash(lastHash);
	}
}

function setLink(n){
	//alert(n);
	self.document.location.hash = n;
}

function callFlash(_hash){
	id = parseInt(_hash.substring(1));
	SWF("swfHolder").onChange(id);
} 
