



/*MAIN*/



/* * *
	ICON
* * */
var icon = {
	initialize: function(target){
		$(target).append('<div>&nbsp;</div>').hover(function(){
			$(this).children().stop().css({display:'block', opacity:0}).fadeTo(200, 1);
		}, function(){
			$(this).children().stop().fadeTo(300, 0);
		});
	}
}


/* * *
	MENU
* * */
var menu = {
	current:false,
	initialize: function(){
		var href = document.location.href.toLowerCase();
		$('#menu a').each(function(){
			if(href.match(new RegExp(this.href)))
				$(this).addClass('select');
		});
		
		// main menu
		$('#menu ul ul').prev().click(function(){
			if(menu.current)
				$(menu.current).next().slideUp(150);
				
			if(menu.current != this){
				$(this).next().slideDown(250);
				menu.current = this;
			}else{
				menu.current = false;
			}
			return false;
		});
		// box serraclick
		$('#box-sc .menu a.item').unbind()
			.click(function(){return false})
			.parent().hoverIntent(function(){
				$(this).addClass('hide').children('ul').stop().css('height', '70px').slideDown(150);
			}, function(){
				var t = $(this);
				t.children('ul').stop().slideUp(100, function(){t.removeClass('hide')});
			});
	}
};

/* * *
	NEWS
* * */
var news = {
	fonts:[['10px', '12px'], ['12px', '14px'], ['14px', '16px'], ['16px', '19px'], ['18px', '22px']],
	current:2,
	initialize:function(){
		$('#iZoomOut').click(function(){
			if(news.current > 0){
				news.current--;
				var font = news.fonts[news.current];
				$('#util .text').css({fontSize:font[0], lineHeight:font[1]});
			}
			return false;
		});
		$('#iZoomIn').click(function(){
			if(news.current < news.fonts.length-1){
				news.current++;
				var font = news.fonts[news.current];
				$('#util .text').css({fontSize:font[0], lineHeight:font[1]});
			}
			return false;
		});
		// comment
		$('#iComment').click(function(){
			$('#comment-box').slideToggle(function(){$.scrollTo('#iBack', 300)});
			$('#error-box').slideUp();
			return false;
		});
		$('#comment-box').submit(function(){
			var msg = '';
			if(!$('#comment-name').val()) msg += "- Você deve preencher seu nome.\n";
			if(!$('#comment-email').val()) msg += "- Você deve preencher seu e-mail.\n";
			if(!$('#comment-message').val()) msg += "- Você deve preencher um comentário.\n";
			if(msg){
				alert(msg); return false;
			}
			return true;
		});
		// share
		$('#iShare').click(function(){
			return false;
		});
		// error
		$('#iError').click(function(){
			$('#error-box').slideToggle(function(){$.scrollTo('#iBack', 300)});
			$('#comment-box').slideUp();
			return false;
		});
		$('#error-box').submit(function(){
			var msg = '';
			if(!$('#error-name').val()) msg += "- Você deve preencher seu nome.\n";
			if(!$('#error-email').val()) msg += "- Você deve preencher seu e-mail.\n";
			if(!$('#error-message').val()) msg += "- Você deve preencher os detalhes do erro.\n";
			if(msg){
				alert(msg); return false;
			}
			return true;
		});
		
		$('#iPrint').click(function(){
			window.print();
			return false;
		});
	}
};


