/* == Begin Functions ================================================= */

$(function() {
	
	$("q").prepend("&ldquo;").append("&rdquo;");
	$("q q").prepend("&lsquo;").append("&rsquo;");
	// Add classes to input pseudotypes
	$("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 classes to link pseudotypes
	$("a[href^='mailto:']").addClass("mailto");
	$("a[href$='.pdf']").addClass("docpdf");
	$("a[href$='.swf']", "a[href$='.fla']", "a[href$='.swd']").addClass("docflash");
	$("a[href$='.xls']", "a[href$='.csv']", "a[href$='.xlt']", "a[href$='.xlw']").addClass("docexcel");
	$("a[href$='.ppt']", "a[href$='.pps']").addClass("docppoint");
	$("a[href$='.doc']", "a[href$='.rtf']", "a[href$='.txt']", "a[href$='.wps']").addClass("docword");
	$("a[href$='.zip']", "a[href$='.gzip']", "a[href$='.rar']").addClass("doczip");

	// Add Class Last to Each Last Child
	$('li:last-child').addClass('last');
	
	// Take focus off the body tag
	$('body').prepend('<input class="hidden" type="text" id="focus-stealer" type="hidden" />');
	$("#focus-stealer").focus();
			
	// Wrap <hr> so bg-image doesn't have a border in IE6 and Opera
	$("hr").wrap('<div class="hr"></div>');
	
	// Home Page Slideshow
	$('.anythingSlider').anythingSlider({
		easing: "swing",
		delay: 6000,
		buildNavigation: true,
		pauseOnHover: true
	});
	
	/* Apply special styles to forms, to activate add class "cmxform" to a fieldset */
	cmxform();
	
	// choose text for the show/hide link - can contain HTML (e.g. an image)
	var showText='Show More Information';
	var hideText='Hide';
	// append show/hide links to the element directly preceding the element with a class of "toggle"
	jQuery('.toggle-content').prev().append('<a href="#" class="toggle-switch">'+showText+'</a>');
	// hide all of the elements with a class of 'toggle'
	jQuery('.toggle-content').hide();
	// capture clicks on the toggle links
	jQuery('a.toggle-switch').click(function() {
	// change the link depending on whether the element is shown or hidden
	jQuery(this).html (jQuery(this).html()==hideText ? showText : hideText);
	// toggle the display - uncomment the next line for a basic "accordion" style
	jQuery(this).parent().next('.toggle-content').slideToggle();
		// return false so any link destination is not followed
		return false;
	});
	
	// CSS Sprites 2 - http://www.alistapart.com/articles/sprites2/
	// remove link background images since we're re-doing the hover interaction below 
	// (doing it this way retains the CSS default hover states for non-javascript-enabled browsers)
	// we also want to only remove the image on non-selected nav items, so this is a bit more complicated
	$("#sub .sidebar li").children("h2").each(function() {
		var current = "sidebar " + ($(this).attr("class"));
		var parentClass = $(".sidebar").attr("class");
		if (parentClass != current) {
			$(this).children("a").css({backgroundImage:"none"});
		}
	});	

	// create events for each nav item
	attachNavEvents(".sidebar", "widgettitle");

	function attachNavEvents(parent, myClass) {
		$(parent + " ." + myClass).mouseover(function() {
			$(this).append('<div class="nav-' + myClass + '"></div>');
			$("div.nav-" + myClass).css({display:"none"}).fadeIn(300);
		}).mouseout(function() {
			$("div.nav-" + myClass).fadeOut(200, function() {
				$(this).remove();
			});
		}).mousedown(function() {
			$("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click");
		}).mouseup(function() {
			$("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);
		});
	}
	
	// Add class="obfuscated" and href="mailto:name(at)example.com" to an email address link
	$('.obfuscated').defuscate();
	
	// Marquee on home page
	$('#home-content marquee').marquee('pointer').mouseover(function () {
            $(this).trigger('stop');
        }).mouseout(function () {
            $(this).trigger('start');
        }).mousemove(function (event) {
            if ($(this).data('drag') == true) {
                this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
            }
        }).mousedown(function (event) {
            $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
        }).mouseup(function () {
            $(this).data('drag', false);
        });
	
});
/* ==================================================================== */

/* == Begin jQuery plugins ============================================ */

/*
 * Email Defuscator - jQuery plugin 1.0 alpha
 *
 * Copyright (c) 2007 Joakim Stai
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id$
 *
 */

/**
 * Converts obfuscated email addresses into normal, working email addresses.
 *
 * @name defuscate
 * @param Boolean link If true, all defuscated email addresses will be turned into links, defaults to true (optional)
 * @descr Converts obfuscated email addresses into normal email addresses
 */
 
jQuery.fn.defuscate=function(b){b=jQuery.extend({link:true},b);var c=/\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;return this.each(function(){if($(this).is('a[@href]')){$(this).attr('href',$(this).attr('href').replace(c,'$1@$2'));var a=true}$(this).html($(this).html().replace(c,(b.link&&!a?'<a href="mailto:$1@$2">$1@$2</a>':'$1@$2')))})}

/* ==================================================================== */

jQuery.fn.doFade = function(settings) {

    // if no paramaters supplied...
   settings = jQuery.extend({
      fadeColor: "black",
      duration: 200,
      fadeOn: 0.95,
      fadeOff: 0.65
   }, settings);

    var duration = settings.duration;
    var fadeOff = settings.fadeOff;
    var fadeOn = settings.fadeOn;
    var fadeColor = settings.fadeColor;

    jQuery(this).hover(function(){
     jQuery(this)
         .stop()
         .data("origColor", jQuery(this).css("background-color"))
         .animate({
             opacity: fadeOn,
             backgroundColor: fadeColor
         }, duration)
   }, function() {
     jQuery(this)
         .stop()
         .animate({
             opacity: fadeOff,
             backgroundColor: jQuery(this).data("origColor")
         }, duration)
   });

};

this.randomizer = function(){
	var length = jQuery("#randomizer li").length;
	var ran = Math.floor(Math.random()*length) + 1;
	jQuery("#randomizer li:nth-child(" + ran + ")").show();
};

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false );

function cmxform(){
  // Hide forms
  jQuery( 'fieldset.cmxform' ).hide().end();
  
  // Processing
  jQuery( 'fieldset.cmxform' ).find( 'li label' ).not( '.nocmx' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild( labelSpan );
  } ).end();
  
  // Show forms
  jQuery( 'fieldset.cmxform' ).show().end();
}

