/// <reference path="jquery-1.3.2.min-vsdoc.js" />
/*
	
	Verity Credit Union Global Javascript Functions
	By  - ISITE Design

*/

//make sure IE has the abbr and acronym tag
if (document.all) {
	document.createElement("abbr");
	document.createElement("acronym");
}

//start the jQuery functions
$(document).ready(function() {

	// sitemap expand/collapse
	enableTree("div.sitemap ul");
	
	document.getElementById('username').focus();

	// Text resizing controls
	$('#text-size').show();
	$('#small-text').click(function() { resizeText('small'); return false; });
	$('#regular-text').click(function() { resizeText('medium'); return false; });
	$('#large-text').click(function() { resizeText('large'); return false; });
	$('#regular-text').addClass('active');
	// /Text resizing controls 	

	// Few setups for IE6
	if (document.all) {
		//add class to drop downs and buttons 
		$("#nav li, button").hover(
			function() { $(this).addClass("over"); },
			function() { $(this).removeClass("over"); }
	    );
	} // if document.all

	// add class "last" to last li in a group
	$('ul li:last').addClass('last');

	/* set up default tab behavior */
	$('dl.tabs dt').click(function() {
		tab = $(this).attr('class').match(/tab-(\w+)/);
		$('dl.tabs dt, dl.tabs dd').removeClass('on');
		$('dl.tabs dt.tab-' + tab[1] + ', dl.tabs dd.pane-' + tab[1]).addClass('on');
	});
	/* go through tabs and turn the first one on if there is no other default */
	if (!$('dl.tabs dt').hasClass('on')) $('dl.tabs dt').filter(':first').trigger('click');

	// turn on Input Setter for #site-search
	$("#site-search input").inputSetter();

	// add inner spans to buttons for growing background
	$("button").addClass('btn').wrapInner("<span></span>");

	// main navigation dropdowns. initialize by ripping title and adding to dropdown listing (li.overview), then bind hover behavior.
	// over class simply repositions to bottom left edge of parent li, revealing dropdown.
	$("#nav > li:has(ul)")
	.each(function() {
		var $a = $('a:first', this);
		var overview = $a.attr('title');
		$a.removeAttr('title');
		$('ul', this).append('<li class="overview">' + overview + '</li>');
	})
	.hover(function() { $(this).addClass('over').find('ul').addClass('active'); }, function() { $(this).removeClass('over').find('ul').removeClass('active'); });

	// callout-listing hover behavior; add over class to li to change background on .description, capture clicks on li to simulate link behavior on entire li.
	// if ($('.callout-listing .content h2 a').length) {
	//	$('.callout-listing .content')	
	//	.hover(function() { $(this).addClass('over'); }, function() { $(this).removeClass('over'); })
	//	.data('link', $('.description h2 a', this).attr('href'))
	//	.click(function() { window.location = $(this).data('link'); });
	//}
	
	//callout-listing for content
	if ($('#primary .callout-listing .content h2 a').length) {
	$('#primary .callout-listing .content').each(function(){
	$(this)
	.hover(function() { $(this).addClass('over'); }, function() { $(this).removeClass('over'); })
	.data('link', $('.description h2 a', this).attr('href'))
	.click(function() { window.location = $(this).data('link'); });
	});
	}		
	// callout-listing for sidebars
	if ($('#secondary .callout-listing .content h2 a').length) {
	$('#secondary .callout-listing .content').each(function(){
	$(this)
	.hover(function() { $(this).addClass('over'); }, function() { $(this).removeClass('over'); })
	.data('link', $('.description h2 a', this).attr('href'))
	.click(function() { window.location = $(this).data('link'); });
	});
	}		
	/*if ($('#secondary .callout').length) {
		$('#secondary .callout')
		.data('link', $('div h3 a', this).attr('href'))
		.hover(function() { $(this).addClass('over'); }, function() { $(this).removeClass('over'); })
		.click(function() { window.location = $(this).data('link'); });
	}*/	
	//inner span for a.button
	$('a.button,a.btn').wrapInner('<span></span>');

	// login form interception
	$("form#sign-on-form").submit(function() {
		var uid = $("input#username").val();
		Verity.Web.Data.GetUserS.SetS(uid, function() {
			var loginform = $("form#sign-on-form");
			loginform.unbind("submit");
			loginform.submit();
		}, function() {
			var loginform = $("form#sign-on-form");
			loginform.unbind("submit");
			loginform.submit();
		}, null);
		return false;
	});
	
	

});             // document ready / end jquery functions

var resizeText = function(size) {
    $('#small-text, #regular-text, #large-text').removeClass('active');
    $('body').removeClass('smalltext').removeClass('largetext');
    
    if (size.toLowerCase() == "small") {
        $('#small-text').addClass('active');
        $('body').addClass('smalltext');
    } else if (size.toLowerCase() == "large") {
        $('#large-text').addClass('active');
        $('body').addClass('largetext');
    } else {
        $('#regular-text').addClass('active');
    }
}

// Input Setter
jQuery.fn.inputSetter = function(lower) {	
	return this.each(function() {
		var $input = $(this);
		var $label = $("label[for='"+$input.attr("id")+"']");
		var labeltext = lower && lower==1 ? $label.text().toLowerCase() : $label.text();
		$label.hide();	
		$input.val(labeltext);		
		$input.focus(function() { if (this.value == labeltext) { this.value = ""; }	})
			  .blur(function() { if (!this.value.length) { this.value = labeltext; } });		
	});
};
// end inputSetter

// create open/close tree. receives ul.
var enableTree = function(el) {
                                
	$(el).addClass("expander").find("li li ul").hide();
	
	$("li li",el).each(function(){                                                                                                          
		if($("li",this).length>0) {
			$(this).prepend('<a class="btn-toggle" href="#">+</a>')
			$("a.btn-toggle",this).click(function(){                                                                                                                                                     
				var textinsert = $(this).text() == "+" ? "-" : "+";
				$(this).text(textinsert).toggleClass("open").parent("li").find("ul:first").slideToggle("fast");                                                                
				return false;                                       
			});
		}              
	});           
};

// clean console.log
function cl(logit){ if(window.console&&window.console.firebug) { console.log(logit) } };//cl()
// example: cl("If you use cl() instead of console.log(), it won't break IE when you forget to take it out.");



