var resizeTimer = null;

var start=1;

$(document).ready(function(){
	$("#first_page").val(1);					   
	resize();
	
	$(window).bind('resize', function() {
		resize();
	});
	
	init_cat_but();
	
	load_prev();
	resize();
	
	
	
	$("#prev_pic").hover(function(){
		if(start>1)							  
			$(this).css("background-image", "url(img/prev.png)");						  
	},function(){
									  
		$(this).css("background-image", "none");						  
	});
	
	$("#prev_pic").click(function(){
		if(start>1)
		{
			start--;
			$("#thumb_"+start).click();
		}
	});
	
	$("#next_pic").hover(function(){
		if(start<$("div[id*=thumb_]").length)							  
		$(this).css("background-image", "url(img/next.png)");						  
	},function(){
									  
		$(this).css("background-image", "none");						  
	});
	
	
	$("#next_pic").click(function(){
		if(start<$("div[id*=thumb_]").length)		
		{
			start++;
			$("#thumb_"+start).click();
		}
	});
	
	
	$("#contact").click(function(){
								 
		
		contact();
	
	
	});
	
	$(".fitem_cat").click(function(){
		
		var cat = $(this).html();
		
		
		if(cat!='Contact')
		{
			$("#selected_cat").fadeOut(200,function(){
						
						
				$("[title="+cat+"]").click();
				
				
			});
		}
		else
		{
			$("#contact").click();
					
		}
		
		hidefirst();
								   
	});
	
	$("#title img").click(function(){
									   
		if($("#first_page").val()==0)
		{
			$("#first_page").val(1);
			
			
			resize();
				
			$("#left_transp1").fadeOut(500);
			$("#right_transp1").fadeOut(500);
			$("#prev_pic").fadeOut(500);
			$("#next_pic").fadeOut(500);
			$("#title").animate({"backgroundColor": "#111111"},500);
		
			$("#list_images").fadeOut(500);
			$("#bottom").fadeOut(500);
			$("#contact").fadeOut(500);
			$("#picture").fadeOut(500);
			$("#list_cat").fadeOut(2000, function(){
					
				$("#first_picture").fadeIn(500,function(){
														
					showfirst();
					
				});
					
			});
			
		}
	
	});
	
	
	
});


function showfirst()
{
		var i=0;
		
		var a = 0;
		
		
		$(".fitem_cat[style*=none]").each(function(){
			
				$(this).css("display","block");
				$(this).animate({"marginRight": "0px", "opacity":"1"}, 200,function(){ showfirst(); });
				//$(this).fadeIn(200,function(){ showfirst(); });
				
				return false;	
			
		});
		
}

function hidefirst()
{
		var i=0;
		var l=$(".fitem_cat[style*=block]").length;
		var a = 0;
		
		
		$(".fitem_cat[style*=block]").each(function(){
			i++;
			if(i==l) 
			{
				$(this).animate({"marginRight": "50px", "opacity":"0"}, 200, function(){
				$(this).css("display","none");
				hidefirst();
				});
				
				
				//$(this).fadeOut(200,function(){  });
				a=1;
			}
			
		});
		
		if(a==0)
		{
			
			$("#first_picture").fadeOut(500,function(){
			
			$("#first_page").val(0);
			
			resize();
			
			$("#left_transp1").fadeIn(500);
			$("#right_transp1").fadeIn(500);
			$("#prev_pic").fadeIn(500);
			$("#next_pic").fadeIn(500);
			$("#title").animate({"backgroundColor": "#000000"},500);
			
			$("#list_images").fadeIn(500);
			$("#bottom").fadeIn(500);
			$("#contact").fadeIn(500);
			$("#list_cat").fadeIn(1000, function(){
					
				init_cat_but();	
				//load_prev();

				resize();
				
				
			});
			
			$("#picture").fadeIn(500);
			
			resize();
			
			
		});	
			
		}
		
}

function contact()
{
	
	$("#list_images").find("img").fadeOut(1000,function(){
		
		var url = 'utilities/get_data.php';
		var pars ='?modul=photo_gallery&func=contact';
				
		
		$.ajax({type: "POST", url: url+pars,
				success: function(data) {
					$("#picture").find("img").hide();
					$("#picture").css("background-image", "url()");
					$("#contact_data").remove();
					$("#picture").append(data);
					
	
					
					resize();
					
					$(".send").click(function(){
							var url = 'utilities/get_data.php';
							var pars ='?modul=photo_gallery&func=send_mail&name='+$("#contact_name").val()+"&mail="+$("#contact_mail").val()+"&text="+$("#contact_text").val();
									
							
							$.ajax({type: "POST", url: url+pars,
									success: function(data) {
												alert(data);
							}})							  
					});
					
				}});
		
	});	
}

