// doc ready set go
jQuery(document).ready(function() {

	// do the plugin passing in the desired data object and optional options
	jQuery('.slideshow').IX_homeRotator(rotatorcontent, {classname: "home-rotator"});			

}); //victory	


// IX_rotator plugin
// wrap it up with jQuery passed in as $ for name collision safety
(function($) {

	// plugin definition
	$.fn.IX_homeRotator = function(content, options) {

		// if the content didn't get created or isn't an object, bail now before errors get tossed
		if (content == undefined || typeof content !== "object") { return this; }
		
		if (!this.length) { return false; }

		// build main options before element iteration
		// extends exposed defaults property
		// options might include transition effects or speeds or ???
		var opts = $.extend({}, $.fn.IX_homeRotator.defaults, options);

		// vaaarrrrrs	
		var $this = $(this); 			// the rotator container
		var $ul = $('<ul />').addClass(opts.classname || "rotator"); // the new rotator container
		var current = opts.show || 1; // current displayed rotatee
		var total = content.length; // total number of rotatee
		var	displayed = []; // array to store index of previously displayed slides

		// loop over content object and build out each rotatee	
		for (var j = 0; j < total; j++) {
			var rotatee = $.fn.IX_homeRotator.tmpl.replace(/<%=content%>/g, content[j].content),
				links = "";

			// for items that could have more than one, loop to create
			// this example uses multiple links placed into the btn template
			$.each(content[j].links, function(i) {
				if (content[j].links[i].url != null) {
					
					content[j].links[i].url += (content[j].links[i].url.parseQuery()) ? '&promoID=' + encodeURIComponent(content[j].title) : '?promoID=' + encodeURIComponent(content[j].title);
					
					links += $.fn.IX_homeRotator.btn.replace(/<%=url%>/g, content[j].links[i].url)
												.replace(/<%=text%>/g, content[j].links[i].text)
												.replace(/<%=top%>/g, content[j].links[i].top)
												.replace(/<%=left%>/g, content[j].links[i].left)
												.replace(/<%=bgImage%>/g, content[j].links[i].bgImage)
												.replace(/<%=width%>/g, content[j].links[i].width)
												.replace(/<%=height%>/g, content[j].links[i].height)
												.replace(/<%=target%>/g, content[j].links[i].targetBlank);
				}
			});
			var $links = $(links);
			$(rotatee).find('.nav-rotator').append(links).end().appendTo($ul);
		}
		// make the nav and counter
		var pagelist = "";
		for (var p = 1; p <= total; p++) { pagelist += '<li class="pagemarker">Page ' + p + '</li>'; }
		$ul.append($.fn.IX_homeRotator.nav.replace(/<%=pagelist%>/g, pagelist));

		// hide all but the default rotatee
		$('li.rotator-item:not(:eq(' + (current - 1) + '))', $ul).hide();

		// turn on the current page marker in the page list
		$('li.pagemarker', $ul).eq(current - 1).addClass('on');


		// dump in the new html structure
		$this.html($ul);
		
		// report display of current slide to analytics
		var reportImpression = function(index) {
			if(!displayed.match(index)) {
				trackEvent(['_trackEvent', 'Promotion', 'Impressions', content[index].title]);
				displayed.push(index);
			}
		};
		reportImpression(current - 1);		

		// the cycler
		var cycle = function(e,i) {
			//ANIMATE OUT
			var aO = {};
			aO['right'] = '-5000px';
			
			//CSS OUT			
			var cO = {};
			cO['display'] = 'none';
	
			//ANIMATE IN
			var aI = {};
			aI['right'] = '0px';
			
			//RESET CSS IN
			var cI = {};
			cI['right'] = '0px';
			cI['display'] = 'block';
						
			// set current depending on what nav item was pressed	
			current = $(e.target).parents('.arrow-previous').length ? current += -1 : current += 1;
				
			reportImpression(current - 1);
			
			// loop to beginning or end if previous setting resulted in non-existent rotatee
			current = current < 0 ? total - 1 : current == total ? 0 : current;
			
			var currentSlide = $('li.rotator-item:visible', $this);
			
				//CONDITION OUT RESPECTIVE NUMBERS FOR CURRENT AND NEXT SLIDE
				//IN BOTH ARROWS AND NAV BUTTONS
				var c = '';
				if(i == null){
					c = current - 1;
				}else{
					c = i;
					current = (i + 2 > total)? 0 : i+1;
				}
				
			var nextSlide = $('li.rotator-item').eq(c);

			//////////////////////////////////////////////////
				$('.nav-rotator a').fadeOut();
				currentSlide.find('.content img').eq(1).fadeOut(function(){
					currentSlide.find('.content img').eq(0).stop().animate(aO, {
						duration: opts.speed,
						complete: function(){
							//BEGIN NEXT SLIDE TRANSITION
							currentSlide.find('.content img').eq(1).fadeOut(speed, function(){
								currentSlide.css(cO);
								
								nextSlide.css(cI).find('.content img').eq(0).css({
									right: '-5000px'
								});
								
								if (nextSlide.css(cI).find('.content img').eq(1).length > 0) {
									nextSlide.css(cI).find('.content img').eq(1).css({
										display: 'none'
									});
								}
								nextSlide.css(cI).find('.nav-rotator a').css({
									display: 'none'
								});
								nextSlide.css(cI).find('.content img').eq(0).stop().animate(aI, {
									duration: opts.speed,
									complete: function(){
										nextSlide.css(cI).find('.content img').eq(1).fadeIn(opts.speed);
										nextSlide.css(cI).find('.nav-rotator a').fadeIn(opts.speed);
									}
								});
							});
						//END NEXT SLIDE TRANSITION
						}
					});
				});		
			//////////////////////////////////////////////////
			
			$('.pages li.on', $this).removeClass('on');
			$('.pages li.pagemarker').eq(current - 1).addClass('on');
		};

		// start the auto rotating
		if (opts.auto) {
			var speed = $.fn.IX_homeRotator.autospeed || 8000;
			$.fn.IX_homeRotator.rotate = setInterval(function() { $this.trigger('rotate'); }, speed);
		}

		// return and iterate
		return this.each(function() {
			$('a.btn-arrow', this).live('click', function(e) {
				clearInterval($.fn.IX_homeRotator.rotate);
				cycle(e);
				e.preventDefault();
			});
			
			$(this).bind('rotate', function(e) {
				cycle(e);
				e.preventDefault();
			});
			
			$('.pagemarker').each(function(i){
				$(this).click(function(e){
					clearInterval($.fn.IX_homeRotator.rotate);
					
					//CONDITION TO PREVENT SAME SLIDE ANIMATIONS
					if ($(this).attr('class') != "pagemarker on") {
						cycle(e, i);
					}
					$('.pages li.on', $this).removeClass('on');
					$(this).addClass('on');
				});
			});
		});
		
	}; // end plugin definition

	// exposed functions

	// default options
	$.fn.IX_homeRotator.defaults = {
		transition: "fade",
		speed: 700,
		auto: true,
		autospeed: 8000,
		classname: ""
	};

	// base html templates
	// better to base of html?
	$.fn.IX_homeRotator.tmpl = '<li class="rotator-item" style=""><div class="wrapper"><div class="content"><p><%=content%></p></div><div class="nav-rotator"></div></div></li>';
	$.fn.IX_homeRotator.btn = '<a class="btn" href="<%=url%>" style="top:<%=top%>; left:<%=left%>; background-image:url(<%=bgImage%>); width:<%=width%>; height:<%=height%>" target="<%=target%>"></a>';
	$.fn.IX_homeRotator.nav = '<div class="nav-rotate"><ul><li class="arrow-previous"><a class="btn-arrow ir" href="#">Previous</a></li><li class="pages"><ol><%=pagelist%></ol></li><li class="arrow-next"><a class="btn-arrow ir" href="#">Next</a></li></ul></div>';

})(jQuery);


