function wrapLogout() {
	var expires = " expires=Thu, 01-Jan-1970 00:00:01 GMT;";
	document.cookie = "WRAP16=;"+expires+" path=/;domain=ncsu.edu";
	document.cookie = "WRAP_REFERER=;"+expires+" path=/;domain=ncsu.edu";
	document.cookie = "WRAP_REFERER_P=;"+expires+" path=/;domain=ncsu.edu";
	document.cookie = "LIBFUNCTION=;"+expires+" path=/;domain=ncsu.edu";
}

function findWrap(){
	var sindex = document.cookie.indexOf("WRAP16");
	if (sindex != -1){
		return 1;
	}
	return 0;
}

/*
 * jQuery In-field Labels Plugin v1.0
 * http://richardscarrott.co.uk/posts/view/jquery-in-field-labels
 *
 * Copyright (c) 2010 Richard Scarrott
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Requires jQuery v1.3+
 *
 */

(function($) {
	$.fn.inFieldLabels = function(filter) {

		var removeLabel = function($input) {
			// remove label if in 'label' state
			if ($input.hasClass('dummy')) {
				$input.hide().prev('input').show().focus();
			}
			else if ($input.hasClass('label')) {
				$input.removeClass('label').val('');
			}
		};
		
		var addLabel = function($input) {
			var label = $input.data('label'),
				val = $input.val();
			
			// add label if val is empty
			if ($input.is('input:password') && (val === '')) {
				$input.hide().next('input').val(label).show();
			}
			else if (val === '') {
				$input.addClass('label').val(label);
			}
		};

		return this.each(function() {
			var $form = $(this),
				$inputs = $form.find('input[type=text], input[type=password], textarea');

			if (filter) {$inputs = $inputs.filter(filter);}
			
			$inputs.each(function() {
				var $input = $(this),
					label = $form.find('label[for=' + $input.attr('id') + ']').hide().text();

				$input.data('label', label)
						.focus(function() {
							removeLabel($input);
						}).blur(function() {
							addLabel($input);
						});
				
				if ($input.is('input:password')) {
					var $dummy = $('<input type="text" class="dummy label" />').insertAfter($input).focus(function() {
						removeLabel($dummy);
					});
					if ($input.val() !== '') {$dummy.hide();}
				}
				addLabel($input);
			});
			
			$form.submit(function() {
				$inputs.each(function() {
					removeLabel($(this));
				});
			});
		});
	};
})(jQuery);

// console.log(gaID);
//var _gaq = _gaq || [];
//  _gaq.push(['_setAccount', gaID]);
//  _gaq.push(['_setLocalRemoteServerMode']);
//  _gaq.push(['_trackPageview']);
//
//  (function() {
//    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
//    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
//    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
//  })();

$(document).ready(function() {
			

	   $('#search-header form').inFieldLabels();
	
	  // display logout link in utility navigation
	  if(findWrap()) { 
	    $('#libraries_title_menu li.last').before('<li><a href="http://www.lib.ncsu.edu/" onclick="wrapLogout();">Logout</a></li>');
	  }
	
	//Mega menus
	//On Hover Over
	function megaHoverOver(){
	    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	    (function($) {
	        //Function to calculate total width of all ul's
	        jQuery.fn.calcSubWidth = function() {
	            rowWidth = 0;
	            //Calculate row
	            $(this).find("ul").each(function() { //for each ul...
	                rowWidth += $(this).width(); //Add each ul's width together
	            });
	        };
	    })(jQuery); 

	    if ( $(this).find(".row").length > 0 ) { //If row exists...

	        var biggestRow = 0;	

	        $(this).find(".row").each(function() {	//for each row...
	            $(this).calcSubWidth(); //Call function to calculate width of all ul's
	            //Find biggest row
	            if(rowWidth > biggestRow) {
	                biggestRow = rowWidth;
	            }
	        });

	        $(this).find(".sub").css({'width' :biggestRow}); //Set width
	        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

	    } else { //If row does not exist...

	        $(this).calcSubWidth();  //Call function to calculate width of all ul's
	        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

	    }
	}
	//On Hover Out
	function megaHoverOut(){
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	      $(this).hide();  //after fading, hide it
	  });
	}
	
	//Set custom configurations
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 10, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 10, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
	$("ul#mega li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#mega li").hoverIntent(config); //Trigger Hover intent with custom configurations
});

/* 
Title:			footnote printing
Author: 		Laurens van Heems 
Blog post: 	http://blog.vicompany.nl/printing-links-better-with-jquery 
Comment:		Added and modified by Charlie Morris on 9/13/2011
				I changed the thisLink variable so that it grabs the whole URI, not
				just the relative link.  I also addded the ability to ignore certain
				anchors with the class "ignore"
*/

function footnoteLinks(container, target) {
 
    // only append list if there are any links
    if($(container + " a").length != 0){
        // append header and list to the target
        $(target).append("<h2 class='printOnly'>Links</h2>");
        $(target).append("<ol id='printlinks' class='printOnly'></ol>");
    }  
 
    var myArr = []; // to store all the links
    var thisLink;   // to store each link individually
    var thisClass;   // to store each link's class individually
    var num = 1; // to keep count
 
    // loop trough all the links inside container
    $(container + " a").each(function(){
        thisLink = $(this).get(0).href;
        thisClass = $(this).attr("class");
        // if current link has class "ignore" then ignore it
        if(thisClass == "ignore"){
            return true;
        }
        // if current link is not already in the list, add current link to the list
        if($.inArray(thisLink, myArr) == -1){
            $(this).after("<sup class='printOnly'>" + num + "</sup>");
            $("ol#printlinks").append("<li>" + thisLink + "</li>");
            myArr.push(thisLink);
            num++;
        }else{
            $(this).after("<sup class='printOnly'>" + parseInt($.inArray(thisLink, myArr) + 1) + "</sup>");
        }
    });
}
