(function($){


    $.newsletter = function (theForm) {
        return $.newsletter.impl.init(theForm);
    };
    
	$.newsletter.impl = {
	
	   init: function(theForm){
	       var tForm = theForm;
	       var errorContainer = tForm.find(".error_messages");
           var loaderContainer = tForm.find(".ajax-loader").hide().append('<img src="/images/layout/ajax-loader.gif" alt="Loading..." />');
           var formID = theForm.attr("id");
	       theForm.submit(function(){
                var valid = true;
                var errors = $("<ul></ul>");
                tForm.find(".error").removeClass("error");
                errorContainer.empty();
                if(formID=="newsletter_signup"){
                    if($("#nl_name").val() == ""){
                    	errors.append("<li>Enter your <strong>Name</strong> and try again.</li>");
                    	$("#name_label").addClass("error");
                    	valid = false;
                    }
                    if($("#nl_email").val() == ""){
                    	errors.append("<li>Enter your <strong>Email</strong> and try again.</li>");
                    	$("#email_label").addClass("error");
                    	valid = false;
                    }
                    //validate email
                    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                    if(valid && reg.test($("#nl_email").val()) == false){
                        errors.append("<li>Please enter a <strong>valid Email</strong>.</li>");
                    	$("#email_label").addClass("error");
                    	valid = false;
                    }
                }
                else{
                    if($("#nlu_email").val() == ""){
                    	errors.append("<li>Enter your <strong>Email</strong> and try again.</li>");
                    	$("#nlu_email_label").addClass("error");
                    	valid = false;
                    }
                    //validate email
                    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                    if(valid && reg.test($("#nlu_email").val()) == false){
                        errors.append("<li>Please enter a <strong>valid Email</strong>.</li>");
                    	$("#nlu_email_label").addClass("error");
                    	valid = false;
                    }
                }
                if(!valid){
                    errorContainer.append("<strong>There were errors with your submission:</strong>").append(errors);
                
                }
                else{
                    loaderContainer.show();
                    $.post(tForm.attr("action"),tForm.serialize(),function(html){
                        loaderContainer.hide();
                        
                        var formContent = formID=="newsletter_signup"?$("#form_content"):$("#unsubscribe_content");
                        
                        formContent.children().hide();
                        var thankYou = $('<div class="thankyou"></div>').append($(html)).appendTo(formContent);
                        thankYou.find("a").click(function(){
                            thankYou.remove();
                            $("#form_content > *").show();
                            return false;
                        });
                    },"html");
                }
	            return false;
	       });
	   }
	};


    $(function(){
        
        $("#newsletter_signup, #unsubscribe_form").each(function(){
            $.newsletter($(this));
        });
        
        $("#tebmenu").tabs();
        
        var origSearchVal = $("#searchq").val();
        $("#searchq")
        .focus(function(){
            if($(this).val() == origSearchVal){
                $(this).val("");
            }
        })
        .blur(function(){
            if($(this).val()==""){
                $(this).val(origSearchVal);
            }
        });
        
        $("#greenGraphContainer dt a").click(function(){
            var cur = $("#greenGraphContainer dd:visible").css({zIndex:"2",display:"block"});
            $("#greenGraphContainer .current").removeClass("current");
            
            $($(this).attr("href")).css("zIndex","1").show();
            
            cur.fadeOut("normal");
            $(this).parents("dt").addClass("current");
            
            
            return false;
        });
        
        $("#environmentalSavings .nav a").click(function(){
            
            if(!$(this).hasClass("inactive")){
                var current = $("#environmentalSavings li.current");
                current.removeClass("current");
                if($(this).hasClass("prev")){
                    current.prev().addClass("current");
                    if(current.prev().is(":first-child")){
                        $(this).addClass("inactive");
                    }
                    $("#environmentalSavings .nav a.next").removeClass("inactive");
                }
                else{
                    current.next().addClass("current");
                    if(current.next().is(":last-child")){
                        $(this).addClass("inactive");
                    }
                    $("#environmentalSavings .nav a.prev").removeClass("inactive");
                }
            }
            return false;
        });
    
    
    });


})(jQuery);

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=350');");
}
