jQuery(document).ready(function(){
	html_js_class();
	html_clickable();
	menu_behaviour();
	blocks_behaviour();
	input_text_behaviour();
	textarea_behaviour();
	accordion_behaviour();
	swap_behaviour();
	download_area_behaviour();
	slideshow_behaviour();
	fix_page_min_height();
	fix_group_height();
	start_colorbox();
	if( typeof(addthis)!='undefined' ){
		addthis.init();
	}
});

jQuery(window).load(function(){
	slideshow_behaviour_after_load();
	fix_page_min_height();
});

jQuery(window).resize(function(){
	fix_page_min_height();
});


/* browser capabilities */

function html_js_class(){
	var $html = jQuery('html');
	$html.addClass('js_available');
	$html.removeClass('js_unavailable');
}

function html_clickable(){
	var $html = jQuery('html');
	if( is_touch_device() ){
		$html.addClass('touchable');
	} else {
		$html.addClass('clickable');
	}
}

function is_touch_device(){
	if ('ontouchstart' in document.documentElement){
		return true;
	} else {
		return false;
	}
}


/* menu */

function menu_behaviour(){
	// fix_menu_columns();
	
	var $top_buttons			= jQuery('#topo .parent_nav > li');
	var $top_buttons_touchable	= jQuery('#topo .parent_nav > li > a');
	var $rodape_children		= jQuery('#rodape .child_nav');
	var $parent_child_navs 		= jQuery('#topo .child_nav');
	
	$parent_child_navs.append('<li class="child_end" />');
	
	$rodape_children.each(function(){
		var $child = jQuery(this);
		$child.mouseleave(function(){
			var $parent = jQuery(this).parent();
			$parent.removeClass('active');
		});
	
		$child.mouseenter(function(){
			var $parent = jQuery(this).parent();
			$parent.addClass('active');
		});
	});
	
	$top_buttons_touchable.each(function(){
		var $top_button = jQuery(this);
		var $html = jQuery('html');
		
		if( $html.hasClass('touchable') ){
			$top_button.click(function(evt){
				// var $child = jQuery(this).find('.child_nav');
				var $child = jQuery(this).parent().find('.num_cols_3');
				if( $child.length>0 ){
					evt.preventDefault();
					if( $child.parent().hasClass('active') ){
						$child.parent().removeClass('active');
						$child.fadeOut('fast', function(){
							$child.css('top', '-10000px');
						});

					} else {
						$child.css('top', '55px');
						$child.fadeOut(0);
						$child.fadeIn('fast');
						$child.parent().addClass('active');
					}
				}
			});
		}
	});
	
	$top_buttons.each(function(){
		var $top_button = jQuery(this);
		var $html = jQuery('html');
		
		if( $html.hasClass('clickable') ){
			if( $html.hasClass('latest') ){
				$top_button.mouseleave(function(){
					// var $child = jQuery(this).find('.child_nav');
					var $child = jQuery(this).find('.num_cols_3');
					$child.parent().removeClass('active');
					$child.fadeOut('fast', function(){
						$child.css('top', '-10000px');
					});
				});

				$top_button.mouseenter(function(){
					// var $child = jQuery(this).find('.child_nav');
					var $child = jQuery(this).find('.num_cols_3');
					$child.css('top', '55px');
					$child.fadeOut(0);
					$child.fadeIn('fast');
					$child.parent().addClass('active');
				});

			} else {
				$top_button.mouseleave(function(){
					// var $child = jQuery(this).find('.child_nav');
					var $child = jQuery(this).find('.num_cols_3');
					$child.parent().removeClass('active');
					$child.hide();
					$child.css('top', '-10000px');
				});

				$top_button.mouseenter(function(){
					// var $child = jQuery(this).find('.child_nav');
					var $child = jQuery(this).find('.num_cols_3');
					$child.css('top', '55px');
					$child.show();
					$child.parent().addClass('active');
				});
			}
		}

	});
}

function fix_menu_columns(){
	// função sem uso, além de estar com BUG no IE, foi substituída por uma solução CSS.
	// desvantagem da solução CSS: os submenus ficam com largura fixa arbitrária,
	// que devem ser alteradas na mão caso os submenus mudem.
	var $children = jQuery('.parent_nav .child_nav');

	$children.each(function(){
		var $child = jQuery(this);
		var child_width = 0;
		$child.find('.col').each(function(){
			var $col = jQuery(this);
			child_width = child_width + $col.width();
		});
		$child.css('width', (child_width)+'px');
	});
}


