/*

Usage: 
$("contenDivSelector").MediaBox([{parameters}]);

Time: Time to show the item in milisecounds
effectVelocity: The duration of the transition effect (slow, normal, fast or time in milisecounds); 
imageClass: Selector to be used to get image of the itens
categoryClass: Selector to be used to get category of the itens
titleClass: Selector to be used to get title of the itens


$("contenDivSelector").MediaBox({
	Time: 5000,
	effectVelocity: "fast", 
	imageClass: ".mediabox-image",
	categoryClass: ".mediabox-category",
	titleClass: ".mediabox-title"
	
})

*/
function mediaboxVirgula ($){
	$.fn.MediaBox = function (options){
		var defaults = {
			Time: 5000,
			effectVelocity: "fast", 
			imageClass: ".mediabox-image",
			categoryClass: ".mediabox-category",
			titleClass: ".mediabox-title"
		};
		// Join Default Settings with  Parameters
		$.fn.MediaBox.Options = $.extend(defaults, options);
		$.fn.MediaBox.nowShowing = 0;
		
		return this.each(function (){
			// Set Element a Variable to ease manage it
			$this = $(this);
			$.fn.MediaBox.Principal = $this;
			mediaboxDiv = $this;
			
			$this.append("<div class=\"mediabox-subtitle\"></div>");
			$.fn.MediaBox.Subtitle = $(this).find(".mediabox-subtitle");
			$this.append("<div class=\"mediabox-numbers\"></div>");
			var counters = $this.find(".mediabox-numbers");
			$this.prepend("<div id=\"mediaBoxShow\"></div");
			$.fn.MediaBox.ImgFrame = $(this).find("#mediaBoxShow");
			var count = 0;
			//Get Li Itens
			$this.find("li").each(function (){
				var img = $(this).find("span"+$.fn.MediaBox.Options.imageClass).html();
				var link = $(this).find("span"+$.fn.MediaBox.Options.imageClass).find("a").attr("href");
				var target = $(this).find("span"+$.fn.MediaBox.Options.imageClass).find("a").attr("target");
				if(!target){
					target = "_self";
				}
				var category = $(this).find("span"+$.fn.MediaBox.Options.categoryClass).html();
				var title = $(this).find("span"+$.fn.MediaBox.Options.titleClass).html();
				var i = count;
				//Remove UL
				$(this).parent().remove();
				
				$.fn.MediaBox.counters = counters;
				
				counters.append("<a class=\"mediabox-countItem\" id=\"mediabox-item"+count+"\" target="+target+" href=\""+link+"\">"+(count+1)+"</a>");
				var counter = counters.find("a#mediabox-item"+count);

				counter.hover(function () {
					$.fn.MediaBox.nowShowing = i;
					if($.fn.MediaBox.Intervalo){
						clearTimeout($.fn.MediaBox.Intervalo);
					}
					$.fn.MediaBox.changeDest(img, title, category, true);
					$.fn.MediaBox.setCounterSel(counter);
					
				}, function () {
					$.fn.MediaBox.Intervalo = setTimeout($.fn.MediaBox.chandeDestAuto, $.fn.MediaBox.Options.Time, $this);
				})
				
				count++;
			})
			var first = $this.find("a.mediabox-countItem:first");
			
			first.trigger('mouseover');
			first.trigger('mouseout');
			
			$.fn.MediaBox.Intervalo = setTimeout($.fn.MediaBox.chandeDestAuto, $.fn.MediaBox.Options.Time, $this);
		})
	}
	$.fn.MediaBox.changeDest = function (img, title, cat, isHover) {
		var imgFrame = $.fn.MediaBox.ImgFrame;
		var subtitleFrame = $.fn.MediaBox.Subtitle;
		var velocityHide;
		if(isHover == true){
			velocityHide = 10;
		} else {
			velocityHide = $.fn.MediaBox.Options.effectVelocity;
		}
		imgFrame.fadeOut(velocityHide, function (){
			imgFrame.empty();
			subtitleFrame.empty();
			imgFrame.append(img);
			subtitleFrame.append("<h5>"+cat+"</h5>");
			subtitleFrame.append("<p>"+title+"</p>");
			imgFrame.fadeIn($.fn.MediaBox.Options.effectVelocity);
		})
	}
	$.fn.MediaBox.chandeDestAuto = function (target){
		var now = $.fn.MediaBox.nowShowing;
		target = $.fn.MediaBox.Principal;
		var itensLen =  target.find("a.mediabox-countItem").length;
		var next;
		if(now >= (itensLen-1)){
			next = 0;
		} else {
			next = (now+1);
		}
		
		target.find("a#mediabox-item"+next).trigger('mouseover');
		$.fn.MediaBox.nowShowing = next;
		$.fn.MediaBox.Intervalo = setTimeout($.fn.MediaBox.chandeDestAuto, $.fn.MediaBox.Options.Time, target);
		
	}
	$.fn.MediaBox.setCounterSel = function (item){
		var counters = $.fn.MediaBox.counters;
		if(counters.find("a[class*='active']")[0]){
			counters.find("a[class*='active']").removeClass("active");
		}
		item.addClass("active");
	}
}
mediaboxVirgula(jQuery);

function LoadEnquete(){
	$.get('/vgl_enquete/index.php?canal_enq=Home', function(data){
		var enquete = $("#enquete-block0");
		enquete.append("<div id=\"poll-block\"></div>");
		var poll = $("#poll-block", enquete);
		
		var search = String('<script type="text/javascript" language="javascript" src="/vgl_enquete/media/js/lib_enquete.js"></scrip'+'t>');
		data = data.replace(search, "");
		poll.html(data);
		var inputs = $(":radio", poll);
		inputs.removeAttr("onclick");

		inputs.click(function (){
			var value = $(this).attr("value");
			$.get('/vgl_enquete/include/resultado.php?inEnqueteID='+value, function (data){
				poll.html(data);
			})
		})
	});
}

VirgulaComments(jQuery)