﻿
var lightColors = ['blue', 'cyan', 'lime', 'orange', 'purple','red', 'yellow' ];
var lights = [];
var lastCreatedLightColorIndex = -1;
var minDelayForRandomLightSwitch = 100;
var maxDelayForRandomLightSwitch = 400;


$(document).ready( function() {
	
	initLights();
	initAddToBookmarks();
	initSetAsHomepage();
	
	preloadHovers();
	
} );
$(document).ready(  );



function fillWithLights( $aContainer, aNumLights ) {
	
	var i;

	for(i=0;i<aNumLights;i++) {

		addLight( $aContainer );

	}

}


function addLight( $aContainer ) {

	var newLightState;
	var newLightColorIndex;
	var newLightColor;
	var $newLight;

	newLightState = (Math.random()>0.5) ? 'on' : 'off' ;

	do {
		newLightColorIndex = Math.floor( Math.random() * lightColors.length );
	} while( newLightColorIndex==lastCreatedLightColorIndex );

	newLightColor = lightColors[ newLightColorIndex ];
	
	$newLight = $('<img src="img/lights/'+newLightColor+'_'+newLightState+'.jpg" class="light" />');

	$aContainer.append( $newLight );

	lights.push( $newLight);

	lastCreatedLightColorIndex = newLightColorIndex;

}


function initLights() {

	fillWithLights( $('#topLights'), 9 );
	fillWithLights( $('#leftLights'), 1 );
	fillWithLights( $('#rightLights'), 1 );
	fillWithLights( $('#bottomLights'), 9 );

	
	prepareNextRandomLightWitch();

}


function switchRandomLight() {

	var $targetLight;
	var targetLightUrl;
	
	$targetLight = lights[ Math.floor( Math.random() * lights.length ) ];
	targetLightUrl = $targetLight.attr('src');

	if( targetLightUrl.indexOf('_on.jpg')>-1 )
		targetLightUrl = targetLightUrl.replace( /_on\.jpg/, '_off.jpg' );
	else
		targetLightUrl = targetLightUrl.replace( /_off\.jpg/, '_on.jpg' );

	$targetLight.attr('src', targetLightUrl);


	prepareNextRandomLightWitch();

}


function prepareNextRandomLightWitch() {

	var nextDelay;

	nextDelay = minDelayForRandomLightSwitch + ( Math.random() * (maxDelayForRandomLightSwitch - minDelayForRandomLightSwitch) );
	setTimeout( 'switchRandomLight();', nextDelay );

}


function initAddToBookmarks() {

	var userAgent, isChrome;
	
	userAgent = navigator.userAgent;
	isChrome = (userAgent.indexOf('Chrome')>-1) ? true : false ;
	
	if( ! isChrome )
		$('#ad .bookmarks').wrapInner('<a href="javascript:addToBookmarks()"></a>');
	
}


function initSetAsHomepage() {
	
	if( document.all )
		$('#ad .home').wrapInner('<a href="javascript:setAsHomepage()"></a>');
	
}

function setAsHomepage() {
	
	if( document.all ) {
		
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(location.href);
		
	}
	
}


function addToBookmarks() {
	
	var title, url;
	
	title = 'Fortune Sushi';
	url = 'http://www.fortuneshushi.com/';
	
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url, ''); 
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}


function preloadHovers() {
	
	var i;
	var currentImg;
	var imgsArray;
	
	imgsArray = [ 'img/flags/en_on.jpg', 'img/flags/en_off.jpg', 'img/flags/fr_on.jpg', 'img/flags/fr_off.jpg' ];
	
	for( i=0;i<imgsArray.length;i++) {
		
		currentImg = new Image();
		currentImg.src = imgsArray[i];
		
	}
	
}