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


// Method to set an 'example' value (text) for the 'element' using the 
// jQuery Form Example Plugin. Depends on: 'jQuery Form Example Plugin'
function setExample(element,text){
  $(element).example(text);
};

// Method to initialize all radio boxes and radiobuttons using the 'prettyCheckboxes'
// library. Depends on: 'prettyCheckboxes'
function initializePrettyCheckboxes() {
  $('.pretty_checkboxes[type=checkbox],.pretty_checkboxes[type=radio]').prettyCheckboxes({
    checkboxWidth: 20,
    checkboxHeight: 20
  });

  $('.pretty_checkboxes_releases[type=checkbox],.pretty_checkboxes_releases[type=radio]').prettyCheckboxes({
    checkboxWidth: 20,
    checkboxHeight: 20,
    validator: validateReleaseOption
  });
}

function setNotice(text) {
  $('.messages').html('<div class="notice"><p>' + text + '</p></div>');
  $('.messages').effect("pulsate", { times: 3 }, 2000).delay(4000).fadeOut(400);
  $.scrollTo('.messages');
}

function setError(text) {
  var message = '';
  if (typeof text == 'string') {
    message = text;
  } else {
    $.each(text, function(key, value) {
      message += '<p>' + value + '</p>';
    });
  }
  $('.messages').html('<div class="error"><p>' + message + '</p></div>');
  $('.messages').effect("pulsate", { times: 3 }, 2000); //.delay(10000).fadeOut(400);
  $.scrollTo('.messages');
}

// New extended function to jQuery to Prevent double submits
$.fn.preventDoubleSubmit = function(){
  $(this).submit(function() {
    if (this.beenSubmitted) {
      return false;
    } else { 
      this.beenSubmitted = true;
    }
  });
};

// http://media.railscasts.com/videos/136_jquery.mov

jQuery(document).ready(function(){
  
  if ($('.quickboard .logout').length > 0) {
    $('.quickboard .logout').click(function(e) {
      if (confirm('Are you sure you want to log out?')) {
        return true;
      } else {
        return false;
      }
    })
  }
  
  // initialize pretty checkboxes
  initializePrettyCheckboxes();

  $(".update_quantity_microform").submit(function() {
    $.post(jQuery(this).attr("action"), $(this).serialize(), null, "script") ;
    return false;
  });

  if (jQuery('#left_menu').length > 0) {
	  jQuery('#left_menu').corner();
  }

  if (jQuery('.quickboard .cart').length > 0) {
    jQuery('.quickboard .cart').corner("bottom");
  }
  
  // Content Menu Two - Top Rounded Corners
  if (jQuery('#content .menutwo li a').length > 0) {
    jQuery('#content .menutwo li a').corner("top");
    jQuery('#content .menutwo li ul li a').uncorner("top");
  }

  // Content Menu Footer - Top Rounded Corners
  if (jQuery('#content .menu_footer li a').length > 0) {
    jQuery('#content .menu_footer li a').corner("top");
    jQuery('#content .menu_footer li ul li a').uncorner("top");
  }
  
  // Menu Big Tabs
  if (jQuery('#content .menu_big_tabs li a').length > 0) {
    jQuery('#content .menu_big_tabs li a').corner("top");
  }

  if (jQuery('#content .menutwo li.selected .menuthree li').length > 0) {
    jQuery('#content .menutwo li.selected .menuthree li a:not(:last)').css("background", "url('/images/themes/default/content/menu_w_sub/bg-divider.png') no-repeat top right");
  }
  
  // Header - Boxes - Search Query Input - Background Changer
  jQuery('#header_search').addClass(jQuery('#footer_search').val() == '' ? 'off' : 'on');  
  jQuery('#header_search').focus(function() {
    if (jQuery('#header_search').val() == '') {
      jQuery(this).removeClass("off").addClass("on");  
    }
  });  
  jQuery('#header_search').blur(function() {  
    if (jQuery('#header_search').val() == '') {
      jQuery(this).removeClass("on").addClass("off");  
    }
  });

  // Footer - Search Query Input - Background Changer
  jQuery('#footer_search').addClass(jQuery('#footer_search').val() == '' ? 'off' : 'on');  
  jQuery('#footer_search').focus(function() {
    if (jQuery('#footer_search').val() == '') {
      jQuery(this).removeClass("off").addClass("on");  
    }
  });
  
  jQuery('#footer_search').blur(function() {  
    if (jQuery('#footer_search').val() == '') {
      jQuery(this).removeClass("on").addClass("off");  
    }
  });
  
  //> /video_guide/index
  if (jQuery('.video_thumbnail_element').length > 0) {
    jQuery('.video_thumbnail_element').corner('bottom');
  }
  
  // /checkout/cart Cart Contents
  jQuery('.cart .contents .row').mouseover(function() {
    jQuery(this).addClass('hover')
  }).mouseout(function() {
    jQuery(this).removeClass('hover')
  });
  
  jQuery('.home .info .titles .category').mouseover(function() {
    jQuery('.home .info .titles .category').removeClass('active');
    jQuery(this).addClass('active')
  }).mouseout(function() {
    if (jQuery('.home .info .titles .active').length <= 0) {
      jQuery('.home .info .titles .category:first').addClass('active');
    }
  });

  // Cart
  // TODO: Double click to go to the full cart listing at /cart
//  jQuery('.quickboard .cart a').dblclick(function(){
//    window.location = this.attr('href');
//  });
  
  var quickboard_cart_visible = false;
  jQuery('.quickboard .cart a, .button_small_cart_edit_options').click(function(event){
    event.preventDefault();
    
    if (quickboard_cart_visible == false) {
      quickboard_cart_visible = true;
      jQuery("#cart_condensed").fadeIn("slow");
    } else {
      quickboard_cart_visible = false;
      jQuery("#cart_condensed").fadeOut("fast");
    }
  });
  
  // Video - Show
  var video_description_long_visible = [];
  if ($('.video_description .button a').length > 0) {
    $('.video_description .button a').click(function(event) {
      event.preventDefault();
      
      var vid = $(this).attr('video_id');
      
      if (video_description_long_visible[vid] == true) {
        video_description_long_visible[vid] = false;
        $('#video_description_' + vid + ' .button_less').fadeOut("fast");
        $('#video_description_' + vid + ' .button_more').fadeIn("slow");
        $('#video_description_' + vid + ' .long').hide(1, function() {
          $('#video_description_' + vid + ' .short').fadeIn("slow");
        });
      } else {
        video_description_long_visible[vid] = true;
        $('#video_description_' + vid + ' .button_less').fadeIn("slow");
        $('#video_description_' + vid + ' .button_more').fadeOut("fast");
        $('#video_description_' + vid + ' .short').hide(1, function() {
          $('#video_description_' + vid + ' .long').fadeIn("slow");
        });
      }
    });
  }
});