var IntervalID = 0;		// Holds handle identifier for setInterval
var IP = 0;			// Interval Position - Counter like i
var EffectLock = 0;
var ChangeingLang = false;
var ImageNames = new Array(	"Images/Cutted/Center.jpg",
										"Images/Cutted/Center_Left.jpg",
										"Images/Cutted/Center_Right.jpg",
										"Images/Cutted/Footer.jpg",
										"Images/Cutted/Head.jpg",
										"Images/Cutted/Menu_Div.jpg",
										"Images/Cutted/Atoms/Bottom_Center.jpg",
										"Images/Cutted/Atoms/Bottom_Left.jpg",
										"Images/Cutted/Atoms/Bottom_Right.jpg",
										"Images/Cutted/Atoms/Center_Left.jpg",
										"Images/Cutted/Atoms/Center_Right.jpg",
										"Images/Cutted/Atoms/Footer_Gradient.jpg",
										"Images/Cutted/Atoms/Head_Banner.jpg",
										"Images/Cutted/Atoms/Head_Gradient.jpg",
										"Images/Cutted/Atoms/Menu_Center.jpg",
										"Images/Cutted/Atoms/Menu_Left.jpg",
										"Images/Cutted/Atoms/Menu_Right.jpg",
										"Images/Cutted/Atoms/Top_Center.jpg",
										"Images/Cutted/Atoms/Top_Left.jpg",
										"Images/Cutted/Atoms/Top_Right.jpg",
										"Images/Cutted/Main_Bg.jpg");

var ProgressBarInc = Math.round( 382 / ImageNames.length ) - 1;  // progress bar increment counting
var ImageProcessing = false;
var LoadedImage	= 0;
var ContentUrl = "Content/sk/Company.php";
var Language = "sk";

if (window.XMLHttpRequest)																			// check browser capabilities (Mozilla)
	var LoadContentRequest = new XMLHttpRequest()
else if (window.ActiveXObject)																	// (Explorer)
{
	try	{	var LoadContentRequest = new ActiveXObject("Msxml2.XMLHTTP")	} 
	catch (e)
	{
		try	{	var LoadContentRequest = new ActiveXObject("Microsoft.XMLHTTP")	}
		catch (e){}
	}
}

function PreloadImage( ImageSrc )										// preloads one single image & self firing func
{
	if ( ImageProcessing == true )										// if some image is currently loading
	{
		setTimeout ( "PreloadImage( " + ImageSrc + ")", 50);
	}
	else
	{
		ImageProcessing = true;
		if (navigator.appName == "Netscape")						// if browser is netscape-type prepare onload call
			{ document.getElementById( "Preload_Image" ).onload = function() { CheckImageLoad(); }; }
		else
		  setTimeout ( "CheckImageLoad() ", 50 );

		document.getElementById( "Preload_Image" ).src=ImageSrc;
	}
}
	
function CheckImageLoad( Try )											// checking if image is loaded completly for non-netscape browsers
{
if (Try == 1) alert('asd');
	if ( ( document.getElementById( "Preload_Image" ).complete ) || ( Try == 10 ) )
	{
		ImageProcessing = false;
		if (LoadedImage < ImageNames.length-1)		// when needs to preload next image
		{
			LoadedImage++;
	 		SetElementPosSize( "Atom_Center_Inside"   , -191 + ProgressBarInc*LoadedImage, 23, 382 - ProgressBarInc*LoadedImage, 24);
			PreloadImage( ImageNames[LoadedImage] );
		}
		else																			// when all images are loaded ********
		{

			ParseUrl();
	   	LoadContent( ContentUrl );

			var FlashPlayer = new SWFObject("Flash/MusicMiniPlayer.swf", "MusicMiniPlayer", "53", "30", "7", "#8c1e1e");
			FlashPlayer.addVariable("autoPlay", "yes");
			FlashPlayer.addVariable("soundPath", "Flash/song.mp3");
			FlashPlayer.write("Player");

			setTimeout( "StartAnimation(0,550,20);", 500 );
		}
 	}
	else																				// image is loading now - set delay for next check
	{
		if (typeof(Try) == 'undefined') Try = 1;
		setTimeout ( "CheckImageLoad("+(++Try)+") ", 50 );
	}

}

