jQuery.fn.textImage = function(){
    return this.each(function() {
        if(!$(this).hasClass('text-image-ignore')) {
            $(this).hide();
            var text = $(this).text();
            var color = $(this).css('color');
            color = color.replace('#','');
            var size = $(this).css('font-size');
            size = size.substring(0,size.length-2);
            var width = $(this).css('width');
            width = width.substring(0,width.length-2);
            
            $(this).after('<img class="text-image-' + this.tagName + '" alt="' + text + '" src="/handlers/TextImage.ashx?text=' + text + '&color=' + color + '&size=' + size + '&width=' + width + '" />');
            
            // optionally rewrap with link
            var link = $(this).children('a').attr('href');
            if(link != undefined && link != null && link != '') {
                $(this).next().wrap('<a href="' + link + '"></a>');
            }
        }
    });
};

$(document).ready(function() {
	
    $('h1').textImage();
    $('h2').textImage();
    $('h3').textImage();
	
     $('#SearchInput').keypress(function(e)
        {
            if ((e.keyCode || e.which) == 13)
            {
                window.location = '/search.aspx?q=' + $(this).val();
                return false;
            }
            return true;
        });

    $("#SearchInput").focus(function() { $(this).val(""); });
    $("#SearchInput").blur(function() { 
        if($(this).val() == "") $(this).val("Search...");
    });
    
    $("a[href*='openpanel.aspx']").click(function() {
        $.scrollTo(0,500, {
            onAfter:function() {
                $("#tabContact").click();
            }
        });
        return false;
    });
    
     $("a[href*='contactform.aspx']").click(function() {
        $.scrollTo(0,500, {
            onAfter:function() {
                $("#tabContact").click();
                $("#project_form :input")
                    .effect('highlight',{color:'#f69f1a'},500)
                    .effect('highlight',{color:'#f69f1a'},500)
                    .effect('highlight',{color:'#f69f1a'},500)
                    .effect('highlight',{color:'#f69f1a'},500);
            }
        });
        return false;
    });
    
    
    $("#slideContainer").load(
        "/snippets/contact_panel.html",
        "",
        function() {
            
            $("#project_form_name").focus(function() {
                if($(this).val().toLowerCase() == 'name') $(this).val('');
            });
            
            $("#project_form_name").blur(function() {
                if($(this).val() == '') $(this).val('Name');
            });
            
            $("#project_form_org").focus(function() {
                if($(this).val().toLowerCase() == 'organization') $(this).val('');
            });
            
            $("#project_form_org").blur(function() {
                if($(this).val() == '') $(this).val('Organization');
            });
            
            $("#project_form_email").focus(function() {
                if($(this).val().toLowerCase() == 'email@your.org') $(this).val('');
            });
            
            $("#project_form_email").blur(function() {
                if($(this).val() == '') $(this).val('email@your.org');
            });
            
            $("#project_form_phone").focus(function() {
                if($(this).val().toLowerCase() == 'phone') $(this).val('');
            });
            
            $("#project_form_phone").blur(function() {
                if($(this).val() == '') $(this).val('Phone');
            });
            
            $("#project_form_state").focus(function() {
                if($(this).val().toLowerCase() == 'state') $(this).val('');
            });
            
            $("#project_form_state").blur(function() {
                if($(this).val() == '') $(this).val('State');
            });
            
            $("#project_form_details").focus(function() {
                if($(this).val().toLowerCase() == 'amazing ideas!') $(this).val('');
            });
            
            $("#project_form_details").blur(function() {
                if($(this).val() == '') $(this).val('Amazing Ideas!');
            });
            
            
            $("#tabContact").bind("click",function(event) {
	            event.preventDefault();
	            $("#paneContact").slideToggle(500,function() {
	                if($("#tabContact").attr('src') == '/images/tab_close_window.png') {
	                    $("#tabContact").attr('src','/images/tab_contact.png');
	                }
	                else {
	                    $("#tabContact").attr('src','/images/tab_close_window.png');
	                }
	            });
	            
            });

            $("#project_form_button").RecaptchaFacebox(
                {
                    url: '/Handlers/Recaptcha.ashx',
                    successCallback: RecaptchaProjectFormSuccessCallback
                }
            );
            
        }
    );
    
    $("#burst")
        .animate({top:"0px", left:"0px"},1000)
        .animate({top:"0px", left:"0px"},5000)
        .animate({top:"-150px", left:"-150px"},500);
    
});

function RecaptchaProjectFormSuccessCallback() {
    
    projectName = $("#project_form_name").val();
    projectOrg = $("#project_form_org").val();
    projectEmail = $("#project_form_email").val();
    projectPhone = $("#project_form_phone").val();
    projectState = $("#project_form_state").val();
    projectBusinessType = $("#project_form_business_type").val();
    projectDetails = $("#project_form_details").val();
    
    $.ajax({
        url: "/Handlers/PostProjectForm.ashx", 
        data: { 
            name: projectName,
            org: projectOrg,
            email: projectEmail,
            phone: projectPhone,
            state: projectState,
            businessType: projectBusinessType,
            details: projectDetails
        },
        success: function(result) {
            $("#project_form").hide();
            $("#project_form_result").show();
            if(result.toLowerCase() == "false") {
                $("#project_form_result").html("<p>An error occurred when processing your information. Our support team has been notified of this error. Please refresh this page and try again.</p>");
            }
        }
    });
    
}