function load_prev()
{
	$("#prev_list").html("Loading...");
	
	var url = 'utilities/get_data.php';
	var pars ='?modul=photo_gallery&func=show_prev&gallery='+$("#selected_cat").html();
			
	
	$.ajax({
	type: "POST",
	url: url+pars,
	
	success: function(data) {
				
				//alert(data);
				
				$("#prev_list").html(data);
				start = 1;
				
				$(".thumbnail").css("opacity", 0.5);
				
				$(".thumbnail").hover(function(){
					$(this).stop();
					$(this).animate({"opacity": 1}, 200);							   
				},
				function(){
					$(this).stop();
					$(this).animate({"opacity": 0.5}, 200);	
				});
				
				$(".thumbnail").click(function(){
					
					$("#contact_data").remove();
					
				    var p = $(this).css("background-image").replace('url(',"");
					var p = p.replace(')',"");
					
					
					
					p = p.split("/");
					p = p[(p.length-1)];
					var p = p.replace('"',"");
					
					//alert(p);
					
					id = $(this).attr("id").replace("thumb_","");
					start = id;
					
					
					
					$("#list_images").find("img").fadeOut(1000,function(){
						$("#picture").css("background-image", "url(img/load.gif)");
						
						
						//alert(p);
						$("#list_images").find("img").attr("src", "images/"+p);
						
						$("#contact_data").remove();
						$("#list_images").find("img").load(function(){
							$("#contact_data").remove();
							$("#picture").css("background-image", "url()");
							$(this).fadeIn(1000);
							resize();
						});
					});
					goto_thumb();
				});
				
				$(".thumbnail:first").click();
				
				req = null;
			}
	});	
}


function goto_thumb()
{
	
	var p = (start-3)*60;
	$("#prev_list").stop();
	$("#prev_list").animate({"scrollLeft": p+"px"},1500, "easeOutExpo");
	
}


function init_cat_but()
{
	$(".item_cat").unbind("click");
	
	$(".item_cat").click(function(){
								  
		var cat = $(this).html();
		
		//alert(cat);
		
		$(this).fadeOut(200,function(){
			$(this).remove();					
		});
		
		$("#selected_cat").fadeOut(200,function(){
												
			$("#list_cat").append('<div class="item_cat">'+$("#selected_cat").html()+'</div>');
			
			$("#selected_cat").html(cat);
			$("#selected_cat").fadeIn(200);
			
			init_cat_but();		
			load_prev();
		});
	});	
}

function resize()
{
	//redim poza mare
	if($("#first_page").val()==0){
		var o = $("#list_images").find("img").offset();	
		var s1 = $("#list_images").find("img").width()/2+o.left;
		
		$("#prev_pic").css({left: o.left+"px", top: o.top+"px", height:  $("#list_images").find("img").height()+"px"});
		$("#next_pic").css({left: (o.left+$("#list_images").find("img").width()-100)+"px", top: o.top+"px", height:  $("#list_images").find("img").height()+"px"});
		
		//alert($("#list_images").find("img").width());
		
		
		$("#picture").css("background-position", s1+"px");
		
		
		var s2 = $("#picture").height()-20;
		if(s2<600) $("#list_images").find("img").attr("height",s2);	
				else $("#list_images").find("img").attr("height",600);
		
		//redim elemente
		var h1 = $("#title").outerHeight();
		var h2 = $("#bottom").outerHeight();
		var h3 = $("body").height()-h1-h2;
		
		$("#picture").css("height", h3+"px");
		
		
		
		var ppoza = (h3-$("#list_images").height())/2;
		
		$("#list_images").css("top", ppoza+"px");
		$("#contact_data").css("padding-top", Math.round((h3-$("#contact_data").height())/2-20)+"px");
		
		
		var ppcat = h1+h3-$("#list_cat").outerHeight();
		$("#list_cat").css("top", ppcat+"px");
		
		var s3 = $("body").width()-270;
		$("#prev_list").css("width", s3+"px");
		
		
		
		$("#contact").css("top", (ppcat-$("#contact").outerHeight())+"px");
		
		//asez transparentele
		
		var offset = $("#prev_list").offset();
		
		$("#left_transp1").css({"position": "absolute",
								"top": offset.top+"px",
								"left": offset.left+"px"
							   });
		$("#right_transp1").css({"position": "absolute",
								"top": offset.top+"px",
								"left": (offset.left+$("#prev_list").width()-30)+"px"
							   });
		
	
	}
	else
	{
		$("#left_transp1").hide();
		$("#right_transp1").hide();
		$("#prev_pic").hide();
		$("#next_pic").hide();
		//$("#title").hide();
		$("#list_images").hide();
		$("#bottom").hide();
		$("#contact").hide();
		$("#list_cat").hide();
		$("#picture").hide();
		
		
		$("#first_picture").find("img").attr("height", $("body").height());
	
	}
	//if (resizeTimer) clearTimeout(resizeTimer);
	//resizeTimer = setTimeout("resize()", 500);
}


