/*
	
	Concord Consortium Global Javascript Functions
	By Will Moore - ISITE Design

*/

// jquery no conflict. use $j or jQuery outside of ready function
//var $j = jQuery.noConflict(); 

function concordConsortium () {
	this.loader = '<h2 class="loader">Loading...</h2>';
	this.currentTab = 0;
	this.url = window.location;
	this.hash = window.location.hash;
	this.logo = '<img src="/_resources/img/css/logo.png" alt="Concord Consortium Logo"/>';
	this.printIcon = '<li class="print-this-page"><a href="#">Print this page</a></li>';
	this.print = function(){
		if($j('#header .brand img').length <1){
			$j('#header .brand').html(this.logo).css("background-image", "none");
		}
		window.print();	
		};
	} //CoreGlobal

var cc = new concordConsortium;


// jQuery document ready
jQuery(function($) {

	// JS enabled
	$('html').addClass('js');
	
	// Stripe those tables
	$('tbody tr:nth-child(odd)').addClass('odd');
	
	// Wrap <hr> so bg-image doesn't have a border in IE6 and Opera
	$("hr").wrap('<div class="hr"></div>');
	
	var $body = $('body');

	// Few setups for IE6
	$("input[type='text'], input[type='password']").addClass("text");
	$("input[type='button'], input[type='submit'], input[type='checkbox'], input[type='image'], input[type='radio'], input[type='reset'], button").addClass("nontext");
	$("input[type='button'], input[type='submit'], input[type='reset'], button").addClass("button");
	//add class to drop downs and buttons 
	$('#nav li, button, input[type="button"]').hover(
		function() { $(this).addClass('over'); },
		function() { $(this).removeClass('over'); }
	);
	// dropdowns on top of selects
	$('#nav div.drop-down').bgiframe();
	//add class to last li in certain contexts
	$('.highlighted-activities').each(function(){
		$('li:last', this).addClass('last');
	});
	
	$('#header input, #activity-keyword, #project-signin input').inputSetter();
	
	if (!$body.is('.front')) {
		
		$('.page-utility').prepend(cc.printIcon);
        $('.print-this-page a').click(function() { cc.print(); });
	}
	
	$('#primary thead th:last').addClass('last');
	
	// Tabs
	$("ul.tabs").tabs("div#tabwrap > div",{
		/* we need the current class on the li too. */
		onClick: function(event, tabIndex) {
			$('ul.tabs li').removeClass('current');
			$('ul.tabs li').eq(tabIndex).addClass('current');
			
			if($body.is('.section-meta-project')){
				var $currentTab = $('div#tabwrap > div').eq(tabIndex);
				metaProjectTabs($currentTab);
			}	
		}
	});
	
	var tabsAPI = $("ul.tabs").data('tabs');
	
	//bubblers. Avoid onClick attributes by adding conditions here. 
	$body.click(function(event) {
		var $target = $(event.target);
	
		// left nav
		if($target.is('#nav-sub span')){
			var $parentLI = $target.parent().parent();
			$parentLI.find("ul").eq(0).slideToggle();

			// .active spins the arrow.
			if ($parentLI.is(".active")) {
				$parentLI.removeClass('active');
			} else if($parentLI.find("ul").length > 0){ // don't spin the arrow if there are no children uls to display.
				$parentLI.addClass('active');
			};
			
			return false;
		}	
		
		// example.
		// if($target.is('#some-jquery-selector')){
		// 	//do stuff
		// }
		
		return true;
	});
	
	$('#nav-sub li ul').each(function(){
		$(this).prev('a').prepend('<span class="toggle"></span>').end().hide();		
	});
	


});// document ready


