



/*MAIN*/



/* * *
	UTILS
* * */
var utils = {
	font_current:2,
	fonts:['0.8em', '0.9em', '1em', '1.3em', '1.6em'],
	initialize:function(){
		// ICON
		utils.icon('.icon');
		
		// TOP MENU 
		$('#top .submenu').parent().hoverIntent(function(){
			$(this).children('.submenu').stop().css('height', 'auto').slideDown(200)
				.children('.bar').css('width', $(this).width()-2);
		}, function(){
			$(this).children('.submenu').stop().css('height', 'auto').slideUp(150);
		}).children('a').click(function(){return false});
		
		// NOTCLICK
		$('#menu .notclick, #sidebar .notclick').click(function(){
			if($(this).next().hasClass('level2'))
				$(this).next().slideToggle(300);
			return false;																										
		});	
		
		// A IMG
		$('a img').parent().hover(function(){
			$(this).children('img').stop().fadeTo(250, .8);
		}, function(){
			$(this).children('img').stop().fadeTo(300, 1);
		});
		
		//  MENU 
		$('#menu .level2 .level').parent().hoverIntent(function(){
			var t = $(this).css('position', 'relative').children('.level');
			t.children('.bar').height($(this).height()+2);
			t.stop().show().css({width:0, height:t.children('ul').height()+10, overflow:'hidden'}).animate({width:200}, 150, function(){
				$(this).css('overflow', 'visible');																																																													
			});
		}, function(){
			$(this).children('.level').stop().css({overflow:'hidden'}).animate({width:0}, 100, function(){
				$(this).hide().parent().css('position', 'static');																														 
			});
		});
		
		// MENU TITLE
		var menu_title = $('#menu_title');
		if(menu_title.length)
			swfobject.embedSWF("swf/menu_title.swf", "menu_title", "168", "26", "8.0.0", "swf/expressInstall.swf", {title:menu_title.html().toUpperCase()}, {wmode:"transparent", allowscriptaccess:"always"});		
		// TITLE
		var title = $('#title');
		if(title.length)
			swfobject.embedSWF("swf/title.swf", "title", "536", "38", "8.0.0", "swf/expressInstall.swf", {title:title.html().toUpperCase()}, {wmode:"transparent", allowscriptaccess:"always"});
		// BOX TITLE
		$('.box h3 span, .box2 h3 span, ').each(function(){
			swfobject.embedSWF("swf/box_title.swf", $(this).attr('id'), "496", "28", "8.0.0", "swf/expressInstall.swf", {title:this.innerHTML}, {wmode:"transparent", allowscriptaccess:"always"});
		});
		
		// FONT
		$('#font_A0').click(function(){
			if(utils.font_current > 0){
				utils.font_current--;
				var font = utils.fonts[utils.font_current];
				$('.text, .list').css({fontSize:font, lineHeight:font});
			}
			return false;												 
		});
		$('#font_A1').click(function(){
			if(utils.font_current < utils.fonts.length-1){
				utils.font_current++;
				var font = utils.fonts[utils.font_current];
				$('.text, .list').css({fontSize:font, lineHeight:font});
			}
			return false;												 
		});
		
		// NAVIGATION
		$('#iTop').click(function(){
			$.scrollTo('#top', 300);
			return false;
		});
		$('#iBack').click(function(){
			history.back();
			return false;											 
		});
	},
	icon:function(target){
		$(target).append('<div>&nbsp;</div>').hover(function(){
			$(this).children('div').stop().css({display:'block', opacity:0}).fadeTo(200, 1);
		}, function(){
			$(this).children('div').stop().fadeTo(250, 0);
		});
	}
};
/* * *
	INDEX NEWS
* * */
(function($){
	$.fn.index_news = function(imgs, size){
		var t = this;
		var p = $('#'+this.attr('id')+'_photo');
		var p_span = p.children();
		var m = this.children('ul');
		var current = 0;
		var interval = 0;
		
		/* initialize */
		function initialize(){
			m.children().each(function(){
				var link = $(this);
				var num =	link.attr('class').substr(4);
				link.mouseenter(function(){
					clearInterval(interval);
					// wait animation
					interval = setTimeout(function(){next(num)}, 500);
				});
			});
			// first
			m.children('.news0').addClass('select');
			next_interval();
		};

		/* next */
		function next(to){
			clearInterval(interval);
			t.unbind();
			if(!to){
				var num = current + 1;
				if(num == imgs.length)
					num = 0;
			}else{
				t.mouseleave(next_interval);
				var num = parseInt(to);
			}
		
			var link = m.children('.news'+num);
			if(current != num){
				m.children('.news'+current).removeClass('select');
				// load image
				var img = new Image();
				img.onload = function(){
					if(current == num){
						p.attr('href', link.addClass('select')
							.children('a').attr('href')
						);
						
						p_span.prepend(
							p_span.children('img').clone().attr('src', img.src)
						).css('left', -size).animate({left:0}, 500, function(){
							p_span.children('img:last').remove();																									
						});
						if(!to)
							next_interval();
					}
					img.onload = function(){};
				};
				img.src = 'upload/news_index/'+imgs[num];
			}
			current = num;
		};
		/* next_interval */
		function next_interval(){
			clearInterval(interval);
			interval = setTimeout(function(){next()}, 8000);
		};
		
		initialize();
	};
})(jQuery);

