var randArr = new Array();
var currentFoto = 1;
var currentKey = 1;
window.onload = init;
function init () {
	$(".menu_bg>a").hover(
      function () {
		var className = $(this).attr("id");
		$(this).parent().children(".fire").css("display", "block");
        $(this).parent().children(".fire").addClass(className);
      }, 
      function () {
        var className = $(this).attr("id");
		$(this).parent().children(".fire").css("display", "none");
        $(this).parent().children(".fire").removeClass(className);
      }
    );
	$(".reset").hover(
	  function () {
        $(this).css("background", "url(/images/content_button_bg.jpg)");
      }, 
      function () {
        $(this).css("background", "none");
      }
    );
	init_home ();
}
function init_home () {
    currentKey = 1;
    getArray();
	currentFoto = randArr[0];
	changeFoto (1);
}
function waitTime() {
	setTimeout(changeFoto, 3000);
}

/* new */
function getArray()
{   var i = 0;
   	$(".home_fotos a img").each(function() {
        i++;
	    randArr[i-1] = i;
	});
    randArr = shuffle(randArr);
}

function shuffle (arr)
{
    b = false;
    var i = arr.length, j, t;
    while( i )
    {
        j = Math.floor( ( i-- ) * Math.random() );
        t = b && typeof arr[i].shuffle!=='undefined' ? arr[i].shuffle() : arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    if(arr[0]==currentFoto)  shuffle(arr);

    return arr;
};

function changeFoto (first)
{
       currentFoto = randArr[currentKey];
       if(currentKey==randArr.length)
       {    currentKey = 1;
    		topChange = 0;
            //randArr = shuffle (randArr);
       }
       else
       {   	topChange = -(currentFoto-1) * 394;
       }
//       alert(currentFoto);

        currentKey++;

    	i = 0;
    	var thisFoto;
    	var thisFotoObj;
    	$(".home_fotos a img").each(function() {
    		i = i+1;
    		thisFotoObj = $(this);
    		if (i == currentFoto) return false;
    	});

    	thisFoto = thisFotoObj.attr("src");

     if(first!==1) {

		$(".loader").hide();
    	$(".home_fotos").animate( { opacity:0 }, 700, null, function () {
    		var img = document.createElement('img');
    		img.onload = function ()
    		{
    			$(".home_fotos").css("top", topChange+"px");
    			$(".home_fotos").animate( { opacity:1 }, 700, null, function () {
    				waitTime ();
    			});
    		}
    		$(img).attr('src', thisFoto);
    	});
     }
     else
     {
    		var img = document.createElement('img');
    		img.onload = function ()
    		{
                $(".home_fotos").css( {visibility:"visible", opacity:0} );
    			$(".loader").hide();
    			$(".home_fotos").css("top", topChange+"px");
    			$(".home_fotos").animate( { opacity:1 }, 700, null, function () {
    				waitTime ();
    			});
    		}
    		$(img).attr('src', thisFoto);
    		$(".loader").show();
     }


}

/*
function changeFoto () {
	var i = 0;
	$(".home_fotos>img").each(function() {
		i=i+1;
	});
	var fotos = i;

	if (currentFoto == fotos) {
		topChange = 0;
		currentFoto = 1;
	} else {
		topChange = parseInt($(".home_fotos").css("top").substr(0, $(".home_fotos").css("top").length-2)) - 394;
		currentFoto = currentFoto +1;
	}

	i = 0;
	var thisFoto;
	var thisFotoObj;
	$(".home_fotos>img").each(function() {
		i = i+1;
		thisFotoObj = $(this);
		if (i == currentFoto) return false;
	});
	thisFoto = thisFotoObj.attr("src");

	$(".home_fotos").animate( { opacity:0 }, 700, null, function () {
		var img = document.createElement('img');
		img.onload = function ()
		{
			$(".loader").hide();
			$(".home_fotos").css("top", topChange+"px");
			$(".home_fotos").animate( { opacity:1 }, 700, null, function () {
				waitTime ();
			});
		}
		$(img).attr('src', thisFoto);
		$(".loader").show();
	});
}
*/
