var processing = false;

function showPhoto(id, path)
{
	if(!processing && !$("#photo-"+id).hasClass("current"))
	{
		if($("#photo-"+id).find("img").length > 0)
		{
			$(".gallery.active").find(".slideshow > .current").fadeOut(800, function(){ $(this).removeClass("current"); });
			$("#photo-"+id).fadeIn(800, function(){processing = false;}).addClass("current");
		}
		else
		{
			processing = true;
			var img = new Image();
				img.src = "img/loader.gif";
				
			$(".gallery.active").find(".slideshow > .current").fadeOut(800, function(){ $(this).removeClass("current"); processing = false; });
			$(".gallery.active").find(".slideshow").append('<div id="photo-'+id+'" class="photo"></div>');
	  
			$(img).load(function()
			{
				$("#photo-"+id).append(this);
				
				$(this).parent().fadeIn(800, function(){processing = false;}).addClass("current");
			}).attr('src', 'image_gallery/'+path+'/full_image.jpg');

			$(this).addClass("active");
		}
		
		$("#photo-nav-"+id).siblings(".active").removeClass("active");
		$("#photo-nav-"+id).addClass("active");
	}
}

function showGallery(id)
{
	var activeGallery = $(".gallery.active");
	
	if((activeGallery.attr("id") != "gallery-"+id) && $("#gallery-"+id).length > 0)
	{
		activeGallery.fadeOut(800, function(){ $(this).removeClass("active"); });
		$("#gallery-"+id).fadeIn(800, function(){ $(this).addClass("active"); });
		
		$("#categoryBox .active").removeClass("active");
		$("#gallery-nav-"+id).addClass("active");
	}
}


function showBox(box)
{
	$(".modalbox:visible").fadeOut(800);
	$("#modalHiddenOverlay").css("display", "none");
	
	if(box)
	{
		if($("#"+box).is(':hidden'))
		{
			$("#modalHiddenOverlay").css("display", "block");
			$("#"+box).fadeIn(800);
		}
	}
}

function hideModals()
{
	$("#modalHiddenOverlay").css("display", "none");
	$(".modalbox:visible").fadeOut(800);
}

function moveGalleryLeft(gallery_id)
{
	var left_pos = $("#carousel-"+gallery_id).css("left");
	var new_pos = parseInt(left_pos)+(27*5);
	var t_pos = parseInt($("#thumbnail-container-"+gallery_id).css("left"));
	
	if(new_pos!=7)
	{
		$("#carousel-"+gallery_id).animate({ left: new_pos+"px" }, 800);
		$("#thumbnail-container-"+gallery_id).css({ left: (t_pos+135)+"px" });
	}
	else
	{
		$("#carousel-"+gallery_id).animate({ left: new_pos+"px" }, 800);
		$("#ml-button-"+gallery_id).css("visibility", "hidden");
		$("#thumbnail-container-"+gallery_id).css({ left: (t_pos+135)+"px" });
	}
	
	$("#mr-button-"+gallery_id).css("visibility", "visible");
}

function moveGalleryRight(gallery_id)
{
	var length = $("#carousel-"+gallery_id+" li").length;
	var left_pos = parseInt($("#carousel-"+gallery_id).css("left"));
	var new_pos = parseInt(left_pos)-(27*5);
	var t_pos = parseInt($("#thumbnail-container-"+gallery_id).css("left"));
	
	if((length*27+left_pos) > 790)
	{
		$("#carousel-"+gallery_id).animate({ left: new_pos+"px" }, 800);
		$("#thumbnail-container-"+gallery_id).css({ left: (t_pos-135)+"px" });
	}
	else
	{
		$("#carousel-"+gallery_id).animate({ left: new_pos+"px" }, 800);
		$("#mr-button-"+gallery_id).css("visibility", "hidden");
		$("#thumbnail-container-"+gallery_id).css({ left: (t_pos-135)+"px" });
	}
	
	$("#ml-button-"+gallery_id).css("visibility", "visible");
}


function sendInquiryForm()
{
	var fullname = document.getElementById('fullname').value;
	var email = document.getElementById('levelcim').value;
	
	var reg=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var emailtest = reg.test(email);
	
	if(fullname != "" && email != "" && checkEmail(email) && emailtest)
	{
		$("#inquiryForm").ajaxSubmit({
			success: handleInquirySuccess,
			error: handleInquiryError
		});
	}
}

function handleInquirySuccess(msg)
{
	$("#form_container").fadeOut("slow", function(){ $(this).html($("#success_message").html()); $(this).fadeIn("slow"); });
}

function handleInquiryError(msg)
{
	alert(msg);
}


// init
$(document).ready(function(){

	var hash = unescape(self.document.location.hash.substring(1));
	
	if(hash != null && hash != "" && $("#"+hash).hasClass("modalbox"))
		showBox(""+hash);

	$("#modalHiddenOverlay").bind("click", hideModals);
	$(".close_button").bind("click", hideModals);
	
	$(".carousel_clip_region ul li").hover(function(){ 
		var thumb_id = $(this).attr("id");
		var real_id = thumb_id.substr(10, thumb_id.length);
		$("#thumb-"+real_id).show();
		}, function(){
		
			$(".thumbnail").hide();
		});
	
	$(".menu_item").hover(function(){$(this).find(".hover_image").fadeIn("fast");}, function(){$(this).find(".hover_image").hide();});
	
	$("#inquiryForm").ajaxForm();
});


function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)){
		//var response = executeSyncronRequest('/checkmail.php?mail='+email);
		//if ("OK" == response)
			return true;
	}
	return false;
}
