/*! This file has *//* NOT *//*! been minimized. */


/*In minimized files, *//*! credits of reused code can be found by removing '-min' from the url. */


if (typeof SOUPGIANT == "undefined" || !SOUPGIANT) {
	//create SOUPGIANT namespace if it hasn't been done already
	SOUPGIANT = {};
}

SOUPGIANT.base = function() {
	var $ = jQuery, //covers noConflict Mode
		$body, //defined later
		
		WIN = window, //for compression
		DOC = document,
		NUL = null,
		TRU = true,
		FALS = false,
		i = 0;
		
	$(function(){
		$body = $('body');
		$body.removeClass('nojs').addClass('js');
		compactForms()
		viewAllTagCloud();
	});
	
	$(WIN).ready(function(){
		$body.removeClass('nojswin').addClass('jswin');
		if (typeof $body.validate == 'function') {
			$('#commentForm').validate({
				onClick: true,
				messages: {
					comment: "A comment is required",
					name: "Your name is required",
					email: {
						required: "Your email is required"
					}
				}

			});
		}
	});
	
	function nav(navSelector, hoverClass, focusClass){
		/* ****
		DESCRIPTION
			Keyboard accessabile suckerfish menus for all browsers
			Mouse in/out suckerfish menus required by IE6
		
		INPUT: 
			* navSelector
				jQuery selector for menus 
				default: 'ul.nav'
			* hoverClass
				class added on to list items on mouse hover
				default: 'sfhover'
			* focusClass
				class added to list items on keyboard focus of links/sublinks
				default: value of hoverClass
		
		RETURN: nil
		
		DEPENDENCIES:
			jQuery
		**** */
		if (navSelector == NUL) {
			navSelector = "ul.nav li"; //all unordered lists with class nav
		}
		else {
			//only needed when navSelector is passed
			var navSelectors = navSelector.split(','),
				nsL = navSelectors.length;
			navSelector = '';
			for (i = 0; i<nsL; i++){
				navSelector += navSelectors[i] + ' li';
				if (i<nsL-1){
					navSelector += ', ';
				}
			}
		}
		if (hoverClass == NUL) {
			hoverClass = "sfhover"; //default class name
		}
		if (focusClass == NUL) {
			focusClass = hoverClass; //defaults to hoverClass
		}
		
		//allow multiple selectors
		$(function() {
			$(navSelector).each(function () {
				var $li = $(this);
			
				$li.hover(
					//mouse - in
					function() {
						$li.addClass(hoverClass);
					},
					//mouse - out
					function() {
						$li.removeClass(hoverClass);
					}
				);
			
				//keyboard in
				$li.find("a").each(function() { //find = children + grandchildren + etc
					var $a = $(this);
					
					$a.focus(function() {
						$li.addClass(focusClass);
					});
					
					$a.blur(function() {
						$li.removeClass(focusClass);
					});
				});				
			});
		});
	}

	var viewAllTagCloud = function() {
		var $theWidget = $('div.widget_nktagcloud'),
			$firstLink = $('a:eq(0)', $theWidget);
			
		if ($theWidget.length > 0) {
			var	firstLinkURL = $firstLink.attr('href'),
				firstLinkSegments = firstLinkURL.split('/');

			if (firstLinkSegments[firstLinkSegments.length-1] == '') {
				firstLinkSegments.pop();
			}
			firstLinkSegments.pop();
			
			firstLinkURL = firstLinkSegments.join('/');
			
			$theWidget.append('<p><a href=\"' +  firstLinkURL + '\">View All &raquo;</a></p>');
		}
	}

	
	function equalHeight(selector, className, media, context){
		/* ****
		DESCRIPTION
			Creates equal height columns for CSS layout
			Adds <style> to header of document, allowing media type targeting

		INPUT: 
			* selector
				jQuery selector for all columns
				default: NULL/nil
				
			* className
				name of class to add to each column (a random number is appended to this value)
				default: 'equalHeight' 
				
			* media
				css media to target
				default: 'screen, projection, handheld'
				
			* context
				css context to add to selector in <style> section
				eg context='#content' will output #content .equalHeight-xxxx {}
				default: '' (empty string)
				
		RETURN: className with random number appendix

		DEPENDENCIES:
			jQuery
			SOUPGIANT.base.createStyleRule()
		**** */
		if (selector == NUL) {
			//function called incorrectly, exit
			return NUL;
		}
		if (className == NUL) {
			className = 'equalHeight';
		}
		if (media == NUL) {
			media = 'screen, projection, handheld'; //default
		}
		if (context == NUL) {
			context = '';
		} else {
			context = ' ' + context + ' ';
		}

		var classRandom = Math.floor(Math.random()*999999),
			tallestCol = 0,classDeclaration,ie6Declaration;
		className = className + '-' + classRandom;
		$(selector).each(function (){
			var $this = $(this);
			if ($this.height() > tallestCol) {
				tallestCol = $this.height();
			}
			$this.addClass(className);
		});
		
		//create css declaration
		className = '.' + className;
		classDeclaration = 'min-height: ' + tallestCol + 'px;';
		ie6Declaration = 'height: ' + tallestCol + 'px;';
		createStyleRule(context + className, classDeclaration, media);
		createStyleRule('* html ' + context + className, ie6Declaration, media);
		
		return className; //in case it's needed for later manipulation
	}
	
	function createStyleRule(selector, declaration, media){
		/* ****
		DESCRIPTION
			Adds <style> to header of document, allowing media type targeting

		INPUT: 
			* selector
				css selector
				default: NULL/nil
				
			* declaration
				css properties
				default: NULL/nill
				
			* media
				css media to target
				default: 'screen, projection, handheld'
				
				
		RETURN: NULL/nil

		DEPENDENCIES: NULL/nil
		
		CREDIT
		dynamicCSS.js v1.0 <http://www.bobbyvandersluis.com/articles/dynamicCSS.php> */
		/*! Copyright 2005 Bobby van der Sluis */
		/*! This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/> *//*
		
		changes
		- added media to passed variables
		
		**** */
		if (media == NUL) {
			media = 'screen, projection, handheld';
		}
		if (!DOC.getElementsByTagName ||
		  !(DOC.createElement || DOC.createElementNS)) 
			{return;}
		var agt = navigator.userAgent.toLowerCase(),
			is_ie = ((agt.indexOf("msie") != -1) &&  (agt.indexOf("opera") == -1)),
			is_iewin = (is_ie &&  (agt.indexOf("win") != -1)),
			is_iemac = (is_ie &&  (agt.indexOf("mac") != -1));
		if (is_iemac) return; // script doesn't work properly in IE/Mac
		var head = DOC.getElementsByTagName("head")[0],
			style = (typeof DOC.createElementNS != "undefined") ?
		  DOC.createElementNS("http://www.w3.org/1999/xhtml", "style") :
		  DOC.createElement("style");
		if (!is_iewin) {
			var styleRule = DOC.createTextNode(selector + " {" + declaration + "}");
				style.appendChild(styleRule); // bugs in IE/Win
		}
			style.setAttribute("type", "text/css");
		style.setAttribute("media", media); 
		head.appendChild(style);
		if (is_iewin &&  DOC.styleSheets &&  DOC.styleSheets.length > 0) {
			var lastStyle = DOC.styleSheets[DOC.styleSheets.length - 1];
			if (typeof lastStyle.addRule == "object") {
				lastStyle.addRule(selector, declaration);
			}
		}
		
	}

	
	function slideShow(slides, slideWrap, slideSelector, displayLength, transitionLength, firstSlidePlaced){
		/* ****
		DESCRIPTION
			creates a javascript slideshow from a group of images
			images may include a link
			
		WARNING
			ie6 and ie7 can be problematic with z-index, 
			depending on the positioning of  surrounding elements
			
		INPUT: 
		
			slides 
				Array of each slide as html code
				
			slideWrap 
				jQuery selector of the slides wrapper (usually a div)
				default: '#slides'
				
			slideSelector
				jQuery selector of all the slides
				default: '#slides > img'
				
			displayLength 
				how long each slide is displayed for (seconds)
				default: 5
				
			transitionLength
				how long the transition b/w slides is (milliseconds)
				default: 400
				
			firstSlidePlaced
				true / false
				default: true
				
		
		
		RETURN: NUL/nill
		
		DEPENDENCIES: 
			jQuery
 		**** */
		if (slides == NUL) {
			return FALS;
		}
		if (slideWrap == NUL) {
			slideWrap = '#slides';
		}
		if (slideSelector == NUL) {
			slideSelector = '#slides > img';
		}
		if (displayLength == NUL) {
			displayLength = 5;
		}
		if (transitionLength == NUL) {
			transitionLength = 400;
		}
		if (firstSlidePlaced == NUL) {
			firstSlidePlaced = TRU;
		}
		
		
		
		var $wrapper = $(slideWrap),
			$slides = $(slideSelector),
			numSlides = slides.length,
			$slideI, tmpShowing,
			
			//defaults/counters
			nowShowing = 0,
			nextShowing = 0,
			placeFrom = 0,
			zIndexTop = numSlides + 1;
		
		if (firstSlidePlaced == TRU) {
			placeFrom = 1;
			
			
			$slides.eq(0).css({zIndex: zIndexTop, position: 'absolute'});
		}
		
		for (i=placeFrom; i<numSlides; i++) {
			if ((slides[i] instanceof $) == TRU) {
				$slideI = slides[i];
			}
			else {
				$slideI = $(slides[i]);
			}
			$slideI.css({
				position: 'absolute',
				zIndex: numSlides - i
			});
			$wrapper.append($slideI);
		}
		
		$slides = $(slideSelector); //recache slides
		
		$slides.stop().fadeTo(1, 0);
		$slides.eq(1).stop().fadeTo(1,1);
	
		//make a transition
		function nextSlide(){
			nextShowing = nowShowing + 1;
			if (nextShowing == numSlides) {
				nextShowing = 0;
			}
			tmpShowing = nowShowing; //used for callback
			$slides.eq(nextShowing).css('z-index', zIndexTop+1);
			$slides.eq(nowShowing).css('z-index', zIndexTop);
			$slides.eq(nextShowing).stop().fadeTo(transitionLength, 1,function(){
				$slides.eq(tmpShowing).fadeTo(1, 0);
			});
			nowShowing = nextShowing;
			setTimeout(nextSlide, displayLength * 1000);
		}
		
		setTimeout(nextSlide, displayLength * 1000);
		
		
		
	}
	
	
	function setCookie(name, value, expire, path, domain, secure) {
		/* ****
		DESCRIPTION
			sets a cookie
			emulates the setCookie function in php
			
		INPUTS
			name
				The name of the cookie
				default: NULL/nil
				
			value
				The value of the cookie
				default: NULL/nil
				
			expire 
				The time the cookie expires. 
				This is either
					a Unix timestamp (sec since 1/1/1970)
					Javascript time object
				default: end of current session
				
			path
				The path on the server in which the cookie will be available on.
				default: '/'
			
			domain
				The domain the cookie is available on
				default: current domain
				
			secure
				Indicates that the cookie should only be transmitted over a secure HTTPS connection
				default: false (ie, insecure xmission allowed)
				
		RETURN
			false: cookie not set
			true: cookie set
			
		Credit due: based on http://techpatterns.com/downloads/javascript_cookies.php
		**** */
		
		if ((name == NUL) || (value == NUL)) {
			return FALS;
		}
		
		if (path == NUL) {
			path = '/';
		}
		
		var expires_date;
		//emulating php, so time is in seconds - change to milliseconds
		if (typeof expire == 'object') {
			//js time object
			expires_date = expire;
		}
		else if (expire != NUL) {
			//null or php time stamp
			expire = expire * 1000;
			expires_date = new Date(expire);
		}
		
		
		DOC.cookie = name + "=" +escape( value ) +
			( ( expire ) ? ";expires=" + expires_date.toGMTString() : "" ) +
			( ";path=" + path ) +
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );

	}
	
	function getCookie(name) {
		/* ****
		DESCRIPTION
			sets a cookie
			emulates the setCookie function in php
			
		INPUTS
			name
				The name of the cookie
				default: NULL/nil
		
		RETURN
			value of the cookie
			
		Credit due: based on http://techpatterns.com/downloads/javascript_cookies.php
		**** */
	
		// first we'll split this cookie up into name/value pairs
		// note: DOC.cookie only returns name=value, not the other components
		var a_all_cookies = DOC.cookie.split( ';' ),
			a_temp_cookie = '',
			cookie_name = '',
			cookie_value = '',
			b_cookie_found = FALS,
			allCookLength = a_all_cookies.length; // set boolean t/f default f

		for ( i = 0; i < allCookLength; i++ ) {
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );


			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

			// if the extracted name matches passed name
			if ( cookie_name == name ) {
				b_cookie_found = TRU;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 ) {
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = NUL;
			cookie_name = '';
		}
		if ( !b_cookie_found ) {
			return NUL;
		}
	
	}
	
	function popup(event,href,width,height,popupId,scrollbars,locationBar,toolbar,statusBar,resizable) {
		/* ****
		DESCRIPTION
			Ultra basic popup window
			- if stopped by popup blocker, send main window to page
			- bring popup into focus
			
		INPUTS
			event
				passed so jQuery can prevent the defaut event
				
			href
				Page to go to
				default: NULL/nil (exits if no value passed)
				
			width: window width (default: 500)
			height: popup height (default: 320)
			popupId: name of popup window (default: SGpopup)
			scrollbars: show scrollbars (default: yes) 
			locationBar: show location input (default: no)
			toolbar: show toolbar (default: no)
			statusBar: show status bar (default: no)
			resizable: allow user to resize (default: yes)
		RETURN
			NULL/nil
		**** */
		if (!href) {
			href = "/";
		}
		if (!width) {
			width = 500;
		}
		if (!height) {
			height = 320;
		}
		if (!popupId) {
			popupId = "SGpopup";
		}
		if (!scrollbars) {
			scrollbars = "yes";
		}
		if (!locationBar) {
			locationBar = "no";
		}
		if (!toolbar) {
			toolbar = "no";
		}
		if (!statusBar) {
			statusBar = "no";
		}
		if (!resizable) {
			resizable = "yes";
		}

		var idPopup = WIN.open(href,popupId,"width="+width+",height="+height+",scrollbars="+scrollbars+",location="+locationBar+",toolbar="+toolbar+",status="+statusBar+",resizable="+resizable);

		if (idPopup != NUL) {
			if (WIN.focus) {
				idPopup.focus();
			}
			event.preventDefault();
		}
		return NUL;
	}
	
	function compactForms() {
		
		function fieldFocus($label) {
			$label.addClass('active');
		}
		
		function fieldBlur($label, $field) {
			if ($field.val() == '') {
				$label.removeClass('active');
			}
		}
		
		var $networkForm = $('#networkSearch'),
			$networkLabel = $('label', $networkForm),
			$networkInput = $('input.searchInput', $networkForm),
			
			$searchForm = $('form.search-form'),
			$searchLabel = $('label', $searchForm),
			$searchInput = $('input.searchInput', $searchForm);
		
		//hide while page is loading
		fieldFocus($networkLabel);
		fieldFocus($searchLabel);
		
		//add events to forms
		$networkInput.focus(function(){fieldFocus($networkLabel);});
		$networkInput.blur(function(){fieldBlur($networkLabel, $networkInput);});
		
		$searchInput.focus(function(){fieldFocus($searchLabel);});
		$searchInput.blur(function(){fieldBlur($searchLabel, $searchInput);});
		
		//once page has loaded, wait 50ms and run blur event
		$(WIN).ready(function() {
			setTimeout(function() {
				fieldBlur($networkLabel, $networkInput);
				fieldBlur($searchLabel, $searchInput);
			},50);
		});
		

	}
	
	
	return {
		nav: nav,
		equalHeight: equalHeight,
		createStyleRule: createStyleRule,
		slideShow: slideShow,
		setCookie: setCookie,
		getCookie: getCookie,
		popup: popup,
		compactForms: compactForms
	};
}();
