var maxRandomDelay = 15000;
var lastRandom = [0,0,0,0,0,0];
var carIMGS = new Array();
var homeIMGS = new Array();
var yachtIMGS = new Array();
var corporateIMGS = new Array();
var corporate2IMGS = new Array();
var corporate3IMGS = new Array();
var home1IMGS = new Array();
var home2IMGS = new Array();
var home3IMGS = new Array();

var carIndex = 0;
var homeIndex = 0;
var yachtIndex = 0;
var corporateIndex = 0;
var corporate2Index = 0;
var corporate3Index = 0;
var home1Index = 0;
var home2Index = 0;
var home3Index = 0;

//Google Maps
function initialize() {
    var myLatlng = new google.maps.LatLng(51.376405, -0.09664);
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
    
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map
    }); 
  }
  
//PR slide effect
function pressReleases(){
	$('#press-releases li h3').click(function(){
		$(this).toggleClass('open');
		$(this).next('div').slideToggle();  
		return false; 
	});
}

//Login slide effect
function loginSlide(){	
	if ( $('span.ErrorLabel').text().length == 0 ){
		$('.account-form').hide();
	}
	 
	$('#account-button').toggle(function(){
		$(this).css('background-position','0px -27px');
		$('.account-form').show();
	}, function(){
		$(this).css('background-position','0px -1px');
		$('.account-form').hide();
	});
	return false;
}


//BANNER FUNCTIONS

function fadeThisImage(id,imgArray){
	var altID = "";
	if(id.indexOf("a")==-1){
		altID = id.replace("b","a");
	}
	else{
		altID = id.replace("a","b");
	}
	if ($("#"+altID+">img")[0].complete && isNotVisible($("#"+altID+">img")[0])){
		
		$("#"+id).fadeOut("slow",function(){
			$("#"+altID).addClass("topMost");
	
			eval("var arrLength = "+imgArray+".length;");
			eval("var rand = Math.round(Math.random()*"+arrLength+");");	
			eval("var imgName = "+imgArray+"["+rand+"]");

			$(this).removeClass("topMost").html("<img src='"+imgName+".jpg' alt='' width='336' height='260' />");
			$(this).show();
			lastRandom.pop();
			lastRandom.unshift(rand);
			setTimeout("fadeThisImage('"+altID+"','"+imgArray+"')",Math.round(Math.random()*maxRandomDelay));
		});
	}
	else{
		setTimeout("fadeThisImage('"+id+"', '"+imgArray+"')",Math.round(Math.random()*maxRandomDelay));
	}
}

function isNotVisible(img)
{
	var topMosts = $(".topMost img");
	var notVisible = true;
	if(topMosts.length>3)
	{
		topMosts.each(function(){
			if($(this)[0].src==img.src){
				notVisible = false;
			}
		});
	}
	return notVisible;
}

function populateIMGArrays(){

	var imgName = 0;
	var iSearch = 0;
	var iMaxJPEG = 0;
	var iJPEGCount = 0;

	iMaxJPEG = images.length;
	iMaxJPEG = iMaxJPEG - 1;

while (iJPEGCount < iMaxJPEG)
   {
    imgName = images[iJPEGCount];

    //Check for Car image
    iSearch = imgName.search(/Car/);

    if(iSearch != -1) {
	carIMGS[carIndex] = imgName;
	carIndex ++;
     }

    //Check for Home image
    iSearch = imgName.search(/Home/);

    if(iSearch != -1) {
	homeIMGS[homeIndex] = imgName;
	homeIndex ++;
     }

    //Check for Yacht image
    iSearch = imgName.search(/Yacht/);

    if(iSearch != -1) {
	yachtIMGS[yachtIndex] = imgName;
	yachtIndex ++;
     }
	 
	//Check for Corporate images
    iSearch = imgName.search(/left/);

    if(iSearch != -1) {
	corporateIMGS[corporateIndex] = imgName;
	corporateIndex ++;
     }
	 //Check for Corporate2 images
    iSearch = imgName.search(/middle/);

    if(iSearch != -1) {
	corporate2IMGS[corporate2Index] = imgName;
	corporate2Index ++;
     }
	 //Check for Corporate3 images
    iSearch = imgName.search(/right/);

    if(iSearch != -1) {
	corporate3IMGS[corporate3Index] = imgName;
	corporate3Index ++;
     }
	 //Check for Home1 images
    iSearch = imgName.search(/home1/);

    if(iSearch != -1) {
	home1IMGS[home1Index] = imgName;
	home1Index ++;
     }
	  //Check for Home2 images
    iSearch = imgName.search(/home2/);

    if(iSearch != -1) {
	home2IMGS[home2Index] = imgName;
	home2Index ++;
     }
	  //Check for Home3 images
    iSearch = imgName.search(/home3/);

    if(iSearch != -1) {
	home3IMGS[home3Index] = imgName;
	home3Index ++;
     }

    iJPEGCount ++;
   }

}

function startSlideshowImage(imgArray, position){
	var imagesLoaded = 0;
	eval("var noImages = " + imgArray + ".length - 1;")
	
	//Add class to indicate topmost elements
	$("#si-" + position + "a").addClass("topMost").hide();

	//Load images into empty divs
	$("#si-" + position + "b").each(function(){
		$(this).hide();
	eval("var rand = Math.floor(Math.random()*" + imgArray + ".length);");
	$(this).append("<img src='"+eval(imgArray+"[rand]")+".jpg' alt='' width='336' height='260' />");
 
	lastRandom.pop();
	lastRandom.unshift(rand);	
	});

	$("#si-" + position + "a img, #si-" + position + "b img").load(function(){
		imagesLoaded++;
		//if(imagesLoaded==noImages)
		if(imagesLoaded==2)
		{

			$("#si-" + position + "a").fadeIn("normal",function(){$("#si-" + position + "b").fadeIn();});
			
			//trigger fade behaviour
			$("#si-" + position + "a").each(function(){
				var thisid = $(this)[0].id;
				setTimeout("fadeThisImage('"+thisid+"', '"+imgArray+"')",Math.round(Math.random()*maxRandomDelay));
			});
		}
	});	
}


$(document).ready(function(){
    if($("#map").length>0){
        initialize();
    }

    if($("#press-releases").length>0)
    {
	pressReleases();
    }
	if($(".account-form").length>0)
    {
	loginSlide();
    }
    
    if($("#photo-animation").length>0)
    {
		images.pop(); //Remove null element
		populateIMGArrays();
    	startSlideshowImage('homeIMGS', 1);
    	startSlideshowImage('carIMGS', 2);
    	startSlideshowImage('yachtIMGS', 3);
    }
	if($("#photo-animation2").length>0)
    {
		
		images.pop();//Remove null element
		populateIMGArrays();
		startSlideshowImage('corporateIMGS', 1);
		startSlideshowImage('corporate2IMGS', 2);
		startSlideshowImage('corporate3IMGS', 3);
	}
	
	if($("#photo-animation3").length>0)
    {
		
		images.pop();//Remove null element
		populateIMGArrays();
		startSlideshowImage('home1IMGS', 1);
		startSlideshowImage('home2IMGS', 2);
		startSlideshowImage('home3IMGS', 3);
	}
	
});



