// JOYRIDE WordPress Theme Javascript Functions
d=document;
window.onload=startUp;

// SITE PAGE DETECTION AND FUNCTION CALLS
function startUp(){
   defineBrowser(navigator.userAgent);
   
   // Add Event Page stuff

   if(d.getElementById("event-form")){
   $("input#vehicletype_SelectAll").click(function() {
	   $("input[name^='Event[vehicletype']").attr("checked", $(this).is(":checked"));
   });

   $("input[name^='Event[vehicletype']").click(function(){
 
        if($("input[name^='Event[vehicletype']").length == $("input[name^='Event[vehicletype']:checked").length) {
            $("input#vehicletype_SelectAll").attr("checked", "checked");
        } else {
            $("input#vehicletype_SelectAll").removeAttr("checked");
        }
 
    });

   $('#Event_recurring').click( function() {
	   $('#repeat-frequency').toggle('slow');
   });

   }

   $('.slideshow img').css('display','block');

   $('#headerPhotos.slideshow li').css({opacity: 0.0});
   $('#headerPhotos.slideshow li:first').addClass('shown').css({opacity: 1.0});
   
   $('#headerPhotos').everyTime(2000, 'main-rotation', function() {
      continueRotationNoAnimate('#headerPhotos.slideshow li');
   });
   
// 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 continueRotationNoAnimate(element) {
   	   currentIndex = $(element + '.shown').prevAll().length;
   	   fullcount = $(element + ':last').prevAll().length;
   	   if (currentIndex == fullcount) {
   	      $(element + '.shown').css({opacity: 0.0}).removeClass('shown');
   	      $(element + ':first').eq(0).css({opacity: 1.0}).addClass('shown');
   	   } else {
   	      $(element + '.shown').css({opacity: 0.0}).removeClass('shown');
   	      $(element).eq(currentIndex+1).css({opacity: 1.0}).addClass('shown');
   	   }
   	}
   
   if(d.getElementById("article")){
      $('.content .article a').lightBox({fixedNavigation:true});
     }
   
   if(d.getElementById("news")){

      //Colorbox for news page
      $("a[rel='botsVideoImage']").colorbox({inline:true, href:"#mediaBOTS"});
      $("a[rel='botsVideoLink']").colorbox({inline:true, href:"#mediaBOTS"});
      $("a[rel='botsVideoButton']").colorbox({inline:true, href:"#mediaBOTS"});
      
      $("a[rel='botsOutVideoImage']").colorbox({inline:true, href:"#mediaBOTSOut"});
      $("a[rel='botsOutVideoLink']").colorbox({inline:true, href:"#mediaBOTSOut"});
      $("a[rel='botsOutVideoButton']").colorbox({inline:true, href:"#mediaBOTSOut"});

      $("a[rel='saviniVideoImage']").colorbox({inline:true, href:"#mediaSavini"});
      $("a[rel='saviniVideoLink']").colorbox({inline:true, href:"#mediaSavini"});
      $("a[rel='saviniVideoButton']").colorbox({inline:true, href:"#mediaSavini"});

      $("a[rel='dub1']").colorbox();
      $("a[rel='dub2']").colorbox();
      $("a[rel='dub3']").colorbox();


      $("a[rel='sema1']").colorbox();
      $("a[rel='sema2']").colorbox();
      $("a[rel='sema3']").colorbox();

      $("a[rel='localMotors1']").colorbox();
      $("a[rel='localMotors2']").colorbox();
      $("a[rel='localMotors3']").colorbox();
}
   
   $('#full h1').parent().css("background","none");
}

// 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;
}

//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);});});
