// JOYRIDE WordPress Theme Javascript Functions
d=document;
window.onload=startUp;

// SITE PAGE DETECTION AND FUNCTION CALLS
function startUp(){
	defineBrowser(navigator.userAgent);
	
	cufonFonts();
	
	// note you can call tehm seperately still because they also have IDs.
	
	$('.slideshow img').css('display','block');
	
	$('#headerPhotos.slideshow li').css({opacity: 0.0});
	$('#headerPhotos.slideshow li:first').addClass('shown').css({opacity: 1.0});
	
	$('#headerPhotos').everyTime(5000, 'main-rotation', function() {
		continueRotation('#headerPhotos.slideshow li');
	});
	
	$('#callout.slideshow a').css({opacity: 0.0});
	$('#callout.slideshow a:first').addClass('shown').css({opacity: 1.0});
	
	$('#callout').everyTime(4000, 'rotation', function() {
		continueRotation('#callout.slideshow a');
	});
	
	
	//mouseover pause
	$('#headerPhotos').mouseover(function(){
      	$(this).stopTime('main-rotation');
    }).mouseout(function(){
		$('#headerPhotos').everyTime(5000, 'main-rotation', function() {
			continueRotation('#headerPhotos.slideshow li');
		});
  	});
	
	if(d.getElementById("article")){
		$('.content .article a').lightBox({fixedNavigation:true});
  	}
	
	if(d.getElementById("news")){
		$('.article a.localMotors').lightBox({fixedNavigation:true});
		$('.article a.bots').lightBox();
  	}
	
	$('#full h1').parent().css("background","none");
}

// CUFON FONT FOR TITLES, NAV AND OTHER ITEMS - ADD NEW ITEMS HERE
function cufonFonts() {
	Cufon.replace('h1');
}


// BROWSER DETECTION - ADDS CLASSES TO DOM FOR BROWSER SPECIFIC STYLES
function defineBrowser(u){
	var ua = u.toLowerCase();
	is=function(t){return ua.indexOf(t)>-1;};
	g='gecko';
	w='webkit';
	s='safari';
	o='opera';
	h=document.getElementsByTagName('html')[0];
	b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):
		is('firefox/2')?g+' ff2':
		is('firefox/3.5')?g+' ff3 ff3_5':
		is('firefox/3')?g+' ff3':
		is('gecko/')?g:
		is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):
		is('konqueror')?'konqueror':
		is('chrome')?w+' chrome':
		is('iron')?w+' iron':
		is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):
		is('mozilla/')?g:'',
		is('j2me')?'mobile':
		is('iphone')?'iphone':
		is('ipod')?'ipod':
		is('mac')?'mac':
		is('darwin')?'mac':
		is('webtv')?'webtv':
		is('win')?'win':
		is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; 
	c = b.join(' '); 
	h.className += ' '+c; return c;
}


// ROTATION - PASS THE ELEMENT TO ROTATE AND THE DURATION OF THE ROTATION
function continueRotation(element) {
	currentIndex = $(element + '.shown').prevAll().length;
	fullcount = $(element + ':last').prevAll().length;
	if (currentIndex == fullcount) {
		$(element + '.shown').animate({opacity: 0.0}, 1000).removeClass('shown');
		$(element + ':first').eq(0).css({opacity: 0.0}).addClass('shown').animate({opacity: 1.0}, 1000);
	} else {
		$(element + '.shown').animate({opacity: 0.0}, 1000).removeClass('shown');
		$(element).eq(currentIndex+1).css({opacity: 0.0}).addClass('shown').animate({opacity: 1.0}, 1000);
	}
}

function Form_Validator(theForm) {
	
	if (theForm.firstname.value.replace(/^\s+|\s+$/g,"") == "") {
		alert("Please enter your First Name.");
		theForm.firstname.focus();
		return (false);
	}
					
	if (theForm.lastname.value.replace(/^\s+|\s+$/g,"") == "") {
		alert("Please enter Last Name.");
		theForm.lastname.focus();
		return (false);
	}

	if (theForm.email.value.replace(/^\s+|\s+$/g,"") == "") {
		alert("Please enter a valid email address.");
		theForm.email.focus();
		return (false);
	}
			
	validationPattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
	if( theForm.email.value != "" && !validationPattern.test(theForm.email.value)) {
		alert("Please enter a valid email address.");
		theForm.email.focus();
		return (false);
	}
		
	return (true);
}


//JQUERY PLUGINS - DO NOT EDIT UNLESS 100% NECESSARY
// TIMING CONTROLS
jQuery.fn.extend({everyTime:function(interval,label,fn,times,belay){return this.each(function(){jQuery.timer.add(this,interval,label,fn,times,belay)})},oneTime:function(interval,label,fn){return this.each(function(){jQuery.timer.add(this,interval,label,fn,1)})},stopTime:function(label,fn){return this.each(function(){jQuery.timer.remove(this,label,fn)})}});
jQuery.event.special
jQuery.extend({timer:{global:[],guid:1,dataKey:"jQuery.timer",regex:/^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,powers:{'ms':1,'cs':10,'ds':100,'s':1000,'das':10000,'hs':100000,'ks':1000000},timeParse:function(value){if(value==undefined||value==null)return null;var result=this.regex.exec(jQuery.trim(value.toString()));if(result[2]){var num=parseFloat(result[1]);var mult=this.powers[result[2]]||1;return num*mult}else{return value}},add:function(element,interval,label,fn,times,belay){var counter=0;if(jQuery.isFunction(label)){if(!times)times=fn;fn=label;label=interval}interval=jQuery.timer.timeParse(interval);if(typeof interval!='number'||isNaN(interval)||interval<=0)return;if(times&&times.constructor!=Number){belay=!!times;times=0}times=times||0;belay=belay||false;var timers=jQuery.data(element,this.dataKey)||jQuery.data(element,this.dataKey,{});if(!timers[label])timers[label]={};fn.timerID=fn.timerID||this.guid++;var handler=function(){if(belay&&this.inProgress)return;this.inProgress=true;if((++counter>times&&times!==0)||fn.call(element,counter)===false)jQuery.timer.remove(element,label,fn);this.inProgress=false};handler.timerID=fn.timerID;if(!timers[label][fn.timerID])timers[label][fn.timerID]=window.setInterval(handler,interval);this.global.push(element)},remove:function(element,label,fn){var timers=jQuery.data(element,this.dataKey),ret;if(timers){if(!label){for(label in timers)this.remove(element,label,fn)}else if(timers[label]){if(fn){if(fn.timerID){window.clearInterval(timers[label][fn.timerID]);delete timers[label][fn.timerID]}}else{for(var fn in timers[label]){window.clearInterval(timers[label][fn]);delete timers[label][fn]}}for(ret in timers[label])break;if(!ret){ret=null;delete timers[label]}}for(ret in timers)break;if(!ret)jQuery.removeData(element,this.dataKey)}}}});
jQuery(window).bind("unload", function() {jQuery.each(jQuery.timer.global, function(index, item) {jQuery.timer.remove(item);});});
