(function($){
	$.fn.AdRotation=function(jsonArgs){
		var curAd = 0;
		var speed = 3000; //auto play speed
		var fadeSpeed=300;
		var _this = this
		var playTime;
		initialize();
		function initialize(){
			var	adImg = $("<div class='adImg'>");
			//load the images
			for(var i=0; i<jsonArgs.length; i++){
				$(adImg).append("<div><a href='" + jsonArgs[i].linkUrl + "'><img src='" + jsonArgs[i].pic + "' /></a></div>");
			}
			$(adImg).children("div:first").nextAll().css("display","none");
			$(_this).append(adImg);
			$(_this).append("<div class='adAlt'>");		//add alter layer
			$(_this).append("<div class='adMask'>");	//add mask layer
			$(_this).find(".adAlt").html(jsonArgs[curAd].alt);
			var adSelector = $("<div class='adSelector'>");
			for(var i=1;i<6;i++){
				$(adSelector).append("<div class='section'>" + i + "</div>");
			}
			$(_this).append(adSelector);
			$(_this).find(".adSelector").children().eq(0).addClass("selected");
			$(_this).find(".adMask").css("opacity","0.6");
			playTime = setTimeout(play,speed);
			$(_this).find(".adSelector").children().bind("click",function(){
				var index = $(_this).find(".adSelector").children().index($(this));
				curAd = index+4;
				clearTimeout(playTime);
				$(_this).find(".adImg").children().css("display","none");
				play();
			})
		}
		function play(){		
			$(_this).find(".adAlt").fadeOut(fadeSpeed);
			$(_this).find(".adImg").children().eq(curAd%5).fadeOut(fadeSpeed)
			curAd++;
			ResetSelector(curAd%5);
			$(_this).find(".adAlt").html(jsonArgs[curAd%5].alt);
			$(_this).find(".adAlt").fadeIn(fadeSpeed);
			$(_this).find(".adImg").children().eq(curAd%5).fadeIn(fadeSpeed);
			playTime = setTimeout(play,speed);
		}
		function ResetSelector(eqSelected){
			$(_this).find(".adSelector").children().each(function(){
				$(this).removeClass("selected");										   
			})
			$(_this).find(".adSelector").children().eq(eqSelected).addClass("selected");
		}
	}
})(jQuery)
