// JavaScript Home page

function getRandomIndex(object) {
	var rand = (Math.random() * object.length);
	
	return Math.floor(rand);
}

function deleteFromObject(object, index) {
	var myNewObject = [];

	for (var i in object) {
		if (typeof object[i] != 'undefined' && typeof object[i] != 'function' && i != index) {
			myNewObject.push(object[i]);
		}
	}
	
	return myNewObject;
}

function fillImageBoxes() {
	$j('.box>img').each(function() {
		var index = getRandomIndex(images);
		var image = images[index];
		images = deleteFromObject(images, index);
		$j(this).attr('src', '/imf' + image + '?mask=145x145').attr('alt', image);
	});
	
	setTimeout('fillImageBox()', 2000);
}

function fillImageBox() {
	var boxes = $j('.box>img');
	var boxindex = getRandomIndex(boxes);
	var box = $j('.box>img:eq(' + boxindex + ')');
	var index = getRandomIndex(images);
	var image = images[index];
	images = deleteFromObject(images, index);
	images.push(box.attr('alt'));
	
	box.fadeOut(1200, function() {
		$j(this).remove();
	});
	box.clone().attr('src', '/imf' + image + '?mask=145x145').css({'display':'none','position':'absolute','left':'0','top':'0'}).appendTo(box.parent()).fadeIn(1200);
	
	setTimeout('fillImageBox()', 2000);
}

var flag = "";

$j(function(){

	$j('.menuBox').mouseenter(function() {
		if ($j('#lightHomeBox').css('display') == 'none' && flag == ""){
			$j('#lightHomeBox').css({'display':'block','opacity':'0'});
			$j('.hoverBox', this).css({'display':'block','opacity':'0'});
			$j('#lightHomeBox').animate({opacity: 0.8}, '2000');
			$j('.hoverBox', this).animate({opacity: 1}, '500');
			$j(this).css('z-index', '110');
			flag = $j(this);
		}
	});
	
	$j('.menuBox .hoverBox').mouseleave(function() {
		if ($j(this).parent().html() == flag.html()) {
			$j(this).animate({opacity: 0}, '200', function() {
				$j('#lightHomeBox').css('display','none');
				$j(this).css('display','none');		
			});				
			$j(this).parent().css('z-index', '1');
			flag = "";
		}
	});
	
	
	$j('.videoBox img').click(function() {
		$j('#overShadow').css('display', 'block');
		$j('#overVideo').css('display', 'block');	
		if ( $j("#homeVideo").length > 0 ){
			
			var videoHTML = $j("#homeVideo object").attr('data');
			$j('#overVideo').html("  \
				<span id='closer'></span> \
				<object width='800' height='596' type='application/x-shockwave-flash' wmode='transparent' data='"+videoHTML+"' allowfullscreen='true' style='visibility: visible;'> \
				<param name='movie' value='"+videoHTML+"'> \
				<param value='true' name='allowFullScreen'> \
				<param name='wmode' value='transparent'>  \
				<param name='flashvars' value='autostart=true&screencolor=000000'>  \
				</object>  \
			");
			
			$j('#closer').click(function() { closeVideo(); });
		}
	});
	
	
	function closeVideo(){
		$j('#overShadow').css('display', 'none');
		$j('#overVideo').css('display', 'none');
	}
	
	$j('#overShadow').click(function() { closeVideo(); });
	
	fillImageBoxes();
	
	$j('.menuBox, .box, .freeBox, .videoBox').addClass('itemSelector');
		
	$j('#home').masonry({columnWidth: 20, itemSelector: '.itemSelector'});
	
});
