/* ============================================================
Democracy Data & Communications (DDC) - Global JavaScript (with JQuery Help)
---
*  Created by Viget Labs on 2009-08-27.
*  Copyright (c) 2009 Democracy Data & Communications (DDC). All rights reserved.
============================================================ */

// ========================================
// Utility functions (Not Loaded with DOM)
// ========================================

// FadeToggle
$.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

// SlideToggle
$.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

// Get the Index of an Element
$.fn.getIndex = function(){
  var parentSet=$(this).parent().children();
  return $(parentSet).index(this);
};


$(document).ready(function(){
	if($('#carousel').length){
		$('#carousel').after('<div id="nav">').cycle({
		    fx:     'fade', 
		    speed:  'fast', 
		    timeout: 7000, 
				pager:  '#nav',
				cleartypeNoBg: true
		});
	}

// ========================================
// Utility functions (Loaded with DOM)
// ========================================
	
  // Fading
	$.fn.fadeDown = function(speed) {  
     return $(this).animate({	opacity: 0.01	}, speed);
	};
	$.fn.fadeUp = function(speed) {  
     return $(this).animate({	opacity: 1 }, speed);
	};
	
	
	// Input focus and blur
	$("input, textarea", $("form")).focus(function(){
      $(this).addClass("focus");
      $(this).parents(".form-field").addClass("cur");
  });
  $("input, textarea", $("form")).blur(function(){
      $(this).removeClass("focus");
      $(this).parents(".form-field").removeClass("cur");
  });


	// Extra Content
	$("div.extra").each(function(){
		$(this).hide().prev().append('<a href="#" class="show-extra"> More &hellip;</a>');	
	});
	$("a.show-extra").click(function(){
		if ($(this).hasClass("hide-extra")){
		  $(this).text(" More &hellip;").addClass("show-extra").parent().next().toggle();
		} else {
	  	$(this).text(" Less &hellip;").addClass("hide-extra").parent().next().toggle();
		}
		return false;
	});
	
	
  // Wrap Your HRs For Easy Styling
	$("hr").each(function() {
    var hrClass = jQuery(this).attr("class");
    $(this).wrap("<div class='hr " + hrClass +"'></div>");
  });
  
  
  // Wrap the Inside of Button Elements
  $("button, .button").each(function() {
    var hasSpan = $(this).find("span")[0];
    if (hasSpan = "undefined") {
      $(this).wrapInner("<span></span>");
    }
  });
  
  
  // Delaying Elements
  $.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
  };

	
	$('#nav_primary li ul').hide();
   $('#nav_primary li').hover(function() {
     $(this).addClass('hover').children('ul').fadeIn();
			
   }, function() {
     $(this).removeClass('hover').children('ul').hide();

   });
  if ( $('a[rel="lightbox"], a.lightbox').length > 0 ) 
	  $('a[rel="lightbox"], a.lightbox').lightBox(); // Select all links that contains lightbox in the attribute rel
   	
});

$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};
