// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//  Suckerfish for jQuery
//	by Adam Berlin (adamberlin.net)
var jSucker = function(css_id, css_class){
		return {
			
			css_class: "",
			css_id: "",
			
			init: function( css_id, css_class ){
				// if this is ie6
				if( document.all && document.getElementById){
					this.css_id = css_id;
					this.css_class = css_class;
					var lis = css_id + " li";
					jQuery(lis).mouseover( this.over );
					jQuery(lis).mouseout( this.out );
				}
			},
			
			over: function(){
				jQuery(this).addClass(jSucker.css_class);
			},
			
			out: function(){
				jQuery(this).removeClass(jSucker.css_class);
			}
			
		};
}();

var init_sucker = function(){
	jSucker.init("#kdrive-actions", "over");
}

var init_grid_toggle = function() {
  $(document).bind('keydown', 'alt+g', function() {
    if( "" == $('#header').css('background') ) {  
      $("#header,#photobar,#footer,.ground,.bleed-outer .ground").css(
        "background", "transparent url(/images/grid-w-baseline.png) 50% 0 repeat-y"
      );
    } else {
      $("#header,#photobar,#footer,.ground,.bleed-outer .ground").css(
        "background", ""
      );
    }
  });
}

var init_form_field_watermarks = function() {
  $.watermark.options.className = 'form-field-watermark';
  $('input,textarea').each( function() {
    if( $(this).attr('title') ) {
      $(this).watermark($(this).attr('title'));
    }
  });
}

var init_fade_in_class = function() {
  $(".fade-in").css('display', 'none');
  if(typeof init_image_rotate == 'function') {
    $(".fade-in").fadeIn(500, init_image_rotate);
  } else {
    $(".fade-in").fadeIn(500);
  }
}

    function photoFadeIn(photoCollectionSelector, delay, speed) {
      if( $(photoCollectionSelector).length > 0 ) {
        $(photoCollectionSelector).each( function(index, element) {
          setTimeout("photoDelayedFadeIn('" + photoCollectionSelector + "', " + speed + ", " + index + ")", index * delay);
        });
      }
    }

    function photoDelayedFadeIn(photoCollectionSelector, speed, elementIndex) {
      $(photoCollectionSelector).each( function(index, element) {
        if( index == elementIndex ) { 
          $(element).fadeIn(speed);
        }
      });
    }

var init_testimonials_slideshow = function(containerSelector) {
  Cufon.replace('.testimonial-text p');
  if( testimonial_data.length > 0 ) {
    setTimeout("testimonialSlideshow('" + containerSelector + "', 0)", 500);
  }
}

  function testimonialSlideshow(containerSelector, index) {

    var fadeInSpeed = 500; // mills
    var fadeOutSpeed = 500;
    var slideDelay = 10000;

    $(containerSelector).children().remove();
    var src = testimonial_data[index].src;
    var text = testimonial_data[index].text;
    var image = $('<img/>').attr('src', src).addClass('testimonial-image').addClass('hide');
    var textDiv = $('<div/>').addClass('testimonial-text').addClass('hide').html(text).css('left', '50px');
    $(containerSelector).append(image).append(textDiv);
    Cufon.replace('.testimonial-text p');
    
    textDiv.animate({ left: "-=50px", "opacity": "toggle"}, fadeInSpeed, function() {
      textDiv.delay(slideDelay).fadeOut(fadeOutSpeed);
    });

    image.fadeIn(fadeInSpeed).delay(slideDelay).fadeOut(fadeOutSpeed, function() {
      $(containerSelector).children().remove();
      index++;
      if( index >= testimonial_data.length ) { 
        index = 0;
      }   
      setTimeout("testimonialSlideshow('" + containerSelector + "', " + index + ")", 50);
    });

  }
  
$(document).ready(init_sucker);
$(document).ready(init_grid_toggle);
$(document).ready(init_form_field_watermarks);
$(document).ready(init_fade_in_class);

