function isImageLoaded(img) {
	if(!img.complete) {
		return false;
	}
	
	if(typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) {
		return false;
	}
	
	return true; //no other way of checking: assume its ok.
}

var currentBg = 0, nextBg = 1;
var numberBg = 0;

function rotateBackground() {
	var next;
	if(currentBg+1 <= numberBg) { //get the next image
		nextBg = currentBg + 1;
	}else{
		nextBg = 0;
	}
	
	if(isImageLoaded(jQuery('#background-image img')[nextBg])) {
		jQuery('#background-image img').css('zIndex', -2); //set all images -2
		jQuery(jQuery('#background-image img')[nextBg]).css('zIndex', -1); //next image behind the active image
		jQuery(jQuery('#background-image img')[nextBg]).fadeIn('slow', function() {
				jQuery(jQuery('#background-image img')[nextBg]).css('zIndex', 0); //set as front image
				jQuery(jQuery('#background-image img')[currentBg]).hide();
				currentBg = nextBg;
		});
	}
	
	setTimeout('rotateBackground()', 5000); //try again in 5secs
}

(function($){
		$.fn.extend({
				customStyle : function(options) {
					if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
						return this.each(function() {
								var currentSelected = $(this).find(':selected');
								$(this).after('<span class="select-custom-span"><span class="select-custom-inner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
								var selectBoxSpan = $(this).next();
								var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
								var selectBoxSpanInner = selectBoxSpan.find(':first-child');
								selectBoxSpan.css({display:'inline-block'});
								selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
								var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
								$(this).height(selectBoxHeight).change(function(){
										// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
										selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
										// Thanks to Juarez Filho & PaddyMurphy
								});
								
						});
					}
				}
		});
})(jQuery);
		
jQuery(document).ready(function() {
		if((numberBg = jQuery('#background-image img').length) > 1) {
			numberBg--; //we like 0 based, and length obviousy isnt
			setTimeout('rotateBackground()', 5000);
		}
		
		jQuery('#content-hide-container').show();
		jQuery('#content-hide-container a').click(function(e) {
				if(jQuery(this).attr('contentstate') == 'open') {
					var button = jQuery(this); //store so we dont lose 'this' inside other functions
					jQuery('#panels-toggle').fadeOut('fast', function() {
							button.text('OPEN'); //change button text
							jQuery('.control-pagination').hide(); //hide pagination
							jQuery('#page-download-link').hide();
							jQuery('.controls-back-button').hide();
							jQuery('#page-closed-title').show();
							button.attr('contentstate', 'closed'); //chage the button state for next click
					});
				}else{
					var button = jQuery(this); //store so we dont lose 'this' inside other functions
					jQuery('#panels-toggle').fadeIn('fast', function() {
							button.text('CLOSE X');
							jQuery('.control-pagination').show();
							jQuery('#page-download-link').show();
							jQuery('.controls-back-button').show();
							jQuery('#page-closed-title').hide();
							button.attr('contentstate', 'open');
					});
				}
				
				e.preventDefault();
				return false;
		});
		
		jQuery('select.select-custom').customStyle();
});


