// JavaScript Document
// depends on JQuery 1.4

// there's a conditional in the footer code for Google Analytics that checks this var
// the 404 page has code that will set it to a value, 
// I just want to cut down on errors by declaring it here
var pagenotfound = 0;

function resetBox(box, defaultvalue) {
  if (box.value == defaultvalue) {
   box.value = '';
   box.className = box.className + ' txt-black';
   }
}


function setSection() {
	var sNavSection = $('#sectionID').attr('class');
//	$('ul.arrow li.item').after('<img class="icon-corner" src="/assets/images/icons/icon_corner_' + sNavSection + '.png" width="10" height="10" />');
	
	$('#' + sNavSection).addClass('active'); // set main nav tab on state
	$('#' + sNavSection).next().addClass('roll-next');
	var sNavSectionFile = '/' + sNavSection + '/' + sNavSection + '-section-navigation.html';
	$('#section-sub').load(sNavSectionFile, function() {
//	  	  alert('Load was performed:' + sNavSectionFile);
		  var cssLocation = 'http://www.sfaf.org' + '/assets/css/';
		  loadjscssfile( cssLocation + sNavSection + ".css", "css"); //dynamically load and add this .css file
		  setMenu();
		  activateTopnav();
		  activateSubnav();
		  if (sNavSection != 'home') {
			  $('#section-sub').fadeIn();
		  } else {
			  $('#section-sub').fadeOut();
		  }
		  $('.wrapper-content').wait(300).fadeIn();
		});
}
function setLinks() { // sets links of items with class="link" to the rel attribute of that object
	$('.link').click(function() {
		window.location.href = $(this).attr('rel');
	});
}
function setMenu() { // sets the links and on class of current subnav page


	
	var sSubId = $('#sectionID').attr('alt');
	$('#'+sSubId).addClass('subnav-on');
	
	$('.nav-item.active').unbind();

//	$('.nav-item').click(function(){ var id = $(this).attr('id'); window.location.href = '/' + id + '/index.html'; });
//	$('.btn-donate').click(function(){ var id = $(this).attr('id'); window.location.href = '/' + id + '/index.html'; });


}

function activateTopnav() { // turns on the pulldown subnav
	$("li.nav-item:not(li.active)").hover(function() {
		$(this).addClass('roll');
		$(this).next().addClass('roll-next');
		$(this).find("ul.topnav").show();
		$(this).hover(function() {
		}, function(){
			$(this).find("ul.topnav").hide();
			$(this).removeClass('roll');
			$(this).next().removeClass('roll-next');
		});
		}).hover(function() {
			$(this).addClass("subhover");
		}, function() {
			$(this).removeClass("subhover");
	});
}

function activateSubnav() { // turns on the pulldown subnav
	$("ul.subnav2").parent().append("<span></span>");
	$("ul.subnav li").hover(function() {
		$(this).find("ul.subnav2").slideDown('fast').show();
		$(this).hover(function() {
		}, function(){
			$(this).find("ul.subnav2").slideUp('slow');
		});
		}).hover(function() {
			$(this).addClass("subhover");
		}, function() {
			$(this).removeClass("subhover");
	});
}

function loadjscssfile(filename, filetype){
	if (filetype=="js"){ //if filename is a external JavaScript file
	var fileref=document.createElement('script')
	fileref.setAttribute("type","text/javascript")
	fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
	var fileref=document.createElement("link")
	fileref.setAttribute("rel", "stylesheet")
	fileref.setAttribute("type", "text/css")
	fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
	document.getElementsByTagName("head")[0].appendChild(fileref)
}

// Delay Plugin for jQuery
// - http://www.evanbot.com
// - copyright 2008 Evan Byrne
/*
 * Jonathan Howard
 * jQuery Pause
 * version 0.2
 * Requires: jQuery 1.0 (tested with svn as of 7/20/2006)
 * Feel free to do whatever you'd like with this, just please give credit where
 * credit is do.
 * pause() will hold everything in the queue for a given number of milliseconds,
 * or 1000 milliseconds if none is given.
 */
// Wait Plugin for jQuery
// http://www.inet411.com
// based on the Delay and Pause Plugin
 (function($) {
    $.fn.wait = function(option, options) {
        milli = 500; 
        if (option && (typeof option == 'function' || isNaN(option)) ) { 
            options = option;
        } else if (option) { 
            milli = option;
        }
        // set defaults
        var defaults = {
            msec: milli,
            onEnd: options
        },
        settings = $.extend({},defaults, options);

        if(typeof settings.onEnd == 'function') {
            this.each(function() {
                setTimeout(settings.onEnd, settings.msec);
            });
            return this;
        } else {
            return this.queue('fx',
            function() {
                var self = this;
                setTimeout(function() { $.dequeue(self); },settings.msec);
            });
        }

    }
})(jQuery);

/*
 * Stew Houston
 *
 * MIT/GPL
 *
 */

/* Usage
$.windowLoaded(function() {
// some code
});
*/

(function($) {
    $(window).load(function() {
        window.loaded = 1;
    });
    
    $.windowLoaded = function(fn) {
        var windowLoadedFn = window.loaded ? 
          (function(fn) { fn.call(window); }) : // the window has already loaded
          (function(fn) { $(window).load(fn); }); // the window hasn't loaded
        
        windowLoadedFn(fn);
    };
})(jQuery);

// doc loaded and ready
$(document).ready(function() {
  setSection();
});
