// JavaScript Document

/**************************************************************

------------ Hash change function for back button -------------

**************************************************************/
var _paq = _paq || [];

$(window).hashchange(function () {	

	if (location.hash != '#'){

		var divTofade = location.hash.split('#');

	    if (divTofade[1] != null) {

	             url = '/pagina/' + divTofade[1];

		         id = divTofade[1];

	             loadContent(url, id);

				      /*if($('#sidePanel').length != 0){

		                   closeSidePanel();*/

						   _paq.push(['trackPageView', url + '.html']);

						   _paq.push(['setDocumentTitle', 'Twin Towers - ' + id ]);

	

			 

	    }

    }

});



    

$(document).ready(function(){ 



var homeLink = $('#logo a');

var menuitem = $('#menu ul li a');

/**************************************************************

----------Check for hash value in URL on page load ------------

**************************************************************/

    if(window.location.hash) {

    var hash = window.location.hash.substr(1);

   // var href = menuitem.each(function(){

	var id = location.hash.split('#');

	var url = '/pagina/' + id[1];

   // var id = $(this).attr('id').split('_');

    //var url = $(this).attr('href');

        if(hash == id[1]){

            loadContent(url, id[1]);

			//menuAnimation(id[1]);

			//_paq.push(['_trackPageview',url]);

			$('#m_'+id[1]).addClass('active');

        } 

	//});

	}else {

		   loadContent('/pagina/o-edificio', 'o-edificio');
		   
		   $('#m_o-edificio').addClass('active');
		   
		   //_paq.push(['_trackPageview', '/pagina/o-edificio.html']);

	}











   menuitem.each(function(){

	  var page = $(this).attr('id').split('_');

	 // var url = $(this).attr('href');

	  $(this).bind('click', function(event) {

	   if($(this).attr('class') == 'active'){ 

			   return false;  

	      }else{

			  $('#menu a').removeClass('active');

			  //alert(page[1]);

			  event.preventDefault();

		     // _gaq.push(['_trackPageview',url]);

		      window.location.hash = '#' + page[1];

			  $(this).addClass('active');

			  return false;	

	      };

       });

      

    });
	
	
	homeLink.click(function(){
		
		$('#menu a').removeClass('active');
		$('#m_o-edificio').addClass('active');
		
	});





});



/**************************************************************

----------------------Functions--------------------------------

**************************************************************/



function loading() {

	  $('#loading').html('<span><img src="workspace/images/loader.gif" width="48" height="48"/><span>').css('display', 'block').fadeIn(100);

};

  

function loaded() {	      

      $('#loading').fadeOut(500, 'linear');

};

  

function loadContent(url, divTofade) {

	  //Show loading div

	  loading();

	  var currContent = $(".currContent");

	  var oldHeight = $('#ajax_conteiner').height();

	 // alert(divTofade);

	      currContent.animate({

		       opacity: 0,

			   height: oldHeight  

	           },200,

			   function(){

		               $('#ajax_conteiner').load(url,

	                       function(){

							 var newContent = "#ajax_" + divTofade;

							 var newHeight = $(newContent).height();

							 //alert(newHeight);

                             $(newContent).css("height", oldHeight);

                             $(newContent).css("display", "none");

							 //alert(newHeight);

							 

                             $('#ajax_conteiner').animate({

								 

								 height: newHeight 

								    

							 },500, function(){

								  $(newContent).height(newHeight);

								  $(newContent).fadeIn(1000);

								  loaded();  

							 });

	                    });

				

	     });

	  

};



function slider(conteiner, effect, speed, time, nav, elem){

	$(conteiner).cycle({ 

    fx:     effect, 

    speed:  speed, 

    timeout: time, 

    pager:  nav,

	slideExpr: elem,
	
	pagerAnchorBuilder: function(){
		return '<span class="slider_link"></span>'; 
		
	}

    })

};



function bindForm(form){

	$('.input:first').focus();	

	

    $(form).ketchup({

      validationAttribute: 'rel',

      errorContainer: $('.error_div'),

    

      initialPositionContainer: function(errorContainer, field) {

        //errorContainer = the error-container with all childs

        //field = the field that needs to get validated

      },

      positionContainer: function(errorContainer, field) {},

      showContainer: function(errorContainer) {

        errorContainer.fadeIn('fast');

      },

      hideContainer: function(errorContainer) {

        errorContainer.fadeOut('fast');

      }

    });



			$(form).ajaxForm({ 

				//target: '#form_response',

				url: '',

				beforeSerialize: function() {

					 $('body').append('<div id="contact_loading"><img src="workspace/images/loader.gif" width="48" height="48"/><br/>Enviando aguarde!!!</div>');
					 
					 var iWidth = $(window).width();
					 var thisWidth = $('#contact_loading').width();
					 var loaderCenter = iWidth/2-100;
					 
					 $('#contact_loading').css({
						  'position': 'absolute',
						  'top': '35%',
                          'left': loaderCenter + 'px',
						  'background': '#2b1503',
						  'border': '2px solid #fff',
						  'padding': '10px',
						  'text-align': 'center',
						  'font-size': '18px',
						  'color': '#fff',
						  'z-index': '1000'
						  }).fadeIn(100);
						  
						   $("input[type = 'submit']").click(function() {
                             return false;
                           });
				      //$('input[type = submit]').attr('disable', true);
                     
                },

				success: function() {  
                            
						   var iWidth = $(window).width();
						   var loaderCenter = iWidth/2-210;

						  $('#contact_loading').html('<p>Obrigado, sua menssagem foi enviada com sucesso<br/>Entraremos em contato o mais rápido possível</p>').css({
							  'left': loaderCenter 
						  }).delay(6000).fadeOut('fast', function(){
							  
							   window.location.hash = '#o-edificio';
							   $('#menu a').removeClass('active');
		                       $('#m_o-edificio').addClass('active');
							   $(this).remove();
						  });
						  //$('input[type = submit]').attr('disable', false);
				},
				

			});

			

			 

};