// Input Setter
// pull label and insert as field. clear with click.
jQuery.fn.inputSetter = function(lower) {
	return this.each(function() {
		var $input = jQuery(this),
			$label = jQuery("label[for='"+$input.attr("id")+"']"),
			labeltext = lower && lower==1 ? $label.text().toLowerCase() : $label.text();
			
		$label.hide();	
		
		if($input.is(':password')) {
			var $fake = jQuery('<input />',{ value:labeltext, className:$input.attr('class'), type:"text" });
			$input.hide();			
			$fake.focus(function() { jQuery(this).hide(); $input.show().focus(); }).insertBefore($input);
			$input.blur(function() { if (!this.value.length) { jQuery(this).hide(); $fake.show(); } });				
		} else {		
			$input.val(labeltext);		
			$input.focus(function() { if (this.value == labeltext) { this.value = ""; }	})
				  .blur(function() { if (!this.value.length) { this.value = labeltext; } });			
		}		
	});
};
// end inputSetter

// clean console.log
function cl(){ if(window.console&&window.console.firebug) { var args = [].splice.call(arguments,0); console.log(args.join(" ")); } }//cl()
// example: cl("If you use cl() instead of console.log(), it won't break IE when you forget to take it out.");

// IE6 fixes
// make sure IE has the abbr and acronym tag
if(document.all){
	document.createElement("abbr");
	document.createElement("acronym");
}
/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
* Licensed under the MIT License (LICENSE.txt).
*
* Version 2.1.3-pre
*/

(function($){
$.fn.bgiframe = ($.browser.msie && /msie 6\.0/i.test(navigator.userAgent) ? function(s) {
    s = $.extend({
        top : 'auto', // auto == .currentStyle.borderTopWidth
        left : 'auto', // auto == .currentStyle.borderLeftWidth
        width : 'auto', // auto == offsetWidth
        height : 'auto', // auto == offsetHeight
        opacity : true,
        src : 'javascript:false;'
    }, s);
    var html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
                   'style="display:block;position:absolute;z-index:-1;'+
                       (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
                       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
                       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
                       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
                       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
                '"/>';
    return this.each(function() {
        if ( $(this).children('iframe.bgiframe').length === 0 )
            this.insertBefore( document.createElement(html), this.firstChild );
    });
} : function() { return this; });

// old alias
$.fn.bgIframe = $.fn.bgiframe;
function prop(n) { return n && n.constructor === Number ? n + 'px' : n; }
})(jQuery);
// prevent IE6 flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

/*
 *
 * Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * Demo: http://www.texotela.co.uk/code/jquery/checkboxes/
 *
 * $LastChangedDate: 2009-02-07 23:44:21 +0000 (Sat, 07 Feb 2009) $
 * $Rev: 6182 $
 */
eval(function(p,a,c,k,e,r){e=function(c){return c.toString(36)};if('0'.replace(0,e)==0){while(c--)r[e(c)]=k[c];k=[function(e){return r[e]||e}];e=function(){return'[0-9f-i]'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}(';(1(d){d.g.toggleCheckboxes=1(a,b){a=a||"*";b=b||6;2 c=d([]);0.4(1(){2 e=d("7[8=9]",0).5(a).4(1(){0.3=!0.3}).5(":3");c=e});f(!b){c=0}h c};d.g.checkCheckboxes=1(a,b){a=a||"*";b=b||6;2 c=d([]);0.4(1(){2 e=d("7[8=9]",0).5(a).4(1(){0.3=true}).5(":3");c=e});f(!b){c=0}h c};d.g.unCheckCheckboxes=1(a,b){a=a||"*";b=b||6;2 c=d([]);0.4(1(){2 e=d("7[8=9]",0).5(a).4(1(){0.3=6}).5(":i(:3)");c=e});f(!b){c=0}h c};d.radioCheckboxGroup=1(e,a){a=a||"*";2 b="7[8=9]";f(e){b+="[name="+e+"]"}2 c=d(b).5(a);c.click(1(){c.i(0).4(1(){0.3=6}).end()})}})(jQuery);',[],19,'this|function|var|checked|each|filter|false|input|type|checkbox||||||if|fn|return|not'.split('|'),0,{}))
