/*!
 * the vizfader (fullscreen image fader)
 * 2010 Gian Carlo Mingati | gcmingati.net
 * Version: 0.9.9 (8-JULY-2010)
 * Requires: jQuery v1.4.1 or later
 * Built for modostudio.eu - vizlab.it
 * 
 */


$(function(){
	
	var cntrller = $('<div id="controller"><div id="pl"><\/div><div id="pr"><ul><li><a href="#" id="prev">&nbsp;<\/a><\/li><li><a href="#" id="pp">&nbsp;<\/a><\/li><li><a href="#" id="next">&nbsp;<\/a><\/li><\/div><\/div>')
	var ldr = $('<img style="padding-top: 7px" src="images/progress.gif" \/>');
	var ttelem = $('<div class="ttcontainer"><p id="ttleft">&nbsp;</p><p id="ttcontent"></p><p id="ttright">&nbsp;</p><\/div>');
	$(ttelem).insertAfter($(cntrller)).css({'top': $(window).height() - 80, 'left': 20+'px', 'position': 'absolute', 'display': 'block'});
	$(cntrller).insertAfter("#vizfader").css({'top': $(window).height() - $(cntrller).height(), 'left': $(window).width()/2-$(cntrller).width()/2});
	$(cntrller).find('#pp').addClass("pause");
	$(cntrller).find('#pl').css('height',$(cntrller).height()).append($(ldr));
	($.browser.msie)? $('html').css({'overflow': 'hidden'}) : null;
	($(window).width() >= 800) ? $('#header').width($(window).width()-40) : $('#header').width(800);
	$('#header #hr').width($('#header').width() - $('#header #hl').width());
	
	var images = new Array();
	var alts = new Array();
	var count = 0;
	var indexLoaded = 1;
	var $cSlide;
	var manual = false;
	var rev = false;


	//$.getJSON("js/gallerydata.on", function(data){
	$.getJSON("js/gallerydata.js", function(data){

			$.each(data.items, function(i,item){
			images[i] = data.items[i].url;	
			alts[i] = data.items[i].alt;	

			var img = new Image();
	  	$(img).load(function () {	
		        	indexLoaded += 1; 
		        	if( indexLoaded >= 3 && indexLoaded <= 3)
		        		init();
		        }).error(function () {
		      }).attr('src', images[i]);
			});
	});
	
	
	
	
	$(window).bind("resize", function(){
		resize($('#vizfader img'));
		($(window).width() >= 800) ? $('#header').width($(window).width()-40) : $('#header').width(800);
		$('#header #hr').width($('#header').width() - $('#header #hl').width());
		$(cntrller).css({'top': $(window).height() - $(cntrller).height(), "left": $(window).width()/2-$(cntrller).width()/2});
		$(ttelem).css({'top': $(window).height() - 80});
	});
	
	
	function init(){
		  $(cntrller).find('#pl').html((count + 1) +"/"+images.length);		
		  var $p = $('<img src="'+images[count]+'" />');   		
			$p.appendTo('#vizfader').attr({'id': count, 'alt': alts[count]}).css({'opacity': '0'}).attr('alt', alts[count]);
			$(ttelem).find('#ttcontent').html($p.attr('alt'));
			resize($p);
			$cSlide = $p;
			$p.animate({opacity: 1}, 700, function() {
	  		slideshow_interval = setInterval(advance, 3000);
	  	});
	  };
	
	
	function advance(){	
			clearInterval(slideshow_interval);
			var $ps = $('#vizfader img');
			$ps.attr('id', count).animate({opacity: 0}, 700, function() {
		  	$ps.remove();
		  });
			if(!rev)
				(count != images.length-1) ? count++ : count = 0; 
	
			$(cntrller).find('#pl').html((count + 1) +"/"+images.length);
			var $ns = $('<img src="'+images[count]+'" />');
			$ns.attr({'id': 'g'+ count, 'alt': alts[count]});
		  $ns.appendTo('#vizfader').attr('id', count).css({'opacity': '0'});
		  $(ttelem).find('#ttcontent').html($ns.attr('alt'));
		  resize($ns);
			$cSlide = $ns;
	  	$ns.animate({opacity: 1}, 700, function() {
				(!manual) ? slideshow_interval = setInterval(advance, 3000) : null;	 	
		  });
	}
	
	function pause(){
		$cSlide.stop(true, true);
		clearInterval(slideshow_interval);
		$('#controller #pp').removeClass('pause').addClass('play'); 
	}
	
	function fwd(){
		slideshow_interval = setInterval(advance, 0); 
		$('#controller #pp').removeClass('play').addClass('pause'); 
	}		
	
	
	// control links
	$('#controller #pp').click(function() {
		rev = false;
		if (!manual){
			pause();
			manual = true;
		} else {
			fwd();
			manual = false;
		}
		return false;
	});
	
	$('#controller #next').click(function() {
		pause();
		manual = true;
		rev = false;
		slideshow_interval = setInterval(advance, 0); 
		return false;
	});
	
	$('#controller #prev').click(function() {
		pause();
		manual = true;
		rev = true;
		(count >= 1) ? count-- : count = images.length-1; 
		slideshow_interval = setInterval(advance, 0); 
		$('#controller #pp').removeClass('pause').addClass('play'); 
		return false;
	});

//jquery.amail plugin
	$('#cnct').amail('||','|','hackthis');
}); // ends ready


function resize(e){
	var ratio = 768 / 1280;	
	var browserwidth = $(window).width();
	var browserheight = $(window).height();
			if ((browserheight/browserwidth) > ratio){
		    e.height(browserheight);
		    e.width(browserheight / ratio);
		} else {
		    e.width(browserwidth);
		    e.height(browserwidth * ratio);
		}
		e.css('left', (browserwidth - e.width())/2);
		e.css('top', (browserheight - e.height())/2);
}

