/* Author: Richard Czeiger
   GrafX Design Division Pty Ltd
*/

var timer;
var timerOn = 0;

function randomise() {
	var random = Math.floor(Math.random()*95) + 1;
	var random = (random<10) ? '0'+random : random ;
	var random = '#client-'+random+' a';

	effectEnter(random);
	setTimeout(function () {
		effectLeave(random)
	}, 700);

	timer = setTimeout(randomise, 400);
}
function startRandom() {
	if (!timerOn) {
		timerOn = 1;
		randomise();
	}
}
function stopRandom() {
	clearTimeout(timer);
	timerOn = 0;
}


function effectEnter(element) {
	if ($(element).find('img').hasClass('bottom')) {
		$(element).stop(true, false).animate({
			opacity: '1.0',
			'z-index': '5'
		}, 150);
	} else {
		$(element).stop(true, false).animate({
			height: '80px',
			left: '-10px',
			opacity: '1.0',
			top: '-10px',
			width: '80px'
		}, 150, function(){
                	$(this).css({'z-index' : '5'});
            	});
	}
}
function effectLeave(element) {
	$(element).animate({
		height: '60px',
		left: '0',
		opacity: '0.4',
		top: '0',
		width: '60px'
	}, 150, function(){
		$(this).css({'z-index' : '1'});
	});
}


$(document).ready(function() {

	$('#x').addClass('effect');

	$('#x a, nav a').click(function(e){
		e.preventDefault();
	});


	if (($.browser.version < 7) && ($.browser.msie)) {
	} else {


		// Replace backgrounds with images
		$('#x li a').each(function(index) {
			var imageALT = $(this).text();
			var imageURL = $(this).css('backgroundImage');
			var imageURL = imageURL.replace(/"/g,"").replace(/url\(|\)$/ig, "");

			$(this).addClass('has-image');
			$(this).html('<span class="client">'+imageALT+'</span>');
			$(this).append('<img src="'+imageURL+'" class="dynamic" border="0" title="'+imageALT+'" alt="'+imageALT+'" />');

			if ($(this).attr('href')!='') {
				// Popup
				$(this).colorbox({
					title: function(){
						var thistitle = $(this).text();
						return thistitle;
					}
				});
			}

		});

		/* // Set up reflection
		for(i=84; i<=95; i++) {
			$('#client-'+i+' img').reflect({'height':'0.5', 'opacity':'0.5'}).addClass('bottom');
		} */

		// Hover effect
		$('#x li a').hover(
			function(){
				stopRandom();
				effectEnter(this);
			},
			function(){
				effectLeave(this)
				timer = setTimeout(startRandom, 2000);
			}
		);

		// Randomise
		startRandom();

		// Navigation highlights
		$('#nav a').hover(
			function(){
				stopRandom();
				navID = $(this).parent().attr('id');
				$('#x li.'+navID).each(function(index) {
					clientID = $(this).attr('id');
					effectEnter('#x li#'+clientID+' a');
				});
			},
			function(){
				navID = $(this).parent().attr('id');
				$('#x li.'+navID).each(function(index) {
					clientID = $(this).attr('id');
					effectLeave('#x li#'+clientID+' a');
				});
				timer = setTimeout(startRandom, 2000);
			}
		);

	}
});






