var slides = [{src:"images/slides/15m.jpg",title:"Early Years Programme",link:"earlyyearsprogramme.html"},
			  {src:"images/slides/2half.jpg",title:"Pre Primary Programme",link:"preprimaryprogramme.html"},
			  {src:"images/slides/6plus.jpg",title:"Primary Programme",link:"primaryprogramme.html"},
			  {src:"images/slides/9plus.jpg",title:"Middle and High School",link:"middleandhighschool.html"}
			 ];
var counter = timer = 0;
var interval = 6000;
var cont,ncont,schools;
$(function(){
  cont = $('#slidesCont');
  ncont= $('#sls');
  scls = $('#schools');
  cont.addClass("loading");
  for(var i=0;i<slides.length;i++){
	  var img = $('<img>');
	  $('li:eq('+i+')>a',ncont).attr('href',slides[i].link)
	  img.attr({"src":slides[i].src,"title":slides[i].title}).data('link',slides[i].link)
	  .click(function(){window.location.href = $(this).data('link')});
	  cont.append(img);
	  img.load(function(){counter++;$(this).hide();})
  }
  timer = setInterval(function(){ if(counter==slides.length){
	  cont.removeClass('loading')
	  clearInterval(timer);
	  counter=timer=0;
	  showStart();
  }},10);
  
  showStart=function(){
	$('li',ncont).hover(
		function(){
			if(!cont.hasClass('loading')){
				clearInterval(timer);
				if($(this).hasClass('on')) return;
				counter = $(this).parent().find('li').index($(this));
				showImg();
			}
		},
		function(){
			if(!cont.hasClass('loading')){
				counter = $(this).parent().find('li').index($(this));
				counter = (counter == ncont.find('li').length-1)?0:counter+1;
				timer = setInterval(function(){showImg();counter = counter<slides.length-1?counter+1:0;},interval);
			}
		}
	).css({backgroundColor:'#fff',opacity:'0'});
	showImg();
	counter++;
	timer = setInterval(function(){showImg();counter = counter<slides.length-1?counter+1:0;},interval);
  }
  showImg=function(){
	  //var isanimated = $('img:eq('+counter+')',cont).is(':animated');
	  var oldc = $('li',ncont).index($('li.on',ncont).removeClass('on'));
	  var curl = $('li:eq('+counter+')',ncont).addClass('on');
	  $('span',scls).removeClass('on').end().find('.'+$('a',curl).attr('rel')).addClass('on');
	  $('img',cont).hide();
	  $('img:eq('+counter+')',cont).show();
	  /*if(isanimated){
		$('img',cont).stop().css({opacity:0}).hide();
		$('img:eq('+counter+')',cont).stop().css({opacity:1}).show();
	  }
	  else{
		$('img:eq('+counter+')',cont).show().animate({opacity: 1},300);
	  	$('img:eq('+oldc+')',cont).animate({opacity:0},300,function(){$(this).hide()});
	  }*/
  }
});