function StartAnimation( StartPos, StopPos, IntervalLength )
{
	IP = StartPos;
	IntervalID	= setInterval ( "AnimationIteration( " + StopPos + ")" , IntervalLength );
	document.getElementById( "Atom_Center_Inside" ).style.display = "none";
}

function AnimationIteration( IntervalStopPos )
{
	IP += 5;
	if (IP < 200)																										//Horizontal extruding
	{
		//											ElementID						MarginLeft	MarginTop			Width			Height
 		SetElementPosSize( "Atom_Top_Center"      ,   -197-IP,     -101,   394+IP*2,       18 );
		SetElementPosSize( "Atom_Top_Left"        ,   -215-IP,     -101,         18,       18 );
		SetElementPosSize( "Atom_Top_Right"       ,    197+IP,     -101,         18,       18 );
		SetElementPosSize( "Atom_Head_Gradient"   ,   -197-IP,      -83,   394+IP*2,      105 );
		SetElementPosSize( "Atom_Menu_Center"     ,   -187-IP,       22,   374+IP*2,       26 );
		SetElementPosSize( "Atom_Menu_Left"       ,   -197-IP,       22,         10,       26 );
		SetElementPosSize( "Atom_Menu_Right"      ,    187+IP,       22,         10,       26 );
		SetElementPosSize( "Atom_Center_Left"     ,   -215-IP,      -83,         18,      166 );
		SetElementPosSize( "Atom_Center_Right"    ,    197+IP,      -83,         18,      166 );
 		SetElementPosSize( "Atom_Footer_Gradient" ,   -197-IP,       48,   394+IP*2,       35 );
 		SetElementPosSize( "Atom_Bottom_Center"   ,   -197-IP,       83,   394+IP*2,       18 );
		SetElementPosSize( "Atom_Bottom_Left"     ,   -215-IP,       83,         18,       18 );
		SetElementPosSize( "Atom_Bottom_Right"    ,    197+IP,       83,         18,       18 );
	}
	else if (IP == 200)																								// Switch from Atoms to Normal
	{
 		SetElementPosSize( "Atom_Center_Inside"   ,      -392,          48,      784,         0 );
 		SetElementOpacity( "Atom_Center_Inside", 100);

 		document.getElementById( "Header"               ).style.display = "block";
 		document.getElementById( "Left_Border"          ).style.display = "block";
		document.getElementById( "Right_Border"         ).style.display = "block";
		document.getElementById( "Atom_Center_Inside"   ).style.display = "block";
	 	document.getElementById( "Footer"               ).style.display = "block";

		document.getElementById( "Atom_Top_Center"      ).style.display = "none";
		document.getElementById( "Atom_Top_Left"        ).style.display = "none";
		document.getElementById( "Atom_Top_Right"       ).style.display = "none";

		document.getElementById( "Atom_Head_Gradient"   ).style.display = "none";
		document.getElementById( "Atom_Center_Left"     ).style.display = "none";
		document.getElementById( "Atom_Center_Right"    ).style.display = "none";

		document.getElementById( "Atom_Menu_Center"     ).style.display = "none";
		document.getElementById( "Atom_Menu_Left"       ).style.display = "none";
		document.getElementById( "Atom_Menu_Right"      ).style.display = "none";
		document.getElementById( "Atom_Footer_Gradient" ).style.display = "none";

		document.getElementById( "Atom_Bottom_Center"   ).style.display = "none";
		document.getElementById( "Atom_Bottom_Left"     ).style.display = "none";
		document.getElementById( "Atom_Bottom_Right"    ).style.display = "none";
	}
	else if (IP < 300)																								// Vertical Extruding ( before Head reaches top:0 )
	{
 		SetElementPosSize( "Header"               ,      -415, -101-IP+200,     830,        149 );
 		SetElementPosSize( "Left_Border"          ,      -415,   48-IP+200,      24, (IP-200)*2 );
 		SetElementPosSize( "Right_Border"         ,       391,   48-IP+200,      24, (IP-200)*2 );
 		SetElementPosSize( "Atom_Center_Inside"   ,      -392,   48-IP+200,     784, (IP-200)*2 );
 		SetElementPosSize( "Footer"               ,      -415,    1+IP-200,     830,         54 );
	}
	else if (IP < 550)																								// Vertical Extruding ( before Head reaches top:0 )
	{
 		SetElementPosSize( "Left_Border"          ,      -415,         -47,      24,   (IP-100) );
 		SetElementPosSize( "Right_Border"         ,       391,         -47,      24,   (IP-100) );
 		SetElementPosSize( "Atom_Center_Inside"   ,      -392,         -47,     784,   (IP-100) );
 		SetElementPosSize( "Footer"               ,      -415,  101+IP-300,     830,         54 );
	}

	if (IntervalStopPos <= IP)
	{
		clearInterval( IntervalID );
  		ShowContent( "Main" );
		if (Language == "sk" ) { LoadContent( "Content/sk/Menu.php" ); ShowContent( "Menu_Bar" ); }
		if (Language == "en" ) { LoadContent( "Content/en/Menu.php" ); ShowContent( "Menu_Bar" ); }
 		SetElementOpacity( "Main", 0);
		document.getElementById( "Main" ).style.display = "block";
 		SetElementOpacity( "Menu_Bar", 0);

		document.getElementById( "Menu_Bar" ).style.display = "block";

		ElementFadeIn( "Menu_Bar",10, function()
		{
	  	ElementFadeIn( "Main", 10, function()
			{
				document.getElementById( "Atom_Center_Inside" ).style.display = "none";
				document.getElementById( "Player" ).style.display = "block";
				SetActiveButton();
			});
 		});

	}
}

