jQuery.noConflict();

(function($) { // Compliant with jquery.noConflict()
	
	$.fn.extend({
		
		gte: function(gteid){
			$(this).click(function(){
				$("#"+gteid).toggle();
			});
		},
		
		selChan: function(ulWidth){
			
			var moving = false;
			var jThis = $(this);
			var ulCount = jThis.find(".scroll ul").length;
			if(null == ulCount || ulCount === 0)
				ulCount = jThis.find(".scroll .scrollableBox").length;
			
			jThis.find(".next a").click(function(){
				if(moving) return;
				moving=true;
				jThis.find(".scroll").animate({left: '-=' + ulWidth}, 700, 'swing', function() {
					$(jThis).selChanToggleArrows(ulWidth, ulCount);
					moving=false;
				});
				$(this).blur();
			});

			jThis.find(".prev a").click(function(){
				if(moving) return;
				moving=true;
				jThis.find(".scroll").animate({left: '+=' + ulWidth}, 700, 'swing', function() {
					$(jThis).selChanToggleArrows(ulWidth, ulCount);
					moving=false;
				  });
				$(this).blur();
			});
			
			$(jThis).selChanToggleArrows(ulWidth, ulCount);

		},
		
		selChanToggleArrows: function(ulWidth, ulCount){
			
			var jThis = $(this);
			
			var left = jThis.find(".scroll").css("left");
			
			// next
			if(-((ulCount-2)*ulWidth)+"px" == left || ulCount < 3){
				jThis.find(".next a").addClass("hide");
				jThis.find(".next span").removeClass("hide");
			}else{
				jThis.find(".next span").addClass("hide");
				jThis.find(".next a").removeClass("hide");
			}
			
			// prev
			if("0"!=left && "0px"!=left){
				jThis.find(".prev span").addClass("hide");
				jThis.find(".prev a").removeClass("hide");
			}else{
				jThis.find(".prev a").addClass("hide");
				jThis.find(".prev span").removeClass("hide");
			}
					
			
		},
		
		topEvent : function(){
			
			var top = $(this);
						
			// init
			top.find(".image-top-event-cont").html("");
			top.find(".thumb-top-event .image-top-event").clone().appendTo(top.find(".image-top-event-cont"));

			top.find(".image-top-event-cont .image-top-event").eq(0).addClass("on");
			top.find(".thumb-top-event").eq(0).addClass("on");
			
			// click
			top.find(".thumb-top-event .thumb-btn-top-event").bind("click tefire", function(){
				
				var sld_spd = 300;
				var index = top.find(".thumb-top-event .thumb-btn-top-event").index( $(this) );
				
				if(!($(this).parent(".thumb-top-event").hasClass("on")))
					top.find(".image-top-event-cont .image-top-event.on").fadeTo(sld_spd, 0, function () {
						
						$(this).removeClass("on");
						$(this).attr("style", "opacity: 1;");
						
						var current = top.find(".image-top-event-cont .image-top-event").eq(index);
						current.attr("style", "opacity: 0;");
						current.addClass("on");
						current.fadeTo(sld_spd, 1);
	
					});
				
				top.find(".thumb-top-event").removeClass("on");
				$(this).parent(".thumb-top-event").addClass("on");
				$(this).blur();
				
				// riparte il timeout
				clearTimeout ( fireTimeout );
				fireTimeout = setTimeout(top.tefireTrigger, tefireSpeed);
				
			});
			
			// velocita auto
			var tefireSpeed = parseInt(top.attr("speed"));
			if(tefireSpeed<1000 || isNaN(tefireSpeed)) tefireSpeed = 1000*30;
			
			var fireTimeout = -1;
			
			top.tefireTrigger = function() {
				
				var allThumbs = top.find(".thumb-top-event");
				var index = allThumbs.index( top.find(".thumb-top-event.on") );
				index = (index+1) % allThumbs.size();
				
				var next = top.find(".thumb-top-event .thumb-btn-top-event").eq(index);
				
				next.triggerHandler("tefire");
				
				fireTimeout = setTimeout(top.tefireTrigger, tefireSpeed);

			};
			
			fireTimeout = setTimeout(top.tefireTrigger, tefireSpeed);

			
		}
		

	});
		
})(jQuery); // Compliant with jquery.noConflict()
