/*CSS Browser Selector v0.3.4 (Sep 29, 2009)Rafael Lima (http://rafael.adm.br)http://rafael.adm.br/css_browser_selectorLicense: 
http://creativecommons.org/licenses/by/2.5/Contributors: http://rafael.adm.br/css_browser_selector#contributors*/
function css_browser_selector(u) { var ua = u.toLowerCase(), is = function(t) { return ua.indexOf(t) > -1; }, g = 'gecko', w = 'webkit', s = 'safari', o = 'opera', h = document.getElementsByTagName('html')[0], b = [(!(/opera|webtv/i.test(ua)) && /msie\s(\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('firefox/2') ? g + ' ff2' : is('firefox/3.5') ? g + ' ff3 ff3_5' : is('firefox/3') ? g + ' ff3' : is('gecko/') ? g : is('opera') ? o + (/version\/(\d+)/.test(ua) ? ' ' + o + RegExp.$1 : (/opera(\s|\/)(\d+)/.test(ua) ? ' ' + o + RegExp.$2 : '')) : is('konqueror') ? 'konqueror' : is('chrome') ? w + ' chrome' : is('iron') ? w + ' iron' : is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/.test(ua) ? ' ' + s + RegExp.$1 : '') : is('mozilla/') ? g : '', is('j2me') ? 'mobile' : is('iphone') ? 'iphone' : is('ipod') ? 'ipod' : is('mac') ? 'mac' : is('darwin') ? 'mac' : is('webtv') ? 'webtv' : is('win') ? 'win' : is('freebsd') ? 'freebsd' : (is('x11') || is('linux')) ? 'linux' : '', 'js']; c = b.join(' '); h.className += ' ' + c; return c; }; css_browser_selector(navigator.userAgent);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// lightbox for newsletter signup
$(document).ready(function() {
	$('#sidebarTiles a.newsletter').click(function() {
		_buildLightbox();
		
		var windowHeight = $(window).height();
		var lightboxHeight = $('#lightboxContent').height();
		
		$('#lightboxContentWrap').css('top', (windowHeight/2)-(lightboxHeight/2));

		_loadContent('/newslettersignup.aspx');

		return false;
	});
});
function _showNewsletterLightbox() {
    _buildLightbox();

    var windowHeight = $(window).height();
    var lightboxHeight = $('#lightboxContent').height();

    $('#lightboxContentWrap').css('top', (windowHeight / 2) - (lightboxHeight / 2));

    _loadContent('/newslettersignup.aspx');

    return false;
}
// ------------------------------------------------------------------------------------
function _buildLightbox() {
	$('body').append('<div id="lightboxOverlay"></div>');
	$('body').append('<div id="lightboxContentWrap"><div id="lightboxContent"><a class="close" href="#">Close</a><div id="lightboxLoadTarget"></div></div></div>');
	
	$('#lightboxOverlay').click(function() {
		_destroyLightbox()
	});
	$('#lightboxContentWrap').click(function() {
		_destroyLightbox()
	});
};
// ------------------------------------------------------------------------------------
function _destroyLightbox() {
	$('#lightboxOverlay').remove();
	$('#lightboxContentWrap').remove();
};
// ------------------------------------------------------------------------------------
function _loadContent(url) {
    $('#lightboxLoadTarget').html('<iframe src="' + url + '" border="0" frameborder="0" width="440px" height="626px" scrolling="no" />');
};

//Handle newsletter signup prompts being inside the boxes
$(document).ready(function() {
    $('div.newsletterSignup input').each(function() {

        if ($(this).val() == "") {
            $(this).val($(this).parents('label').find('span').text()).toggleClass('defaultText');
        }

        $(this).focus(function() {
            if ($(this).val() == $(this).parents('label').find('span').text()) {
                $(this).val("").toggleClass('defaultText');
            }
        });
        $(this).blur(function() {
            if ($(this).val() == "") {
                $(this).val($(this).parents('label').find('span').text()).toggleClass('defaultText');
            }
        });
    });

    $('div.newsletterSignup a').click(function() {
        $('div.newsletterSignup input').each(function() {
            if ($(this).val() == $(this).parents('label').find('span').text()) {
                $(this).val("")
            }
        });
    });
});



// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Image map hover box for team page
$(document).ready(function() {
	$('#teamMembers div').hide();
	$('#Map area').click(function() {
		var href = $($(this).attr('href')+' a').attr('href');
		var server = window.location.href.substring(0,window.location.href.indexOf('/',8));
		if (href.indexOf('http://') > -1)
			window.location.href = href;
		else
			window.location.href = server+href;
		return false;
	});
	$('#Map area').mouseenter(function() {
		$('#teamMembers div').hide();
		$($(this).attr('href')).show();
	});
	$('#Map area').mouseleave(function() {
		$($(this).attr('href')).hide();
	});
	$('#Map area').mouseleave(function() {
		$($(this).attr('href')).hide();
	});
	$('#Map area').mousemove(function(e) {
		$($(this).attr('href')).css('top',e.pageY+'px').css('left',(e.pageX+20)+'px');
	});
});

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//Handle search prompt being inside search box
$(document).ready(function() {
    $('#siteSearch input').each(function() {

        if ($(this).val() == "") {
            $(this).val($('#siteSearch label').text()).toggleClass('defaultText');
        }

        $(this).focus(function() {
            if ($(this).val() == $('#siteSearch label').text()) {
                $(this).val("").toggleClass('defaultText');
            }
        });
        $(this).blur(function() {
            if ($(this).val() == "") {
                $(this).val($('#siteSearch label').text()).toggleClass('defaultText');
            }
        });
    });

    $('#siteSearch a').click(function() {
        $('.siteSearch input').each(function() {
            if ($(this).val() == $('#siteSearch label').text()) {
                $(this).val("")
            }
        });
    });
});

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//handle blog comment labels being inside form
$(document).ready(function() {
    $('#blogComments input, #blogComments textarea').each(function() {

        if ($(this).val() == "" || $(this).val() == $(this).parent('label').find('span').text()) {
			$(this).val($(this).parent('label').find('span').text()).addClass('defaultText');
        }

        $(this).focus(function() {
            $(this).addClass('focusText');
            if ($(this).val() == $(this).parent('label').find('span').text()) {
                $(this).val("").removeClass('defaultText');
            }
        });
        $(this).blur(function() {
            $(this).removeClass('focusText');
            if ($(this).val() == "") {
                $(this).val($(this).parent('label').find('span').text()).addClass('defaultText');
            }
        });
    });

    $('#blogPostButton a').click(function() {
        $('#blogComments input, #blogComments textarea').each(function() {
            if ($(this).val() == $(this).parent('label').find('span').text()) {
                $(this).val("")
            }
        });
    });
});

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//handle contact form labels being inside form
$(document).ready(function() {
    $('#contactForm input, #contactForm textarea').each(function() {

        if ($(this).val() == "" || $(this).val() == $(this).parent('label').find('span').text()) {
			$(this).val($(this).parent('label').find('span').text()).addClass('defaultText');
        }

        $(this).focus(function() {
            $(this).addClass('focusText');
            if ($(this).val() == $(this).parent('label').find('span').text()) {
                $(this).val("").removeClass('defaultText');
            }
        });
        $(this).blur(function() {
            $(this).removeClass('focusText');
            if ($(this).val() == "") {
                $(this).val($(this).parent('label').find('span').text()).addClass('defaultText');
            }
        });
    });

    $('#contactForm a').click(function() {
        $('#contactForm input, #contactForm textarea').each(function() {
            if ($(this).val() == $(this).parent('label').find('span').text()) {
                $(this).val("")
            }
        });
    });
});

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// cufon
$(document).ready(function() {
	Cufon.replace('#mainContent h3, div.homeHeader, div.headHeadingWrap h2, div.sectionBlock h2, #quicklinks h2, #secondaryNav h2, div.Survey h2,div.questionCount,p#footerTagline', { fontFamily: 'Interstate' });
	
	Cufon.replace('div.sectionBlockTwo h2 a', {
		hover: true,
		hoverables: { a: true }
	});
	
	$('#teamMembers div, #teamMembers div span.title').supersleight();
});

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// questionnaire
$(document).ready(function() {
    // Set up the initial counter
    $('#counter').text($('.Survey').length - 1);
    Cufon.refresh('#counter');

    //hide all panels except the first
    $('.Survey').toggleClass('Survey_hide');
    $('.Survey:first').toggleClass('Survey_hide');

    $('.completeSurvey').toggleClass('completeSurvey Survey_hide');

    $('.helpBox').attr('style', 'display:block');
    
    // set up all of the tool tips
    for (var i = 0; i < $('.Survey').length + 1; i++) {
        $("#help" + i).easytooltip("helpBox" + i, { backgroundcolor: "transparent", border: "0px", display: "none", event: "click" });
        $("#help" + i).click(function(event) {
            if ($(this).text() == 'Show Prompts') {
                $(this).css('display', 'none');
                $(this).parent().find('.close').css('display', 'block');
            } else {
                $(this).text('Show Prompts');
            }
        });
    }
    for (var j = 0; j < $('.Survey').length + 1; j++) {
        $("#close" + j).click(function(event) {
            if ($(this).text() == 'Hide Prompts') {
                $(this).css('display', 'none');
                $(this).parent().find('.help').css('display', 'block');
                var id = $(this).attr('id');
                $("#tool_tip_action_" + "helpBox" + id.substring(5)).hide().css({ "left": 0, "top": 0 });
            } else {
                $(this).text('Hide Prompts');
            }
        });
    }
    function displayTip() {

    }

    $('.surveyNext').click(function() {
        //validate selection
        var numSelected = 0;
        $(this).parents('.SurveyBox').find('input.inputRadio').each(function() {
            if (this.checked)
                numSelected++;
        });
        if (numSelected > 0)
            showNextStep(this);
        else
            alert('Please choose your answer before continuing');
        return false;
    });
    $('a.surveyTest').click(function(event) {
        //validate details
        var errorMsg = '';
        if ($('#tbFirstName').val() == '' ||
            $('#tbFirstName').val() == 'Enter Your First Name')
            errorMsg += 'Please enter your first name\n';
        if ($('#tbLastName').val() == '' ||
            $('#tbLastName').val() == 'Enter Your Last Name')
            errorMsg += 'Please enter your last name\n';
        if ($('#tbBusiness').val() == '' ||
            $('#tbBusiness').val() == 'Enter Your Business')
            errorMsg += 'Please enter your business name\n';
        if ($('#tbEmail').val() == '' ||
            $('#tbEmail').val() == 'Enter Your Email Address')
            errorMsg += 'Please enter your email address\n';
        else if (!isValidEmail($('#tbEmail').val()))
            errorMsg += 'Please enter a valid email address\n';
        if ($('#tbPhone').val() == '' ||
            $('#tbPhone').val() == 'Your Phone Number')
            errorMsg += 'Please enter your phone number\n';
        if (errorMsg != '') {
            alert(errorMsg);
            return false;
        }
        else {
            showNextStep(event.target);
            return false;
        }
    });
    $('input#tbFirstName, input#tbLastName, input#tbBusiness, input#tbEmail, input#tbPhone').click(function() {
        if ($(this).val().indexOf('Enter Your ') != -1 ||
            $(this).val().indexOf('Your Phone ') != -1) {
            $(this).val("")
        }
    });
    $('input#tbFirstName').blur(function() {
        if ($(this).val() == '') {
            $(this).val('Enter Your First Name')
        }
    });
    $('input#tbLastName').blur(function() {
        if ($(this).val() == '') {
            $(this).val('Enter Your Last Name')
        }
    });
    $('input#tbBusiness').blur(function() {
        if ($(this).val() == '') {
            $(this).val('Enter Your Business')
        }
    });
    $('input#tbEmail').blur(function() {
        if ($(this).val() == '') {
            $(this).val('Enter Your Email Address')
        }
    });
    $('input#tbPhone').blur(function() {
        if ($(this).val() == '') {
            $(this).val('Your Phone Number')
        }
    });
    $('input.inputRadio').click(function() {
        $(this).parents('.SurveyBox').find('input[type="hidden"]').val(this.id);
    });
});

function showNextStep(currentElement) {
    $(currentElement).parents('.Survey').next('.Survey').toggleClass('Survey_hide');
    //$(this).parents('.buttons').css('display', 'none').hide("slow");
    $(currentElement).parents('.buttons').slideUp("normal");
    // scroll to target area
    var target = $(currentElement).parents('.Survey').next();
    target = target.size() && target;
    if (target.size()) {
        var targetOffset = target.offset().top;
        $('html').animate({ scrollTop: targetOffset - 40 }, 1000);
    };
    // update the counter area
    $('#counter').text($('#counter').text() - 1);
    // if one question remains change the text
    if ($('#counter').text() == '1') {
        $('#counterText').replaceWith('question left!');
    }
    // if no questions remain, show the send button and hide questions remaining button
    if ($('#counter').text() == '0') {
        $('.questionCount').hide();
        $('.completeSurvey').show();
    }
    // refresh cufon
    Cufon.refresh('#counter, #counterText');
    return false;
}

function PostBackOnReturn(event, postbackMethod) {
    if (event) {
        if (event.keyCode == 13) {
            __doPostBack(postbackMethod, '');
        }
    }
}
function isValidEmail(strEmail) {
    var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailReg.test(strEmail);
}
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
String.prototype.startsWith = function(str) { return (this.match("^" + str) == str); }
function setupDefaultTextReset(obj, message) {
    obj.click(function() {
        if (obj.val() == message)
            obj.val('');
    });
    obj.blur(function() {
        if (obj.val() == '')
            obj.val(message);
    });
}
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'), '');
}
/*~~~~~ start blog ~~~~~*/
$(document).ready(function() {
    $('.blog-reply').click(function() {
        $('div#footer').hide();
        $('.blog-reply-comment').html('');
        $(this).parents('div.comment').nextAll('.blog-reply-comment').eq(0).html(
        '<div class="commentFormContainer" id="replyform">' +
          '<div class="clearer"></div>' +
          '<textarea id="tbBlogCommentComment" name="tbBlogCommentComment" class="comment" rows="5" style="margin-top:5px;">Enter Your Comment</textarea>' +
          '<label for="tbBlogCommentName"><input id="tbBlogCommentName" name="tbBlogCommentName" type="text" class="commentorName" value="Enter Your Name [required]" /></label>' +
          '<label for="tbBlogCommentEmail"><input id="tbBlogCommentEmail" name="tbBlogCommentEmail" type="text" class="commentorEmail" value="Enter Your Email [required, will not be published]" /></label>' +
          '<div class="clearer"></div>' +
          '<p class="error blog-reply-error" style="display:none"></p>' +
          '<div class="clearer"></div>' +
          '<a href="#" class="blog-reply-send">Send</a>' +
          '<a href="javascript:void(0)" class="blog-reply-cancel">cancel</a>' +
          '<div class="clearer"></div>' +
          '<p class="blog-reply-wait">submitting..</p>' +
          '<div class="clearer"></div>' +
        '</div><div class="clearer"></div>');
        $('div#footer').show();

        $('textarea#tbBlogCommentComment, input#tbBlogCommentName, input#tbBlogCommentEmail').focus(function() {
            $(this).addClass('focusText');
            if ($(this).val() == $(this).parent('label').find('span').text()) {
                $(this).val("").removeClass('defaultText');
            }
        });
        $('textarea#tbBlogCommentComment, input#tbBlogCommentName, input#tbBlogCommentEmail').blur(function() {
            $(this).removeClass('focusText');
            if ($(this).val() == "") {
                $(this).val($(this).parent('label').find('span').text()).addClass('defaultText');
            }
        });

        setupDefaultTextReset($('div.blog-reply-comment div.commentFormContainer textarea.comment'), 'Enter Your Comment');
        setupDefaultTextReset($('div.blog-reply-comment div.commentFormContainer input.commentorName'), 'Enter Your Name [required]');
        setupDefaultTextReset($('div.blog-reply-comment div.commentFormContainer input.commentorEmail'), 'Enter Your Email [required, will not be published]');

        window.location.hash = '#replyform';

        $('.blog-reply-cancel').click(function() {
            $('div#footer').hide();
            $(this).parents('.blog-reply-comment').html('');
            $('div#footer').show();
        });
        $('.blog-reply-send').click(function() {
            $('div#footer').hide();
            $('div#footerInner').hide();
            var name = $(this).parent().find('label input[name=tbBlogCommentName]').val();
            var email = $(this).parent().find('label input[name=tbBlogCommentEmail]').val();
            var comment = $(this).siblings('textarea[name=tbBlogCommentComment]').val();

            //validation
            $(this).siblings('p.blog-reply-error').html('');
            $(this).siblings('p.blog-reply-error').css('display', 'none');
            var isValid = true;
            var errorMsg = '';
            if (comment.trim() == '' || comment.trim().toLowerCase() == 'enter your comment') {
                isValid = false;
                errorMsg += 'Please enter your comment<br />';
            }
            if (name.trim() == '' || name.trim().toLowerCase() == 'enter your name [required]') {
                isValid = false;
                errorMsg += 'Please enter your name<br />';
            }
            if (email.trim() == '' || email.trim().toLowerCase() == 'enter your email [required, will not be published]') {
                isValid = false;
                errorMsg += 'Please enter your email address<br />';
            }
            else if (!isValidEmail(email)) {
                isValid = false;
                errorMsg += 'Please enter a valid email address<br />';
            }
            if (!isValid) {
                $(this).siblings('p.blog-reply-error').html(errorMsg);
                $(this).siblings('p.blog-reply-error').css('display', 'block');
                $('div#footer').show();
                $('div#footerInner').show();
                return false;
            }

            var commentID = $(this).parents('.blog-reply-comment').next('input:hidden').val();
            $('p.blog-reply-wait').html('Submitting... Please Wait');
            $('p.blog-reply-wait').css('display', 'block');

            jQuery.post('/handlers/BlogCommentReply.ashx', { commentid: commentID, name: name, email: email, comment: comment }, function(data) {
                $('p.blog-reply-wait').css('display', 'none');
                if (data.startsWith('Success')) {
                    if (data.startsWith("Success:posted")) {
                        $('#divBlogReplyComment-' + commentID).html('<div class="reply"><div class="replyInner"><p>' + comment + '<br /><span class="commentor">' + name + '</span></p></div></div>');
                    }
                    else {
                        $('#divBlogReplyComment-' + commentID).html('<div class="reply"><div class="replyInner"><p>' + comment + '<br /><em>Your reply is now submitted and pending approval</em><br /></p><p class="cite replyCite">' + name + '</p><div class="clearer"></div></div><div class="clearer"></div></div>');
                    }
                }
                else {
                    $('#divBlogReplyComment-' + commentID + ' div.commentFormContainer p.blog-reply-error').html('An error had occurred. Please try again later.');
                    $('#divBlogReplyComment-' + commentID + ' div.commentFormContainer p.blog-reply-error').css('display', 'block');
                }
                $('div#footer').show();
                $('div#footerInner').show();
            });
            
            return false;
        });
        return false;
    });
});
/*~~~~~ end blog ~~~~~*/
