/**
* Arquivo responsavel pelas funcionalidades do site Beiramar
*
* @author: Mateus Moura chagas[dot]mateus[at]gmail[dot]com
* @date: 17/10/2010
*
* Copyright(c) Todos os direitos reservados a 
*/ 

if (window.console == null) window.console = { log: function(p) { }, error: function(p) { } };
		
var oThis;

var Actions = function()
{
	oThis = this;
	
	this.init();
};

Actions.fn = Actions.prototype;
Actions.fn.extend = jQuery.extend;
Actions.fn.extend(
{
    init: function()
	{
		this.content = $( "#content" );
		this.header = $( "#header" );
		this.footer = $( "#footer" );
		
		if( !$.browser.msie )
			$( "html" ).addClass( "ff" );
		
		var navigation = $( "div.navegacao", oThis.footer );
		
		$( "#box_empreendimentos", oThis.content ).length
			&& oThis.navigationBox( $( "#box_empreendimentos", oThis.content ) );
			
		$( "div.suacasa", oThis.header ).length
			&& oThis.suacasa( $( "div.suacasa", oThis.header ) );
		
		$( "div.destaque", oThis.content ).length
			&& oThis.slideDestaque( $( "div.destaque", oThis.content ) )
			
		$( "#trabalhe_conosco", oThis.content ).length
			&& oThis.trabConosco( $( "#trabalhe_conosco div.cpf", oThis.content ) );
		
		$( "#quero_anunciar", oThis.content ).length
			&& oThis.queroAnunciar( $( "#quero_anunciar div.cpf", oThis.content ) );
		
		
		$( "#meu_perfil", oThis.content ).length
			&& oThis.trabConosco( $( "#meu_perfil div.usuario", oThis.content ) );
		
		$( "#detalhe_imovel", oThis.content ).length
			&& oThis.destaqueDetalhes( $( "#detalhe_imovel div.galeria" ) );
			
		oThis.navigationFooter( navigation, $( "div.conteudo", navigation ), $( "ul.menu", navigation ) );
		oThis.navigationFooter( $( "div.fale", navigation ), $( "div.fale div.subconteudo", navigation ), $( "div.fale ul.submenu", navigation ) );
		oThis.modal();
		oThis.atendimento();
		oThis.navigationHeader( $( "#box_busca", oThis.header ) );
		oThis.resizefonts( oThis.header );
	},
	
	resizefonts: function( div )
	{
		$( ".bt_up, .bt_down", div ).unbind().click( function()
		{
			var ps = $( "p, a" ),
				bt = $( this );
			
			if( bt.is( ".bt_up" ) )
			{
				ps.each( function()
				{
					var i = $( this ),
						c = Number( i.css( "font-size" ).split( "px" )[ 0 ] );
					
					if( c <= 15 )
						i.css( "font-size", c + 1 );
				});
			}
			else
			{
				ps.each( function()
				{
					var i = $( this ),
						c = Number( i.css( "font-size" ).split( "px" )[ 0 ] );
					
					if( c >= 11 )
						i.css( "font-size", c - 1 );
				});
			}
			
			
			
			
			return false;
		});
		

	},
	
	destaqueDetalhes: function( div )
	{
		console.log( div )
		$( ".bt_proximo, .bt_anterior, .bt_anterior_inativo", div ).unbind().click( function()
		{
			var ul = $( "ul", div ),
				liAtivo = $( "li.ativo", ul );
				total_li = $("#ul_imagens > li").length - 1;
				li_atual = new Number(liAtivo.index());
			if($( this ).is( ".bt_proximo_inativo" ))
				return false;
			if($( this ).is( ".bt_anterior_inativo" ))
				return false;
			/* alert(total_li);
			alert("atual"+li_atual)
			alert(li_atual+1); */
				
			$( this ).is( ".bt_proximo" )
				? liAtivo.next().length
					? liAtivo.fadeOut( "fast" ).removeClass( "ativo" ).next().fadeIn( "fast" ).addClass( "ativo" )
					: ( liAtivo.fadeOut( "fast" ).removeClass( "ativo" ) ) && $( "li:first", ul ).fadeIn( "fast" ).addClass( "ativo" )
				: liAtivo.prev().length
					? liAtivo.fadeOut( "fast" ).removeClass( "ativo" ).prev().fadeIn( "fast" ).addClass( "ativo" )
					: ( liAtivo.fadeOut( "fast" ).removeClass( "ativo" ) ) && $( "li:last", ul ).fadeIn( "fast" ).addClass( "ativo" )
			
			
			if( li_atual+1 == total_li ){
				if($( this ).is( ".bt_proximo" ))
				{
					$(this).removeClass("bt_proximo");
					$(this).addClass("bt_proximo_inativo");
				}
			}else{
				$("#bt_proximo").removeClass("bt_proximo_inativo");
				$("#bt_proximo").addClass("bt_proximo");
			}
			
			if( li_atual == 1 ){
				if($( this ).is( ".bt_anterior" ))
				{
					$(this).removeClass("bt_anterior");
					$(this).addClass("bt_anterior_inativo");
				}
			}else{
				$("#bt_anterior").removeClass("bt_anterior_inativo");
				$("#bt_anterior").addClass("bt_anterior");
			}
			
			return false;
		});
	},
	
	bordaDivs: function()
	{
		 $('#box_busca ul.box_menu li').removeCorners();
	},
	
	navigationHeader: function( div )
	{
		var menu = $( "ul.box_menu", div ),
			conteudo = $( "div.busca", div );
		
		$( "a", menu ).unbind().click( function()
		{
			
			
			
			var $this = $( this ),
				parent = $this.parent(),
				cl = $this.attr( "class" );
			
			if(cl == 'vender')
			{
				url = $this.attr("href");
				window.location.href=""+url+"";
				return false;
			}
			//parent.parent().remove();
			//parentClone.prependTo( div );
			
			if( parent.is( ".ativo" ) )
				return false;
				
			$( "div.ativo", conteudo ).fadeOut( function()
			{
				$( this ).removeClass( "ativo" );
				$( "div." + cl, conteudo ).fadeIn().addClass( "ativo" );
				$( "li.ativo", menu ).removeClass( "ativo" );
				$( "li." + cl, menu ).addClass( "ativo" );
				oThis.navigationHeader( div );
			});
			
			return false;
		});
	},
	
	// Slide do destaque na home
	slideDestaque: function( div )
	{
		var conteudo = $( "div.conteudo", div ),
			menu = $( "ul.navegacao", div ),
			ul = $( "ul", conteudo ),
			changeAuto = function()
			{
				var next = $( "li.ativo", menu ).next().length
							? $( "li.ativo", menu ).next()
							: $( "li:first", menu );
				
				$( "a", next ).trigger( "click" );
			},
			loop = setInterval( function()
			{
				changeAuto();
			}, 4000 );
			
		$( "a", menu ).unbind().click( function()
		{
			var $this = $( this ),
				parent = $this.parent(),
				classe = parent.attr( "class" ).split( " " )[ 0 ];
			
			clearInterval( loop );
			loop = setInterval( function()
			{
				changeAuto();
			}, 4000 )
			
			if( parent.is( ".ativo" ) )
				return false;
			
			$( "li.ativo", conteudo ).fadeOut( function()
			{
				$( "li." + classe, ul ).fadeIn().addClass( "ativo" );
				$( "li.ativo", menu ).removeClass( "ativo" );
				parent.addClass( "ativo" );
				
			}).removeClass( "ativo" );
			
			
			return false;
		});
	},
	
	// Navegação dos imaveis 
	navigationBox: function( div )
	{
		var ul = $( "ul.imoveis", div ),
			details = $( "div.detalhes", div ),
			iNext = 0,
			iPrev = 0,
			slideImage = function( actClass, liActive, boxActive, $this )
			{	
				liActive.add( boxActive.hide() ).removeClass( "ativo" );
				
				$( "li." + actClass, ul ).addClass( "ativo" );
				$( "div." + actClass, details ).show().addClass( "ativo" );
				
				if( $this != undefined )
				{
					if( $( "li." + actClass, ul ).is( ".lastLi" ) || $( "li." + actClass, ul ).is( ".first" ) )
						$this.addClass( "inativo" );
					else
					{
						$this.is( ".bt_proximo" )
							? $( ".bt_anterior", div ).removeClass( "inativo" )
							: $( ".bt_proximo", div ).removeClass( "inativo" )
					}
				}
				
			},
			ulAnimate = function( op, i, $this )
			{
				var ulLeft = Number(ul.css( "left" ).split( "px" )[ 0 ]);
				
				if( $this.is( ".inativo" ) )
					return false;
				
				if( op == "center" )
				{
					ul.animate(
					{
						left: 0
					});
				}
				else
				{
					if( op)
					{ 
						ul.animate(
						{
							left: ulLeft + -328
						});
						
						iNext = 0;
						iPrev = 0;
					}
					else
					{
						ul.animate(
						{
							left: ulLeft - ( -328 )
						});
						
						iPrev = 0;
						iNext = 0;
					}
				}
			};
		
		/*$( "a", ul ).unbind().click( function()
		{
			var $this = $( this ),
				parent = $this.parent(),
				cl = parent.attr( "class" ).split( " " )[ 0 ],
				liActive = $( "li.ativo", ul ),
				boxActive = $( "div.ativo", details );
				
			if( parent.is( ".ativo" ) )
				return false;
				
				
			$( "li", ul ).each( function( i )
			{
				var c = $( this ).attr( "class" ).split( " " )[ 0 ],
					lic = liActive.attr( "class" ).split( " " )[ 0 ];
				
				if( c == lic )
					ulAnimate( true, "", $this );
				
				if( cl == c )
				{
					
					
					
					slideImage( cl, liActive, boxActive )
					
					
					
					return false;
				}
				
			});
			
			return false;
		});*/
		
		$( ".bt_proximo, .bt_anterior", div ).unbind().click( function()
		{
			var $this = $( this ),
				liActive = $( "li.ativo", ul ),
				boxActive = $( "div.ativo", details );
			
			if( $this.is( ".inativo" ) )
				return false;
			
			if( $( this ).is( ".bt_proximo" ) )
			{
				var classe = liActive.next().attr( "class" ).split( " " )[ 0 ];
				
				iNext++;
				iPrev--;
				
				slideImage( classe, liActive, boxActive, $this );
			}
			else
			{
				var classe = liActive.prev().attr( "class" ).split( " " )[ 0 ];
				
				iPrev++;
				iNext--;
				
				slideImage( classe, liActive, boxActive, $this );
			}
			
			if( iNext == 2 )
				ulAnimate( true , iNext, $this );
			
			if( iPrev == 2 )
				ulAnimate( false, iPrev, $this );
			
			return false;
		}); 
	},
	
	// Abrir modal de Login
	modal: function()
	{
		$( ".loginCadastro" ).unbind().click( function()
		{
			$( "div.modal" ).modal();
			
			return false;
		});
		
		
		$( ".ImagemModal" ).unbind().click( function()
		{
			id = this.id;
			src = $("#"+id+" img").attr('src');
			$("#img_modal").attr('src',src);
			$( "div.modal2" ).modal();
			return false;
		});
		$( ".porEmailLink" ).unbind().click( function()
		{
			$( "div.porEmail" ).modal();
			
			return false;
		});
	
		
		
		$( ".porTelLink" ).unbind().click( function()
		{
			$( "div.porTel" ).modal();
			
			return false;
		});
	},
	
	
	
	
	// Funcionalidade das abas em suacasa
	suacasa: function( div )
	{
		$( "ul.abas li a", div ).unbind().click( function()
		{
			var $this = $( this ),
				conteudo = $( "div.conteudo", div ),
				classe = $this.parent().attr( "class" );
			
			if( div.is( ".aberto" ) )
			{
				/*$( " > div:visible", conteudo ).fadeOut( function()
				{
					$( "div." + classe ).fadeIn();	
				})*/
				div.removeClass( "aberto" );
				conteudo.hide();
				
				$( "div." + classe ).fadeOut();	
			}
			else
			{
				div.addClass( "aberto" );
				conteudo.show();
				
				$( "div." + classe ).fadeIn();	
			}
			
			
			return false;
		});
	},
	
	// Navegação no rodapé
	navigationFooter: function( div, cont, menu )
	{
		$( "a", menu ).unbind().click( function()
		{
			var $this = $( this ),
				parent = $this.parent(),
				classe = $this.attr( "class" );
			
			if( parent.is( ".ativo" ) )
				return false;
			
			$( "li.ativo", menu ).removeClass( "ativo" );
			parent.addClass( "ativo" );
			
			$( "> div.ativo", cont ).fadeOut( function()
			{
				$( this ).removeClass( "ativo" );
				
				$( "div." + classe ).fadeIn( function()
				{
					var t = $( this );
					
					t.is( ".fale" )
						&& oThis.navigationFooter( t, $( "div.subconteudo", t ), $( "ul.submenu", t ) )
				}).addClass( "ativo" );
			});
			
			return false;
		});
	},
	
	// Abrir atendimento online
	atendimento: function()
	{
		$( ".bt_abrir_atendimento" ).unbind().click( function()
		{
			$( "#atendimento" ).fadeIn();
			
			return false;
		});
	},
	
	trabConosco: function( div )
	{
		console.log( div )
		var ul = $( "ul", div );
		
		$( "a", ul ).unbind().click( function()
		{
			var $this = $( this ),
				parent = $this.parent(),
				forms = $( "form", parent ),
				active = $( "li.active", ul );
			
			$( "form", active ).slideUp( function()
			{
				active.removeClass( "active" );
			});
				
			forms.slideDown( function()
			{
				parent.addClass( "active" );
			});
			
			return false;
		});
		
		$( ".bt_next", ul ).unbind().click( function()
		{
			var $this = $( this ),
				parent = $this.parents( "li" );
				
			//alert($this.id);
			if( parent.next().length )
			{
				$( "form", parent ).slideUp( function()
				{
					parent.removeClass( "active" );
				});
				
				$( "form", parent.next() ).slideDown( function()
				{
					parent.next().addClass( "active" );
				});
			}
			else
			{
				//console.log( "submit" );
			}
				
			
			return false;
		});
	},
	
	queroAnunciar: function( div )
	{
		console.log( div )
		var ul = $( "ul", div );
		
		$( "a", ul ).unbind().click( function()
		{
			var $this = $( this ),
				parent = $this.parent(),
				forms = $( "form", parent ),
				active = $( "li.active", ul );
			
			$( "form", active ).slideUp( function()
			{
				active.removeClass( "active" );
			});
				
			forms.slideDown( function()
			{
				parent.addClass( "active" );
			});
			
			return false;
		});
	}
	
	
});

$( function()
{
	var actions = new Actions();
});

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


