/* Author: 
Nicolas Delrez
Cible Communication 
*/

var photos = [ {"image" : "banner1.jpg"}, {"image" : "banner2.jpg"}, {"image" : "banner3.jpg"}, {"image" : "banner4.jpg"}, {"image" : "banner5.jpg"}, {"image" : "banner6.jpg"}, {"image" : "banner7.jpg"}, {"image" : "banner8.jpg"}, {"image" : "banner9.jpg"}, {"image" : "banner10.jpg"}, {"image" : "banner11.jpg"}, {"image" : "banner12.jpg"}, {"image" : "banner13.jpg"}, {"image" : "banner14.jpg"}, {"image" : "banner15.jpg"}, {"image" : "banner16.jpg"}, {"image" : "banner17.jpg"}, {"image" : "banner18.jpg"}, {"image" : "banner19.jpg"}, {"image" : "banner20.jpg"}, {"image" : "banner21.jpg"}, {"image" : "banner22.jpg"}, {"image" : "banner23.jpg"}, {"image" : "banner24.jpg"}, {"image" : "banner25.jpg"}, {"image" : "banner26.jpg"}, {"image" : "banner27.jpg"}
];

$(document).ready(function(){  
	/******************************************************/
  	/* Header random image
	/******************************************************/	
	var slideshowSpeed = 5000;
	
	if ($('body').hasClass('home') === true){
		
		var interval;
		var activeContainer = 1;	
		var currentImg = 0;
		var animating = false;
		var navigate = function(direction) {
			if(animating) {
				return;
			}
		
			
			if(direction == "next") {
				currentImg++;
				if(currentImg == photos.length + 1) {
					currentImg = 1;
				}
			}
		
			
			var currentContainer = activeContainer;
			if(activeContainer == 1) {
				activeContainer = 2;
			} else {
				activeContainer = 1;
			}
		
			showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
		};
	
		var currentZindex = -1;
		var showImage = function(photoObject, currentContainer, activeContainer) {
			animating = true;
		
			currentZindex--;

			$("#headerimg" + activeContainer).css({
				"background-image" : "url(http://www.cesw.be/img/header/" + photoObject.image + ")",
				"display" : "block",
				"z-index" : currentZindex
			});
	
		
			$("#headerimg" + currentContainer).fadeOut(1200,
				function() {
					setTimeout(
						function() {
							animating = false;
						}
					, 2000);
				}
			);
		};
		
	
		navigate("next");
	
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
		
		
	}else{
	 var numLow = 1;
     var numHigh = 27;
     var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
	 var randomImg =  Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
	 var imageB = 'url(http://www.cesw.be/img/header/banner'+randomImg+'.jpg)';
	 $("#wrapper").css('background-image', imageB);
	}
  
  	/******************************************************/
  	/* Navigation 
	/******************************************************/
	
    $("nav ul li a").hover(function() { 
    	if( $(this).parent().children("ul").size() > 0)
       	{ 
       		var a = $(this);
        	a.parent().find("ul:first").slideDown('fast').show(); //Drop down the subnav on click 
        	a.addClass("current"); 
        	a.parent().hover(function() {  
        	}, function(){  
        	    a.parent().find("ul:first").slideUp(400);
            	a.removeClass("current");
        	});
        }  
        });
	
	 $("#topLink ul li a").hover(function() { 
    	if( $(this).parent().children("ul").size() > 0)
       	{ 
       		var a = $(this);
        	a.parent().find("ul:first").slideDown('fast').show(); //Drop down the subnav on click 
        	a.addClass("current"); 
        	a.parent().hover(function() {  
        	}, function(){  
        	    a.parent().find("ul:first").slideUp('fast');
            	a.removeClass("current");
        	});
        }  
        });
    
    /******************************************************/
  	/* Clear a input field on focus
	/******************************************************/
        
    $.fn.clearField = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	
	$(".clearField").clearField();   
	
	/******************************************************/
  	/* Accordion on Home page
	/******************************************************/
	/*$('#accordion li').hover(
		function(){
			
			
			
			$(this).addClass('open');
			$(this).animate({width:399}, 300);
			$(this).children('.slide_caption').animate({height:175}, 300,function(){
				$(this).children('p.slide_content').slideDown('fast',function(){
					
				});
			});
			
			$('#accordion li').not('.open').children('.slide_caption').animate({height:70}, 150);
			$('#accordion li').not('.open').children('.slide_caption').children('.slide_content').slideUp('fast');
			$('#accordion li').not('.open').animate({width:134}, 300);
			
		}, function(){
			$(this).removeClass('open');
		}
	); */
	
	
	 
	$(".tablesorter").tablesorter({sortList: [],widthFixed: false,widgets: ["zebra"], dateFormat: 'uk',headers: { 0:{sorter: false},4:{sorter: false}}}).tablesorterPager({container: $("#pager")}); 
    

});  
























