//Showcase
jQuery.extend({
	showcase: {
		TIMER_TRANSITION: 7000,
		BQTD: 0,
		BID: 1,

		init: function(durl,ext,container,timer) {
			//Variables:
			var container = $("div#"+container);

			//Actions:

				//Init load
				$.ajax({
					type:"GET",
					url: durl +'.'+ ext,
					dataType: ext,
					success:
						//Rescue data
 						function(data){
							//Variables:
								//Main node
								var mnode = $(data).find('showcase');
								var bqtd = mnode.find("banner").length;
								var bID = 0;

							//Actions:
								//Insert structure
								//Sets qtd total of the banners
								$.showcase.BQTD = bqtd;
								$.showcase.add_structure(container);

							//Functions:
								$(mnode).find('banner').each(function(){
									//Variables:
									var BannerIndex	= $(this).attr("zindex");
									var BannerID	= $(this).attr("id");
									var BannerURL	= $(this).find("url").text();
									var BannerLink	= $(this).find("link").text();
									var BannertText = $(this).find("text").text();
									var BannerLI;
									//Actions:
										//Add link if link's content isn't empty
										//Else don't add link
										if ( BannerLink != "" ) {
											BannerLI = $("<li><a href='"+ BannerLink +"' target='_blank'></a></li>").css('z-index',BannerIndex);
											BannerLI.find('a').append($("<img src='" + BannerURL + "' alt='" + BannertText + "' />"));
										} else {
											BannerLI = $("<li><img src='" + BannerURL + "' alt='" + BannertText + "' /></li>").css('z-index',BannerIndex);
										}

										//Adds ul to banners
										//Adds painel to banners's button and sets its position
										//Sets class to first and others banners
										container.find("ul#banners").append(BannerLI);
										container.find("div#painel").append($("<a>&nbsp;</a>").attr("id",BannerID).attr("href","javascript:void(0);"));
										container.find("div#painel a#"+BannerID).css("right",(11*(BannerIndex-2))+"px");
										container.find("div#painel a#"+BannerID).attr('class', ( BannerID == 1 ) ? 'detached' : 'other');
										container.find("ul#banners li").eq(BannerID-1).attr('class', ( BannerID == 1 ) ? 'detached' : 'other');
								});//End each function

								//Call 'add_events' to showcase's features
								$.showcase.add_events(container, timer);
								if ( timer == true ) $.showcase.add_timer();

						}//End function(data)
				});//End $.ajax
		},//End init:

		add_structure: function(container){
			//Actions:
				//Add ul to banners
				//Add div to buttons
				container.append($("<ul></ul>").attr("id","banners"));
				container.append($("<div></div>").attr("id","painel").css("z-index",$.showcase.BQTD+1));
		},//End add_structure:

		add_events: function(container, timer) {
			//Variables:
			var ShowcaseButtons = container.find('div#painel a');
			//Functions:
			ShowcaseButtons.each(function() {
				//Variables:
				var ButtonID = $(this).attr('id');
				//Events:
					//Click event to change banners
					//If is there timer adds mouseover and mouseout events
					$(this).click( function() { $.showcase.change_banner( container, ButtonID ); });
					if ( timer == true )
						$(this).mouseover( function() { $(document).stopTime('timer'); }).mouseout(function(){ $.showcase.add_timer(); });
			});
		},

		change_banner: function (container, id) {
			//Actions:
				//Sets banner's id
				$.showcase.BID = ( $.showcase.BID >= $.showcase.BQTD ) ? 1 : ++$.showcase.BID;

				//Change detached banner to secondary
				//Detaches banner clicked
				//Show banner clicked with fade
				container.find("ul#banners li.detached").attr('class','other');
				container.find("div#painel a.detached").attr('class','other');

				container.find("ul#banners li").eq(id-1).attr('class','detached');
				container.find("div#painel a").eq(id-1).attr('class','detached');

				container.find("ul#banners li.detached").fadeTo('fast',1);

				//Hide others banners with fade
				container.find("ul#banners li.other").each(function(){
					$(this).fadeTo('fast',0);
				});
		},

		add_timer: function() {
			$(document).everyTime( $.showcase.TIMER_TRANSITION , 'timer', function() {
				//Variables:
				var bqtd = $('ul#banners li').length;
				//Actions:
					//Adds 1 in value banner's id
					//Dispatch click event
					$('div#painel a#'+$.showcase.BID).trigger('click');
			});
		}
	}
});