function SetActiveButton()		// Set Main Menu Active Btn Color
{
	     if ( document.location.hash == '#Firma' )		{ document.getElementById('Btn_1').style.color = '#d7c8c8'; }
	else if ( document.location.hash == '#Company' )	{ document.getElementById('Btn_1').style.color = '#d7c8c8'; }
	else if ( document.location.hash == '' )				{ document.getElementById('Btn_1').style.color = '#d7c8c8'; }

	else if ( document.location.hash == '#Produkty' )	{ document.getElementById('Btn_2').style.color = '#d7c8c8'; }
	else if ( document.location.hash == '#Products' )	{ document.getElementById('Btn_2').style.color = '#d7c8c8'; }

	else if ( document.location.hash == '#Partneri' )	{ document.getElementById('Btn_3').style.color = '#d7c8c8'; }
	else if ( document.location.hash == '#Partners' )	{ document.getElementById('Btn_3').style.color = '#d7c8c8'; }

	else if ( document.location.hash == '#Kontakt' )	{ document.getElementById('Btn_4').style.color = '#d7c8c8'; }
	else if ( document.location.hash == '#Contact' )	{ document.getElementById('Btn_4').style.color = '#d7c8c8'; }
	else if ( document.location.hash == '#Odosli' )		{ document.getElementById('Btn_4').style.color = '#d7c8c8'; }
	else if ( document.location.hash == '#Send' )		{ document.getElementById('Btn_4').style.color = '#d7c8c8'; }
	else { document.getElementById('Btn_2').style.color = '#d7c8c8'; }
}

function SetElementPosSize ( ElementID, EMarginLeft, EMarginTop, EWidth, EHeight )
{
  var ElementStyle = document.getElementById( ElementID ).style;
	ElementStyle.marginLeft	= EMarginLeft + "px";
	ElementStyle.marginTop	= EMarginTop + "px";
	ElementStyle.width	= EWidth + "px";
	ElementStyle.height	= EHeight + "px";
}

