// Place your application-specific JavaScript functions and classes here

/* hack for to jQuery AJAX play nice with Rails */
// Always send the authenticity_token with ajax
$(document).ajaxSend(function(event, request, settings) {
  if ( settings.type == 'POST' ) {
    // settings.data = (settings.data ? settings.data + "&" : "")
    //         + "authenticity_token=" + encodeURIComponent( AUTH_TOKEN );
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  }
});

// When I say html I really mean script for rails
$.ajaxSettings.accepts.html = $.ajaxSettings.accepts.script;

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

function init_carrier_selects(){
	$("#carrier").sSelect({ddMaxHeight: '250px'});
	$(".select-carrier-country #carrier-country").sSelect({ddMaxHeight: '250px'}).change(function() {
		$(".select-carrier-country #carrier").load_select("/carriers", {country : this.value}, function(){select.resetSS();});
	});
}

// document ready handlers
$(function() {

  // homepage
	$(".get-started #phone-make").sSelect({ddMaxHeight: '250px'}).change(function() {
	  $(".get-started #phone-model").load_select("/makes/" + this.value + "/models", {}, function(){select.resetSS();});
	});

  // models/show
  init_carrier_selects();

	$('#carrier-container #carrier-submit').live("click", function(){
		if($('#carrier-country').val() == '0') {
			$('#notification-container').alertMessage({
				alertMessage: 'Please select country and carrier'
			});
			return false;
		} else {
			$('#carrier-container').fadeOut(800, function() {
				$('#carrier-container').load("/models/" + $("#model_id").val() + "/price", { carrier : $("#carrier").val() },
				  function(){
					init_carrier_selects();
					$('#carrier-container').fadeIn(800);
				  });
			});
		}
	});

  $('.unlocking-price #change-carrier').live("click", function(){
		$('#carrier-container').fadeOut(800, function() {
			$('#carrier-container').load("/models/" + $("#model_id").val() + "/carrier", { carrier : $("#carrier").val() },
			  function(){ 
				init_carrier_selects();
				$('#carrier-container').fadeIn(800); 
			});
		});
  });

	$('.carrier-block #carrier-submit').live("click", function(){
		if($('#carrier-country').val() == '0') {
			$('#notification-container').alertMessage({
				alertMessage: 'Please select country and carrier'
			});
			return false;
		} else {
			$('.carrier-block').fadeOut(800, function() {
        $.ajax({
          type: "POST",
          url: "/models/" + $("#model_id").val() + "/carrier_service",
          data: { carrier : $("#carrier").val() }
        });
			});
		}
	});

  $('.unlocking-price #change-carrier').live("click", function(){
    $('.unlocking-price').fadeOut(800, function() {$('.carrier-block').fadeIn(800)});
	});

  // Contact-us form validation
  if($('#form_contact_us').size()) {
    $('#form_contact_us').submit(function() {
      $('.requiredtext', this).removeClass('requiredtext')
      var is_valid = true
      if(!$('#full_name').val()) {
        $('label[for=full_name]').addClass('requiredtext'); is_valid = false
      }
      if(!$('#email').val() || !/.+@.+\.[a-zA-Z]{2,4}$/.test($('#email').val())) {
        $('label[for=email]').addClass('requiredtext'); is_valid = false
      }
      if(!$('#message').val()) {
        $('label[for=message]').addClass('requiredtext'); is_valid = false
      }
      if(!is_valid) { $('.errormsg', this).show(); return false }
    })
    $('#reset').click(function() {
      $('.errormsg').hide(); $('.requiredtext').removeClass('requiredtext')
    })
  }

  // In field label
  $(':input[title]').each(function() {
    var $this = $(this)
    if(!$this.hasClass('in_field_label')) { return false }
    if($this.val() === '') { $this.val($this.attr('title')) }
    $this.focus(function() {
      if($this.val() === $this.attr('title')) { $this.val('') }
    }).blur(function() {
      if($this.val() === '') { $this.val($this.attr('title')) }
    })
  })

  // Hide flash notifications on click
  $('#notification-container .close').bind('click', function() { $('#notification-container').text('').fadeOut('slow') })
})

