// Input Focus
(function ($) {
  $.extend($.fn, {
    clearingInput: function (options) {
      var defaults = {blurClass: 'blur'};

      options = $.extend(defaults, options);

      return this.each(function () {
        var input = $(this).addClass(options.blurClass);
        var form  = input.parents('form:first');
        var label, text;

        text = options.text || textFromLabel() || input.val();

        if (text) {
          input.val(text);

          input.blur(function () {
            if (input.val() === '') {
              input.val(text).addClass(options.blurClass);
            }
          }).focus(function () {
            if (input.val() === text) {
              input.val('');
            }
            input.removeClass(options.blurClass);
          });

          form.submit(function() {
            if (input.hasClass(options.blurClass)) {
              input.val('');
            }
          });

          input.blur();
        }

        function textFromLabel() {
          label = form.find('label[for=' + input.attr('id') + ']');
          // Position label off screen and use it for the input text
          return label ? label.css({position: 'absolute', left: '-9999px'}).text() : '';
        }
      });
    }
  });
})(jQuery);

//	jflow slideshow
(function($) {

	var rotateSpeed = 7000;
	
	$.fn.jFlow = function(options) {
		var opts = $.extend({}, $.fn.jFlow.defaults, options);
		var randNum = Math.floor(Math.random()*11);
		var jFC = opts.controller;
		var jFS =  opts.slideWrapper;
		var jSel = opts.selectedWrapper;
		var rotate = true;
		var cur = 0;
		var maxi = $(jFC).length;
		
		rotateSpeed = opts.rotateSpeed;
		
		// sliding function
		var slide = function (dur, i) {
						
			$(opts.slides).children().css({
				overflow:"hidden"
			});
			
			$(opts.slides + " iframe").hide().addClass("temp_hide");
			
			$(opts.slides).animate({
								   
				marginTop: "-" + (i * $(opts.slides).find(":first-child").height() + "px")}, 
				opts.duration*(dur),
				opts.easing,
				function(){
					$(opts.slides).children().css({
						overflow:"auto"
					});
				
					$(".temp_hide").show();
					
					resizeCont();
					
				}
			);
			
			
		}

		$(this).find(jFC).each(function(i){
			$(this).click(function(){
				if ($(opts.slides).is(":not(:animated)")) {

					rotate = false;
					
					/*$(jFS).find('.jFlowSlideContainer:eq('+ cur +')').animate({
								   opacity: 0
								   }, 150);*/
					
					$(jFC).removeClass(jSel);
					$(this).addClass(jSel);
					var dur = Math.abs(cur-i);
					slide(dur,i);
					cur = i;
					/*$(jFS).find('.jFlowSlideContainer:eq('+ cur +')').animate({
								   opacity: 1
								   }, 150);*/
					
				}
			});
		});	
		
		var resizeCont  = function () {
			
			}
		
		$(opts.slides).before('<div id="'+jFS.substring(1, jFS.length)+'"></div>').appendTo(jFS);
		
		$(opts.slides).find("div.slide").each(function(){
			$(this).before('<div class="jFlowSlideContainer"></div>').appendTo($(this).prev());
			
		});
		
		//initialize the controller
		$(jFC).eq(cur).addClass(jSel);
		
		var resize = function (x){
			$(jFS).css({
				position:"relative",
				width: opts.width,
				height: opts.height,
				overflow: "hidden"
			});
			//opts.slides or #mySlides container
			$(opts.slides).css({
				position:"relative",
				width: $(jFS).width()*$(jFC).length+"px",
				height: $(jFS).height()+"px"
				//overflow: "hidden"
			});
			// jFlowSlideContainer
			$(opts.slides).children().css({
				position:"relative",
				width: $(jFS).width()+"px",
				height: $(jFS).height()+"px",
				"float":"left",
				overflow:"auto"
			});
			
			$(opts.slides).css({
				marginTop: "-" + (cur * $(opts.slides).find(":eq(0)").height() + "px")
			});
		}
		
		// sets initial size
		resize();

		// resets size
		$(window).resize(function(){
			resize();						  
		});
		
		$(opts.prev).click(function(){
									
			if ($(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur > 0)
					cur--;
				else {
					cur = maxi -1;
					dur = cur;
				}
				$(jFC).removeClass(jSel);
				slide(dur,cur);
				$(jFC).eq(cur).addClass(jSel);
				
			}
		});
		
		$(opts.next).click(function(){
			if ($(opts.slides).is(":not(:animated)")) {
				var dur = 1;
				if (cur < maxi - 1)
					cur++;
				else {
					cur = 0;
					dur = maxi -1;
				}
				$(jFC).removeClass(jSel);
				slide(dur, cur);
				$(jFC).eq(cur).addClass(jSel);
			}	
		});
		
		$.fn.jFlow.autoRotate = function() {
			if ((rotate) && ($(opts.slides).is(":not(:animated)"))) {
				var nextItem = $('.jFlowSelected').next('.jFlowControl');
				if (nextItem.get(0) == undefined) {
					nextItem = $('#controller .jFlowControl:first');
				}
				//nextItem.click();
				var dur = 1;
				if (cur < maxi - 1)
					cur++;
				else {
					cur = 0;
					dur = maxi -1;
				}
				$(jFC).removeClass(jSel);
				slide(dur, cur);
				$(jFC).eq(cur).addClass(jSel);
				setTimeout('$.fn.jFlow.autoRotate()', rotateSpeed);
			}
		}
		
		setTimeout('$.fn.jFlow.autoRotate()', rotateSpeed);
	};
	
	$.fn.jFlow.defaults = {
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		easing: "swing",
		duration: 5000,
		rotateSpeed: 7000,
		width: "100%",
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext" // must be class, use . sign
	};
	
	

	
})(jQuery);



