/*This will add some nice behavior to form inputs and text fields
  add the class "clear-text" to the input or text-field to include
  this behavior*/
function clearText(field){
  if (field.defaultValue == field.value) field.value = '';
  else if (field.value == '')
    field.value = field.defaultValue;
}
function initFormTextClear(){
  jQuery("input[type=text].clear-text, textarea.clear-text").blur(
    function(event){clearText(event.target);}
  );
  jQuery("input[type=text].clear-text, textarea.clear-text").focus(
    function(event){clearText(event.target);}
  );
}


function loginText(field){
  if (field.value == ''){
    jQuery(field).removeClass('login-over');
  }
  else{
    jQuery(field).addClass('login-over');
  }
}
function initLoginTextClear(){
  jQuery("input[type=text].login-box, input[type=password].login-box").blur(
    function(event){loginText(event.target);}
  );
  jQuery("input[type=text].login-box, input[type=password].login-box").focus(
    function(event){jQuery(event.target).addClass('login-over');}
  );
  jQuery("input[type=text].login-box, input[type=password].login-box").each(
    function(intIndex){
      setTimeout(
        function(){
          //console.log(jQuery(this).val());
          if(jQuery(this).value != ''){
            jQuery(this).addClass('login-over');
          }
        }
        , 1000
      );
    }
  );
}

/*Adds list classes for first and last list elements*/
function initListClasses(){
  jQuery('ul li:first-child').addClass( 'list-first' );
  jQuery('ul li:last-child').addClass( 'list-last' );
  jQuery('ol li:first-child').addClass( 'list-first' );
  jQuery('ol li:last-child').addClass( 'list-last' );
  jQuery('.order-it *:first-child').addClass( 'first-element' );
  jQuery('.order-it *:last-child').addClass( 'last-element' );
}
/*Adds the drop down menu*/
function initSuperfish(){
 jQuery("ul.sf-menu").superfish({delay:200, speed:'fast'}).find('ul').bgIframe({opacity:false});
}
/*Does cool input effects*/
function initInputEffects(){

  // Select all textboxes and assign them to an array
  var textboxes = jQuery('input.input-text, textarea.input-text');

  // Iterate through all textboxes in the form
  textboxes.each(function(index){
  
		var input = jQuery(this);
    var label = jQuery(this).prev();

    // Fade the label back when a field gains focus		
    input.focus(function(){
      if (input.val()==""){
        label.addClass('focus');            
      }
    });

    // Check if a field is empty when the user switches out
   input.blur(function(){
      if (input.val()==""){
        label.removeClass('focus').removeClass('hastext');          
      }
    });

    // Fade the label back if a field has text		
    if (!input.val()=="") {
      label.addClass('hastext');
    }

    // Fade the label back when the user starts to type		
    input.keypress(function(){
      label.addClass('hastext');
    });
  });

}

function initQTip(){
	jQuery('.qtip').each(function() {
		jQuery(this).qtip({
			content: getContent(jQuery(this).attr('alt')),
			position: {
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				},
				//target: 'mouse',
				adjust: {
					y: -1
				}
			},
			show: {
				when: 'mouseover',
				solo: false,
				delay: 0,
				effect: {
					length: 100
				}
			},
			hide: { 
				effect: {
					length: 200
				}
			},
			style: {
				tip: {
					corner: 'bottomMiddle',
					size : {
						x: 6,
						y: 6
					}
				},
				border: {
					width: 1,
					radius: 1
				},
				padding: 5,
				width: 'auto',
				textAlign: 'left',
				name: 'light'
			}
		});
	});
}
//split title and subtitle for rollovers
function getContent(txt) {
	var parts = txt.split('|');
	var htmlTip = '<div class="qtip-inner">';
	jQuery.each(parts, function(intIndex, objValue){
	 htmlTip = htmlTip + '<span class="qtip-element qtip-element-' + intIndex +'">' + objValue + '</span>';
	});
	htmlTip = htmlTip + '</div>';
  return htmlTip;
}

function initFont(){
  Cufon.replace('.cufon, h1, h3, h2, blockquote', {hover: true});
}

function initCaptify(){
	jQuery('img.captify').captify({
		// all of these options are... optional
		// ---
		// speed of the mouseover effect
		speedOver: 'fast',
		// speed of the mouseout effect
		speedOut: 'normal',
		// how long to delay the hiding of the caption after mouseout (ms)
		hideDelay: 200,	
		// 'fade', 'slide', 'always-on'
		animation: 'slide',		
		// text/html to be placed at the beginning of every caption
		prefix: '',		
		// opacity of the caption on mouse over
		opacity: '0.7',					
		// the name of the CSS class to apply to the caption box
		className: 'caption-bottom',	
		// position of the caption (top or bottom)
		position: 'bottom',
		// caption span % of the image
		spanWidth: '100%'
	});
}
function initCycle(){
  jQuery('#slideshow').cycle({
  	fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
  });
}
jQuery(document).ready(function() {
  initFormTextClear();  
  initLoginTextClear();
  //initQTip();
  initListClasses();
  initFont();
  initCycle();
  //initCaptify();
  //initSuperfish();
  //initInputEffects();
});
