$(document).ready(function()
{
	window.itemWidth = 383;
	window.itemCount = $(".rotaryItemInfo").size();
	window.sliderWidth = window.itemWidth*window.itemCount;
	window.rotLocation = 0;
	window.items = new Array();
	
	$(".rotaryItemInfo img").resizeImg({
		maxWidth: window.itemWidth,
		maxHeight: 270
	});

	$(".rotaryItemInfo").each(function()
	{
		src = $(this).attr("src");
		img = "<div class='rotaryItem'><img src='" + src + "'/></div>";
	});
	
	$("#slider").css("width", window.sliderWidth + "px");
	
	window.moveRotTo = function(from, to)
	{
		newPosition = 0;
		
		$("#slider").css("left", -window.itemWidth + "px");
		second =	$("#rii_" + from).html();
		first = $("#rii_" + to).html();
		
		if((from == window.itemCount-1) && to == 0)
		{
			newPosition = -window.itemWidth;
			$("#slider").css("left", 0 + "px");
			second =	$("#rii_" + to).html();
			first = $("#rii_" + from).html();
		}
		else if(from == 0 && (to == window.itemCount-1))
		{
			
		}
		else if(from < to)
		{
			newPosition = -window.itemWidth;
			$("#slider").css("left", 0 + "px");
			second =	$("#rii_" + to).html();
			first = $("#rii_" + from).html();
		}
		
		document.getElementById("riLeft").innerHTML = first;
		document.getElementById("riRight").innerHTML = second;
			
		$("#slider").animate({left : newPosition + "px"}, 500);
		
		
		$(".text").each(function()
		{
			tpos = $(this).attr("pos");
			if(tpos == to)
			{
				html = $(this).html();
				clr = $(this).attr("color");
				if(clr == null || clr == "")
					clr = "#486d88";
				//GOD DAMN IE!!
				document.getElementById("itemText").innerHTML = html;
				$("#itemText").css("background-color", clr);
				return true;
			}
		});
	}
	
	window.moveRotUp = function()
	{
		from = window.rotLocation;
		window.rotLocation++;
		to = window.rotLocation;
		
		if(to == window.itemCount)
		{
			window.rotLocation = 0;
			to = 0;
		}
		
		$(".rotButton").each(function()
		{
			$(this).removeClass("rbselected");
			totext = $(this).attr("pos");
			if(totext == window.rotLocation)
				$(this).addClass("rbselected");
		});
			
		window.moveRotTo(from, to);
	}
	
	$(".rotButton").click(function()
	{
		$(".rbselected").removeClass("rbselected");
		$(this).addClass("rbselected");
		to = $(this).attr("pos");
		from = window.rotLocation;
		window.rotLocation = to;
		
		window.moveRotTo(from, to);
	});
	
	//window.moveRotTo(1);
	document.getElementById("riLeft").innerHTML = $("#rii_0").html();
	$(".text").each(function()
	{
		pos = $(this).attr("pos");
		if(pos == 0)
		{
			html = $(this).html();
			color = $(this).attr("color");
			$("#itemText").css("background-color", color);
			document.getElementById("itemText").innerHTML = html;	
		}	
	});
	setInterval("moveRotUp()", 8000);
});

(function($) {
    $.fn.resizeImg = function(options) {
 
        var settings = $.extend({
            scale: 1,
            maxWidth: null,
			maxHeight: null
        }, options);
        
        $(this).one('load', function() {
 
        //return this.each(function() {
			
			if(this.tagName.toLowerCase() != "img") {
				// Only images can be resized
				return $(this);
			} 

			var width = this.naturalWidth;
			var height = this.naturalHeight;
			if(!width || !height) {
				// Ooops you are an IE user, let's fix it.
				var img = document.createElement('img');
				img.src = this.src;
				
				width = img.width;
				height = img.height;
			}
			
			if(settings.scale != 1) {
				width = width*settings.scale;
				height = height*settings.scale;
			}
			
			var pWidth = 1;
			if(settings.maxWidth != null) {
				pWidth = width/settings.maxWidth;
			}
			var pHeight = 1;
			if(settings.maxHeight != null) {
				pHeight = height/settings.maxHeight;
			}
			var reduce = 1;
			
			if(pWidth < pHeight) {
				reduce = pHeight;
			} else {
				reduce = pWidth;
			}
			
			if(reduce < 1) {
				reduce = 1;
			}
			
			var newWidth = width/reduce;
			var newHeight = height/reduce;
			
			return $(this)
				.attr("width", newWidth)
				.attr("height", newHeight);
			
        })
        .each(function(){
			if (this.complete)
			{
				$(this).trigger("load");
			}
		});
    }
})(jQuery);

