// <!--
//
var currSub;
var subWatcher;
var subShowTime = 1000;
//
//
//
//
function stripslashes (str) {
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +      fixed by: Mick@el
    // +   improved by: marrtins
    // +   bugfixed by: Onno Marsman
    // +   improved by: rezna
    // +   input by: Rick Waldron
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   input by: Brant Messenger (http://www.brantmessenger.com/)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: stripslashes('Kevin\'s code');
    // *     returns 1: "Kevin's code"
    // *     example 2: stripslashes('Kevin\\\'s code');
    // *     returns 2: "Kevin\'s code"
    return (str+'').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
            case '\\':
                return '\\';
            case '0':
                return '\u0000';
            case '':
                return '';
            default:
                return n1;
        }
    });
}
//
//
function showSub( id ){

	if (currSub!=null){
		setVis(currSub);	
		clearWatch();
	}
	// get current sub
	currSub = document.getElementById("sub"+id);
	//
	setVis(currSub,true);

}
function setVis(el,boo){
	if(el==null)return;
	el.style.display = boo ? "block" : "none";
}
function watchSub(){
	subWatcher = setTimeout("hideSub();",subShowTime);
}
function hideSub(){
	setVis(currSub);
}
function clearWatch(){
	clearTimeout( subWatcher );
}

//
//	image gallery functions
//
var imgWatcher;
var imgShowDuration = 6500;

//
//	news ticker functions
//
var newsTickerTimer;
var newsTickerDuration = 6000;
//
var news_index = 0;
var news_arr;
//
var imagePath = "resources/images/";
var archiveImagePath = imagePath + "gallery/archive/";
// array of iamges
var img_arr = [  
			{thumb: archiveImagePath + "tesla_thumb.jpg", lrg:archiveImagePath + "tesla.jpg", title:"Tesla"},
			
			{thumb: archiveImagePath + "lambo_thumb.jpg", lrg:archiveImagePath + "lambo.jpg", title:"Lamborghini"},
			
			{thumb: archiveImagePath + "aston_thumb.jpg", lrg:archiveImagePath + "aston.jpg", title:"Aston Martin"},
			
			{thumb: archiveImagePath + "truck_thumb.jpg", lrg:archiveImagePath + "truck.jpg", title:"Loading a truck into covered transport"},
			
			 

			{thumb: archiveImagePath + "Audi-TTS_thumb.jpg", lrg:"resources/images/gallery/Audi-TTS.jpg", title:"Audi TTS"},
			
			{thumb: archiveImagePath + "bonneville-dorchester-hotel_thumb.jpg", lrg:"resources/images/gallery/bonneville-dorchester-hotel.jpg", title:"Bonneville Race Car, Dorchester Hotel"},
		
			{thumb: archiveImagePath + "Ferrari-360_thumb.jpg", lrg:"resources/images/gallery/Ferrari-360.jpg", title:"Ferrari 360"},
			
			{thumb: archiveImagePath + "honda-showcar-brackley_thumb.jpg", lrg:"resources/images/gallery/honda-showcar-brackley.jpg", title:"Honda showcar, Brackley"},
			
			{thumb: archiveImagePath + "IMG_0471_thumb.jpg", lrg:"resources/images/gallery/IMG_0471.jpg", title:"Loading an Aston Martin DBRS9"},
			
			{thumb: archiveImagePath + "motorbike_thumb.jpg", lrg:"resources/images/gallery/motorbike.jpg", title:"Motorbike jump"},
			
			{thumb: archiveImagePath + "Picture-003_thumb.jpg", lrg:"resources/images/gallery/Picture-003.jpg", title:"Landrover"},
			
			{thumb: archiveImagePath + "IMG_1349_thumb.jpg", lrg:archiveImagePath + "IMG_1349.jpg", title:""},
			
			{thumb: archiveImagePath + "05112007032_thumb.jpg", lrg:archiveImagePath + "05112007032.jpg", title:""},
			
			{thumb: archiveImagePath + "IMG_1338_thumb.jpg", lrg:archiveImagePath + "IMG_1338.jpg", title:""},
			
			{thumb: archiveImagePath + "Race-car-Simulator1_thumb.jpg", lrg:"resources/images/gallery/Race-car-Simulator1.jpg", title:"Race car simulator"},
			
			{thumb: archiveImagePath + "Untitled-9_thumb.jpg", lrg:"resources/images/gallery/Untitled-9.jpg", title:"Ferrari 360"},
			

			{ thumb: archiveImagePath + "audi_r8_v10_white_thumb.jpg", lrg: archiveImagePath + "audi_r8_v10_white.jpg", title: "Audi R8 v10" },
			
			{ thumb: archiveImagePath + "infinite_g37_thumb.jpg", lrg: archiveImagePath + "infinite_g37.jpg", title: "Infiniti G37s" },
			
			{ thumb: archiveImagePath + "single_covered_transport_thumb.jpg", lrg: archiveImagePath + "single_covered_transport.jpg", title: "Single Covered car transport" },
			         
			{ thumb: archiveImagePath + "brawn_gp_showcar_thumb.jpg", lrg: archiveImagePath + "brawn_gp_showcar.jpg", title: "Brawn Gp Show Car" },
     
			
			{ thumb: archiveImagePath + "audi_r8_v10_thumb.jpg", lrg: archiveImagePath + "audi_r8_v10.jpg", title: "Audi R8 v10" },
			
			{ thumb: archiveImagePath + "aston_race_car_thumb.jpg", lrg: archiveImagePath + "aston_race_car.jpg", title: "Aston Martin Race Car" },
			
			{ thumb: archiveImagePath + "audi_q8_thumb.jpg", lrg: archiveImagePath + "audi_q8.jpg", title: "Audi Q8 Delivery to Geneve, Switzerland" },
			
			{ thumb: archiveImagePath + "audi_r8_pace_cars_thumb.jpg", lrg: archiveImagePath + "audi_r8_pace_cars.jpg", title: "Audi R8 Pace Cars" }
			
			
		];
