var $j = jQuery.noConflict();
flashManifestati="";
flashManifestatiDue="";
monitorOn=false;






$j(document).ready(function() {

	//fa schifo e controlla il caricamento del flash splash e della fiamma
flashManifestati=	 function() {
		try {
		
			// SPLASH
			$j('#flashmovieOne').show();
			$j('#flashmovieOne').css('z-index','-1110');
			$j('#flashmovieOne').css('width','0px');
			$j('#flashmovieOne object').css('width','0px');
			myJavascriptObject.accendiSuono();			
			$j('#flashmovieOne').css('width','950px');
			$j('#flashmovieOne object').css('width','950px');
			
			$j('#sfondoFake').fadeOut('fast');
			$j('#flashmovieOne').css('z-index','3');
			$j('#flashmovieOne').fadeIn('slow');
			
			// FIAMMA
			$j('#flashmovieThree').show();
			myJavascriptObject.getFlashMovieById('flashFiamma').fiammaNulla();

			
			// MANINA
			$j('#manina').fadeIn('slow');
			setInterval(function() {
				$j('#manina').animate({left:'-30px'},200,function()
						{
						$j('#manina').animate({left:'-17px'},200); 
						});

			}, 4000);
			
			// BARRA
			if( ($j.cookie("bar"))!="closed" )
			{
				$j('#container').animate({top: '+31px'},200);
				$j('#barContainer').slideDown(200,function () { $j('#barText').fadeIn(); });
			}
			
		} catch(err) {
			setTimeout(
				function() {
					flashManifestati();
				}, 1000);
		}
		
	};
	
	// fine
flashManifestatiDue=function() {
			try {
					$j('#sfondoFake').fadeIn('fast');
					$j('#flashmovieTwo').css('z-index','-1110');
					$j('#flashmovieTwo').css('width','0');
					$j('#flashmovieTwo object').css('width','0');
					$j('#flashmovieTwo').show();
					
					flash = myJavascriptObject.getFlashMovieById('flashHome');
					//flash.kookoo(); meglio una funzione che non fa nulla !!! oppure meglio ancora flash che richiama funzione js non appena caricato ...
					flash.homeNulla();

					try {
						if(!$j.cookie('daccapo'))
						{	
							flash.primaVolta();
							$j.cookie("daccapo", "eccerto", {expires: 15});
						}
						else
						{
							if ($j.cookie('paypal')) {
								flash.payPal();
							} else {
								flash.ricorrente();
							}
						}
					} catch(err) {
						alert(err);
					}
					
				
					setTimeout(function() {
							$j('#flashmovieTwo').css('width','950px');
							$j('#flashmovieTwo object').css('width','950px');
							//$j('#sfondoFake').fadeOut('fast');
							$j('#flashmovieTwo').css('z-index',9);
							$j('#flashmovieTwo').fadeIn('slow');
						}, 3000);
						
					// BARRA
					if( ($j.cookie("bar"))!="closed" )
					{
						$j('#container').animate({top: '+31px'},200);
						$j('#barContainer').slideDown(200,function () { $j('#barText').fadeIn(); });
					}
				
			} catch(err) {
				//alert(err);
				setTimeout(
					function() {
						
						//$j('#flashmovieTwo').hide();
						flashManifestatiDue();
					}, 5000);
			}
		};
		
		
	
	
	// Your code here
		actualFrame = 1;
		
		
		$j('#accensione').click(
		function()
		{

			if(!monitorOn)
			{
				$j('#capsula_pre_splash').fadeIn();
				$j('#macSpento').fadeOut('slow');
				setActualFrame(1);
				start();
				try{
					myJavascriptObject.accensione();
				}
				catch(err)
				{
					
				}
				monitorOn=true;
			}
			$j(this).css('cursor','auto');
			$j(this).css('backgroundImage',"url(./images/pre_splash/accensione_on.png)");
		});
		$j.getJSON("http://jsonip.appspot.com?callback=?",function(data){
			$j('#myIp').text(data.ip);
		});
		
		variables = {};
		variables["flashReady"] = "yes";
		$j('#male').click(
		        function(){
		        	myJavascriptObject.talkToFondo();
		        });
		$j('#female').click(
		        function(){
		        	myJavascriptObject.talkToFondo();
		        });
		$j('#trans').click(
		        function(){
		        	myJavascriptObject.talkToFondo();
		        	myJavascriptObject.trans();
		        });
		$j('.withX').hover(function() {
			$j(this).text('[x]');
			// spostato
			// myJavascriptObject.bipSelecta();
		},
		function() {
			$j(this).text('[ ]');
		}
		);
        $j('#go').click(function() {
			// FIAMMA SPOSTATA A CLICK SU GO
			// aspetto caricamento fiamma
			$j('#flashmovieThree').css('z-index', '10');
			$j('#flashmovieThree').show();
			
			fiamma = myJavascriptObject.getFlashMovieById('flashFiamma').attivaFiamma();
			// tubo etc ...
			myJavascriptObject.talkToFlash();
			// nascondo fiamma e vado a home ...
			setTimeout(function(){
				$j('#flashmovieThree').css('z-index','-10');
				$j.address.path('/home/popup/warning');
			}
			,5000);
        });
        		//Trigger per il select
		$j('select').change(function() {
			if (!isNaN(parseInt($j(this).val()))) {
				$j(getActualFrameId(actualFrame)).hide();

				gotoFrameFunct($j(this).val());
				processFrame();
			}
			//if(parse($j(this).val()))
			});
		$j('.ask a').click(function() {
			if (!isNaN(parseInt($j(this).attr("rel")))) {
				$j(getActualFrameId(actualFrame)).hide();

				gotoFrameFunct($j(this).attr("rel"));
				processFrame();
			}
			//if(parse($j(this).val()))
			});

		//setActualFrame(1);
		//start();

	});