/* * *
	TOP PANEL
* * */
(function($){
	$.fn.top_panel = function(banners){
		var target = this;
		var current = 0;
		var length = banners.length;
		
		/* _initialize */
		function _initialize(){
			var banner = banners[current];
			_set_banner();
		}
		
		/* _start */
		function _start(){
			var banner = banners[current];
			current++;
			if(current >= length)
				current = 0;
			setTimeout(_set_banner, banner[1]*1000);
		};
		
		/* _set_banner */
		function _set_banner(){
			var last = target.children();
			var banner = banners[current];
			var img = new Image();
			img.onload = function(){
				target.append('<img src="'+banner[0]+'" width="740" height="200" />')
					.children(':last').animate({top:0}, 500);
				last.animate({top:-200}, 500, function(){$(this).remove()});
				img.onload = null;
			}
			img.src = banner[0];
			
			if(length > 1)
				_start();
		};
			
		_initialize();
	};
})(jQuery);

/* * *
	BIDDING
* * */
var bidding = {
	change:function(){
		var cpfcnpj = $('#type').val();
		if(cpfcnpj){
			if(cpfcnpj == 'CPF'){
				$('#cpfcnpj').unmask().mask('999.999.999-99');
				$('#description_label').html('Nome *');
				$('#cpfcnpj_label').html('CPF *');
				$('#contact_p').hide();
			}else{
				$('#cpfcnpj').unmask().mask('99.999.999/9999-99');
				$('#description_label').html('Empresa *');
				$('#cpfcnpj_label').html('CNPJ *');
				$('#contact_p').show();
			}
			$('#bidding_form').show();
		}else{
			$('#bidding_form').hide();
		}
	}
};

/* * *
	PANEL
* * */
(function($){
	$.fn.panel = function(time, size, tag, visible){
		var current = -1;
		var length;
		var target = '#'+this.attr('id');
		if(!tag) tag = 'a';
		if(!visible) visible = 1;
		/* _initialize */
		function _initialize(){
			length = $(target+'-inner-move').css('left', size).children().length;
			setTimeout(function(){
				for(var i=0; i<visible-1; i++) // joga elementos no fim da fila
					$(target+'-inner-move '+tag+':eq('+i+')').clone(true).insertAfter(target+'-inner-move '+tag+':last');
			}, 500);
			$(target+' .iLeft').show().bind('click', left);
			$(target+' .iRight').show().bind('click', right);
			right();
		};
		/* left */
		function left(){
			current--;
			if(current < 0){
				$(target+'-inner-move '+tag+':eq('+(length-1)+')').clone(true).insertBefore(target+'-inner-move '+tag+':first');
				$(target+'-inner-move').css('left', -size).animate({left:0}, time, function(){
					current = length-1;
					$(this).children(tag+':first').remove();
					$(this).css('left', -current*size);
				});
			}else{			
				$(target+'-inner-move').animate({left:-current*size}, time);
			}
			return false;
		};
		/* right */
		function right(){
			current++;
			if(current >= length){
				$(target+'-inner-move '+tag+':eq('+(visible-1)+')').clone(true).insertAfter(target+'-inner-move '+tag+':last');
				$(target+'-inner-move').css('left', -((length-1)*size)).animate({left:-length*size}, time, function(){
					current = 0;
					$(this).children(tag+':last').remove();
					$(this).css('left', 0);
				});
			}else{
				$(target+'-inner-move').animate({left:-current*size}, time);
			}
			return false;
		};
		_initialize();
	};
})(jQuery);

/* * *
	PANEL LIMITED
* * */
(function($){
	$.fn.panel_limited = function(time, size, pos, tag, visible){
		var current = 0;
		var length;
		var target = '#'+this.attr('id');
		if(!tag) tag = 'a';
		/* _initialize */
		function _initialize(){
			length = $(target+'-inner-move').children().length-visible;
			if(length <= 0){
				pos = 0;
				length = 0;
			}
			if(pos > length/2)
				current = length;
			$(target+'-inner-move').css('left', -current*size);
				
			$(target+' .iLeft').show().click(function(){move(current-visible); return false;});
			$(target+' .iRight').show().click(function(){move(current+visible); return false;});
			move(pos);
		};
		/* move */
		function move(pos){
			if(!length)
				pos = 0;
			if(pos <= 0){
				pos=0;
				$(target+' .iLeft').addClass('iLeft_hide');
			}else{
				$(target+' .iLeft').removeClass('iLeft_hide');
			}
			if(pos >= length){
				pos = length;
				$(target+' .iRight').addClass('iRight_hide');
			}else{
				$(target+' .iRight').removeClass('iRight_hide');
			}
			var shift = Math.abs(current-pos);
			if(shift)
				$(target+'-inner-move').animate({left:-pos*size}, shift*time);
			current = pos;
		};
		_initialize();
	}
})(jQuery);

/*-----------------------------------------------------------------------------------------------*/

$(document).ready(function(){
	utils.initialize();
	
	// s
	$('#s').focus(function(){
		$(this).parent().addClass('select');											 
	}).blur(function(){
		$(this).parent().removeClass('select');											 
	})
	
	// back
	$('#iBack').click(function(){
		history.back();
		return false;
	});
});