//HP img rotator
 function hprotator() {
       $('#homepageBanner a').css({opacity: 0.0});
       $('#homepageBanner a:first').css({opacity: 1.0}).addClass('show');
       timedRotate()
 }
 
 function timedRotate() {
     play = setInterval('rotateAll()',6000);
 };
 
 function rotateAll() {    
     var current = ($('#homepageBanner a.show')?  $('#homepageBanner a.show') : $('#homepageBanner a:first'));
     var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#homepageBanner a:first') :current.next()) : $('#homepageBanner a:first'));    
     next.css({opacity: 0.0})
     .addClass('show')
     .animate({opacity: 1.0}, 1000);
     current.animate({opacity: 0.0}, 1000)
     .removeClass('show');
 };
//end

//Banner img rotator
    //Set Default State of each portfolio piece
    $("#paging").show();
    $("#paging a:first").addClass("active");
    //Get size of images, how many there are, then determin the size of the image reel.
    var imageWidth = $("#bannerPosition-1").width();
    var imageSum = $("#bannerPosition-1 .banner-inner img").size();
    var imageReelWidth = imageWidth * imageSum;
    //Adjust the image reel to its new size
    $("#bannerPosition-1 .banner-inner").css({'width' : imageReelWidth});
    //Paging + Slider Function
    rotate = function(){    
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
        $("#paging a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
        //Slider Animation
        $("#bannerPosition-1 .banner-inner").animate({
            left: -image_reelPosition
        }, 500 );
    };
    //Rotation + Timing Event
    rotateSwitch = function(){        
        play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
            $active = $('#paging a.active').next();
            if ( $active.length === 0) { //If paging reaches the end...
                $active = $('#paging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 7000); //Timer speed in milliseconds (3 seconds)
    };
//end


$(document).ready(function() {
//HP img rotator
	if($('#homepageBanner a').length > 1){
		hprotator();
	}

//Banner img rotator
if($('html.ie6').length != 1) {	
	if($('#bannerPosition-1 .banner-inner a').length > 0){
	    rotateSwitch(); //Run function on launch    
	    //On Hover
	    $("#bannerPosition-1 .banner-inner a").hover(function() {
	        clearInterval(play); //Stop the rotation
	    }, function() {
	        rotateSwitch(); //Resume rotation
	    });    
	    //On Click
	    $("#paging a").click(function() {    
	        $active = $(this); //Activate the clicked paging
	        //Reset Timer
	        clearInterval(play); //Stop the rotation
	        rotate(); //Trigger rotation immediately
	        rotateSwitch(); // Resume rotation
	        return false; //Prevent browser jump to link anchor
	    });
	} 
}	   
//Button Ads
	if($('#bannerPosition-2 .banner-inner a').length > 4){
		//var buttonAds =	$('#bannerPosition-2 .banner-inner a');
		var buttonAds =	document.getElementById('bannerPosition-2').getElementsByTagName('a');
		$('#bannerPosition-2 .banner-inner a').css({display: 'none'});
	    //var numbers = document.getElementsByTagName('span');
	    var ranAd = Math.floor(Math.random()*buttonAds.length);
	    (ranAd > (buttonAds.length - 4)) ? ranAd -= 4 : ranAd ;
		for (i = ranAd; i < buttonAds.length ; i++) {
	    
		buttonAds[i].style.display = 'block';
	    if (i == (ranAd + 3)) { break };
		}
	}
//end    
    
});


