$( function () {
	$.ajax({
		type: "GET",
		url: "images/xml/banner.xml",
		dataType: "xml",
		success: function(xml) {
			$output = '<ul>';
			$(xml).find('item').each( function () {
				$path = $(this).find('path').text();
				$link = $(this).find('link').text();
				$target = $(this).find('target').text();
				$caption = $(this).find('caption').text();
				$normalPrice = $(this).find('price').text();
				$specialPrice = $(this).find('specialPrice').text();
				$description = $(this).find('description').text();
				$price = $specialPrice || $normalPrice;
				$readmore = $(this).find('readmore').text();
												
				$output += '<li>';
					$output += '<a class="banner_link" href="' + $link + '"';
					
					if($target != '_self') {
						$output += ' target="' + $target + '"';
					}
					
					$output += '>';
						
						$output += '<img src="' + $path + '" alt="' + $caption + '" title="' + $caption + '" />';
						$output += '<div class="caption">';						
						$output += '</div>';
						$output += '<div class="holder">';						
							$output += '<span class="banner_title">' + $caption + '</span>';
							$output += '<p>' + $description + '</p>';
							if($specialPrice){
								$output += '<span class="banner_price">' + $price + ' <s>' + $normalPrice + '</s></span>';
							}else{
								$output += '<span class="banner_price">' + $price + '</span>';
							}
							$output += '<div class="banner_more"><span>' + $readmore + '</span></div>';
						$output += '</div>';
				
					$output += '</a>';
				$output += '</li>';
			});
			
			$output += '</ul>';
			
			$("div#image_rotator").html( $output );
			kick_rotate_functions();
		}
	});
});

function kick_rotate_functions() {
	$slider = {
		currentFrame: 1,
		slideSpeed: 5000,
		animateSpeed: 1000,
		slide: function () {
			$slider.currentFrame = ($slider.currentFrame >= $("div#image_rotator ul li").length ? 1 : $slider.currentFrame+1);
			
			$("div#image_rotator ul").animate({
				left: ( '-' + $("div#image_rotator ul li:eq(" + ($slider.currentFrame-1) + ")").position().left )
			}, $slider.animateSpeed);
		},
		start: function() {
			$interval = setInterval( function () {
				$slider.slide();
			}, $slider.slideSpeed);
		}
	};
	
	$slider.start();
}
