/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
    //Adjust content padding on-load
    content_padding();
    //Padding Adjuster on window resize
    $(window).resize(function() {
        content_padding();
    });

    //Handle #keep_updated Email Checker
    $('#input_2_4').keyup(function(e) {
        var current_value = e.currentTarget.value; //Get current value from input
        //console.log (e.currentTarget.value);
        if(verify_email(current_value) != -1){ $('#gform_2 .button').show(); } //If it's and email, show input.submit
        else { $('#gform_2 .button').hide(); } //Else, hide it
    });

    //Sort Tables (if applicable)
    if(typeof jQuery().tablesorter == 'function'){
        $("table.investment_table").tablesorter();
    }

    //Clear form inputs on click
		$('.medium').each(function() {
		    var default_value = this.value;
		    $(this).focus(function() {
		        if(this.value == default_value) {
		            this.value = '';
		        }
		    });
		    $(this).blur(function() {
		        if(this.value == '') {
		            this.value = default_value;
		        }
		    });
		});
});

function content_padding(){
    var window_height = $(window).height(); //Get window height
    var content_padding = Math.floor((window_height-330)/2); //Calc new padding height
    //console.log( 'window_height = ' + window_height + ' | content_padding = ' + content_padding );
    if(content_padding < 0){ content_padding = 0; }
    if($.browser.msie && $.browser.version=="6.0"){
	  } else {
	    $('#container div.content:not(.no-padding)').css('padding-bottom', content_padding); //Set padding height
	    $('#container div.content:not(.no-padding)').css('padding-top', content_padding); //Set padding height
		}
}

function verify_email(string){
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    return string.search(emailRegEx); //Search the string for email regex
}

// Scrolling in iPad and iPhone
var myScroll;

function loaded() {
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	if (isiPad == true) {
		if ($('#container div.no-padding').length == 0){
		  myScroll = new iScroll('container');
	  }
	}
}

if (document.addEventListener){
	document.addEventListener('touchmove', function(e){ e.preventDefault(); }, false);
	document.addEventListener('DOMContentLoaded', loaded, false);
} else if (document.attachEvent){
	document.attachEvent('touchmove', function(e){ e.preventDefault(); }, false);
	document.attachEvent('DOMContentLoaded', loaded, false);
}
