(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(function() {
    var name = $("#name"),
    	comment = $("#comment"),
    	allFields = $([]).add(name).add(comment),
    	tips = $("#validateTips");

    function updateTips(t) {
    	tips.text(t).effect("highlight",{},1500);
    }

    function checkLength(o,n,min,max) {

    	if ( o.val().length > max || o.val().length < min ) {
    		o.addClass('ui-state-error');
    		updateTips("Please specify your " + n + ", at least "+min+" characters long.");
    		return false;
    	} else {
    		return true;
    	}

    }

    function checkRegexp(o,regexp,n) {

    	if ( !( regexp.test( o.val() ) ) ) {
    		o.addClass('ui-state-error');
    		updateTips(n);
    		return false;
    	} else {
    		return true;
    	}

    }

    function SubmitContactUsForm() 
    {
      var bValid = true;
      allFields.removeClass('ui-state-error');

      bValid = bValid && checkLength(name,"Name",3,100);
      bValid = bValid && checkLength(comment,"Comment",10,4000);

      //bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
      if(bValid) {
          $.ajax({
            type: 'POST',
            url: '/mailer.php',
            data: {
                  name: $("#name").val(),
                  phone: $("#phone").val(),
                  email: $("#email").val(),
                  comment: $("#comment").val()
            },
            success: function(msg) {
                pageTracker._trackEvent('Contact Form', 'Submit');
            },
            complete: function(msg) {
                   //alert(msg);
            },
            error: function(msg) {
                   //alert(msg);
            } 
          })
          ShowQuickMessage('Thank you for your interest in cliKit! Someone will get in touch with you shortly.');
          $('#dialog').dialog('close');
      }
    }

    $("#dialog").dialog({
    	bgiframe: true,
    	autoOpen: false,
        resizable: false,
    	width: 550,
    	height: 520,
    	modal: true,
    	buttons: {
    		'Send': function() {
                  SubmitContactUsForm();
    		}
    	},
    	close: function() {
    		allFields.val('').removeClass('ui-state-error');
    	}
    });

    $('#contact-us').click(function() {
    	$('#dialog').dialog('open');
    })

    $('#fcontact').submit(function() {
        SubmitContactUsForm();
        return false;
    })
});

var ImgStepHeight = 109;

function divStep4_OnMouseOver()
{
  $('#btn-menu-3').attr('src', 'img/btn-menu-4-over-left-empty.jpg');
  $('#btn-menu-4').attr('src', 'img/btn-menu-4-over-empty.jpg');
}

function divStep4_OnMouseOut()
{
  $('#btn-menu-3').attr('src', 'img/btn-menu-3-empty.jpg');
  $('#btn-menu-4').attr('src', 'img/btn-menu-4-empty.jpg');
}

function liStep_OnHover(Name, Inner)
{
  h = Math.round($('#div'+Name).height())-ImgStepHeight;
  $('#spacer'+Name).height(h);
  if(Inner)
    $('#div'+Name).css('top', -h-131);
}

//--
function liStep1_OnHover()
{
  liStep_OnHover('Step1', 1);
}

function liStep2_OnHover()
{
  liStep_OnHover('Step2', 1);
}

function liStep3_OnHover()
{
  liStep_OnHover('Step3', 1);
}

function liStep4_OnHover()
{
  liStep_OnHover('Step4', 1);
}

function liStep5_OnHover()
{
  liStep_OnHover('Step5', 1);
}

//--
function index_liStep1_OnHover()
{
  liStep_OnHover('Step1', 0);
}

function index_liStep2_OnHover()
{
  liStep_OnHover('Step2', 0);
}

function index_liStep3_OnHover()
{
  liStep_OnHover('Step3', 0);
}

function index_liStep4_OnHover()
{
  liStep_OnHover('Step4', 0);
}

function index_liStep5_OnHover()
{
  liStep_OnHover('Step5', 0);
}

//--
$(document).ready(function() {
  var sfEls = document.getElementById("nav").getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++) {
  	sfEls[i].onmouseover=function() {
  		this.className+=" sfhover";
  	}
  	sfEls[i].onmouseout=function() {
  		this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
  	}
  }
});

