var count = 0;
var blocca=false;
var image = new Array();
var run_ani=false ;
var ore_p6 = new Array('6','12','18','24','30','36','42','48','54','60','66','72','78','84','90','96','102','108','114','120','126','132','138','144','150','156','162','168','174','180','186','192');
var ore_192 = new Array('0','6','12','18','24','30','36','42','48','54','60','66','72','78','84','90','96','102','108','114','120','126','132','138','144','150','156','162','168','174','180','186','192');
var menuore ;
var statiche=false;
var lasttype ;
var preloading=false ;
var fade=1500;
/*
$(window).bind('load', function() {

$('#homegfs').Carousel(
				{
					itemWidth: 75,
					itemHeight: 75,
					itemMinWidth: 20,
					items: 'a',
					reflections: .9,
					rotationSpeed: 3,
					slowOnHover: true
			}

);
$('#homegfs').show();
});
*/
function fadechange(vel){
if (vel == 0 ) { fade = 1 }
if (vel == 1 ) { fade = 2000 }
if (vel == 2 ) { fade = 1500 }
if (vel == 3 ) { fade = 500 }
}

function loaddrop (zona) {
		$('#root-menu-div').unbind();
		$('#root-menu-div').empty();
		var options = {minWidth: 120, arrowSrc: 'img/arrow_right.gif',onClick: function(e, menuItem){ $.Menu.closeAll(); }};

		if ( zona == 'it' ) { $('#menudrop>img').menu(options, '#menuitalia'); 	}
		if ( zona == 'eu' ) { $('#menudrop>img').menu(options, '#menueuropa');  }

		$('#root-menu-div').hover(  function () {}, function () { $.Menu.closeAll(); } );

}


function loadmenu () {
$('#loader').hide();
$('#ore > li').unbind();
if ( menuore == 'p6' ) { $('#ore').replaceWith('<ul id="ore"><li>06</li><li>12</li><li>18</li><li>24</li><li>30</li><li>36</li><li>42</li><li>48</li><li>54</li><li>60</li><li>66</li><li>72</li><li>78</li><li>84</li><li>90</li><li>96</li><li>102</li><li>108</li><li>114</li><li>120</li><li>126</li><li>132</li><li>138</li><li>144</li><li>150</li><li>156</li><li>162</li><li>168</li><li>174</li><li>180</li><li>186</li><li>192</li></ul>') ; }
if ( menuore == '192' ) { $('#ore').replaceWith('<ul id="ore"><li>00</li><li>06</li><li>12</li><li>18</li><li>24</li><li>30</li><li>36</li><li>42</li><li>48</li><li>54</li><li>60</li><li>66</li><li>72</li><li>78</li><li>84</li><li>90</li><li>96</li><li>102</li><li>108</li><li>114</li><li>120</li><li>126</li><li>132</li><li>138</li><li>144</li><li>150</li><li>156</li><li>162</li><li>168</li><li>174</li><li>180</li><li>186</li><li>192</li></ul>') ; }



$('#ore > li').addClass('normale');
$('#ore > li').hover(  function () { if ( $(this).hasClass('normale') ) { $(this).attr('class','hover'); } }, function () { if ( $(this).hasClass('hover') ) { $(this).attr('class','normale'); }   } );


$('#ore > li').click(function () {
		if ( run_ani==true ) { stop(); }
		if ( blocca == false ) {
			$('#ore > li').attr('class','normale');
    		$(this).attr('class','selected');
			blocca=true;
			date = new Date();
			millisec = date.getTime();
			if (statiche) {
				$('.fade').append('<img class="nuova" src="'+image[$("#ore > li").index(this)]+'"/>');
			}
			else {
				$('.fade').append('<img class="nuova" src="'+image[$("#ore > li").index(this)]+'&v='+millisec+'"/>');
			}
			count = $("#ore > li").index(this);
			$('#loader').show();
			$('.nuova').load(function () {
						$('#loader').hide();
						$('.nuova').fadeIn(fade,function () {
						$('.corrente').remove(); $('.nuova').attr('class','corrente');
						blocca=false;

						}
					);
				}
			);
		}
    });

}

function setsize (tipo) {
	if ( tipo == 1 ) {
		$('.container').css("height","851px");
		$('.container').css("width","814px");
		$('.fade').css("height","851px");
		$('.fade').css("width","1628px");
	}
	if ( tipo == 2 ) {
		$('.container').css("height","747px");
		$('.container').css("width","866px");
		$('.fade').css("height","747px");
		$('.fade').css("width","1732px");

	}

}
function loaddata (type) {
	$('#minigfs').hide();
	lasttype = type ;

	if ( run_ani==true ) { stop(); }
		if ( blocca == false ) {

//		count = 0;
		image = [];
		run_ani=false ;

		var rif = ore_192 ;
		menuore = '192' ;
		immagini = 32
		if ( type.slice(0,6) == 'prec6h' ) { rif = ore_p6 ; menuore = 'p6' ; immagini = 31 }
//		if ( type.slice(-5,-3) == 'cf' ) { rif = ore_cf ; menuore = 'cf' ;}
//		if ( type.slice(-6,-3) == 'p12' ) { rif = ore_p12 ; menuore = 'p12' ;}
//		if ( type == 'T2m_eu' ) { rif = ore_192 ; menuore = '192' ; }
		date = new Date();
		millisec = date.getTime();
		for ( i=0; i<=immagini; i++ ) {
		image[i] = "http://gfs.meteonetwork.it/"+type+"_"+rif[i]+".000001.png"+"?c="+millisec;
		}

		loadmenu();

		if ( $('#homegfs').css("display") == 'block' ) { $('#homegfs').hide(); }
		if ( $('#footerzone').css("display") == 'none' ) { $('#footerzone').fadeIn('2000'); }
		if ( $('.topmenu').css("display") == 'none' ) { $('.topmenu').fadeIn('2000'); }
		blocca=true;
		if (statiche) {
			preload ("on") ;
			$('.fade').append('<img class="nuova" src="'+image[count]+'"/>');


		}
		else {
			$('.fade').append('<img class="nuova" src="'+image[count]+'&v='+millisec+'"/>');
		}
		$('#loader').show();
		$('.nuova').load(function () {
				if (!preloading) {
					blocca=false;
					$('#loader').hide();
					}
<!--				$('.time').html(image[count].slice(-7,-4));    -->
					$('.nuova').fadeIn(fade,function () {
					$('.corrente').remove(); $('.nuova').attr('class','corrente');

					attiva(count);
					}
				);
			}
		);
		}

}


