/************************************************
* 
*  File     :  frontend/assets/js/main.js 
*  Version  :  1.0 
*  Author   :  Transmitter Studios 2009
* 
************************************************/

$(document).ready(function(){

// Set external links - XHTML Compatibility reasons
    $("a[rel='external']").attr("target","_blank");

// Logo onclick return home
    $("h1").click(function () {
        window.location = "/";
    });

// disable right click on images
$("#gallery img").bind("contextmenu",function(e){
	return false;
});


// Tooltip init
    //$("#thumbs > li > a").tooltip();
 
// Form validation 
    $(".required").after('<strong class="astrix">*</strong>');

    $("#contact-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() {
              $('#message').append( "There was a problem sending your message, please try again." ).fadeIn();
              $('#contact-form').get(0).reset();
                },
            success: function(r) { 
              $('#message').append( "Thank You. Your information has been sent." ).fadeIn();
              $('#contact-form').get(0).reset();
            }
          })        
        
        }

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

// gallery opacity

$(function () {
  $(".opacify").fadeTo(1, 0.5);
  $(".opacify").hover(
    function () {
      $(this).fadeTo("fast", 1);
    },
    function () {
      $(this).fadeTo("normal", 0.5);
    }
  );
  
   // "show/hide" functionality 
   
   jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

};

  
  $('div#listForm').hide();
  
  $('a.full-toggle').click(function() {
    $('div#listForm').fadeToggle(400);
    return false;
  });
});

$.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;
  });
};

