// JQuery Document. Requires JQuery 1.4.2



/*###############################
To go in header of the html document:

<script type="text/javascript"> 

	$(window).resize(function() {
		
		imageWidth($("#bgImage"));
	
	});
	
	$(document).ready(function(){
		initBackground();
	});

</script>


###############################*/


/*###############################
 To go at the bottom of the html page. bgLoader div is optional depending on whether you want a page 'loader':

<div id="site_background">
	<div id="bgLoader" style="height: 100%;">
		<img src="images/backgrounds/ajax-loader.gif" style="margin: 30% auto;"/> 
	</div>
</div>

###############################*/












function init_energyFM(){ 

	plainBackground($("#primaryContainer")) 
	contentToggler("#bannerElement","#toggleBanner");
	//alert("Platform: " + navigator.platform);
	simpleSlide();
	$(".side").sidecontent();
	
	
};


// Set background image here. Define file path, background color, width, height.
// File path should be relative to the target document. 
// Background color: this will be the color that the image fades in from.
// Width & Height are set so that the imageWidth function can preserve the aspect ratio of the image

var backgroundImage = ["../images/backgrounds/redBackground.jpg","#450102",1500,900];

/*
function plainBackground(backgroundID) {

		backgroundID.css('backgroundColor', backgroundImage[1]);
		backgroundID.append('<img id="bgImage" src="' + backgroundImage[0] + '" title=""/>');
		$(window).bind("load", function(){
			$("#bgLoader, #bgImage").hide();
			$("#bgImage").fadeIn(500, function() { $("#primaryContainer").css("visibility","visible") });
		
		});
		
};*/


function plainBackground(backgroundID) {
		
		var siteWidth = $("#wrapper").width();
		
		backgroundHolder = "bgImage";
		backgroundID.append('<div id="' + backgroundHolder + '"></div>');
		$("#" + backgroundHolder).css({
			'backgroundColor': backgroundImage[1],
			'backgroundImage': "url('" + backgroundImage[0] + "')",
			'backgroundPosition': 'top center',
			'backgroundRepeat': 'no-repeat',
			'min-width': siteWidth,
			'height': '100%'
		});
			
		$(window).bind("load", function(){
			$("#bgLoader, #" + backgroundHolder).hide();
			$("#" + backgroundHolder).fadeIn(500, function() { $("#primaryContainer").css("visibility","visible") });
		});
		
};


function contentToggler(toggleArea, toggleButton) {
	
	$(toggleButton).click(function() {
		$(toggleArea).slideToggle('slow', function() {
			$(toggleButton).toggleClass('closed');	
		});
	});




}




//function for displaying a simple slide show
function simpleSlide(options) {
	
	var defaults = {
		listClass: "randomList",	// The class assigned to the ul.
		delay: 1700,				// The delay in milliseconds before switching to a new list item.	
		startSlide: 'random',				// The slide to start with. Allowed values: index or 'random'.
		randomOrder: false,			// Display the slides in a random order.
		transitionSpeed: 500,
		transitionType: 'outIn' 	// Transition types: 
									// 'outIn' fades current slide out before showing new slide (best for text); 
									// 'replace' fades next slide in over current slide then hides it once transition is complete. 
	}
	
	var opts = $.extend(defaults,options);
	
	var curZindex = 1
	var items = $("."+ opts.listClass + " li");
	var itemsLength = items.length;
	var currentIndex = (!isNaN(opts.startSlide)) ? opts.startSlide:randomIndex();
	
	items.css('position','absolute');
	items.eq(currentIndex).css("z-index",curZindex);
	items.hide();
	items.eq(currentIndex).show();
	
	function randomIndex() { 
		var thisIndex = Math.ceil(Math.random() * itemsLength); 
		return thisIndex;
	}
	
	function getNextIndex() { 
	 
		if(opts.randomOrder == true) {
			
			var newIndex = randomIndex();
			
			if(newIndex == currentIndex) {
				return getNextIndex(); // If the new index is the same as the current index run function again.
			} else {
				currentIndex = newIndex;
				return newIndex;	
			}
			
		} else {
			
			currentIndex++;
			
			if(currentIndex > itemsLength) {
				currentIndex = 1;
			}
			
			var newIndex = currentIndex;
			
			return newIndex;	
			
		}
	}
	
	function nextSlide() {
		
		if(opts.transitionType == 'replace') {
			
			getNextIndex();
			
			curZindex++;
			items.eq(currentIndex -1).css('z-index',curZindex);
			
			completeSlideTransition();

		} else {
			
			items.eq(currentIndex - 1).fadeOut(opts.transitionSpeed, function() {
			
			getNextIndex();		
			
			completeSlideTransition();

			});
		}
		
		function completeSlideTransition() {
			
			items.eq(currentIndex - 1).fadeIn(opts.transitionSpeed, function() {
				for(var i = 0;i < itemsLength;i++) {
					if(i != currentIndex -1) {
						items.eq(i).hide();
					}
				}
			
				var t=setTimeout(nextSlide,opts.delay);
			
			});	
			
		}
	}
	
	
	nextSlide();
}