/* page */

function fix_page_min_height(){
	var $html = jQuery('html');
	
	if( !$html.hasClass('ie6') ){
		var $container = jQuery('#container');
		var $rodape = jQuery('#rodape');
		var $window = jQuery(window);
		$container.css('minHeight', ($window.height()-$rodape.height()-30)+'px' );
	}
}

function fix_group_height( $context ){
	var $html = jQuery('html');
	
	if( !$html.hasClass('ie6') ){
		if( typeof($context)=='undefined' ){
			var $groups = jQuery('.group');
		} else {
			var $groups = jQuery($context).find('.group');
		}

		var $slides = $groups.closest('.slideshow').find('.slide');
		if( $slides.length>0 ){
			$slides.each(function(){
				var $this = jQuery(this);
				if( $this.is(":hidden") ){
					$this.addClass("hide_after_function");
					$this.show();
				};
			});
		}

		$groups.each(function(){
			var $this = jQuery(this);
			
			if( !$this.hasClass('fixed_height') ){
				var $target = $this.children();
			
				// requires jsizes plug-in
				var z = $target.padding().bottom + $target.margin().bottom;
				if( $html.hasClass('ie7') ){
					z = z-26;
				}
			
				$target.css('height', ($this.height()-z)+'px');
				$this.addClass('fixed_height');
			}
		});
		
		if( $slides.length>0 ){
			fix_slides_height( $slides );
			$slides.filter('.hide_after_function').removeClass('hide_after_function').hide();
		}
	}
}

function fix_slides_height( $slides ){
	var this_height = 0;
	var max_height = 0;
	
	if( $slides.length>0 ){
		$slides.each(function(){
			var $this = jQuery(this);
			this_height = $this.children().height();
			max_height = this_height>max_height ? this_height : max_height;
		});
	
		$slides.height( max_height );
		$slides.parent().height( max_height );
	}
}


/* blocks */

function blocks_behaviour(){
	var $blocks = jQuery('.article_link_wide, .article_link_highlight');
	
	$blocks.click(function(evt){
		var $this = jQuery(this);
		var href = $this.find('.thumbnail_link').attr('href');
		location.href = href;
	});
}


/* forms */

function input_text_behaviour(){
	var $inputs_text = jQuery('input[type=text]');
	
	$inputs_text.each(function(){
		var $input = jQuery(this);
		var $label = jQuery('label[for='+$input.attr('id')+']');
		
		if( $label.length>0 ){
			$input.focus(function(){
				if( $input.attr('value')==$label.text() ){
					$input.attr('value', '');
				}
			});
		
			$input.blur(function(){
				if( $input.attr('value')=='' ){
					$input.attr('value', $label.text());
				}
			});
		}
	});
}

function textarea_behaviour(){
	var $textareas = jQuery('textarea');
	
	$textareas.each(function(){
		var $textarea = jQuery(this);
		var $label = jQuery('label[for='+$textarea.attr('id')+']');
		
		if( $label.length>0 ){
			$textarea.focus(function(){
				if( $textarea.html()==$label.text() ){
					$textarea.html('');
				}
			});
		
			$textarea.blur(function(){
				if( $textarea.html()=='' ){
					$textarea.html($label.text());
				}
			});
		}
	});
}


/* sidebar */

function accordion_behaviour(){
	accordion_hide_targets();
	
	var $accordion_buttons = jQuery('.accordion_button');
	var $html = jQuery('html');
	
	$accordion_buttons.each(function(){
		var $this = jQuery(this);
		$this.click(function(){
			accordion_select( $this, $html, false );
		});
	});
	
	var $currently_selected = jQuery('.accordion_button.selected');
	accordion_select( $currently_selected, $html, true );
}

function accordion_select( $this, $html, prevent_toggle ){
	$this.siblings('.accordion_button').removeClass('selected');
	if( !prevent_toggle ){
		$this.toggleClass('selected');
	}
	
	var $parent = $this.parent();
	
	if( $html.hasClass('latest') ){
		$parent.find('.accordion_target').slideUp();
		$parent.find('.selected').next('.accordion_target').slideDown('fast');
	} else {
		$parent.find('.accordion_target').hide();
		$parent.find('.selected').next('.accordion_target').show();
	}
}

function accordion_hide_targets(){
	var $accordion_targets = jQuery('.accordion_target');
	$accordion_targets.hide();
}


/* swap */

