// Plugins
jQuery.fn.extend({
	
	// Tabs Plugin
	myTabs: function() {
		
		var el 	= this,
				lis = $( "li", this );
				
		function reStack(current) {
			lis.each(function(index) {
				$( this ).css( "z-index", 100 - index );
			});
			current.css( "z-index", 200 );
		}

		lis.click(function(e) {
			if ( ! $(this).hasClass( "follow" ) ) {
				var a = $( this ).find( "a" ),
						tab_to_open = a.attr( "href" ).replace(/#/, "");

				$( ".current", el).removeClass( "current" );
				$( ".tab-content" ).hide();
				$( this ).addClass( "current" );
				$( ".tab-content[rel="+ tab_to_open +"]" ).show();
				reStack( $( this ) );
			}
		});

		if ( location.hash ) {
			$( "a[href="+ location.hash +"]", this ).click();
		} else {
			$( "a", this ).first().click();
		}
		
		lis.first().addClass( "first" );

		return this;
	},
	
	// Projects Slider Plugin
	pSlider: function() {
		
		return this.each(function() {

			var slider 					= $(this),
					slides 					= [],
					current_slide 	= 0,
					slides_count 		= slider.find( ".slide" ).length,
					slider_container, title, leftBtn, rightBtn;
					
			// Slides: populate slides array of objects
			slider.find( ".slide" ).each(function(index) {
				img = $(this).find( "img" );
				slides.push( { title: img.attr( "alt" ), position: - index * 208 } );
			});
			
			// Insert Rounded Corners
			slider.before( "<div class='bottom-left-corner'></div><div class='bottom-right-corner'></div>" );
			
			// Title Bar
			slider.before( "<div class='title-bar'><span class='title'></span> <div class='al'></div><div class='ar'></div></div>" );
			title = slider.parent().find( ".title" );
			title.html( slides[current_slide].title );
			
			// Container
			slider.find( ".slide" ).wrapAll( "<div class='slider-container'></div>" );
			slider_container = slider.find( ".slider-container" );
			slider_container.css( "width" , slides_count * 208 );
			
			// Arrow Buttons
			left_btn 	= slider.parent().find( ".al" );
			right_btn = slider.parent().find( ".ar" );
			
			left_btn.click(function(e) {
				
				if ( current_slide == 0 ) {
					current_slide = slides_count - 1;
				} else {
					current_slide--;
				}
				
				title.html( slides[current_slide].title );
				
				slider_container.animate({
					left: slides[current_slide].position
				});
				
			});
			
			right_btn.click(function(e) {
				
				if ( current_slide == (slides_count - 1) ) {
					current_slide = 0;
				} else {
					current_slide++;
				}
				
				title.html( slides[current_slide].title );
				
				slider_container.animate({
					left: slides[current_slide].position
				});
				
			});
			
		});
		
	},
	
	// Billboard News Plugin
	bbNews: function(options) {
		
		var defaults = { speed: 4000 };
		
		// extends defaults with options provided
		if (options) {
			$.extend(defaults, options);
		}

		// iterate over matched elements
		return this.each(function() {
			
			var headlines = $(this).find( ".headline" ),
					last_page	= headlines.length - 1,
					next_page = 1,
					pages, timer;
			
			function changeHeadline(index) { 
				//var li = pages.find( "a[href='"+ index +"']" ).closest( "li" );
				var li = pages.find( "a[rel='"+ index +"']" ).closest( "li" );
				pages.find( ".toggle a" ).removeClass( "pause" ).addClass( "play" );
				pages.find( ".current" ).removeClass( "current" );
				li.addClass( "current" );
				headlines.hide();
				headlines.eq(index).show(); 
				next_page = parseInt(index) == last_page ? 0 : parseInt(index) + 1;
				clearInterval(timer);
				timer = setInterval(function() {
					changeHeadline(next_page);
				}, defaults.speed);
			}
					
			// Pages placeholder
			$(this).append( '<ul class="pages clearfix"><li class="toggle"><a href="#"></a></li></ul>' );
			pages = $(this).find( ".pages" );
			
			// Play / Pause Toggle
			pages.find( ".toggle a" ).click(function(e) {
				e.preventDefault();
				if ( $(this).hasClass( "play" ) ) {
					$(this).removeClass( "play" ).addClass( "pause" );
					clearInterval(timer);
				} else {
					$(this).removeClass( "pause" ).addClass( "play" );
					pages.find( ".current a" ).click();
				}
			});
			
			// Generate pages for each headline
			headlines.each(function(index) {
				pages.append( '<li><a href="'+ index +'" rel="'+ index +'" >'+ (index + 1) +'</a></li>' );
			});
			
			// Bind click event to pages
			pages.find( "a[rel]" ).each(function() {
				$(this).click(function(e) { 
					e.preventDefault();
					if(!$(this).attr( "rel" )) 
						vez=0; 
					else 
						vez = $(this).attr( "rel" );
							
					changeHeadline(vez);
				}).mouseenter(function(e) { //alert(3);  vez
					$( "body" ).append( headlines.eq( $(this).attr( "rel" ) ).find( ".thumb" ).clone().css({ position: 'absolute', border: '2px solid #fff' }) );
				}).mouseleave(function(e) { //alert(1);
					$( "body > .thumb" ).remove();
				}).mousemove(function(e) {
					//alert(2);
					//alert($(this).attr( "rel" ));//thumb
					//$(this).attr( "rel" )
					//headlines.eq(index).show();
					
					$( "body > .thumb" ).css({ top: (e.pageY + 20) + "px", left: (e.pageX - 30) + "px" });
				});
			}).first().click();
			
		});
		
	},
	
	// Large Banners: Home Page
	largeBanners: function(options) {
		
		var defaults = { speed: 4000 };
		
		// extends defaults with options provided
		if (options) {
			$.extend(defaults, options);
		}

		// iterate over matched elements
		return this.each(function() {
			
			var banners = $(this).find( ".banner" ),
					last_page	= banners.length - 1,
					next_page = 1,
					pages, timer;
			
			function changeBanner(index) { 
				var li = pages.find( "a[rel='"+ index +"']" ).closest( "li" );
				pages.find( ".current" ).removeClass( "current" );
				li.addClass( "current" );
				banners.hide();
				banners.eq(index).show();
				next_page = parseInt(index) == last_page ? 0 : parseInt(index) + 1;
				clearInterval(timer);
				timer = setInterval(function() {
					changeBanner(next_page);
				}, defaults.speed);
			}
					
			// Pages placeholder
			$(this).prepend( '<div class="pages"></div>' );
			pages = $(this).find( ".pages" );
			
			// Rounded Corners
			$(this).prepend( '<div class="tl"></div><div class="tr"></div><div class="bl"></div><div class="br"></div>' );
			
			banners.each(function(index) {
				pages.append( "<li><a href='"+ index +"' rel='"+ index +"' >"+ (index + 1) +"</a></li>" );
			});
			
			pages.find( "a" ).each(function() {
				$(this).click(function(e) {
					e.preventDefault();
					changeBanner( $(this).attr( "rel" ) );
				});
			}).first().click();
			
		});
		
	},
	
	// Parceiros
	bannerZoom: function() {
		
		// iterate over matched elements
		return this.each(function() {
			
			var image = $(this);
			
			image.css("height", 80);
			
			image.mouseover(function(e) {
				$(this).stop(true, true).animate({ height: 100 }, 800, "easeOutElastic");
			}).mouseout(function(e) {
				$(this).stop(true, true).animate({ height: 80 }, 800, "easeOutElastic");
			});
			
		});
		
	}

});

jQuery(function ($) {
	$( "#banner_large" ).largeBanners();
	$( "#billboard .billboard-news" ).bbNews({ speed: 6000 });
	$( "#billboard .billboard-video" ).bbNews();
	$( "#billboard .tabs" ).myTabs();
	$( "#billboard_textual .tabs" ).myTabs();
	$( "#projects .slider" ).pSlider();
	$( "#parceiros img" ).bannerZoom();
	
	//$(".headline").
});
