/*View Element*/
function viewElement(obj){
	$('.txt_nossos_negocios').hide();
	$('#' + obj).show();
}

/*MOUSEOVER THE BUTTON FOR IE6*/
function mouseoverButton(buttonElement){
	$(buttonElement).mouseover(function(){
		$(this).css({"background-position" : " left bottom" })
	})

	$(buttonElement).mouseout(function(){
		$(this).css({"background-position" : " left top" })
	})
}

/*MODAL*/
var urlModal;
var modalAlign;

function openModal(url, callback){
	$('button, input:button, a').blur();
	
	/*URL MODAL*/
	urlModal = url;
	
	$('.view_modal, .bg_modal, .load').remove();
	
	/*CREATE ELEMENTS*/
	$('body').append('<img src="img/load.gif" width="88" height="78" alt="Carregando..." class="load" /><div class="bg_modal"></div><div class="view_modal"></div>');
	
	/*OPACITY*/
	$('.bg_modal').css('opacity', 0);
	
	/*SELECTS HIDE IE6*/
	if($.browser.msie && $.browser.version <= 6) $('select').css('visibility', 'hidden');

	/*SHOW BACKGROUND*/
	$('.bg_modal').fadeTo('normal', 0.8, function(){
		$('.bg_modal').height($('html')[0].scrollHeight);
		
		$('.view_modal').load(url, function(){
			/*CALLBACK*/
			if(callback != undefined) callback();
			
			/*REMOVE LOAD*/
			$('.load').remove();
			
			/*MODAL ALIGN*/
			if(modalAlign == 'T') $('.view_modal').css({top: 0, left: '50%', marginLeft: -$('.view_modal').width()/2 + 'px'});
			else if(modalAlign == 'TL') $('.view_modal').css({top: 0, left: 0, margin: 0});
			else if(modalAlign == 'TR') $('.view_modal').css({top: 0, right: 0, margin: 0});
			
			else if(modalAlign == 'B') $('.view_modal').css({bottom: 0, left: '50%', marginLeft: -$('.view_modal').width()/2 + 'px'});
			else if(modalAlign == 'BL') $('.view_modal').css({bottom: 0, left: 0, margin: 0});
			else if(modalAlign == 'BR') $('.view_modal').css({bottom: 0, right: 0, margin: 0});
			
			else if(modalAlign == 'L') $('.view_modal').css({top: '50%', left: 0, marginTop: -$('.view_modal').height()/2 + 'px'});
			else if(modalAlign == 'R') $('.view_modal').css({top: '50%', right: 0, marginTop: -$('.view_modal').height()/2 + 'px'});

			else $('.view_modal').css({top: '50%', left: '50%', marginTop: -$('.view_modal').height()/2 + 'px', marginLeft: -$('.view_modal').width()/2 + 'px'});
		
			/*MODAL HIDE*/
			$(this).fadeIn('show');
		
			/*CLOSE MODAL*/
			$("a[rel='modal_close'], .bg_modal").click(function(){
				closeModal();
				return false;
			})
			
			/*INIT*/
			if(typeof(init) == 'function') init();
		})
	})
}

/*CLOSE MODAL*/
function closeModal(callback){
	$('.bg_modal, .view_modal').fadeOut('hide', function(){
		$('.view_modal, .bg_modal').remove();

		/*SHOW SELECT IE6*/
		if($.browser.msie && $.browser.version <= 6) $('select').css('visibility', 'visible');
		
		/*CALLBACK*/
		if(callback != undefined) callback();
	})
}

/*INIT MODAL*/
function initModal(){
	$("a[rel='modal']").click(function(){
		$(this).blur();
		openModal($(this).attr('href'));		
		return false;
	})
}

/*INIT SIFR*/
function initSifr(){
	if(sIFR != null && sIFR.replaceElement != null){
		sIFR.replaceElement('.sifr', 'swf/65_helvetica_bold.swf', '#000000', '#000000', '#000000', 'transparent', 0, 0, 0, 0);
	}
}

/*TYPE NUMBER*/
function typeNumber(obj){
	function returnTypeNumber(obj){
		var padrao = /[^0-9]/;
		
		for(var i = 0; i < obj.val().length; i++){
			obj.val(obj.val().replace(padrao.exec(obj.val()), ''));
		}
	}
	
	$(obj).keyup(function(){
		returnTypeNumber($(this));
	})
	
	$(obj).keydown(function(){
		returnTypeNumber($(this));
	})
	
	$(obj).keypress(function(){
		returnTypeNumber($(this));
	})
	
	$(obj).focus(function(){
		returnTypeNumber($(this));
	})
	
	$(obj).blur(function(){
		returnTypeNumber($(this));
	})
	
	returnTypeNumber($(this));
}


/*SELECT*/
function SelectLinkHref(page){
	window.open(page.value)
}

