var intervalID;

function theRotator() {
    //Set the opacity of all images to 0
    $('div.brand').css({ opacity: 0.0 });
    
    //Get the first image and display it (gets set to full opacity)
    $('div.show').css({ 'opacity': '1.0', 'z-index':'6' });
    
    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    intervalID = setInterval('rotate()', 6000);
}

var rotateToThis=function(opt) {
	if(opt=='next') {
		
	} else if (opt='prev')  {
		
	}
}


function rotate(id) {
	//alert(id);
    //Get the first image
    var current = $('div.show');
    current.css({'z-index':'2'});
    //Get next image, when it reaches the end, rotate it back to the first image
    if(id != null)
    {
        var name = 'div#'+id;
        var next = $(name);
        clearInterval(intervalID);
        intervalID = setInterval('rotate()', 6000);
    }
    else
    	var next = ((current.next().hasClass('brand')) ? ((current.next().hasClass('show')) ? $('div.brand:first') : current.next()) : $('div.brand:first'));
    
        
    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({ opacity: 0.0,'z-index':'3' })
	.addClass('show')
	.animate({ opacity: 1.0 }, 3000);

    //get id then call setid() of flash rotator
    nextID = next.attr('id');
   rotator_onChange(nextID);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000)
	.removeClass('show');
}

function rotator_onChange(myId) {
  //  alert(myId);
   // thisMovie("rotateController").selectID(myId);
}

//finds flash movie plugin consistently across browsers
function thisMovie(movieName) {
    return document.getElementById(movieName);
}

function loadRotateController()
{
    var so = new SWFObject("images/home-rotate.swf", "Rotate", "273", "60", "9", "#ffffff");
    so.addParam("allowScriptAccess", "always");
    so.addParam("wmode","transparent");
    so.write("rotateController");
}

$(document).ready(function() {
    theRotator();    	
    if($('#rotateController').length>0)
    	loadRotateController();
});
