$(document).ready(function(){
	$('.next').click( function(e) {
		e.preventDefault();
		nextImage();
	});
	
	$('.prev').click( function(e) {
		e.preventDefault();
		$('.photo img:animated').stop(true, true);
/*
		fout = $(".photo img:last");
		fin = $(".photo img:first");
		img = fin.css('display', 'none').detach();
		fout.after(img);
		fin.fadeIn( 1000, function() {
			fout.css('display', 'none');
		});
*/
		fout = $(".photo img:first");
		fin = $(".photo img:last");
		fin.fadeIn( 1000, function() {
			img = fin.detach();
			$(".photo img:first").before(img);
			fout.css( 'display', 'none' );
		});
	});
	
	$('.hide').click( function(e) {
		e.preventDefault();
		$(this).addClass('hidden');
		$('.show').removeClass('hidden');
		$('.space').slideDown('slow');
	});
	
	$('.show').click( function(e) {
		e.preventDefault();
		$(this).addClass('hidden');
		$('.hide').removeClass('hidden');
		$('.space').slideUp('slow');
	});
	
	$('.newstitle a, .newsimg a').click( function(e) {
		e.preventDefault();
		var news = $(this).parents('.news');
		i = news.index() / 3;
		if ( !news.next('.detail').is(":visible") ) {
			slideToggle($('.detail:visible'), false );
			setTimeout( "slideToggle( $('.detail').eq("+i+"), true )", 600 );
			$('.newsimg img').removeClass('hidden');
			$('.newsimg img', news).addClass('hidden');
		}
	});
	
	var shape = Math.floor( Math.random() * 3 ) + 1;
	$('.shape').css( 'background-image', "url('images/background/bg-shape"+shape+".jpg')" );
	
	var imgload = 0;
	$(".photo img").load(function() {
		if ( ++imgload >= 11 ) {
			setTimeout( "var interval=self.setInterval('nextImage()', 7000)", 5000 );
		}
	});
//	setTimeout( "var interval=self.setInterval('nextImage()', 7000)", 5000 );
});


function nextImage() {
/*
	$('.photo img:animated').stop(true, true)
	fout = $(".photo img:last");
	fin = fout.prev();
	fout.fadeOut( 1000, function() {
		img = $(this).detach();
		$(".photo img:first").before(img);
	});
	fin.css('display', 'block');
*/
	$('.photo img:animated').stop(true, true)
	fout = $(".photo img:first");
	fin = fout.next();
	fin.fadeIn( 1000, function() {
		img = fout.detach();
		$(".photo img:last").after(img);
		fout.css( 'display', 'none' );
	});
}

function slideToggle(el, bShow){
  var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
  
  // if the bShow isn't present, get the current visibility and reverse it
  if( arguments.length == 1 ) bShow = !visible;
  
  // if the current visiblilty is the same as the requested state, cancel
  if( bShow == visible ) return false;
  
  // get the original height
  if( !height ){
    // get original height
    height = $el.show().height();
    // update the height
    $el.data("originalHeight", height);
    // if the element was hidden, hide it again
    if( !visible ) $el.hide().css({height: 0});
  }

  // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
  if( bShow ){
    $el.show().animate({height: height}, {duration: 600});
  } else {
    $el.animate({height: 0}, {duration: 600, complete:function (){
        $el.hide();
      }
    });
  }
}