/*VALIDATION*/
function validation(){
	var container = $('div.container_error');
	var validator = $(".form_fale_conosco").validate({
			errorContainer: container,
			errorLabelContainer: $("div", container),
			meta: "validate"
		});
}


/*VARS*/
var areaWidth, areaHeight, zindex;

/*RESIZE PAGE*/
function resizePage(){
	$('.content, .container, .box_destaques').height('auto');
		
	var h = $('html')[0].scrollHeight;
	var header = $('.header').height();
	var footer = $('.footer').height();
	
	if($('.content.internas').is(':visible')){
		$('.container').height(h - footer + 'px');
		
	}else{
		$('.container').height(h - footer + 'px');
		$('.content').height(h - header - footer - 27 + 'px');
		
		$('.box_destaques').height($('.content').height() - $('.box_marcas').height() - 90 + 'px');
	}
}

/*START DRAG*/
function startDrag(obj){
	if($(obj.container).is(':visible')){
		/*VARS*/
		var autoDrag, posX, posY;
		
		var objDrag = $(obj.drag);
		var objDragWidht = objDrag.width();
		var objDragHeight = objDrag.height();
		
		var top = obj.top;
		var right = obj.right;
		var bottom = obj.bottom;
		var left = obj.left;
		var timeLineClick = true;
		
		var container = $(obj.container);
		
		/*VERIFICATION*/
		if(top == undefined) top = 0;
		if(right == undefined) right = areaWidth - objDragWidht;
		if(left == undefined) left = 0;
		if(bottom == undefined) bottom = areaHeight - objDragHeight;
		
		/*MOUSE DOWN*/
		objDrag.mousedown(function(e){
			timeLineClick = true;
			
			/*OBJ DRAG*/
			objDrag = $(this);
			
			/*AUTO DRAG*/
			autoDrag = true;
			
			/*X | Y*/
			var x = e.pageX - objDrag[0].offsetLeft;
			var y = e.pageY - objDrag[0].offsetTop;
			
			/*MOUSE MOVE*/
			$(document).mousemove(function(e){
				if(autoDrag == true){
					posX = e.pageX - x;
					posY = e.pageY - y;
					
					if(container != undefined){
						top = 0;
						right = parseInt(container.width() - objDragWidht);
						bottom = parseInt(container.height() - objDragHeight);
						left = 0;
					}
					
					/*AREA*/
					if(posY <= top) posY = top;
					if(posX >= right) posX = right;
					if(posY >= bottom) posY = bottom;
					if(posX <= left) posX = left;
	
					/*POS OBJ DRAG*/
					objDrag.css({top: posY + 'px', left: posX + 'px'});
				}
			})
		})
		
		/*MOUSE UP*/
		$(document).mouseup(function(){
			autoDrag = false;
			
			if(timeLineClick == true){
				var timeLine = $('.time_line a');
				
				for(var i = 0; i < timeLine.length; i++){
					if(i == 0){
						if(posX <= parseInt(timeLine.parent().eq(i).width() + timeLine.parent()[i].offsetLeft) / 2){
							timeLine.eq(i).click();
							return false;
						}
					}
					
					if(posX <= parseInt(timeLine.parent().eq(i).width() + timeLine.parent()[i].offsetLeft) - (objDragWidht / 2)){
						timeLine.eq(i).click();
						return false;
					}
					
					if(i == timeLine.length - 1){
						if(posX <= parseInt(timeLine.parent().eq(i).width() + timeLine.parent()[i].offsetLeft)){
							timeLine.eq(i).click();
							return false;
						}
					}
				}
			}
		})
		
		container.find('a').click(function(){
			var year = $(this).parent();
			timeLineClick = false;
	
			$('.drag').animate({left: year[0].offsetLeft + 'px'}, 300, function(){
				$('.historias li').hide();
				$('.time_line li').removeClass('selected');
				
				$('.historia_' + year.attr('class').replace('tit_', '')).show();
				year.addClass('selected');
			})
		})
		
		container.find('a').eq(0).click();
	}
}

/*INIT*/
function init(){
	/*AREA*/
	areaWidth = $('html')[0].clientWidth;
	areaHeight = $('html')[0].clientHeight;
	
	/*START DRAG*/
	startDrag({drag: '.drag', container: '.time_line'});
	
	$('body').attr('onresize', 'resizePage()');
	resizePage();
	
	/*VALIDATION*/
	validation();

	/*TYPENUMBER*/
	typeNumber('.box_dados .ddd');
	typeNumber('.box_dados .telefone');

	/*MODAL*/
	initModal();

	/*MOUSEOVER THE BUTTON FOR IE6*/
	mouseoverButton('.bt_ok');
	mouseoverButton('.bt_enviar');

	/*View ELEMENT*/
	$('#box_minuano').show();
}
	
$(function(){
	init();
})