function swap_behaviour(){
	var $html = jQuery('html');
	var $swap_target_on  = jQuery('.swap_target .swap_button').closest('.swap_target');
	var $swap_target_off = $swap_target_on.next('.swap_target');
	
	$swap_target_off.hide();
	
	$swap_target_on.find('.swap_button').click(function(evt){
		evt.preventDefault();
		
		if( $html.hasClass('latest') ){
			$swap_target_on.slideUp('fast');
			$swap_target_off.slideDown();
		} else {
			$swap_target_on.hide();
			$swap_target_off.show();
		}
	});
}


/* download area */

function download_area_behaviour(){
	var $html = jQuery('html');
	var $big_download_icon = jQuery('#marca #area_2 #downloads .download_icon');
	var $download_area = jQuery('#marca #area_2 #downloads .download_options');
	
	if( $html.hasClass('clickable') ){
		$download_area.mouseenter(function(){
			$big_download_icon.addClass('on');
		});
	
		$download_area.mouseleave(function(){
			$big_download_icon.removeClass('on');
		});
	}
}


/* slideshows */

function slideshow_behaviour(){
	// home
	fadefx_setup( jQuery('#home #slideshow_1'), true, "fade", jQuery('#home #slideshow_arrow_prev_1'), jQuery('#home #slideshow_arrow_next_1') );
	
	// colecao
	thumbslider_setup( jQuery('#colecao #slideshow_nav_1 .thumbnails_wrap'), 4, 6, 119, 150 ); // thumbslider_setup sempre antes de fadefx_setup
	fadefx_setup( jQuery('#colecao #slideshow_1'), true, "fade", jQuery('#colecao #slideshow_arrow_prev_1'), jQuery('#colecao #slideshow_arrow_next_1'), jQuery('#colecao #slideshow_nav_1 .thumbnails_wrap') );
	fadefx_setup( jQuery('#colecao #slideshow_2'), false, "horizontal", jQuery('#colecao #slideshow_arrow_prev_2'), jQuery('#colecao #slideshow_arrow_next_2'), jQuery('#colecao #slideshow_nav_2 .steps_wrap') );

	// marca
	fadefx_setup( jQuery('#marca #slideshow_2'), false, "horizontal", jQuery('#marca #slideshow_arrow_prev_2'), jQuery('#marca #slideshow_arrow_next_2'), jQuery('#marca #slideshow_nav_2 .steps_wrap') );

	// produtos
	fadefx_setup( jQuery('#products_list .slideshow'), false, "horizontal", jQuery('#products_list .slideshow_arrow.prev'), jQuery('#products_list .slideshow_arrow.next'), jQuery('#products_list .slideshow_nav .steps_wrap') );
}

function slideshow_behaviour_after_load(){
	// home
	if( jQuery('#home').length>0 ){
		var home_timer = setInterval('autoclick_home_slideshow()', 8000);

		jQuery('#home #slideshow_arrow_next_1, #home #slideshow_arrow_prev_1').click(function(){
			clearInterval( home_timer );
			home_timer = setInterval('autoclick_home_slideshow()', 8000);
		});
	}
}

function autoclick_home_slideshow(){
	jQuery('#home #slideshow_arrow_next_1').trigger('click');
}


/* fade slides */

function fadefx_setup( $target, loop, animation_type, $prev, $next, $menu ){
	if( typeof($target)!='undefined' ){
		fadefx_css_setup( $target );
		fadefx_menu_behaviour( $target, animation_type, $prev, $next, loop, $menu );
		fadefx_prev_next_behaviour( $target, animation_type, $prev, $next, loop, $menu );

		// first animation
		fadefx_animate_slides( $target, animation_type, "left", $target.children().first() );
		fadefx_mark_menu_as_slide( $target, loop, $menu, $prev, $next );
	}
}

function fadefx_css_setup( $target ){
	var $slides = $target.children();
	$slides.css('position', 'absolute');
	$slides.css('top', '0');
	$slides.css('left', '0');
	$slides.hide();
}

function fadefx_mark_slide( $target, $slide ){
	$slide.siblings().removeClass('selected');
	$slide.addClass('selected');
}

function fadefx_mark_menu_as_slide( $target, loop, $menu, $prev, $next ){
	if( typeof($menu)!='undefined' ){
		var i = $target.children('.selected').index();
		var q = i;
		var count = $target.children().length;
		if( i>-1 ){
			var $buttons = $menu.children().eq(q);
			$buttons.siblings().removeClass('selected');
			$buttons.addClass('selected');
			fadefx_thumbslider_auto_click( $menu );
			fadefx_prev_next_state( loop, q, count, $prev, $next );
		}
	}
}

