// JavaScript Document

function myFadeIn(object){
      object.fadeIn(20, function () {
            object.children('a').animate({opacity: 0.75}, 25, function () {
                  if ( object.next().length )    {				
                        myFadeIn(object.next());
                        object.bind('click',displayLayer);
                  }
            });    
      });
}

function displayLayer(){
	var $id = $(this).children('a').attr('id');
	if($.browser.msie)	{
		$id = $id.substring($id.lastIndexOf('/')+1);
	}

	var $sid = $('#sid').val();
	
	$('#layer').css('display','block');
	$('#layer').css('width',$(document).width());
	$('#layer').css('height',$(document).height());
	
	$('#layer').load('php/image.php?id=' + $id+'&sid='+$sid,null, function()	{
		$('#layer img').bind('click',closeLayer);
		$('#layer .previous').bind('click',prevNextLayer);
		$('#layer .next').bind('click',prevNextLayer);
	});
	
	$('#title').addClass('p25');
	$('#title div').css('display','none');
	window.scrollTo(0,0);
}

function prevNextLayer()	{
	var $id = $(this).attr('id');

      if($.browser.msie)	{
		$id = $id.substring($id.lastIndexOf('/')+1);
	}

	var $sid = $('#sid').val();

	$('#layer').empty();
	$('#layer').load('php/image.php?id=' + $id+'&sid='+$sid,null, function()	{
		$('#layer img').bind('click',closeLayer);
		$('#layer .previous').bind('click',prevNextLayer);
		$('#layer .next').bind('click',prevNextLayer);
	});
	
	$('#title').addClass('p25');
	$('#title div').css('display','none');
	
	return false
}


function closeLayer()	{
	$('#layer').empty();
	$('#layer').css('display','none');
	$('#layer').css('width','0px');
	$('#layer').css('height','0px');
      $('#title').removeClass('p25');
	$('#title div').css('display','block');
}

function loadForm()	{	
	$('#layer').css('display','block');
	$('#layer').css('width',$(document).width());
	$('#layer').css('height',$(document).height());
	$('#layer').load('form.html',null,function(){
		$('#layer #close').bind('click',closeLayer);
		$('#layer #submit').click( function(){
                  return ajaxFileUpload();
            });
	});
	
	$('#title').addClass('p25');
	$('#title div').css('display','none');
	
}

function setFileName()  {
      $('#file').attr('value',$(this).val());
}

function ajaxFileUpload()
{
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
      var $author = $('#author').val();
      var $title = $('#pictitle').val();
      var $subtitle = $('#subtitle').val();
      var $email = $('#email').val();
	var $sid = $('#sid').val();
      
	$.ajaxFileUpload	({
		url:'php/upload.php?author='+$author+'&title='+$title+'&subtitle='+$subtitle+'&email='+$email+'&sid='+$sid,
		secureuri:false,
		fileElementId:'filename',
		dataType: 'json',
		success: function (data, status)	{
			closeLayer();
			$.get("php/thumbnail.php?id="+data.id, function(data){
				$('#main div:last').before(data);
				$('#main div:last').prev().bind('click',displayLayer);
			});
      	}
	})
	
	return false;

}

function loadContact()  {
	$('#layer').css('display','block');
	$('#layer').css('width',$(document).width());
	$('#layer').css('height',$(document).height());
	$('#layer').load('contact.html',null, function()	{
		$('#layer #contacttext').bind('click',closeLayer);
	});
      $('#title').addClass('p25');
	$('#title div').css('display','none');
	window.scrollTo(0,0);
  
	return false;
}

function loadImprint()  {
	$('#layer').css('display','block');
	$('#layer').css('width',$(document).width());
	$('#layer').css('height',$(document).height());
	$('#layer').load('imprint.html',null,function()	{
		$('#layer #imprinttext h3').bind('click',closeLayer);
	});
      $('#title').addClass('p25');
	$('#title div').css('display','none');
	window.scrollTo(0,0);
	return false;
}

$(document).ready(function() {
  if (navigator.appVersion.indexOf("MSIE 6.") == -1) {	

      if($('body').height() < 700 )  {
            $('#meta').css('margin-top','90px');
      }
      
      $(window).resize(function(){
            if($('#layer').css('display') == 'block')    {
                  $('#layer').css('width',$(document).width());
                  $('#layer').css('height',$(document).height());
            }
      });
      
      $('#meta #contact').bind('click',loadContact);
      $('#meta #imprint').bind('click',loadImprint);
       
      $('#main').load('php/images.php', null, function(){
            myFadeIn($('#main div:first'));
            $('#upload').bind('click', loadForm);
      });
  }
});

