﻿/*
	Originally created by Designm.ag
	Source: http://designm.ag/tutorials/image-rotator-css-jquery/
	Demo: http://www.sohtanaka.com/web-design/examples/image-rotator/

	Modified by vitorccsiqueira@gmail.com
	Small Imagerotator v1.0
	Last update on 27 June 2009
*/
var imageRotator = {
	records : null,
	
	insertRightSideMenu : function (jsonObject) {
		imageRotator.records = jsonObject.items;

		$(imageRotator.records).each(function (i) {
			// create li and anchor tag
			var liElement = jQuery("<li>");
			var anchorElement = jQuery("<a>");
			
			// set attributtes for anchor tag
			$(anchorElement)
				.attr("href", imageRotator.records[i].link)
				.html(i+1)
				.click(function () {
					//Click and Hover events for thumbnail list
					var picture = imageRotator.records[i].picture;
					var description = imageRotator.records[i].text;
					var heading = jQuery("<h2>").html(imageRotator.records[i].title);
					var link = jQuery("<a>")
									.html(description)
									.attr("href", imageRotator.records[i].link);
					var paragraph = jQuery("<p>").append(link);

					var imgDescHeight = $(".main_image").find('.headline').height(); // Calculate height of block
					
					if ($(this).parent().is(".active")) { // If it's already active, then...
						return false; // Don't click through
					} else {
						// Animate the Teaser
						$(".main_image .headline")
							.animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
								$(".main_image .headline")
									.empty()
									.append(heading)
									.append(paragraph)
									.animate({ opacity: 0.85, marginBottom: "0" }, 250 );
								$(".main_image").css({ background: "url('" + picture + "')"});
						});
					}
					
					$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
					$(".image_thumb ul li").eq(i).addClass('active');  //add class of 'active' on this list only
					return false;
				});

			// Insert anchor element inside li
			$(liElement).append(anchorElement);
			
			// Insert just created li element inside ul
			$(".image_thumb ul").append(liElement);
		});

		if (imageRotator.records.length <= 1) { // it does not make sense to display menu if there is one choice
			$(".image_thumb ul").hide();
		}

		imageRotator.startAnimation();
	},
	
	startAnimation : function () {
		// Show Banner
		$(".main_image .desc").show(); // Show Banner
		$(".main_image .headline").animate({ opacity: 0.85 }, 1 ); //Set Opacity
		
		// Start animation
		$(".image_thumb ul li:first a").click();
	},
	
	init : function () {
		imageRotator.insertRightSideMenu();
	}
}