//Sie sliding drawers
(function($)
{
	// This script was written by Steve Fenton
	// http://www.stevefenton.co.uk/Content/Jquery-Side-Content/
	// Feel free to use this jQuery Plugin
	// Version: 3.0.2
	
	var classModifier = "";
	var sliderCount = 0;
	var sliderWidth = "320px";
	
	var attachTo = "rightside";
	
	var totalPullOutHeight = 0;
	
	function CloseSliders (thisId) {
		// Reset previous sliders
		for (var i = 0; i < sliderCount; i++) {
			var sliderId = classModifier + "_" + i;
			var pulloutId = sliderId + "_pullout";
			
			// Only reset it if it is shown
			if ($("#" + sliderId).width() > 0) {

				if (sliderId == thisId) {
					// They have clicked on the open slider, so we'll just close it
					showSlider = false;
				}

				// Close the slider
				$("#" + sliderId).animate({
					width: "0px"
				}, 500);
				
				// Reset the pullout
				if (attachTo == "leftside") {
					$("#" + pulloutId).animate({
						left: "0px"
					}, 500);
				} else {
					$("#" + pulloutId).animate({
						right: "0px"
					}, 500);
				}
			}
		}
	}
	
	function ToggleSlider () {
	
		var rel = $(this).attr("rel");

		var thisId = classModifier + "_" + rel;
		var thisPulloutId = thisId + "_pullout";
		var showSlider = true;
		
		if ($("#" + thisId).width() > 0) {
			showSlider = false;
		}
		
		CloseSliders(thisId);
		
		if (showSlider) {
			// Open this slider
			$("#" + thisId).animate({
				width: sliderWidth
			}, 500);
			
			// Move the pullout
			if (attachTo == "leftside") {
				$("#" + thisPulloutId).animate({
					left: sliderWidth
				}, 500);
			} else {
				$("#" + thisPulloutId).animate({
					right: sliderWidth
				}, 500);
			}
		}
		
		return false;
	};

	$.fn.sidecontent = function (settings) {
	
		var config = {
			classmodifier: "sidecontent",
			attachto: "rightside",
			width: "320px",
			opacity: "1",
			pulloutpadding: "5",
			textdirection: "vertical",
			clickawayclose: false
		};
		
		if (settings) {
			$.extend(config, settings);
		}
		
		return this.each(function () {
		
			$This = $(this);
			
			// Hide the content to avoid flickering
			$This.css({ opacity: 0 });
			
			classModifier = config.classmodifier;
			sliderWidth = config.width;
			attachTo = config.attachto;
			
			var sliderId = classModifier + "_" + sliderCount;
			var sliderTitle = config.title;
			
			// Get the title for the pullout
			sliderTitle = $This.attr("title");
			
			// Start the totalPullOutHeight with the configured padding
			if (totalPullOutHeight == 0) {
				totalPullOutHeight += parseInt(config.pulloutpadding);
			}

			if (config.textdirection == "vertical") {
				var newTitle = "";
				var character = "";
				for (var i = 0; i < sliderTitle.length; i++) {
					character = sliderTitle.charAt(i).toUpperCase();
					if (character == " ") {
						character = "&nbsp;";
					}
					newTitle = newTitle + "<span>" + character + "</span>";
				}
				sliderTitle = newTitle;
			}
			
			// Wrap the content in a slider and add a pullout			
			$This.wrap('<div class="' + classModifier + '" id="' + sliderId + '"></div>').wrap('<div style="width: ' + sliderWidth + '"></div>');
			$("#" + sliderId).before('<div class="' + classModifier + 'pullout" id="' + sliderId + '_pullout" rel="' + sliderCount + '">' + sliderTitle + '</div>');
			
			if (config.textdirection == "vertical") {
				$("#" + sliderId + "_pullout span").css({
					display: "block",
					textAlign: "center"
				});
			}
			
			// Hide the slider
			$("#" + sliderId).css({
				position: "absolute",
				overflow: "hidden",
				top: "0",
				width: "0px",
				zIndex: "2",
				opacity: config.opacity
			});
			
			// For left-side attachment
			if (attachTo == "leftside") {
				$("#" + sliderId).css({
					left: "0px"
				});
			} else {
				$("#" + sliderId).css({
					right: "0px"
				});
			}
			
			// Set up the pullout
			$("#" + sliderId + "_pullout").css({
				position: "absolute",
				top: totalPullOutHeight + "px",
				zIndex: "1000",
				cursor: "pointer",
				opacity: config.opacity
			})
			
			$("#" + sliderId + "_pullout").live("click", ToggleSlider);
			
			var pulloutWidth = $("#" + sliderId + "_pullout").width();
			
			// For left-side attachment
			if (attachTo == "leftside") {
				$("#" + sliderId + "_pullout").css({
					left: "0px",
					width: pulloutWidth + "px"
				});
			} else {
				$("#" + sliderId + "_pullout").css({
					right: "0px",
					width: pulloutWidth + "px"
				});
			}
			
			totalPullOutHeight += parseInt($("#" + sliderId + "_pullout").height());
			totalPullOutHeight += parseInt(config.pulloutpadding);
			
			var suggestedSliderHeight = totalPullOutHeight + 30;
			if (suggestedSliderHeight > $("#" + sliderId).height()) {
				$("#" + sliderId).css({
					height: suggestedSliderHeight + "px"
				});
			}
			
			if (config.clickawayclose) {
				$("body").click( function () {
					CloseSliders("");
				});
			}
			
			// Put the content back now it is in position
			$This.css({ opacity: 1 });
			
			sliderCount++;
		});
		
		return this;
	};
})(jQuery);