/* * *
	BANNER
* * */
(function($){
	$.fn.banner = function(width, height, banners){
		var target = $(this);
		var current = 0;
		var length = banners.length;
		var name = this.attr('id')+'_';
		var nav = target.parent().children('.banner_nav');
		var vertical = name == 'bannerV_';
		var paused = false;
		var time;
		
		var top = 0;
		var pos = 0;
		var b_height = height+30;
		var p_height = 0;
		
		/* _initialize */
		function _initialize(){
			var b = banners[current];
			if(b[0] == 'swf')
				swfobject.embedSWF('upload/'+b[2], name+current, width, height, "8.0.0", "swf/expressInstall.swf", null, {wmode:"transparent"});
			$.get(b[4]+'/p');
			
			// navigation
			if(!vertical && name != 'bannerX_' && name != 'bannerH_'){
				$(banners).each(function(i){
					var n = i < 9 ? '0'+(i+1) : (i+1);
					$('<a href="">'+n+'</a>').appendTo(nav)
						.css({'top': height-24, 'right':(length-n)*20+75})
						.click(function(){
							current = i;
							_set_banner();
							return false;
						});
				});
				nav.children(':eq(3)').addClass('selected');
			}
			nav.children().css('top', height-24);
			
			nav.children('.prev').click(function(){
				_start(-1); return false;
			});
			nav.children('.next').click(function(){
				_start(+1); return false;
			});
			nav.children('.stop').click(function(){
				paused = !paused;
				if(!paused)
					_start(+1);
				else
					clearTimeout(time);
				$(this).toggleClass('stop').toggleClass('play');
				return false;
			});
			
			// close / open
			target.parent().children('.banner_close').click(function(){
				clearTimeout(time);					
				target.parent().prev('.banner_open').slideDown(400);
				target.parent().slideUp(400);
				return false;
			}).animate({top:8}, 600);
			target.parent().prev('.banner_open').click(function(){
				_start();
				target.parent().prev('.banner_open').slideUp(300);
				target.parent().slideDown(300);
				return false;
			});
			
			// vertical
			if(vertical && $('#util').height() > height){
				top = target.parent().offset().top;
				pos = top + height/2;
				p_height = getPageSize()[3];
				
				$(window).scroll(_set_position);
			}
			
			// final
			if(length > 1){
				nav.animate({top:0}, 600);
				_start();
			}
		}
		
		/* _start */
		function _start(num){
			clearTimeout(time);
			var b = banners[current];
				
			if(num){
				current += num;
				if(current >= length)
					current = 0;
				else if(current < 0)
					current = length-1;
				_set_banner();
			}else{
				var next = current+1;
				if(next >= length)
					next = 0;
				var bn = banners[next];
				
				if(bn[0] == 'img'){
					var img = new Image();
					img.onload = function(){
						time = setTimeout(function(){
							_set_banner(current = next);										 
						}, b[1]*1000);
						img.onload = null;
					}
					img.src = 'upload/'+bn[2];
				}else{
					time = setTimeout(function(){
						_set_banner(current = next);										 
					}, b[1]*1000);
				}
			}
		};
		
		/* _set_banner */
		function _set_banner(){
			var last = target.children();
			var banner = banners[current];
			var lastImg = last.get(0).tagName == 'A' || last.get(0).tagName == 'SPAN';
			
			nav.children().removeClass('selected');
			nav.children('a:eq('+(3+current)+')').addClass('selected');
				
			if(banner[0] == 'img'){
				if(banner[3])
					target.append('<a href="'+banner[4]+'" id="'+name+current+'" style="left:-'+width+'px" target="_blank"><img src="upload/'+banner[2]+'" width="'+width+'" height="'+height+'" /></a>').children(':last').animate({left:0}, 500);
				else
					target.append('<span id="'+name+current+'" style="left:-'+width+'px"><img src="upload/'+banner[2]+'" width="'+width+'" height="'+height+'" /></span>').children(':last').animate({left:0}, 500);
			}else{
				target.append('<div id="'+name+current+'"></div>');
				swfobject.embedSWF('upload/'+banner[2], name+current, width, height, "8.0.0", "swf/expressInstall.swf", null, {wmode:"transparent"});
				$('#'+name+current).css('opacity', 0).fadeTo(500, 1);
			}
			
			$.get(banner[4]+'/p');
			
			if(lastImg && banner[0] == 'img')
				last.animate({left:width}, 500, function(){$(this).remove()});
			else
				last.fadeOut(500, function(){$(this).remove()});
			
			if(!paused)
				_start();
		};
		
		/* _set_position */
		function _set_position(){
			var scrollTop = $(window).scrollTop()+p_height/2;
			
			var go = false;
			if(scrollTop > pos + b_height/3){
				pos = scrollTop + b_height/3;
				go = true;
			}else if(scrollTop < pos - b_height/3){
				pos = scrollTop - b_height/3;
				go = true;
			}
			
			if(go){
				if(pos < top + (b_height/2))
					pos = top + (b_height/2);
				else if(pos > $('#util').height()+top-(b_height/2))
					pos = top + $('#util').height()-(b_height/2);
				target.parent().stop().animate({top:pos-(b_height/2)-top}, 300);
			}
		};
			
		_initialize();
	};
})(jQuery);


/* * *
	TOOLTIP
* * */
var tooltip = {
	initialize:function(){
		$('a[rel=tooltip]').hover(function(event){
			$('#tooltip').remove();
			var html = tooltip.t_title = this.title;
			tooltip.t_img = false;
			this.title = "";
			if(html.substr(0, 4) == 'img|'){
				var html = '<img src="'+html.substr(4)+'" />';
				tooltip.t_img = true;
			}
			$('<div id="tooltip">'+html+'</div>').appendTo(document.body)
				.addClass('t_'+this.className.split(' ')[0])
				.css({top:0, left:0})
			$(this).mousemove(tooltip.update);
			tooltip.update(event);
		}, function(){
			$('#tooltip').remove();
			$(this).attr('title', tooltip.t_title);
		});
	},
	update:function(event){
		var t = $('#tooltip');
		var w = t.width();
		if(tooltip.t_img){
			var left = event.pageX+30;
			var top = event.pageY-10;//+20;
		}else{
			var left = event.pageX+50-w;
			var top = event.pageY-t.height()-20;
		}
		
		if($(window).width() < left+w)
			left = $(window).width()-w;
			
		t.css({top:top, left:left});
	}
}


