var width, height;

var image = 0;

var slidetimer = 2500;

var timeout = null;

Event.observe(window, 'resize', function() {
	location.replace('./');
});

Event.observe(window, 'load', function() {
	width = $('content-cell').getWidth();
	height = $('content-cell').getHeight();

	handleSlideshow();
	
	$('content-holder').setStyle({height:height});
	$('content-holder').setStyle({width:width});

	new Ajax.Request('php/get/music.php', {
		method: 'get',
		onSuccess: function(transport) {
			$('music').innerHTML = transport.responseText;
		}
	});
	
	Event.observe('form-login-submit', 'click', function() {
		new Ajax.Request('php/get/login.php', {
			method: 'post',
			parameters: {username:$('username').value, password:$('password').value},
			onSuccess: function(transport) {
				if (transport.responseText > 0) {
					clearTimeout(timeout);
					location.replace('./');
				} else {
					$('form-login-error').innerHTML = 'ERROR: Invalid Login.';
				}
			},
			onFailure: function(transport) {
				$('form-login-error').innerHTML = 'ERROR: Invalid Login.';
			}
			
		});
	});

	$$("div").each(function(el){
		if (el.getAttribute('navigation') == 'true') {
			Event.observe(el.getAttribute('id'), 'click', function(e) {
				new Effect.Fade('new-content', {
					afterFinish: function() {
						new Ajax.Request('php/get/content.php', {
							method: 'get',
							parameters: {id:el.getAttribute('id')},
							onSuccess: function(transport) {
								$('new-content').innerHTML = transport.responseText;
								new Insertion.Bottom('new-content', '<div style="position:absolute; top: 5px; right: 20px; cursor:pointer;" id="new-content-close">[x]</div>');
								
								Event.observe('new-content-close', 'click', function() {
									new Effect.Fade('new-content');
								});								
								$('new-content').setStyle({position:'absolute',top:((height/2)/2),left:((width/2)/2),height:(height/2),width:(width/2),background:'#bc5e00'});
								
								new Effect.Appear('new-content', {to:1});
							}
						});
					}
				});
			});
		}
	})
});

function handleSlideshow() {
	
	if (image == 1) {
		new Effect.Fade('image-1');
		image++;
		timeout = setTimeout('handleSlideshow()', 1000);
		return false;
	}
	
	image++;
	
	var max = 1;
	
	slogan = 0;
	
	if (image == 1) {
//		image = 0;
		slogan = 1;
	}

	new Ajax.Request('php/get/homecontent.php', {
		method: 'get',
		parameters: {image:image, slogan:slogan},
		onSuccess: function(transport) {
			new Insertion.Bottom('home-content',transport.responseText);
			
			var imagewidth = $('image-'+image).getAttribute('width');
			var imageheight = $('image-'+image).getAttribute('height');
			
			new Ajax.Request('php/get/random.php', {
				method: 'get',
				parameters: {low:0,high:width-imagewidth},
				onSuccess: function(t) {
					var randomleft = t.responseText;
					new Ajax.Request('php/get/random.php', {
						method: 'get',
						parameters: {low:0,high:height-imageheight},
						onSuccess: function(t) {
							var randomtop = t.responseText;
							$('image-'+image).setStyle({top:randomtop,left:randomleft});
							if (image == 1) {
								$('image-'+image).setStyle({top:((height-imageheight)/2),left:((width-imagewidth)/2)});
							}
							
							new Effect.Appear('image-'+image, {
								afterFinish: function() {
									if (image == 0) {
										removeimage = 'image-'+ max;
									} else {
										removeimage = 'image-'+(image-1);
									}
									if ($(removeimage)) {
										new Effect.DropOut(removeimage, {
											afterFinish: function() {
												$(removeimage).remove();
											}
										});
									}
									timeout = setTimeout('handleSlideshow()', slidetimer);
								}
							});
						}
					});
				}
			});
		}
	});
}

function mouseOver(which) {
	$(which).removeClassName('home-nav');
	$(which).addClassName('home-nav-hover');
}

function mouseOut(which) {
	$(which).addClassName('home-nav');
	$(which).removeClassName('home-nav-hover');
}

