/************************************************
 *
 *  File     :  main.js
 *  Version  :  v1.0
 *  Website  :  SilverBirch Mobile
 *  Author   :  kevinbirch.com (c) 2010
 *
 ************************************************/

// Import other required JS files(s)
document.write('<script type="text/javascript" src="/assets/js/preload.js"></script>');

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

    // Studios Logo onclick creates email
	$("#division").click(function () {
	     window.location = "mailto:info@silverbirchventures.com";
	});
	
    // Coming soon alert for in progress features
    $(".soon").click(function(e) {
        e.preventDefault();
        alert( 'Curently under maintenance.' );
    });

    // Twitter links in new window
    $('#twitter_update_list a').click(function(){
        window.open(this.href);
        return false;
    });
    
    $("#contactForm").submit(function(e) {

        var trigger = false;
        var namere = /^[a-z -']+$/i;
        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( trim( $(this).attr("value") ) == '') {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Required Field</div>');
                trigger = true;
            }
            else {
                $(listItem).removeClass("invalid");
            }
        });

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

            if((!trim( $(this).attr("value") ).match(namere)) && (trim( $(this).attr("value") ) != '')) {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Invalid Name</div>');
                trigger = true;
            }
        });       

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

            if((!$(this).attr("value").match(emailre)) && (trim( $(this).attr("value") ) != '')) {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Invalid Email</div>');
                trigger = true;
            }
        });        
        
        if(trigger == true) {
            $(".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();
            		$('#contactForm').get(0).reset();
                },
                success: function(r) { 
                	$('#message').append( "Thank you, your message has been sent!" ).fadeIn();
                	$('#contactForm').get(0).reset();
                }
          });        
        }

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

});


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