/* * *
	RESTRICTED
* * */
var restricted = {
	count:1,
	initialize: function(){
		$('.iMoreFile').click(function(){
			if(restricted.count < 3){
				var element = $('.form-file fieldset').get(0);
				$('<fieldset>'+element.innerHTML+'</fieldset>').insertAfter(element);
				restricted.count++;
			}else{
				alert('Você não pode enviar mais que três arquivos de uma única vez.');
			}
			return false;
		});
	}
}


/* * *
	UTILS
* * */
var utils = {
	initialize: function(){
		$('#iBack').click(function(){
			history.back();
			return false;
		});
		// serraclick
		$('#box-sc div.more').click(function(){
			if($(this).children('span').css('right') == '165px')
				$(this).children('span').stop().animate({right:0}, 200);																		 
			else
				$(this).children('span').stop().animate({right:165}, 300);
		});
		
		// forms
		$('#top-search')
			.focus(function(){
				if(this.value == 'BUSCAR') this.value = '';
			}).blur(function(){
				if(!this.value) this.value = 'BUSCAR';
			});
		$('#news-name')
			.focus(function(){
				if(this.value == 'NOME') this.value = '';
			}).blur(function(){
				if(!this.value) this.value = 'NOME';
			});
		$('#news-email')
			.focus(function(){
				if(this.value == 'E-MAIL') this.value = '';
			}).blur(function(){
				if(!this.value) this.value = 'E-MAIL';
			});
		$('#restricted-login')
			.focus(function(){
				if(this.value == 'LOGIN') this.value = '';
			}).blur(function(){
				if(!this.value) this.value = 'LOGIN';
			});
		$('#restricted-password')
			.focus(function(){
				if(this.value == 'SENHA'){
					var target = this.cloneNode(false);
					target.value = '';
					target.type = 'password';
					this.parentNode.replaceChild(target, this);
					$('#restricted-password').focus(function(){});
					setTimeout(function(){target.focus()}, 10);
				}
			});
	}
};


/* * *
	PANEL
* * */
(function($){
	$.fn.panel = function(time, size){
		var current = -1;
		var length;
		var target = '#'+this.attr('id');
		var status = true;
		var timer, timer_fnc;
		/* _initialize */
		function _initialize(){
			length = $(target+'-inner-move').css('left', size).children().length;
			$(target+' .iLeft').show().bind('click', left);
			$(target+' .iRight').show().bind('click', right);
			right();
		};
		/* left */
		function left(){
			if(!status) return false; busy(left);
			current--;
			if(current < 0){
				$(target+'-inner-move :last').clone(true).insertBefore(target+'-inner-move :first');
				$(target+'-inner-move').css('left', -size).animate({left:0}, time, function(){
					current = length-1;
					$(this).children(':first').remove();
					$(this).css('left', -current*size);
					ready();
				});
			}else{			
				$(target+'-inner-move').animate({left:-current*size}, time, ready);
			}
			return false;
		};
		/* right */
		function right(){
			if(!status) return false; busy(right);
			current++;
			if(current >= length){
				$(target+'-inner-move :first').clone(true).insertAfter(target+'-inner-move :last');
				$(target+'-inner-move').css('left', -((length-1)*size)).animate({left:-length*size}, time, function(){
					current = 0;
					$(this).children(':last').remove();
					$(this).css('left', 0);
					ready();
				});
			}else{			
				$(target+'-inner-move').animate({left:-current*size}, time, ready);
			}
			return false;
		};
		/* ready */
		function ready(){
			$(target+'-container').attr('href', $(target+'-inner-move :eq('+current+')').attr('href'));
			timer = setTimeout(timer_fnc, 5000);
			status = true;
		};
		/* busy */
		function busy(fnc){
			timer_fnc = fnc;
			clearTimeout(timer);
			status = false;
		};
		_initialize();
	};
})(jQuery);

/*-----------------------------------------------------------------------------------------------*/

$(function(){
	icon.initialize('.icon');
	icon.initialize('#top ul a');
	utils.initialize();
	
	menu.initialize();
	tooltip.initialize();
});