function processFrame() {
	//la stringa dell' id del  frame
	var actualFrameId = getActualFrameId(actualFrame);

	//Variabili delle funzionalita da usare
	var millisec = extractValueFromClass("showFor");
	var variableName = extractValueFromClass("var");
	var colorBg = extractValueFromClass("bgColor_");
	var ifvar = extractValueFromClass("ifvar_");
	var setcookie = extractValueFromClass("setcookie_");
	var ifcookie = extractValueFromClass("ifcookie_");
	var telex = extractValueFromClass("telex");
	var blink = extractValueFromClass("blink");
	
	/****
	 * gestione personalizzata
	 ******/
	if(actualFrame == 19 || actualFrame == 22 || actualFrame == 25 || actualFrame == 28) {
		myJavascriptObject.bipSelecta();
	}
	if(actualFrame == 30)
	{

	}
	if(actualFrame == 32)
	{
		// FIAMMA 
		
		
		$j('.colonna4 a').each(
				function() {
					
					$j(this).attr('href',$j(this).attr('rel'));
					$j(this).css('cursor','pointer');
					$j(this).css('color','#3d3d3d');
				}
				);
		
		$j('.colonna4 a').hover(
				function() {
					$j(this).css('text-decoration','underline');
				},
				function() {
					$j(this).css('text-decoration','none');
				});
		$j('#stillHere').hide();
		$j('#stillHere').css('color', '#FFF');
		$j('#stillHere').fadeIn('slow');
		$j('#comeIn').css('cursor','pointer');
		$j('#comeIn').click(
					        function(){
							if(!IAmAtHome) // ma a che caxxo serve dato che so gia' che non sono a home?
							{
							

								
								$j( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
								$j.address.path('/home/popup/warning');
								
								/* RIVEDI
								$j("#minimizzabile div").fadeOut('fast',function(){$j("#minimizzabile").animate(
								{
									
									height: '20px',
									paddingTop: '0px',
									paddingRight: '0px',
									paddingBottom: '0px',
									paddingLeft: '0px'
								},200,"linear");});
								$j('#minimizza').css("backgroundImage","url(./images/bottone_show_footer.png)");
								*/
								
								/* non si sa mai si volesse vedere l'animazione ...
								myJavascriptObject.talkToFlash();								
								setTimeout(function(){
									$j('#minimizzabile').hide();
									$j('#minimizza').css("backgroundImage","url(./images/bottone_show_footer.png)");
									$j.address.path('/home/popup/warning');
								},5000);
								*/
							}
							else
							{
								//window.location.href="http://www.blacktrend.com/sketch";
							}

					        });
		$j('#comeIn').removeClass('cancel disabled');
	}
	
	
	if (blink != -1) {

		$j(actualFrameId).find('.blink').blink( {
			delay : blink
		});
	}
	if (telex != -1) {
		telexSec = 0.1;
		telexSec = parseInt(telex) / 1000;
		$j(actualFrameId).find('.telex').jTypeWriter( {
			duration : telexSec
		});
	}

	if (setcookie != -1) {
		cookieVal = setcookie.split("_");
		$j.cookie(cookieVal[0], cookieVal[1]);
	}
	if (millisec == -1 && ifcookie != -1) {
		cookieVal = ifcookie.split("_");
		//0:nome
		//1:valore
		//2: "gotoFrame"
		//3: numeroFrame
		if ($j.cookie(cookieVal[0]) == cookieVal[1]) {
			$j(actualFrameId).hide();
			gotoFrameFunct(cookieVal[3]);
			processFrame();
			return false;
		}

	}
	//mostro il frame
	$j(".mac_frame").css('display', 'none');
	$j(actualFrameId).css('display', 'block');
	//parte finale: 
	//nasconde e passa avanti

	if (variableName == "BlueColor" && variables[variableName] == true) {
		$j(actualFrameId).css("background", "blue");
	}

	if (colorBg != null) {
		$j(actualFrameId).css("background", colorBg);
	}

	if (millisec != -1) {
		setTimeout(function() {
			$j(actualFrameId).hide();

			var gotoFrame = extractValueFromClass("gotoFrame");
			if (gotoFrame != -1) {

				gotoFrameFunct(gotoFrame);
				processFrame();
			} else {
				if (ifcookie != -1) {
					cookieVal = ifcookie.split("_");
					//0:nome
				//1:valore
				//2: "gotoFrame"
				//3: numeroFrame
				if ($j.cookie(cookieVal[0]) == cookieVal[1]) {
					$j(actualFrameId).hide();
					gotoFrameFunct(cookieVal[3]);
					processFrame();

				} else {

					if (hasNext(actualFrame)) {
						//alert(actualFrame);
						actualFrame++;
						processFrame();
					}
				}

			} else {
				if (ifvar != -1) {
					varVal = ifvar.split("_");
					//0:nome
					//1:valore
					//2: "gotoFrame"
					//3: numeroFrame
					if (variables[varVal[0]] == varVal[1]) {
						$j(actualFrameId).hide();
						gotoFrameFunct(varVal[3]);
						processFrame();

					} else {
						if (hasNext(actualFrame)) {
							//alert(actualFrame);
							actualFrame++;
							processFrame();
						}
					}
				} else {
					if (hasNext(actualFrame)) {
						//alert(actualFrame);
						actualFrame++;
						processFrame();
					}
				}
			}
		}
	}, parseInt(millisec));
	}

	$j(actualFrameId).queue(function() {
		//$j(this).css('background','red');
			//$j(this).dequeue();
		});

};
function numberToStr(num, stringLength) {

	numString = "";
	if (num < 10) {
		numString = "0" + num;
	} else {
		numString += num;

	}

	return numString;
}
function start() {
	processFrame();
};
function setActualFrame(newActualFrame) {
	actualFrame = newActualFrame;
};

function gotoFrameFunct(frameNumber) {
	actualFrame = parseInt(frameNumber);
	//	processFrame();
}
function getActualFrameId(actualFrameNumber) {
	return '#frame_' + numberToStr(actualFrameNumber);
}
function hasNext(actualFra) {
	//alert(getActualFrameId(actualFra+1));
	if ($j(getActualFrameId(actualFra + 1)).length != 0) {
		////alert("esiste "+getActualFrameId(actualFra+1));
		return true;
	}
	return false;
}
function typeWriter() {

}
function extractValueFromClass(classPrefix) {
	var actualFrameId = getActualFrameId(actualFrame);
	var result = -1;
	regExpr = "^" + classPrefix;

	classesArray = $j(actualFrameId).attr('class').split(' ');
	//millisec=-1;
	$j.each(classesArray, function(v) {
		if (this.match(regExpr))
			result = this.substring(classPrefix.length);
	});

	return result;
}