var imgThumb;
// random number
function getRandomNumber(l){
	return Math.floor( Math.random() * l);
}
/* return random image tag */
function getRandomArchiveImage(){

	// get random number
	var id = getRandomNumber( img_arr.length );
	// pick image data obj
	var imgObj = img_arr[ id ];
	//
	
	$(imgThumb).attr("title",imgObj.title );
	$(imgThumb).attr("href",imgObj.lrg ); //.hide()
	
	$(imgThumb).children().remove();
	
	var img = $('<img/>').attr( { src: imgObj.thumb, alt: imgObj.title } ).css('opacity',0).appendTo( $(imgThumb) ).animate({opacity:1});
}
//
//
//
var doc_hash;
var hashInterval;
//
//
function checkHash(){
	doc_hash = window.location.hash.substr(1);
	//
	//alert(doc_hash)
	$(".collapse_content").each(
	
		function( intindex ){
			var header = $(this).prevAll(".collapse_header").eq(0);
			
			if( header.attr('id') == doc_hash ){
				//change text to state going to
				$(this).slideDown("medium",function(){ 
							header.removeClass("collapse_open");
							header.addClass("collapse_close");
				});
				
			}
		}
	);
}

//
//
function mouseUpCheckHash(){
	clearInterval(hashInterval);
	checkHash();
}
//
//	setup collapsable areas
//
function setupCollapseAreas(){
	//
	//	loops content collapsables and collapses and sets header button state
	//
	function collapseContentOnStart(){
		$(".collapse_content").each(
		
			function( intindex ){
				var header = $(this).prevAll(".collapse_header").eq(0);
				
				if( $(this).hasClass("closed") ){
					header.removeClass("collapse_close");
					header.addClass("collapse_open");
				}else if( $(this).hasClass("open")){
					header.addClass("collapse_close");
					
				}
			}
		);
	}
	//	
	collapseContentOnStart( );
	//
	checkHash();
	
	//	collapsable behaviour
	//
	$(".collapse_header").mouseup( function(){
		var content = $(this).nextAll(".collapse_content").eq(0);
		//
		var hidden = content.is(":hidden");
		var header = $(this);
		//
		if( hidden ){
			//change text to state going to
			content.slideDown("medium",function(){ 
							header.removeClass("collapse_open");
							header.addClass("collapse_close");
						})
		}else{
			content.slideUp("fast",function(){
							header.removeClass("collapse_close");
							header.addClass("collapse_open");
						});
		}
		
	})
}
//
function getNewsTitle(){
	$("#newsticker").fadeOut('fast',
	
		function(){
			$(this).html( stripslashes( news_arr[ news_index ].title ) ).fadeIn();
			$(this).attr("href", "news.php#nid_"+news_arr[ news_index ].id)
			news_index = (news_index == news_arr.length-1) ? 0 : news_index+1;
		}
	)
		

	
	
}
//
function startNewsTicker( arr ){
	news_arr = arr;
	//
	getNewsTitle();
	//
	newsTickerTimer = setInterval("getNewsTitle();",newsTickerDuration );
}
// 
//	NEWS TICKER
//
function initNewsTicker(){
	
	$("#newsticker").html( 'Loading...' );
	// in case called more than once
	clearInterval( newsTickerTimer );
	// show first image
	// getRandomArchiveImage();
	$("#newsticker").hover(
	function(){
		clearInterval( newsTickerTimer );
	},
	function(){
		newsTickerTimer = setInterval("getNewsTitle();",newsTickerDuration );
	}
	
	);
	
	//
	$("#newsticker").mouseup(
		function(){
			if( $('.news_item') != undefined ) hashInterval = setInterval("mouseUpCheckHash();", 500)
		}
	);
	
	// init timer
	//newsTickerTimer = setInterval("getNewsTitle();",newsTickerDuration );
	$.ajax({
		url: 'resources/scripts/getNews.php',
		type: 'POST',
		timeout: 3000,
		error: function(){
		alert('Error loading document');
		},
		success: function(data){
		//alert(xml); //for test
		var json = eval(data);
		startNewsTicker( json );
		}
	});

}
// 
function initArchiveImage(){
	imgThumb = $("#gallery_thumb"); 
	// in case called more than once
	clearInterval( imgWatcher );
	// show first image
	getRandomArchiveImage();
	// init timer
	imgWatcher = setInterval("getRandomArchiveImage();",imgShowDuration);
}
//
//