function SetElementOpacity( ElementID, Opacity)
{
	var ElementStyle = document.getElementById( ElementID ).style;
	ElementStyle.opacity = (Opacity / 100);
 	ElementStyle.MozOpacity = (Opacity / 100);
 	ElementStyle.KhtmlOpacity = (Opacity / 100);
 	ElementStyle.filter = "alpha(opacity=" + Opacity + ")";
} 

function ElementFadeIn( ElementID, IterationDelay, Action )
{
	if ( EffectLock == 0 )
	{
		EffectLock = 1;

		for ( i = 0; i <= 100; i=i+2 )
		{
			setTimeout( "SetElementOpacity( '" + ElementID + "' , " + i + " )" , i * IterationDelay );
		}
		setTimeout( "EffectLock = 0;" , 102 * IterationDelay );
		if ( Action != "none" ) { setTimeout( Action , 104 * IterationDelay ); }
	}
}

function ElementFadeOut( ElementID, IterationDelay, Action )
{
	if ( EffectLock == 0 )
	{
		EffectLock = 1;

		for ( i = 100; i >= 0; i=i-2 )
		{
			setTimeout( "SetElementOpacity( '" + ElementID + "' , " + i + " )" , (100-i) * IterationDelay );
		}
		setTimeout( "EffectLock = 0;" , 102 * IterationDelay );
		if ( Action != "none" ) { setTimeout( Action , 104 * IterationDelay ); }
	}
}

function ParseUrl()
{
// 	Slovak Content
	if 	(document.location.hash == "#Firma")		{ Language = "sk"; ContentUrl="Content/sk/Company.php"; }
	else if (document.location.hash == "#Produkty")		{ Language = "sk"; ContentUrl="Content/sk/Products.php"; }
	else if (document.location.hash == "#IS")		{ Language = "sk"; ContentUrl="Content/sk/IS.php"; }
	else if (document.location.hash == "#Servery")		{ Language = "sk"; ContentUrl="Content/sk/Server.php"; }
	else if (document.location.hash == "#Siete")		{ Language = "sk"; ContentUrl="Content/sk/Net.php"; }
	else if (document.location.hash == "#Hardver")		{ Language = "sk"; ContentUrl="Content/sk/Hardware.php"; }
	else if (document.location.hash == "#Weby")		{ Language = "sk"; ContentUrl="Content/sk/Web.php"; }
	else if (document.location.hash == "#Partneri")		{ Language = "sk"; ContentUrl="Content/sk/Partners.php"; }
	else if (document.location.hash == "#Kontakt")		{ Language = "sk"; ContentUrl="Content/sk/Contact.php"; }
	else if (document.location.hash == "#Odosli")		{ Language = "sk"; } // Url builded with parameters in func SendMsg() (sk/Contact.php)
// 	English Content
	else if (document.location.hash == "#Company")		{ Language = "en"; ContentUrl="Content/en/Company.php"; }
	else if (document.location.hash == "#Products")		{ Language = "en"; ContentUrl="Content/en/Products.php"; }
	else if (document.location.hash == "#Information_Systems")	{ Language = "en"; ContentUrl="Content/en/IS.php"; }
	else if (document.location.hash == "#Servers")		{ Language = "en"; ContentUrl="Content/en/Server.php"; }
	else if (document.location.hash == "#Net")		{ Language = "en"; ContentUrl="Content/en/Net.php"; }
	else if (document.location.hash == "#Hardware")		{ Language = "en"; ContentUrl="Content/en/Hardware.php"; }
	else if (document.location.hash == "#Web")		{ Language = "en"; ContentUrl="Content/en/Web.php"; }
	else if (document.location.hash == "#Partners")		{ Language = "sk"; ContentUrl="Content/en/Partners.php"; }
	else if (document.location.hash == "#Contact")		{ Language = "en"; ContentUrl="Content/en/Contact.php"; }
	else if (document.location.hash == "#Send")		{ Language = "en"; } // Url builded with parameters in func SendMsg() (en/Contact.php)
	else { Language = "sk"; ContentUrl="Content/sk/Company.php"; }
}

