//outbound link tracking
function recordOutboundLink(link, category, action) {
  try {
    var myTracker=_gat._getTrackerByName();
    _gaq.push(['myTracker._trackEvent', ' + category + ', ' + action + ']);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}

//internal adverts code
var gab_ads;
var gab_ad=0;
function next_gab_ad(){
  $('<img />').addClass('img').attr('src',  gab_ads[gab_ad].img ).one('load',function(){
    var new_img=this;
    $('#internal_ads_content').slideUp(1500,function(){
      $('#internal_ads_content .title').html( gab_ads[gab_ad].title );
      $('#internal_ads_content .description').html( gab_ads[gab_ad].description );
      $('#internal_ads_content .call_to_action').html( gab_ads[gab_ad].call_to_action );
      $('#internal_ads_content .img').replaceWith( new_img );
      $('#internal_ads_content').slideDown(1500,function(){
        gab_ad++;
        if(gab_ad>=gab_ads.length){
          gab_ad=0;
        }
        setTimeout('next_gab_ad()',5000);        
      });  
    });
  }).each(function() {
    if(this.complete) $(this).load();
  });
}

//internal jobs code
var gab_jobs;
var gab_job=0;
var gab_job_speed;
var gab_job_delay;
function next_gab_job(){
  $('#internal_jobs_content').slideUp(gab_job_speed,function(){
    $('#internal_jobs_content .title').html( $("<a>").attr('href',gab_jobs[gab_job].link).attr('target','_blank').html(gab_jobs[gab_job].title) );
    $('#internal_jobs_content .description').html( gab_jobs[gab_job].description );
		$('#internal_jobs_content .link').html('<a href="http://www.gabbitas.co.uk/vacancies/">GABBITAS INDEPENDENT EDUCATION RECRUITMENT LISTINGS</a>');
    $('#internal_jobs_content').slideDown(gab_job_speed,function(){
      gab_job++;
      if(gab_job>=gab_jobs.length){
        gab_job=0;
      }
      setTimeout('next_gab_job()',gab_job_delay);        
    });  
  });
}

//init jquery plugins
$(document).ready(function() {

  //init gallery lightbox
	$("a.gabgal, a.isd-gal").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
    'titlePosition': 'inside',
		'opacity'		: true,                
    'cyclic' : true
	});
	
  //popup lightbox for link exchange and such
	$("a.link_exchange, a.popup").fancybox({
		'width'				: 380,
		'height'			: 520,
    'autoScale'     	: false,
    'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});
	
  //setup the banner rotate
	$('#rotate').nivoSlider({
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        slices:15,
        animSpeed:750, //Slide transition speed
        pauseTime:8000,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:false, //Next & Prev
        directionNavHide:true, //Only show on hover
        controlNav:false, //1,2,3...
        controlNavThumbs:false, //Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, //Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', //Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
        keyboardNav:false, //Use left & right arrows
        pauseOnHover:true, //Stop animation while hovering
        manualAdvance:false, //Force manual transitions
        captionOpacity:0.8, //Universal caption opacity
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){}, //Triggers after all slides have been shown
        lastSlide: function(){}, //Triggers when last slide is shown
        afterLoad: function(){} //Triggers when slider has loaded
    });			
	
  //open all external links in new tab/window 
  $("a[href^='http:']:not([href*="+document.domain+"])")
    .not("[href^=#]")
    .not("[href^=mailto]")
    .attr('target', '_blank');
  
  //load the internal ads
  $.getJSON('http://cdn.dotnology.co.uk/gabbitas_internal_ads/ads.json.php?callback=?', function(data) {
    gab_ads=data;
    next_gab_ad();
  });

  //load the internal jobs
  $.getJSON('http://www.gabbitas.co.uk/feed-jobs/?callback=?', function(data) {
    gab_jobs=data.jobs;
    gab_job_speed=data.speed;
    gab_job_delay=data.delay;
    next_gab_job();
  });


});