//
function preloadOverImg( el ){
	// if it doesn not have a preloaded over image already
	if ( el.outSrc == null && el.overImg == null ){
		// store old src
		el.outSrc = el.src;
		// get src length string
		var len = el.outSrc.length;
		// get file type from start image
		var fType = el.outSrc.substring(len-4,len);
		// get start of file name index
		var fNameStartPos = el.outSrc.lastIndexOf("/") + 1;
		// get file name
		var fName = el.outSrc.substring(fNameStartPos,len-4);
		// preload over image
		el.overImg = new Image();
		// set over image source path
		el.overImg.src = new String( imagePath + fName + "_over" + fType );
	}
}
// over image method
function onOverImg(el){ 
	if( el.overImg.src == null ) return;
	el.src = el.overImg.src; 
}
// out image method
function onOutImg(el){ 
	if( el.outSrc == null ) return;
	el.src = el.outSrc;
}
//
//
//
function checkMailSafe( str){
	if (str=="" || str == null)return false;
	var nono = new Array("content-type:", "mime-version:", "content-transfer-encoding:", "subject:", "bcc:", "charset=");
	var txt = str.toLowerCase();
	
	for (var i = 0; i < nono.length ; i++ ){
		if ( txt.indexOf( nono[ i ] ) != -1 || txt == "" ){
		return false;
		}
	}
	return true;
}

function isEmailAddr( email ){
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0)
	{
	var pindex = theStr.indexOf(".",index);
	if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
	}
	return result;
}
function validateEnquiryForm( f ){
	// name
	 if ( !checkMailSafe( f.name.value ) ){
		 alert("Please enter your name");
		 f.name.focus();
		 return false;
	 }
	 /*
	 //job_title
	 if ( !checkMailSafe( f.job_title.value ) ){
		 alert("Please enter your job title");
		 f.job_title.focus();
		 return false;
	 }
	 //company_name
	 if ( !checkMailSafe( f.company_name.value ) ){
		 alert("Please enter your company name");
		 f.company_name.focus();
		 return false;
	 }*/
	 //tel_number
	 if ( !checkMailSafe( f.tel_number.value ) ){
		 alert("Please enter your telephone number");
		 f.tel_number.focus();
		 return false;
	 }
	 //email_address
	 if ( !checkMailSafe( f.email_address.value ) || !isEmailAddr( f.email_address.value ) ){
		 alert("Please enter a valid e-mail address");
		 f.email_address.focus();
		 return false;
	 }
}
//
//	IMAGE VIEWER
//
//
// get url var by name
// 
function getUrlVar( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	//
	if( results == null ){
	    return "";
	 }else{
	    return results[1];
	}
}
//
// loads in image from url vars
//
function loadImageFromURL(){
	var el = document.getElementById( "img" );
	el.src = getUrlVar("imgPath");
	el.width = getUrlVar("w");
	el.height = getUrlVar("h");
	el.alt = document.title = unescape( getUrlVar("title") );
}

// -->
