/**
* 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 );
		
		if( $( "#box_empreendimentos", oThis.content ).length )
		{
			//oThis.navigationBox( $( "#box_empreendimentos", oThis.content ) );
			oThis.iMoveis( $( "#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 ) );
		
		if( $( "#detalhe_imovel", oThis.content ).length )
		{
			oThis.destaqueDetalhes( $( "#detalhe_imovel div.galeria" ) );
			oThis.openModal( $( "#modal_image" ) );
		}
			
		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 );
		
		
	},
	
	openModal: function( m )
	{
		$( ".ImagemModal" ).unbind().click( function()
		{
			var $i = $( this ),
				paren = $( this ).parents( "ul#ul_imagens" ).clone();
				
			$( "#modal" ).show();
			
			paren.appendTo( $( "div.imageMini", m ).empty() );
			$( "li", paren ).show();
			
			$( "div.imageView img" ).attr(
			{
				src: $( "img", $i ).attr( "src" ) + "?" + new Date().getTime()
			}).load( function()
			{
				var $img = $( this ),
					w = $img.width();
					
				w > 520
					? $img.css(
					{
						width: 520,
						marginLeft: 40
					})
					: $img.css(
					{
						width: w,
						marginLeft: ( 600 - w ) / 2
					});
			});
			
			m.fadeIn( function()
			{
				if( $.browser.msie )
				{
					m.css({
						position: "absolute",
						top: "10%"
					});
					
					/*$( "div.imageView", m ).hide( function()
					{
						$( "div.imageView", m ).css( "position", "relative" ).show( function()
						{
							$( "div.imageView", m ).css( "position", "absolute !important" );
						});
						
					});*/
					
					window.scroll( 0, 0 ); 
				}
				$( "div.nav", m ).fadeIn( function()
				{
					$( "div.imageView", m ).fadeIn();
					$( "div.imageView img", m ).fadeIn();
				});
				
				$( "div.nav a", m ).unbind().click( function()
				{
					var $this = $( this ),
						imageView = $( "div.imageView", m ),
						ul = $( "#ul_imagens", m ),
						active = $( "li.ativo", ul ),
						view = "";
					
					if( $this.is( ".noView" ) )
						return false;
						
					if( $this.is( ".btNext" ) )
					{
						view = active.next().length ? active.next() : $( "li:first", ul );
						active.removeClass( "ativo" );
						view.addClass( "ativo" );
						$( "div.nav .btPrev", m ).removeClass( "noView" );
						$( "div.nav a:not('.noView')", m ).hover( function()
						{
							$( "span", $( this ) ).show();	
						}, function()
						{
							$( "span", $( this ) ).hide();
						});
					}
					else
					{
						view = active.prev().length ? active.prev() : $( "li:last", ul );
						active.removeClass( "ativo" );
						view.addClass( "ativo" );
					}
						
					$( "img", imageView ).fadeOut( function()
					{
						$( this ).attr(
						{
							src: $( "img", view ).attr( "src" ) + "?" + new Date().getTime()
						}).fadeIn();
					});
					
					return false;
				}).hover( function()
				{
					$( "span", $( this ) ).show();	
				}, function()
				{
					$( "span", $( this ) ).hide();
				});
				
				$( "#ul_imagens a", m ).unbind().click( function()
				{
					var $a = $( this ),
						parent = $a.parent();
						
					if( parent.is( ".ativo" ) )
						return false;
						
					parent.siblings( "li.ativo" ).removeClass( "ativo" );
					parent.addClass( "ativo" )
					
					$( "div.imageView img", m ).fadeOut( function()
					{
						$( this ).attr(
						{
							src: $( "img", $a ).attr( "src" )
						}).fadeIn();
					});
					
					return false;
				});
				
				var ul_imagens = $( "#ul_imagens", m ),
					pages = Math.ceil($( "li", ul_imagens ).length / 12 ),
					page = 1;
					
				pages == 1
					&& $( "div.pagination", m ).hide();
				
				$( "div.pagination a", m ).unbind().click( function()
				{
					var $bt = $( this );
					
					if( $bt.is( ".inactive" ) )
						return false;
						
					if( $bt.is( ".btNext" ) )
					{
						page++;
						
						ul_imagens.animate(
						{
							top: Number( ul_imagens.css( "top" ).split( "px" )[ 0 ] ) - 380
						}, 500 );
						
						page == pages
							? $bt.addClass( "inactive" )
							: $bt.removeClass( "inactive" );
						
						$( "div.pagination .btPrev", m ).removeClass( "inactive" );
					}
					else
					{
						page--;
						ul_imagens.animate(
						{
							top: Number( ul_imagens.css( "top" ).split( "px" )[ 0 ] ) + 380
						}, 500 );
						
						page == 1
							&& $bt.addClass( "inactive" )
							
						
						$( "div.pagination .btNext", m ).removeClass( "inactive" );
					}
					
					return false;
				});
				
				$( ".bt_close", m ).unbind().click( function()
				{
					m.fadeOut( function()
					{
						$( "#modal" ).hide();
					});
					
					return false;
				});
			});
			
			
			return false;
		});
		
	},
	
	iMoveis: function( div )
	{
		var ul = $( "div.lancamentos div.overlow ul.imoveis", div ),
			lancamentos = $( "div.lancamentos", div ),
			pages = Math.ceil( Number( $( "li", ul ).length ) / 3 ),
			page = 1;
		
		ul.css( "width", 165 * Number( $( "li", ul ).length ) );
		
		$( ".bt_anterior", lancamentos ).addClass( "inactive" );
		
		$( ".bt_anterior, .bt_proximo", lancamentos ).unbind().click( function()
		{
			var $this = $( this );
			
			if( $this.is( ".inactive" ) )
				return false;
			
			$( "li", ul ).css(
			{
				opacity: "0.4",
				filter: "alpha(opacity=40)"
			});
			
			if( $this.is( ".bt_proximo" ) )
			{
				page++;
				pages == page
					? $this.addClass( "inactive" )
					: $this.removeClass( "inactive" );
				
				$( ".bt_anterior", lancamentos ).removeClass( "inactive" );
			}
			else
			{
				page--;
				page == 1
					? $this.addClass( "inactive" )
					: $this.removeClass( "inactive" );
					
				$( ".bt_proximo", lancamentos ).removeClass( "inactive" );
			}
			
			ul.animate(
			{
				left: $this.is( ".bt_proximo" ) ? Number( ul.css( "left" ).split( "px" )[ 0 ] ) - 476 : Number( ul.css( "left" ).split( "px" )[ 0 ] ) + 476
			}, 500, function()
			{
				$( "li", ul ).css(
			{
				opacity: "1",
				filter: "alpha(opacity=100)"
			});
			});
			
			return false;
		});
	},
	
	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 )
	{
		$( ".bt_proximo, .bt_anterior", 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;
				
			$( 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 == (total_li - 1) ){
				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");
			}
			
			//alert(li_atual);
			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();
			}, 7000 );
			
		$( "a", menu ).unbind().click( function()
		{
			var $this = $( this ),
				parent = $this.parent(),
				classe = parent.attr( "class" ).split( " " )[ 0 ];
			
			clearInterval( loop );
			loop = setInterval( function()
			{
				changeAuto();
			}, 7000 )
			
			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];
  }
});


