/************************************************
 *
 *  File     :  assets/js/main.js
 *  Version  :  v2
 *  Author   :  Andrej Simeonov, Adam Leon, Transmitter Studios 2007-2009
 *
 ************************************************/

$(document).ready(function(){

// Image hover opacity

$(document).ready(function(){
	$("li.social1 img, li.social2 img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 100% when the page loads

	$("li.social1 img, li.social2 img").hover(function(){
		$(this).fadeTo("fast", 1.0); // This should set the opacity to 40% on hover
	},function(){
   		$(this).fadeTo("slow", 0.6); // This should set the opacity back to 100% on mouseout
	});
});

// Set external links - XHTML Compatibility reasons

	$('a[rel="external"]').attr('target','_blank');

// Form validation 
    $(".required").after('<strong class="astrix">*</strong>');

    $("form").submit(function(e) {

        var trigger = "0";
        var emailre = /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
        $(".validation").remove();
        $("input").blur();
        $("#message").html("");
        
        $(".required",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");
            
            if($(this).attr("value") == '') {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Required Field</div>');
                trigger = "1";
            }
            else {
                $(listItem).removeClass("invalid");
            }
        });

        $(".required" + ".email",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");

            if((!$(this).attr("value").match(emailre)) && ($(this).attr("value") != '')) {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Invalid Email</div>');
                trigger = "1";
            }
        });        
        
        if(trigger == "1") {
            $(".validation").fadeIn();
            e.preventDefault();
        }
        else {
            e.preventDefault();
            var inputs = [];
          
            $(':input',this).each(function() {
                inputs.push(this.name + '=' + escape(this.value));
            })
          
            jQuery.ajax({
                data: inputs.join('&'),
                url: this.action,
                timeout: 2000,
                error: function() {
                    console.log("Failed to submit");
                },
            success: function(r) { 
              $('#message').append(r).fadeIn();
              $('form').get(0).reset();
            }
          })        
        
        }

        $(".invalid > .required:first").focus();
    });	

// CLEAR FORM

      $.fn.clearForm = function() {
        return this.each(function() {
          var type = this.type, tag = this.tagName.toLowerCase();
          if (tag == 'form')
            return $(':input',this).clearForm();
          if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
          else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
          else if (tag == 'select')
            this.selectedIndex = -1;
        });
      };
  
 

  
 
        });