function avanti() {
	if ( (count + 1) <= ( image.length - 1) && blocca == false ) {
		blocca=true;
		date = new Date();
		millisec = date.getTime();
		if (statiche) {
			$('.fade').append('<img class="nuova" src="'+image[++count]+'"/>');
		}
		else {
			$('.fade').append('<img class="nuova" src="'+image[++count]+'?v='+millisec+'"/>');
		}
		$('#loader').show();
		$('.nuova').load(function () {
					$('#loader').hide();
					blocca=false;

<!--				$('.time').html(image[count].slice(-7,-4));-->
						$('.nuova').fadeIn(fade,function () {
 						$('.corrente').remove();
						$('.nuova').attr('class','corrente');
						attiva(count);
						}
				);
			}
		);
	}
}

function indietro() {

	if ( (count - 1) >= 0 && blocca == false ) {
		blocca=true;
		date = new Date();
		millisec = date.getTime();
		if (statiche) {
			$('.fade').append('<img class="nuova" src="'+image[--count]+'"/>');
		}
		else {
			$('.fade').append('<img class="nuova" src="'+image[--count]+'?v='+millisec+'"/>');
		}
		$('#loader').show();
		$('.nuova').load(function () {
				$('#loader').hide();
<!--				$('.time').html(image[count].slice(-7,-4));-->
				$('.nuova').fadeIn(fade,function () {
					$('.corrente').remove();
					$('.nuova').attr('class','corrente');
					blocca=false;
					attiva(count);
					}
				);
			}
		);

	}
}


function play() {
	if (run_ani == false) {
		$(".play").toggle();
		$(".stop").toggle();

	}
	if ( (count + 1) <= ( image.length - 1) && blocca == false  ) {
		blocca=true;
		date = new Date();
		millisec = date.getTime();
		if (statiche) {
			$('.fade').append('<img class="nuova" src="'+image[++count]+'"/>');
		}
		else {
			$('.fade').append('<img class="nuova" src="'+image[++count]+'?v='+millisec+'"/>');
		}


		$('#loader').show();
		$('.nuova').load(function () {
		$('#loader').hide();
			$('.time').html(image[count].slice(-7,-4));
			$('.nuova').fadeIn(fade,function () {
					$('.corrente').remove();
					$('.nuova').attr('class','corrente');
					blocca=false;
					attiva(count);
					}
			);

			animazione=setTimeout("play()",3000);
			run_ani=true;
			}
		);
	}
	else { stop(); }
}


function stop() {
	$('#loader').hide();
	$(".play").toggle();
	$(".stop").toggle();
	run_ani=false;
	clearTimeout(animazione);
	blocca = false;

}

function attiva(id) {
$('#ore > li').attr('class','normale');
$('#ore > li:eq('+id+')').attr('class','selected');
}


function preload (stato) {


	if (stato == 'on') {
	$('#title').text('GFS by MeteoNetwork - Run Statico');
	    preloading = true;
		$('.preloadon').hide();
		$('.preloadoff').show();

		$('#loader').show();
		blocca = true ;
		statiche=true;
		var i = 0 ;
		var totimages = image.length  ;


		var numimages = 0 ;
		images = new Array();

		$.each(image, function() {
			images[i] = new Image();
			images[i].src = this;
			images[i].onload = function() {
				++numimages ;
				if (numimages == totimages) {
				$('#loader').hide();
				blocca = false ;
				preloading = false;
				}

			}
			++i;
		    });
		$("#ore > li").hover(
		  function () {

		  },
		  function () {
		  		   	if ( run_ani==true ) { stop(); }
					if ( blocca == false ) {
						$('#ore > li').attr('class','normale');
			    		$(this).attr('class','selected');
						blocca=true;
						date = new Date();
						millisec = date.getTime();
						$('.fade').append('<img class="nuova" src="'+image[$("#ore > li").index(this)]+'"/>');
						count = $("#ore > li").index(this);
						$('#loader').show();
						$('.nuova').load(function () {
									$('#loader').hide();
									$('.nuova').fadeIn(1,function () {
									$('.corrente').remove(); $('.nuova').attr('class','corrente');
									blocca=false;

									}
								);
							}
						);
				}
		  }
		);


	}
	else {
			$('#title').text('GFS by MeteoNetwork - Run Live');
			statiche=false;
			$('.preloadoff').hide();
			$('.preloadon').show();
			loaddata(lasttype);

	}


}