function fadefx_prev_next_state( loop, current_index, count, $prev, $next ){
	if( !loop ){
		if( current_index==0 ){
			$prev.addClass('disabled');
		} else {
			$prev.removeClass('disabled');
		}
		
		if( current_index==(count-1) ){
			$next.addClass('disabled');
		} else {
			$next.removeClass('disabled');
		}
	}
}

function fadefx_animate_slides( $target, animation_type, direction, $current_slide, $next_slide ){
	if( typeof($next_slide)!='undefined' ){
		// other animations
		fadefx_mark_slide( $target, $next_slide );

		// type: fade
		if( animation_type=="fade" ){
			$current_slide.css('zIndex', 3);
			$next_slide.css('zIndex', 2);
			$current_slide.fadeOut();
			$next_slide.fadeIn();
		}
		
		// type: horizontal
		if( animation_type=="horizontal" ){
			if( direction=="left" ){
				var p_in  = "-100%";
				var p_out = "100%";
			} else {
				var p_in  = "100%";
				var p_out = "-100%";
			}
			$current_slide
				.fadeIn(0)
				.css('left', '0%')
				.animate({left: p_in}, function(){
					$current_slide.css('left', '0%').fadeOut(0);
				});
			$next_slide
				.fadeIn(0)
				.css('left', p_out)
				.animate({left: '0%'});
		}
	} else {
		// first animation
		fadefx_mark_slide( $target, $current_slide );
		$current_slide.fadeIn(0);
	}
}

function fadefx_menu_behaviour( $target, animation_type, $prev, $next, loop, $menu ){
	if( typeof($menu)!='undefined' ){
		$menu.children().find('a').click(function(evt){
			evt.preventDefault();
			var $this = jQuery(this);
			if( !$this.parent().hasClass('selected') ){
				var q				= $this.parent().index();
				var $current_slide	= $target.children('.selected');
				var $next_slide		= $target.children().eq(q);
				var direction 		= ($current_slide.index() < $next_slide.index()) ? "left" : "right";

				fadefx_animate_slides( $target, animation_type, direction, $current_slide, $next_slide );
				fadefx_mark_menu_as_slide( $target, loop, $menu, $prev, $next );
			}
		});
	}
}

function fadefx_prev_next_behaviour( $target, animation_type, $prev, $next, loop, $menu ){
	if( typeof($prev)!='undefined' && typeof($next)!='undefined' ){
		$prev.click(function(evt){
			evt.preventDefault();
			fadefx_prev_next_click( $target, animation_type, 'prev', loop, $menu, $prev, $next );
		});
		$next.click(function(evt){
			evt.preventDefault();
			fadefx_prev_next_click( $target, animation_type, 'next', loop, $menu, $prev, $next );
		});
	}
}

function fadefx_prev_next_click( $target, animation_type, which, loop, $menu, $prev, $next ){
	var $current_slide = $target.children('.selected');
	var $next_slide = (which=='next') ? $current_slide.next() : $current_slide.prev();
	var direction = (which=='next') ? "left" : "right";
	
	// loop
	if( loop ){
		if( which=='prev' && !$next_slide.length>0 ){
			$next_slide = $target.children().last();
		}
		if( which=='next' && !$next_slide.length>0 ){
			$next_slide = $target.children().first();
		}
	}
	
	if( $next_slide.length>0 ){
		fadefx_animate_slides( $target, animation_type, direction, $current_slide, $next_slide );
		fadefx_mark_menu_as_slide( $target, loop, $menu, $prev, $next );
	}
}


/* move thumbnails */

function thumbslider_setup( $menu, step, max, thumb_width, speed ){
	if( $menu.length>0 ){
		var max = max-1;
		var needle = $menu.find('.thumbnail').length;

		$menu.data('step',			step);
		$menu.data('max', 			max);
		$menu.data('thumb_width', 	thumb_width);
		$menu.data('speed', 		thumb_width);
		$menu.data('needle', 		needle);

		$menu.find('.thumbnail').eq(0).addClass('current');
		thumbslider_behaviour( $menu );
	}
}