function LoadContent( ContentFileUrl )
{
	LoadContentRequest.open( "GET", ContentFileUrl , false );
 	LoadContentRequest.setRequestHeader("User-Agent",navigator.userAgent);
	LoadContentRequest.send(null);
}

function ShowContent( ElementID )
{
	if ( LoadContentRequest.readyState == 4 )
	{
		document.getElementById( ElementID ).innerHTML = LoadContentRequest.responseText;
	}
	else
	{
		setTimeout("ShowContent( " + ElementID + " )",100);
	}
}

function LoadPage ( ContentID )
{
		document.location.hash = ContentID;
		ParseUrl();
   		LoadContent( ContentUrl );
		document.getElementById( "Atom_Center_Inside" ).style.background = "#d7c8c8";
		document.getElementById( "Atom_Center_Inside" ).style.display = "block";
		ElementFadeOut( "Main", 5, function()
		{
			ShowContent( "Main" );
			ElementFadeIn( "Main", 5, function ()
			{
				document.getElementById( 'Atom_Center_Inside' ).style.display = 'none';
				ChangeingLang = false;

			});
		});
}

function ChangeLanguage( NewLang )
{
	if ( ( Language != NewLang ) && ( ChangeingLang == false ) )
	{
		ChangeingLang = true;
		Language = NewLang;
		//Switching Menu Bar Content
		if (Language == "sk" ) LoadContent( "Content/sk/Menu.php" );
		if (Language == "en" ) LoadContent( "Content/en/Menu.php" );
 		ElementFadeOut( "Menu_Bar", 5,
			function()
			{
				ShowContent( "Menu_Bar" );
				ElementFadeIn( "Menu_Bar", 5, function()
					{
						// 	Switching fom Slovak to English Content
						if 			(document.location.hash == "#Firma")			LoadPage ( "Company" );
						else if (document.location.hash == "#Produkty")		LoadPage ( "Products" );
						else if (document.location.hash == "#IS")		LoadPage ( "Information_Systems" );
						else if (document.location.hash == "#Servery")		LoadPage ( "Servers" );
						else if (document.location.hash == "#Siete")		LoadPage ( "Net" );
						else if (document.location.hash == "#Hardver")		LoadPage ( "Hardware" );
						else if (document.location.hash == "#Weby")		LoadPage ( "Web" );
						else if (document.location.hash == "#Kontakt")		LoadPage ( "Contact" );
						else if (document.location.hash == "#Odosli")		LoadPage ( "Contact" );
						// 	Switching fom English to Slovak Content
						else if (document.location.hash == "#Company")		LoadPage ( "Firma" );
						else if (document.location.hash == "#Products")		LoadPage ( "Produkty" );
						else if (document.location.hash == "#Information_Systems") LoadPage ( "IS" );
						else if (document.location.hash == "#Servers")		LoadPage ( "Servery" );
						else if (document.location.hash == "#Net")		LoadPage ( "Siete" );
						else if (document.location.hash == "#Hardware")		LoadPage ( "Hardver" );
						else if (document.location.hash == "#Web")		LoadPage ( "Weby" );
						else if (document.location.hash == "#Contact")		LoadPage ( "Kontakt" );
						else if (document.location.hash == "#Send")		LoadPage ( "Kontakt" );
						// 	no location.hash specified
						else if ( Language == "en") LoadPage ( "Company" );
						else LoadPage( "Firma" );
						SetActiveButton();
					} );
			} );
	}
}

function SwapImg( ImgName, NewImgSrc )
{
	document.Images[ImgName].src = NewImgSrc;
}

function SendMessage()
{
	var Email = document.getElementById( "Email" ).value;
	var Message = document.getElementById( "Message" ).value.split("\n");
	ContentUrl =  "Content/" + Language + "/Contact.php?LineCount=" + (Message.length+1) + "&Email=" + Email;

	for (i=0; i< Message.length; i++)
	    ContentUrl += "&Msg" + i + "=" + Message[i];
	    
	alert(ContentUrl);

	if(Language=="sk")
		LoadPage("Odosli")
	else	LoadPage("Send");

	return false; // zabrani action formu
}

