/**
 * @author PragmaSoft
 */
$(document).ready( function () {
//Delay per slide
    var  delay = 3000
//Do the Sliders array
    var SlidersArr = new Array();
//get the all the "sliders"
    TheSliders = $('#bottomheader > .homebox')
    AllSliders = TheSliders.length
    Largest = 0
//loop over each one
    $.each(TheSliders, function(key, ThisSlider) {
        $(ThisSlider).css('background-image', 'none');
        TheSlide = $('#'+$(ThisSlider).attr('id')+' > .slide')
//Get The # of pics
        SlideLen = TheSlide.length
//if GT than current, set it
        if(Largest < SlideLen) {
            Largest = SlideLen-1
        }
//get the individual sliders
        SlidersArr[key] = TheSlide
    })
//Prepare new array
    var CleanArr = new Array();
//Array to store de values (more on this, later)
    var ArrValues = new Array();
//Internal IDX, 
    var ii = 0;
//Loop over the sliders and order the slides, using the largest number of slides
    for (i=0;i<=AllSliders*Largest;) {
//Loop over each set and set each slide using the Inertnal IDX    	
        $.each(SlidersArr, function(key2, TheSlide) {
            if(typeof TheSlide[ii] != "undefined") {
                CleanArr[i] = TheSlide[ii]
            } else {
//we need an internal, array dependent counter, initialize it
           	  if(typeof ArrValues[key2] == "undefined") {
            	  	ArrValues[key2] = 0
           	  }
//If current IDX does not exist, try to get it
              if(parseInt(ArrValues[key2]) >= TheSlide.length) {
 					ArrValues[key2]  = 0
                }
//Get next slide using the internal counter
                CleanArr[i] = TheSlide[ArrValues[key2]]
//Increment it
                ArrValues[key2]++             
            }
//if it is the first slide, show it
            if(ii == 0) {
                $(TheSlide[0]).fadeIn(delay)
            }
            i++
        })
//we loop over all sets, increase "Internal" IDX 
        ii++
    }
//Get All Pics index size
    var AllPics = CleanArr.length -1
 //Do not start if no pics
    if(AllPics > 0){
 //Start The Show interval
    	intervalID = window.setInterval(DoShow, delay);
    }
//since we are "preloading" pics, start index must be next round
    var i = TheSliders.length -1
//Do the Show
    function DoShow() {
//We ran out of pics indexes, reset count
        if(i >= AllPics) {
            i = 0
        } else {
//Keep going, show next One
            i++
        }
//Fade Out Siblings slide to do a cool transition
        $(CleanArr[i]).siblings('.slide').fadeOut(delay)
        $(CleanArr[i]).fadeIn(delay);
    }

})