function thumbslider_behaviour( $menu ){
	var step		= $menu.data('step');
	var max			= $menu.data('max');
	var thumb_width	= $menu.data('thumb_width');
	var speed		= $menu.data('speed');
	var needle		= $menu.data('needle');
	
	$menu.closest('.thumbnails').parent().find('.next').click(function(evt){
		evt.preventDefault();
		thumbslider_click( $menu, 'next' );
	});
	$menu.closest('.thumbnails').parent().find('.prev').click(function(evt){
		evt.preventDefault();
		thumbslider_click( $menu, 'prev' );
	});
}

function thumbslider_click( $menu, which, step ){
	var step		= step ? step : $menu.data('step');
	var max			= $menu.data('max');
	var thumb_width	= $menu.data('thumb_width');
	var speed		= $menu.data('speed');
	var needle		= $menu.data('needle');
	
	var $current	= $menu.find('.current');
	var current		= $current.length>0 ? $current.index()+1 : 1;
	var movement	= 0;
	
	if(which=='next'){
		if( (needle-current-step)<max){
			step = needle-current-max;
		}
		movement = step;
		var go = current+step;

	} else {
		if(current-step<1){
			step = current-1;
		}
		movement = -step;
		var go = current-step;
	}
	
	var q = go-1;
	$menu.find('.thumbnail').removeClass('current');
	$menu.find('.thumbnail').eq(q).addClass('current');
	current = go;

	thumbslider_move( $menu, current, movement );
}

function thumbslider_move( $menu, current, movement ){
	var thumb_width	= $menu.data('thumb_width');
	var speed		= $menu.data('speed');
	
	var module		= Math.abs(movement);
	var speed		= module*speed;
	var twidth		= thumb_width;
	
	if(movement>0){
		$menu.animate({ left: -movement*twidth }, speed, function(){
			$menu.find('.thumbnail').css('display', 'block');
			for(var i=1; i<current; i++ ){
				var q = i-1;
				$menu.find('.thumbnail').eq(q).css('display', 'none');
			}
			$menu.css('left', 0);
		});
	}
	
	if(movement<0){
		$menu.find('.thumbnail').css('display', 'block');
		for(var i=1; i<current; i++ ){
			var q = i-1;
			$menu.find('.thumbnail').eq(q).css('display', 'none');
		}
		$menu.css('left', movement*twidth+'px');
		$menu.animate({ left: 0 }, speed);
	}
}


/* thumbnails + slides integration */

function fadefx_thumbslider_auto_click( $menu ){
	if( typeof($menu)!='undefined' ){
		var selected	= $menu.find('.selected').index();
		var current		= $menu.find('.current').index();
		var max			= $menu.data('max');
		
		if( selected<current ){
			var step = current-selected;
			thumbslider_click( $menu, 'prev', step );
		}
		
		if( selected>(current+max) ){
			var step = selected-(current+max);
			thumbslider_click( $menu, 'next', step );
		}
	}
}


/* start colorbox */

function start_colorbox(){
	jQuery('.article.post').each(function(){
		var $this = jQuery(this);
		var id = $this.attr('id');
		$this.find('a[href$="jpg"], a[href$="jpeg"], a[href$="png"], a[href$="gif"]').attr('rel', 'gallery_'+id);
	});
	
	if(jQuery().colorbox) {
		jQuery('img').attr('title', '');

		var $document = jQuery(document);
		var $linked_images = jQuery('a[href$="jpg"], a[href$="jpeg"], a[href$="png"], a[href$="gif"]');
	
		$document.unbind('cbox_complete');
		$document.unbind('cbox_closed');
		$document.bind('cbox_complete', function(){
			jQuery('#cboxSlideshow, #cboxPrevious, #cboxNext, #cboxClose').css('visibility', 'visible');
		});
		$document.bind('cbox_closed', function(){
			jQuery('#cboxSlideshow, #cboxPrevious, #cboxNext, #cboxClose').css('visibility', 'hidden');
		});
	
		$linked_images.colorbox({
			transition: "none",
			width: "97%",
			height: "93%",
			title: function(){
				return jQuery(this).find('img').attr('alt');
			}
		});
	
		start_colorbox_hint();
	 }
}

function start_colorbox_hint(){
	var $html = jQuery('html');
	var $linked_images = jQuery('a[href$="jpg"], a[href$="jpeg"], a[href$="png"], a[href$="gif"]');
	
	if( $html.hasClass('clickable') ){
		$linked_images.tooltip({
			track: true, 
		    delay: 0, 
		    showURL: false, 
		    fade: 50
		});
	}
}


