﻿/// <reference path="_jquery.js" />

$(function() {		
	initLogin();
	initForms();
	initWelcome();
	initScrollPos();
	initExternalLinks();
	initFeedback();

	$(".noScript").hide();
		
});


/* stores y scroll if .scrollToMe clicked
===================================================================================== */
function initScrollPos() {
    var opt = { expires: 10 };
    var name = "scroll.y";
    var target;


    $("input.scrollToMe, .scrollToMe input, a.scrollToMe, .scrollToMe a").click(function() {

    	// either scroll to click element, or to first paging
    	if ($(this).parents(".paging").size() === 1) {
    		target = $(".paging:first").offset()["top"] - 70;
    	} else {
    		target = $(window).scrollTop()
    	}
    	$.cookie(name, target, opt);
    });

    if ($.cookie(name) !== null) {
    	$.scrollTo($.cookie(name));
        $.cookie(name, null, opt);
    }
}


/* login
===================================================================================== */
function initLogin() {
    $("#head .login label").hide();

    // add dummy password label input
    $("#head .login input:password").after('<input type="text" class="dummyPassword" />');
    $("#head .login input:password").hide();
    $("#head .login input.dummyPassword").val( $('.login input:password').prevAll('label').text() );
    
    // add lbl text to inputs and set event handlers
    var cssClass = '';
    $("#head .login input").each(function() { 
        $(this).attr('lbl', $(this).prevAll('label').text());
        cssClass = $(this).attr('class');
        
        if (cssClass === 'usr' && $(this).val() === '') {
            $(this).val($(this).attr('lbl'));
        } 
        
        if (cssClass !== 'pwd') {
            $(this).focus(function() { setVal($(this)); });
        }
        
        if (cssClass !== 'dummyPassword') {
            $(this).blur( function() { setVal($(this)); });
        }
    });
    

    function setVal(txtBox) {
        if ($(txtBox).attr('class') === 'dummyPassword') {
            $("#head .login input:password").show().focus();
            $(txtBox).hide();
            return;
        }
        
        if ($(txtBox).attr('type') === 'password') {
            if ($(txtBox).val() === '') {                
                $("#head .login .dummyPassword").show();
                $(txtBox).hide();
            }
            return;
        }
        
        if ($(txtBox).attr('type') === 'text') {
            if ($(txtBox).val() === '') {
                $(txtBox).val($(txtBox).attr('lbl'));
            } 
            else if ($(txtBox).val() === $(txtBox).attr('lbl')) {
                $(txtBox).val('')
            }
        }
    }
}


/* form helpers 
===================================================================================== */
function initForms() {
   // $("label[title='(required)']").next().after('<strong title="required">*</strong>');
    $(".boxHelp p").before("<h4>help</h4>");
    $(".form div em").attr("title", "public - other hoppers may be able to see this information");

    // fixes js bug with default button and textareas
    $("textarea").keypress(function(event) { event.cancelBubble = true });
}


/* External Links and PDF Links
===================================================================================== */
function initExternalLinks() {
    var h = window.location.host.toLowerCase();
    $("a[href^='http']:not([href^='http://" + h + "']):not([href^='http://www." + h + "']), a[href$='.pdf']").attr("target", "_blank");
}


/* Welcome text
===================================================================================== */
function initWelcome() {
    var s = 500;
    $(".welcome div div:gt(0)").hide();
    $(".welcome h3").css({
        "text-decoration": "underline",
        "cursor": "hand",
        "cursor": "pointer"
    }).click(
	function() {
	    if ($(this).next("div:visible")[0]) return;
	    $(".welcome div div").slideUp(s);
	    $(this).next("div").slideDown(s);
	}
  );

}


/* Feedback thumbs
===================================================================================== */
function initFeedback() {
    var opacity = 0.5;
    $('.feedback span span input').css({'position': 'absolute', 'left': '-8888px'});
    $('.feedback span span input:not(:checked) + label').css({ 'opacity': opacity });
    $('.feedback span span label').click( function() {
    	$('.feedback span span label').fadeTo(500, opacity).dequeue();
    	$(this).fadeTo(500, 1.0);
    });
}


