﻿/*
	Original code from Designm.ag
	Source: http://designm.ag/tutorials/image-rotator-css-jquery/
	Demo: http://www.sohtanaka.com/web-design/examples/image-rotator/
	Modified by vitorccs on June 2009
	Modified by thlopes on June 2009, Nov 2010
*/
var rotate_this = true;

// rotate between elements
function rotate() {
	if (rotate_this) {
		if (jQuery('.image_thumb li:last').attr('class') == 'active') {
			jQuery('.image_thumb li:first').click();
		} else {
		   jQuery('.image_thumb li.active').next().click();
		}
	}
}

jQuery(document).ready(function() {
	function loadXML() { // 1. READ XML
		jQuery.ajax({ 
			type: "GET",
			url: "http://www.guiadeitapevi.com.br/imagerotator/images.xml?ttk="+(new Date()).getTime(),
			success: function(xml) {
				var xmlDocument = jQuery(xml);
				var numberOfRecords = xmlDocument.find("image").size();

				if (numberOfRecords == 0) {
					jQuery(".main_image").html("<p>ERROR: XML file is empty or tags are not named correctly.</p>");
				}

				xmlDocument.find("image").each(function(i) {
					var imgId = jQuery(this).attr("id");
					var imgPath = 'http://www.guiadeitapevi.com.br' + jQuery(this).find("path").text();
					var imgHeading =  jQuery(this).find("heading").text();
					var imgDescription =  jQuery(this).find("description").text();
					var imgLink =  jQuery(this).find("link").text();

					var liElement = '' +
						'<li>' +
							'<a href="' + imgPath + '">' + imgId + '</a>'+
							'<div class="block">'+
								'<h2><a href="' + imgLink + '">' + imgHeading + '</a></h2>'+
								'<p>' + imgDescription + '</p>'+
							'</div>'+
						'</li>';

					jQuery(".image_thumb ul").append(liElement);
				});

				init();
			}
		});
	}

	function init() { // 2. ATTACH EVENTS TO HTML ELEMENTS
		//Show Banner
		jQuery(".main_image .desc").show(); // Show Banner
		jQuery(".main_image .headline").animate({ opacity: 0.85 }, 1 ); //Set Opacity

		//Click and Hover events for thumbnail list
		jQuery(".image_thumb ul li").click(function() { 
			//Set Variables
			var imgTitle = jQuery(this).find('a').attr("href"); // Get Main Image URL
			var imgDesc = jQuery(this).find('.block').html(); // Get HTML of block
			var imgDescHeight = jQuery(".main_image").find('.headline').height(); // Calculate height of block
			
			if (jQuery(this).is(".active")) { // If it's already active, then...
				return false; // Don't click through
			} else {
				// Animate the Teaser
				jQuery(".main_image .headline").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
					jQuery(".main_image .headline").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 250 );
					jQuery(".main_image").css({ background: "url('" + imgTitle + "')"});
				});
			}
			
			jQuery(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
			jQuery(this).addClass('active');  //add class of 'active' on this list only
			return false;
			
		}) .hover(function(){
			jQuery(this).addClass('hover');
			//rotate_this = false;
			}, function() {
			jQuery(this).removeClass('hover');
			//rotate_this = true;
		});

		// Start animation
		jQuery(".image_thumb ul li:first").click();
	}

	if (jQuery('.main_image .desc').length) {
	    loadXML();
	    window.setInterval('rotate()', 6000);
    }
});
