$(document).ready(function(){

	//------------------------------------------------------------------//
	// Activity Indicator
	//------------------------------------------------------------------//
	$.fn.activity.defaults = {segments: 10, width:1, space: 0, length: 5, color: '#FFF', speed: 1}

	//------------------------------------------------------------------//
	// Target _Blank XHTML
	//------------------------------------------------------------------//
	$('a._blank').click( function() {
		window.open(this.href);
		return false;
	});
	//------------------------------------------------------------------//
	// Form Validation
	//------------------------------------------------------------------//
	// Validate
	$('form.validate').validate();
	
	//------------------------------------------------------------------//
	// Form Contact Ajax Submit
	//------------------------------------------------------------------//
	$('form.ajax').submit(function(){
		var form = $(this);
		var contact_button = $('#contactsend');
		var div_alert = $('<div class="contact_alert"><span></span></div>');
		var loader = $('<span class="contact_loader"></span>');
		contact_button.after(loader);
		contact_button.after(div_alert);
		div_alert.hide();
		if(form.valid()){
			var dataString = form.serialize();
			var timeOut = 4;
			loader.activity({
				width:2, length: 6, color: '#FFF'
			});
			
				
			$.ajax({
				type: "POST",
				url: PATH+"contact/send",
				data: dataString,
				dataType: 'json',
				success: function(data) {
					loader.activity(false);
					if(data.type!='success'){
						div_alert.addClass('error');
					}else{
						$('#email').val("");
						$('#message').val("");
						$('#phone').val("");
						$('#name').val("");
					}
					div_alert.find('span').html(data.message)
					div_alert.fadeIn('slow',function(){
						setTimeout(function() {
							div_alert.remove();
						}, timeOut * 1000);
					})
					return false;
				},
				error:function(xhr, ajaxOptions, thrownError){
					loader.activity(false);
					alert(thrownError);
				}
			});
		}
		return false;
	})
	
	

	//------------------------------------------------------------------//
	// Font embedding
	//------------------------------------------------------------------//
	Cufon.replace('blockquote.top',{
		textShadow: '0 1px 1px #000000'
	});
	Cufon.replace('.startproyect-quote p.intro,.start_proyect_message',{
		textShadow: '-1px -1px #0000000'
	});
	
	
	Cufon.replace('h2,h3,p.proyect_name,.us-quote p.intro,.us-quote p.intro2,.team_review h4,.services-text span.text,.servicesli h4');
	
	//------------------------------------------------------------------//
	// Cross Browser Functions
	//------------------------------------------------------------------//
	// add hover class to buttons
	$('.main-menu a.icon,.site_share a,.button').hover(addHover,removeHover);
	
	//add focus class to inputs
	$('input,textarea').focus(addFocus);
	$('input,textarea').blur(removeFocus);
	
	//------------------------------------------------------------------//
	// Anchor in contact link
	//------------------------------------------------------------------//
	$('#menu-contacto,#menu-footer-contacto').click(function(event){
		
	});
	
	
	
	
	
});


$(document).ready(function() {
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target ) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target;
            $('#email').focus();
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }

});

function addHover(e){
	$(e.currentTarget).addClass("hover");
}

function removeHover(e){
	$(e.currentTarget).removeClass("hover");

}

function addFocus(e){
		$(e.currentTarget).addClass("focus");
}

function removeFocus(e){
	$(e.currentTarget).removeClass("focus");

}