// IE6 fixes
// make sure IE has the abbr and acronym tag
if(document.all){
	document.createElement("abbr");
	document.createElement("acronym");
}

// javascript method: "pxToEm"
eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(4($){k.9.7=5.9.7=4(a){3 b=p;2(b.j==5)b=d(b);2(!a)3 a=$(\'g\');2($(a).6()>0){3 c=b*(1/(d($(a).r("n-6"))))+\'m\';8 c}l 8\'i: o h q f s a e t\'}})(u);',31,31,'||if|var|function|String|size|pxToEm|return|prototype||||parseInt|DOM|is|body|scope|Error|constructor|Number|else|em|font|Provided|this|argument|css|not|element|jQuery'.split('|'),0,{}));

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:
   Scott Jehl (scott@filamentgroup.com) 
   Todd Parker (todd@filamentgroup.com)
   http://www.filamentgroup.com
 *
 * Copyright (c) 2007 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights of the first-children of a provided element 
 								  and sets their min-height to the tallest height. Sets in em units by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method								  
 * Usage Example: $(element).equalHeights();
   						      Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 1.0, 08.02.2007
 * Changelog:
 *  08.02.2007 initial Version 1.0
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
				if($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		if(!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm($(this)); //use ems unless px is specified or 
		// for ie6, set height since min-height isn't supported
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
		if ($.browser.msie && (ie6)) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return $(this);
};

document.documentElement.className = "js";

/**
* author Remy Sharp
* url http://remysharp.com/tag/marquee
*/
(function(a){a.fn.marquee=function(b){var e=[],d=this.length;function c(m,k,l){var j=l.behavior,h=l.width,g=l.dir;var i=0;if(j=="alternate"){i=m==1?k[l.widthAxis]-(h*2):h}else{if(j=="slide"){if(m==-1){i=g==-1?k[l.widthAxis]:h}else{i=g==-1?k[l.widthAxis]-(h*2):0}}else{i=m==-1?k[l.widthAxis]:0}}return i}function f(){var h=e.length,j=null,m=null,l={},k=[],g=false;while(h--){j=e[h];m=a(j);l=m.data("marqueeState");if(m.data("paused")!==true){j[l.axis]+=(l.scrollamount*l.dir);g=l.dir==-1?j[l.axis]<=c(l.dir*-1,j,l):j[l.axis]>=c(l.dir*-1,j,l);if((l.behavior=="scroll"&&l.last==j[l.axis])||(l.behavior=="alternate"&&g&&l.last!=-1)||(l.behavior=="slide"&&g&&l.last!=-1)){if(l.behavior=="alternate"){l.dir*=-1}l.last=-1;m.trigger("stop");l.loops--;if(l.loops===0){if(l.behavior!="slide"){j[l.axis]=c(l.dir,j,l)}else{j[l.axis]=c(l.dir*-1,j,l)}m.trigger("end")}else{k.push(j);m.trigger("start");j[l.axis]=c(l.dir,j,l)}}else{k.push(j)}l.last=j[l.axis];m.data("marqueeState",l)}else{k.push(j)}}e=k;if(e.length){setTimeout(f,25)}}this.each(function(j){var n=a(this),g=n.attr("width")||n.width(),o=n.attr("height")||n.height(),p=n.after("<div "+(b?'class="'+b+'" ':"")+'style="display: block-inline; width: '+g+"px; height: "+o+'px; overflow: hidden;"><div style="float: left; white-space: nowrap;">'+n.html()+"</div></div>").next(),m=p.get(0),k=0,l=(n.attr("direction")||"left").toLowerCase(),h={dir:/down|right/.test(l)?-1:1,axis:/left|right/.test(l)?"scrollLeft":"scrollTop",widthAxis:/left|right/.test(l)?"scrollWidth":"scrollHeight",last:-1,loops:n.attr("loop")||-1,scrollamount:n.attr("scrollamount")||this.scrollAmount||2,behavior:(n.attr("behavior")||"scroll").toLowerCase(),width:/left|right/.test(l)?g:o};if(n.attr("loop")==-1&&h.behavior=="slide"){h.loops=1}n.remove();if(/left|right/.test(l)){p.find("> div").css("padding","0 "+g+"px")}else{p.find("> div").css("padding",o+"px 0")}p.bind("stop",function(){p.data("paused",true)}).bind("pause",function(){p.data("paused",true)}).bind("start",function(){p.data("paused",false)}).bind("unpause",function(){p.data("paused",false)}).data("marqueeState",h);e.push(m);m[h.axis]=c(h.dir,m,h);p.trigger("start");if(j+1==d){f()}});return a(e)}}(jQuery));

