load_all_images = false;
load_window = false;
timer = 0;
snd = false;


function firelion_request(url)
{
	if (url.indexOf('?') == -1)
	{
		url += '?';
	}
	
	url += '&ajax=1';
	
	$("loadimg").style.display = "block";
	
	new Ajax(url, {
		method: 'post',
		//if method is post, you can write parameters here.
		//Can be a querystring, an object or a Form element.
		//postBody: $('myForm'),
		onComplete: firelion_request_handler
		//onStateChange: myStateChecker.bindAsEventListner(this),
		//$(element) to insert the response text of the XHR into, upon completion of the request.
		//update: $('windowbody')
		//Execute scripts in the response text onComplete.
		//evalScripts: true
	}).request();
}

function firelion_reload()
{
	new Asset.images(
		[
			'./templates/lightbox/overlay.png',
			'./templates/lightbox/loading.gif',
			
			'./templates/images/window_body.png',
			'./templates/images/window_header.png',
			'./templates/images/window_header.png',
			'./templates/images/menu_footer.gif',
			'./templates/images/menu_body.gif',
			'./templates/images/menu_footer.gif',
			'./templates/images/layout1/body.png',
			'./templates/images/layout1/footer.png',
			'./templates/images/layout1/header.png',
			'./templates/images/layout2/back.png',
			'./templates/images/layout2/infobox1.png',
			'./templates/images/layout2/menuback2.png',
			'./templates/images/layout2/menuback3.png',
			'./templates/images/layout2/menuback.png',
			'./templates/images/menu/about_us_0.gif',
			'./templates/images/menu/about_us_1.gif',
			'./templates/images/menu/contact_0.gif',
			'./templates/images/menu/contact_1.gif',
			'./templates/images/menu/customers_0.gif',
			'./templates/images/menu/customers_1.gif',
			'./templates/images/menu/downloads_0.gif',
			'./templates/images/menu/downloads_1.gif',
			'./templates/images/menu/news_0.gif',
			'./templates/images/menu/news_1.gif',
			'./templates/images/menu/services_0.gif',
			'./templates/images/menu/services_1.gif',
			'./templates/images/menu/products_0.gif',
			'./templates/images/menu/products_1.gif'
		],
		
		{
    onComplete: function(){
        load_all_images = true;
        
        firelion_hideloading();
    }
  	}
	);
	
}

function firelion_request_handler(a)
{
	if ($type(a) == 'string')
	{
		var b = a.split("\n")
		
		var content = "";
		for (var i = 1;i < b.length;i++)
		{
			content += b[i] + "\n";
		}
		
		$('caption').innerHTML = b[0];
		$('windowbody').innerHTML = content;
	}
	
	$("loadimg").style.display = "none";
	
	load_window = true;
	firelion_hideloading();
	
	try
	{
		snap_preview_anywhere.onload();
	}
	catch (ex) {}
	
	snd = true;
	
	var link = $$('a');
	var i;
	
	for (i = 0;i < link.length;i++)
	{
		if (/index\.php\?m=.*/.test(link[i].href) && link[i].href.indexOf("#") == -1)
		{
			//console.log(link[i].href);
			link[i].onclick = function ()
			{
				firelion_request(this.href); return false;
			};
			
			//alert(link[i].onclick) // = "firelion_request(this.href);";
		}
		else if (link[i].getAttribute("rel") == "lightbox")
		{
			link[i].onclick = function ()
			{
				showLightbox(this);
				return false;
			};
		}		
	}
	
	var imgs = $$("a[rel=lightbox] img");
	
	for (i = 0;i < imgs.length;i++)
	{
		imgs[i].onmouseover = function()
		{
			if (this.hover) return;
			this.hover = true;
			
			fx = new Fx.Style(this, 'opacity', {
				duration: 200, 
				transition: Fx.Transitions.quartInOut
			});
			
			fx.start(0.5,1);
		};
			
		imgs[i].onmouseout = function()
		{
			if (!this.hover) return;
			this.hover = false;
			
			fx = new Fx.Style(this, 'opacity', {
				duration: 200, 
				transition: Fx.Transitions.quartInOut
			});
			fx.start(1,0.5);
			
		};
			
		imgs[i].setOpacity(0.5);
	}
	
}

function searchbox_attachevent()
{
	obj = $('searchinput');
	
	obj.addEvent('focus',searchbox_focus);
	obj.addEvent('keypress',searchbox_keypress);
}

function contact_submit(f)
{
	
	url = 'index.php?m=contact&ajax=1';
	
	$("loadimg").style.display = "block";
	
	new Ajax(url, {
		method: 'post',
		//if method is post, you can write parameters here.
		//Can be a querystring, an object or a Form element.
		postBody: $('contactfrm'),
		onComplete: firelion_request_handler
		//onStateChange: myStateChecker.bindAsEventListner(this),
		//$(element) to insert the response text of the XHR into, upon completion of the request.
		//update: $('windowbody')
		//Execute scripts in the response text onComplete.
		//evalScripts: true
	}).request();
}

function searchbox_launch()
{
	if ($('searchinput').value.trim() == '')
	{
		alert("Please enter your keyword");
		return;
	}
	
	firelion_request('index.php?m=search&k=' + $('searchinput').value);
}

function searchbox_focus()
{	
	this.select();
	if (this.value == "Search ...")
	{
		this.value = "";
	}
}

function searchbox_keypress(k)
{
	event = new Event(k);
	
	if (event.key == 'enter')
	{
		searchbox_launch();
	}
}

function nav_click(obj)
{
	i = 0;
	while (o = $("mnu_" + i))
	{
		o.className = "";
		i++;
	}
	
	obj.className = "menuselected";
	
	return false;
}

function firefox_showloading()
{
	
	document.write('<div id=loading2 style="background-image: url(templates/lightbox/overlay.png);display: block; position: absolute; top: 0pt; left: 0pt; z-index: 90; width: 100%; height: 100%;">' + "<div id=loading style=\"position: absolute; top: 0pt; left: 0pt; z-index: 91; background-color: white; padding: 0px; border: 1px solid silver;font-weight: small; color: gray;\"><img src='templates/lightbox/loading.gif'/></div>"+ '</div>');
	
	timer = firefox_centerload.periodical(50);
}

function firefox_centerload()
{
	o = $('loading');
	o2 = $('loading2');
	
	wsize = window.getSize();
	esize = o.getSize();

	//alert(wsize['y'] / 2);
	
	o2.setStyle('height',wsize.scrollSize.y + "px");
	
	o.setStyle('top',((wsize.scroll.y) + (wsize.size.y / 2 - esize.size.y) + "px"));
	o.setStyle('left',((wsize.scroll.x) + (wsize.size.x / 2 - esize.size.x) + "px"));
}

function firelion_hideloading()
{
	if (!load_window || !load_all_images) return;
	
	if ($("loadimg")) $("loadimg").style.display = "none";
        
    if ($('loading'))
    {
     	$('loading').style.display = 'none';
      	$('loading2').style.display = 'none';
       	$clear(timer);
    }
}

firefox_showloading();
firelion_reload();

window.addEvent("load",firelion_request_handler);
window.addEvent("load",searchbox_attachevent);