﻿(function($){
$.fn.extend({
	//plugin name - animatemenu
    az_rotatingBanner: function(options) {
    	var defaults = {
        	width: '100%',
            height: '100%',
			bgcolor: '#ffffff',
            speed: 2000,
            timeout: 2000,
			data: '',
			xmlsrc: null,
			target: '_blank',
			title: ['Play', 'Pause'],
			pagenum: true
        };
             
        var options = $.extend(defaults, options);
         
        return this.each(function() {
        	var o = options;
            var $obj = $(this);               
            var $pauseslide = 0;
            
            $.ajax({
				url: o.xmlsrc,
				type: "get",
				data: o.data,
				dataType: "xml",
				error: function(){
					$obj.html('Error loading XML document');
				},
				success: function(xml){
					parseData(xml, $obj);
					
					$("#db_toggle").click(function() { 
						if($pauseslide == 0){
							$("#slideshow").cycle("pause");
							$pauseslide = 1;
							$(this).toggleClass("pause_sel");
							$("#db_toggle2").toggleClass("play");
						}
					}).blur();
					
					$("#db_toggle2").click(function() { 
						if($pauseslide > 0){
							$("#slideshow").cycle("resume");
							$pauseslide = 0;
							$(this).toggleClass("play");
							$("#db_toggle").toggleClass("pause_sel");
						}
					}).blur();
				}
			});
        });
				
		function parseData(xml, $obj){
			var $gallery = $(xml).find('bannerlist');
			var $html = '', o = options, $slides = '', $navs = '';
			var $total = $('banner', xml).length;
			
			$path    = $gallery.attr('path');
			$width   = $gallery.attr('width') ? parseInt($gallery.attr('width')) : o.width;
			$height  = $gallery.attr('height') ? parseInt($gallery.attr('height')) : o.height;
			$speed   = $gallery.attr('speed') ? parseInt($gallery.attr('speed')) : o.speed;
			$timeout = $gallery.attr('timeout') ? parseInt($gallery.attr('timeout')) : o.timeout;
			$bgcolor = $gallery.attr('bgcolor') ? $gallery.attr('bgcolor') : o.bgcolor;
			
			$('banner', xml).each(function(i){
				$image = $path + $('image', this).text();
				$slides += '<a href="' + $('url', this).text() + '" title="' + $('title', this).text() + '" target="' + o.target + '">';
				$slides += '<img src="' + $image + '" width="' + $width + '" height="' + $height + '" alt="' + $('title',this).text() + '" /></a>';
				$navs += '<li><a' + ((i+1)==1 ? ' class="left"' : ((i+1)==$total ? ' class="right"' : '')) + ' title="' + $('title', this).text() + '" href="javascript:void(0);">' + (o.pagenum ? (i+1) : '') + '</a></li>';
			});
			
			$html += '<div id="db_controls" class="db_controls_l_bg"><div class="db_controls_r_bg"><div class="db_controls_c_bg">';
			$html += '<ul id="db_nav">' + $navs + '</ul>';
			$html += '<span id="slide_play"><a id="db_toggle2" class="play_sel" title="' + o.title[0] + '" href="javascript:void(0);"></a></span>';
			$html += '<span id="slide_pause"><a id="db_toggle" class="pause" title="' + o.title[1] + '" href="javascript:void(0);"></a></span>';
			$html += '</div></div></div>';
			$html += '<div id="slideshow" class="az_slideshow">' + $slides + '</div>';

			$obj.width($width).height($height).html($html).css('position', 'relative');
			
			$("#slideshow", $obj).width($width).height($height).cycle({
				fx: 'fade',
				speed: $speed,
				timeout: $timeout,
				pager: '#db_nav',
				next:   "#db_next", 
				prev:   '#db_prev',
				pagerAnchorBuilder: function(idx, slide) { 
					return "#db_nav li:eq(" + idx + ") a"; 
				}
			});
		}
    }
});
})(jQuery);
