$(document).ready(function(){
	if (!$.browser.msie || ($.browser.msie && parseFloat($.browser.version)>8)) {
		$(".banner.anim").each(function(){
			var t = $(this);
			id = t.attr("id");
			var data = t.data();
			pos = data.pos.split(',');
			delay = data.delay.split(',');
			speed = data.speed.split(',');
			img_anim = data.imganim;
			hImg = t.height();
			plus = 0;
			for (i=0;i<pos.length;i++)
			{
				if (i == 0) class_frame1 = "frame1"; else class_frame1 = "";
				t.append('<div class="frame '+pos[i]+' '+class_frame1+'" data-delay="'+delay[i]+'" data-speed="'+speed[i]+'"></div>');
				t.children(".frame:last").css({"background-position":"0px -"+plus+"px"});
				plus = plus+hImg;
			}
			t.find(".frame").css({"background-image":"url("+img_anim+")"});
			t.children(".frame:first").addClass("current").css({"top":"0px","left":"0px","opacity":"1"});
			anim(id);
			/*$(this).click(function(event){
				event.preventDefault();
				anim(id);
			});*/
		});
	}
});


function anim(id){
	loop = true;
	banner = $("#"+id);
	var current = ( $(banner).children('.frame.current')? $(banner).children('.frame.current') : $(banner).children('.frame:first') );
	var next = ((current.next(".frame").length) ? ( (current.next(".frame").hasClass('current')) ? $(banner).children('.frame:first') : current.next(".frame") ) : $(banner).children('.frame:first') );
	speed = next.data("speed");
	
	if (next.hasClass("frame1")){
		if (banner.hasClass("loop")){
			resetAnim(id,function(){});
		}else{
			loop = false;
		}
	}
	if (loop){
		next.addClass("next").animate({"top":"0%","left":"0%","opacity":"1"},speed,function(){
			current.removeClass("current");
			
			$(this).addClass("current");
			delay = next.data("delay");
			$(this).animate({opacity:1},delay,function(){
				$(this).removeClass("next");
				 anim(id);
			});
		});
	}
}

function resetAnim(id,callback){
	banner = $("#"+id);
	banner.children(".frame").each(function(){
		if ($(this).hasClass("left")) $(this).css("left","-100%");
		if ($(this).hasClass("right")) $(this).css("left","100%");
		if ($(this).hasClass("top")) $(this).css("top","-100%");
		if ($(this).hasClass("bottom")) $(this).css("top","100%");
		if ($(this).hasClass("fade")) $(this).css("opacity","0");
		callback();
	});
}