// bigTarget
(function($) {
	$.fn.bigTarget = function(options) {
		debug(this);
		var opts = $.extend({}, $.fn.bigTarget.defaults, options);
		return this.each(function() {
			var $a = $(this);
			var href = $a.attr('href');
			var title = $a.attr('title');
			var o = $.meta ? $.extend({}, opts, $a.data()) : opts;
			
//			$a.parents(o.clickZone).animate({opacity: .7}, 0);

			//$a.parents(o.clickZone).find('h2 a, p').each(function (){$(this).animate({opacity: .65}, 0);})

			$a.parents(o.clickZone)
				.hover(function() {
					$h = $(this);
					$h.addClass(o.hoverClass);
					
					if(typeof o.title != 'undefined' && o.title === true && title != '') {
						$h.attr('title',title);
					}
					//$h.find('h2 a, p').each(function (){$(this).animate({opacity: 1}, 250);})
					
					/*$h.stop().animate({
						backgroundPosition:"(0 0)"
					}, 250);*/
				
				}, function() {
					$h.removeClass(o.hoverClass);
					if(typeof o.title != 'undefined' && o.title === true && title != '') {
						$h.removeAttr('title');
					}
					//$h.find('h2 a, p').each(function (){$(this).animate({opacity: .65}, 250);})
					
					//$h.animate({opacity: .75}, 125);
					/*$h.stop().animate({
						backgroundPosition:"(0 -50px)"
					}, 250);*/
				})
			
				.click(function() {
					if(getSelectedText() == "")
					{
						if(($a.is('[rel*=external]'))||($a.is('[target*=_blank]'))){
							window.open(href);
							return false;
						}
						else {
							window.location = href;
						}
					}
				});
		});
	};
	function debug($obj) {
		if (window.console && window.console.log)
		window.console.log('bigTarget selection count: ' + $obj.size());
	};

	function getSelectedText(){
		if(window.getSelection){
			return window.getSelection().toString();
		}
		else if(document.getSelection){
			return document.getSelection();
		}
		else if(document.selection){
			return document.selection.createRange().text;
		}
	};
	
	$.fn.bigTarget.defaults = {
		hoverClass	: 'hover',
		clickZone	: 'li:eq(0)',
		title		: true
	};
})(jQuery);

// coda bubbles
(function ($) {
  $('.bubbleInfo').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 200;
	
	var l = $(this).find('.rep a');
	var p = l.position().left + l.width()/2 - 73;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.rep a', this);
    var popup = $('.repBubble', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    
	
	$([trigger.get(0), popup.get(0)]).click(function () {
   
      // stops the hide event if we move from the trigger to the popup element
   
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
   
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
   
        popup.css({
          top: -45,
          left: p,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
   
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
   
          // once the animation is complete, set the tracker variables
   
          beingShown = false;
          shown = true;
        });
      }
   
      return false;
   
    })
    
   /*.mouseout(function () {
   
      // reset the timer if we get fired again - avoids double animations
   
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
   
      hideDelayTimer = setTimeout(function () {
   
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
   
          // once the animate is complete, set the tracker variables
   
          shown = false;
   
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
   
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
    
    */
  });  
});