var ITEMS_PER_PAGE = 18;
var ITEMS_PER_LINE = ITEMS_PER_PAGE/3;


$(setup);

function setup(){		
	var content = $("#content");
	$(".menu-item").hover( 
		function(){	
			if($(".active").attr("id") == "contact"){
				return;
			}
			var id = $(this).attr("id");
			if(content.css("background-image") != "none"){
				content.css("background-image", "url(images/"+id+".jpg)");
			}
		},
		function(){	
			if($(".active").attr("id") == "contact"){
				return;
			}					
			if(content.css("background-image") != "none"){
				content.css("background-image", "url(images/home.jpg)");			
			}
		}
	).click(		
		function(){			
			var id = $(this).attr("id");
			document.location = '/?page='+id;
			/*
			ajaxRequest(id, content);	
			var prev_active = $(this).removeClass(id+"-passive").addClass(id+"-active active").siblings(".active");
			var p_id = prev_active.attr("id");
			prev_active.removeClass("active "+p_id+"-active").addClass(p_id+"-passive");
			*/
		
		}
	);
	
	//ajaxRequest('home', content);	
	
	$('img').live("contextmenu",function(e){
		//alert('aa');
		return false;
	});
	
	//$("#load-helper").remove();
}
function ajaxRequest(id, content){
	$.getJSON("ajax.php", {page: id}, function(json){
			
		if(json){					
			var html = '';
			//alert(json);
			var counter = -1;
			for(var picture in json){
				if(picture != 'text'){
					counter++;
					if(counter%ITEMS_PER_PAGE == 0){
						if(html){
							html += '</div>';
						}
						html += '<div id="page-'+parseInt(counter/ITEMS_PER_PAGE)+'">';	
					}
					
					html += '<li class="thumb">'+							
							'	<a href="images/gallery/'+id+'/'+json[picture].name+'" title=\''+json[picture].description+'\'>'+
									'<img src="images/gallery/'+id+'/thumbs/'+json[picture].name+'" title="'+json[picture].title+'" />'+
								'</a><br/>'+
								json[picture].title+'<br/>'+
							'</li>';
					if(counter%ITEMS_PER_LINE == ITEMS_PER_LINE-1) {
						html += '<br class="clear" />';
					}
				}
			}
			var pagination = '';
			if(html){
				html += '</div>';
				html = '<div id="gallery"><ul>'+html+'</ul></div>';
				
				content.css("background-image","none").html(html);
				$("div[id^=page]:not(#page-0)").hide();
				//$("#page-0").show();					
				$("#gallery").slideDown(500, function(){
					
					if(counter >= ITEMS_PER_PAGE){
						
						for(var i=0;i<=parseInt((counter)/ITEMS_PER_PAGE); i++){
							pagination += '<a href="javascript:showPage('+i+')" class="pagin'+(i==0 ? " current-page" :"")+'" id="pagin-'+i+'">'+(i+1)+'</a>';
						}
						
						pagination = '<div style="width:100px; margin-left: 880px;"><div class="clear"></div><div class="spacer2"></div>'+pagination+'<div class="clear"></div></div>';						
						
					}
					$("#paginator").html(pagination);
					
						
				});
				$('#gallery a').lightBox({fixedNavigation:true});
				
			}else{
				content.css("background-image","url(images/"+id+".jpg)").empty();
				$("#paginator").empty();
			}
			
			//alert(content.css("height"));
			
			
		}
				
		if(json.text){
			if(html){
				$("#text").html('<hr />' + json.text);
			}else{
				$("#text").html(json.text);
			}
		}else{
			$("#text").empty();
		}
								
	});
}

function showPage(id){
	$("div[id^=page]").hide();
	$("#page-"+id).show();	
	
	$("a.current-page").removeClass("current-page");
	$("a#pagin-"+id).addClass("current-page");
					
}
