// Begin Document Ready
$(document).ready(function(){	

//Feature slider
	$.featureList(
		$("#tabs li a"),
		$("#output li div"), {
			start_item:0
		}
	);


//MENU	
	$('.mainMenu ul li').mouseover(function(){
	    $(this).children('.submenu').css('display', 'block');
	});
	$('.mainMenu ul li, .submenu').mouseleave(function(){
	    $('.submenu').css('display', 'none');
	});


// ON FOCUS
    $("input[type='text']").addClass("idleField");
	$("input[type='text']").focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
	    if (this.value == this.defaultValue){ 
	    	this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});

	$("input[type='text']").blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
	    if ($.trim(this.value) == ''){
	    	this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	
    // Select Box
    $("select").selectBox({
        // animDuration: 1000,
        // fixedWidth: true
      });
	
	
	
    $(".tweet").tweet({
            username: "resnetsmarthome",
            join_text: "auto",
            avatar_size: null,
            count: 1,
            auto_join_text_default: "", 
            auto_join_text_ed: "",
            auto_join_text_ing: "",
            auto_join_text_reply: "",
            auto_join_text_url: "",
            loading_text: "loading tweets..."
    });
    
	
	
}); // End Document Ready


// HEADER ANIMATION
$(window).load(function() {
	//Animation timeline

	if ($.browser.msie) {
	    
        // QUICK LEFT TO RIGHT
        $(".balloon1:hidden:first").delay(1500).fadeIn(0).delay(500).fadeOut(0);
        $(".balloon2:hidden:first").delay(2500).fadeIn(0).delay(500).fadeOut(0);
        $(".balloon3:hidden:first").delay(3500).fadeIn(0).delay(500).fadeOut(0);
        $(".balloon4:hidden:first").delay(4500).fadeIn(0).delay(500).fadeOut(0);

        // QUICK LEFT TO RIGHT
        $(".balloon3:hidden:first").delay(1500).fadeIn(0).delay(500).fadeOut(0);
        $(".balloon2:hidden:first").delay(3500).fadeIn(0).delay(500).fadeOut(0);
        $(".balloon1:hidden:first").delay(5500).fadeIn(0).delay(2000).fadeOut(0);
        
        // SLOW MOTION
        $(".balloon2:hidden:first").delay(3500).fadeIn(0).delay(2000).fadeOut(0);
        $(".balloon3:hidden:first").delay(8500).fadeIn(0).delay(2000).fadeOut(0);
        $(".balloon4:hidden:first").delay(13500).fadeIn(0).delay(2000).fadeOut(0);
        $(".balloon1:hidden:first").delay(12500).fadeIn(0);

	}
	else {

        // QUICK LEFT TO RIGHT
        $(".balloon1:hidden:first").delay(1500).fadeIn(500).delay(200).fadeOut(500);
        $(".balloon2:hidden:first").delay(2500).fadeIn(500).delay(200).fadeOut(500);
        $(".balloon3:hidden:first").delay(3500).fadeIn(500).delay(200).fadeOut(500);
        $(".balloon4:hidden:first").delay(4500).fadeIn(500).delay(200).fadeOut(500);

        // QUICK LEFT TO RIGHT
        $(".balloon3:hidden:first").delay(800).fadeIn(500).delay(200).fadeOut(500);
        $(".balloon2:hidden:first").delay(2800).fadeIn(500).delay(200).fadeOut(500);
        $(".balloon1:hidden:first").delay(4800).fadeIn(500).delay(2000).fadeOut(1000);
        
        // SLOW MOTION
        $(".balloon2:hidden:first").delay(3500).fadeIn(1000).delay(2000).fadeOut(1000);
        $(".balloon3:hidden:first").delay(8500).fadeIn(1000).delay(2000).fadeOut(1000);
        $(".balloon4:hidden:first").delay(13500).fadeIn(1000).delay(2000).fadeOut(1000);
        $(".balloon1:hidden:first").delay(12500).fadeIn(1000);

	}

    // On hover function
    $(".button1").hover(function() {
    		stopAnimation();
    		fadeInDiv('.balloon1');
    	});
    	
    $(".button2").hover(function() {
    		stopAnimation();
    		fadeInDiv('.balloon2');
    	});
        	
    $(".button3").hover(function() {
    		stopAnimation();
    		fadeInDiv('.balloon3');
    	});
            	
    $(".button4").hover(function() {
    		stopAnimation();
    		fadeInDiv('.balloon4');
    	});
    	
 });

//Clear all objects from the animation queue
function stopAnimation() {
	$(".balloon1").clearQueue();
	$(".balloon2").clearQueue();
	$(".balloon3").clearQueue();
	$(".balloon4").clearQueue();
}

function fadeInDiv(divID) {
	stopAnimation() ;

	//Hide all objects before showing
	$(".animatedBalloon").hide();

	if ($.browser.msie) {
		$(divID+":hidden:first").fadeIn(0);
	}
	else
	$(divID+":hidden:first").fadeIn(0);

	//Show all objects before showing
	$('.q').show();
}

function fadeOutDiv(divID) {
	stopAnimation() ;

	if ($.browser.msie) {
		$(divID).fadeOut(0);
	}
	else
	$(divID).fadeOut(0);
}

function showJgrowlDialog(err_msg, scroll_to_bottom, custom_class, header){ 

		
		
    	$.jGrowl(err_msg, { 
			sticky: true,
			life: 10000,
			theme: custom_class, //set custom theme to place the alert accordingly
			position: 'center',
			header: header,
			open: function(e,m) {
				$('#jgrowl_overlay').show();
			},
			close: function(e,m) {
				$('#jgrowl_overlay').hide();
			}
		});
    	if(scroll_to_bottom){
    		$("html,body").animate({ scrollTop: $('body').css('height') }, 0);
    	}
}

function processNewsletterSignup(){
	var hidden_field = $('#robot_checker').val();
	var is_robot = false;
	
	$(".error").remove();
	
	if(hidden_field!=''){
		is_robot = true;
		console.log('you are a robot! signup will not continue!');
		return false;
	}	
	
	var email_ad = $('#email_signup').val();
	var is_valid = false;
	//alert(email_ad);
	
	is_valid = validate_email(email_ad);
	//alert(is_valid);
	if(!is_valid){
		//showJgrowlDialog('Please input a valid email address', true, 'newsletter-alert', 'Newsletter Signup');
		$(".inputHolder").after('<span class="error" style="color:red; font-size:11px;">Enter a valid email address.</span>');
	}else{
		//call php to send the email for the signup alert
		console.log('yes');
		$.ajax({
		  url: '/home5/newsletterSignup?email_ad='+email_ad,
		  success: function(data) {
			//showJgrowlDialog('Thanks for signing up to the RESNET Smart Home Newsletter. Starting in January 2012, we\'ll keep you on top of all that\'s happening in home energy!', true, 'newsletter-alert', 'Newsletter Signup');
			$(".inputHolder").after('<span class="error" style="color:red; font-size:11px;">Thanks for signing up to our newsletter!</span>');
			console.log('email submitted');
			$('#email_signup').val('YOUR EMAIL ADDRESS'); //reset the email input box
		  }
		});
	}
	
}

function validate_email(email)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   
   if(reg.test(email) == false)
       return false;
   
   return true;
}
