jQuery.noConflict();

jQuery(document).ready(function() {

  if ( typeof(spConfig) == 'object' ) {
    spConfig.config.chooseText = "Choose size...";
    jQuery("#attribute497 option[value='']").text("Choose size...");
  }
	
  var global_switch_color = 0;

  // Create overlay and append to body:
  jQuery('<div id="overlay"/>').css({
      position: 'fixed',
      top: 0,
      left: 0,
      width: '100%',
      height: jQuery(window).height() + 'px',
      background: '#444 url(/skin/frontend/default/fiveredwoods/images/loadingAnimation.gif) no-repeat center',
      opacity: 0.5,
      'z-index': 1000
  }).hide().appendTo('body');
  
  jQuery('.category-gear .av-colors').remove();
  
  
  // Preload email to friend popup
  if (jQuery('#email-to').length)
  {
	  jQuery('body.catalog-product-view').append('<div class="email-to-friend-load" style="display: none;"></div>');
	  jQuery('.email-to-friend-load').load(
	    jQuery('#email-to').attr('href') + ' .send-to-email-wrapper > form',
	    {},
	    function() {
	      jQuery('.email-to-friend-load > form').addClass('popup').hide();
	      jQuery('.email-to-friend-load').show();
	      
	      var productSendtofriendForm = new VarienForm('product_sendtofriend_form');
	      jQuery("#product_sendtofriend_form").submit(function(){
	        if ( jQuery("#product_sendtofriend_form .validation-failed").length == 0 ) {
	          jQuery("#overlay").fadeIn(500);
	          jQuery("#product_sendtofriend_form").ajaxSubmit({
	            success: function( responseText, responseStatus ) {
	              var answer = jQuery(responseText);
	              var errors = answer.find('.messages .error-msg').parent().html();
	              var success = answer.find('.messages .success-msg').parent().html();
	              jQuery(".email-to-friend-load .popup .content > div .messages").remove();
	              if (success) {
	                jQuery(".email-to-friend-load .popup .content > div").eq(1).remove();
	                jQuery(".email-to-friend-load .popup .content > div h4").remove();
	                jQuery(".email-to-friend-load .popup .content > div ul").remove();
	                jQuery(".email-to-friend-load .popup .content > div").eq(0).append('<div class="email-send-thankyou">Thank You!</div>');
	
	                setTimeout("jQuery('.popup').fadeOut(1000)", 2000);
	              } else {
	                jQuery(".email-to-friend-load .popup .content > div h3").after('<ul class="messages"></ul>');
	                jQuery(".email-to-friend-load .popup .content > div .messages").html( errors );
	
	              }
	              jQuery("#overlay").fadeOut(500);
	            }
	          });
	        }
	        return false;
	      });
	    }
	  );
	  
	  jQuery('#email-to').click(
	    function(){
	        jQuery('.popup').show();
	        jQuery('.popup .remove-buttons').click(function(){jQuery('.popup').fadeOut(500);});
	        return false;
	    }
	  );
  }
  
  jQuery('#colors-details-tab ul, #colors ul').tabs();
  
  jQuery("a.fancybox").fancybox();
  
    jQuery('#home-slider ul').jqGalScroll({auto:true, delay:3400});


  jQuery('.switch-main-image').click(function(){
    var img = this;
    jQuery('#img-large a').attr('href', jQuery(img).attr('rel'));
    jQuery('#img-large a img').eq(1).attr('src', jQuery(img).attr('href'));
    jQuery('#colors-details-content li.over').removeClass('over');
    jQuery(img).parent().addClass('over');

    return false;
  });

  jQuery('#colors ul li a').click(function(){
    var href = jQuery(this).attr('href');
    var id = href.replace(/.+-(\d+)/, '$1');

    if ( global_switch_color == 0 ) {
      global_switch_color = 1;
      jQuery("#attribute76").val( id );
      fireEvent( document.getElementById('attribute76'), 'change' );
      global_switch_color = 0;
    }
    
    jQuery(href + " a:first").click();
  });

  jQuery('#attribute76').change(function(){
    var color = jQuery(this).val();
    if ( color > 0 ) {
      jQuery('#colors > ul > li > a[href="#color-images-'+color+'"]').click();
    }
  });

  // AJAX submit for the form;
  // #newsletter-validate-detail-footer
  jQuery("#newsletter-validate-detail-footer").submit(function() {
    if ( jQuery("#newsletter-validate-detail-footer input.validation-failed").length == 0 ) {
      jQuery("#overlay").fadeIn(500);
      jQuery("#newsletter-validate-detail-footer").ajaxSubmit({
        success: function() {
          jQuery("#overlay").fadeOut(500);
          jQuery("#newsletter").html('<div class="newsletter-thankyou-ajax">Thank you</div>');
        }
      });
    }
    return false;
  });

  jQuery("#qty").keypress(function (e) {
    //if the letter is not digit then display error and don't type anything
    if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
      return false;
    }
  });
  
  
  jQuery('.color-image-gallery .color-images:last-child').addClass('color-images-last-child');
  
  
  /**
   * External links
   */
  jQuery('a')
  	.not('[href^=/]')
  	.not('[href^=#]')
  	.not('[href^=javascript:void(0)]')
  	.not("[href*='fiveredwoods.com/']")
  	.attr('target', '_blank');

});

var i = 0;
var recipCount  = 1;
var maxRecip    = 10;


function remove_recipient(i){
    $('recipients_name'+i).up(2).remove();
    recipCount--;
    if(recipCount<maxRecip && maxRecip != 0) {
        $('add_recipient_button').show();
        $('max_recipient_message').hide();
    }
    return false;
}

function add_recipient(){
    ul = $('recipients_options');
    var li_mail = Element.extend(document.createElement("LI"));
    li_mail.addClassName('addElement');
    li_mail.innerHTML = '<div align="right"><a href="delete_email" onclick="remove_recipient('+i+');return false"><img src="/skin/frontend/default/fiveredwoods/images/list_remove_btn.gif" alt="Remove Email"/><\/a><\/div>';
    li_mail.innerHTML += '<div class="left"><label for="recipients_name">Name <span class="required">*<\/span><\/label><br /><div style="width:250px"><input name="recipients[name][]" type="text" class="input-text required-entry" id="recipients_name'+i+'" style="width:250px;" /><\/div><br /><br /><\/div>';
    li_mail.innerHTML += '<div class="right"><label for="recipients_email">Email Address:<span class="required">*<\/span><\/label><br /><div style="width:250px"><input name="recipients[email][]" value="" title="Email Address" id="recipients_email'+i+'" type="text" class="input-text required-entry validate-email" style="width:250px;" /><\/div><\/div>';
    i++;
    recipCount++;
    if(recipCount>=maxRecip && maxRecip != 0) {
        $('add_recipient_button').hide();
        $('max_recipient_message').show();
    }

    ul.appendChild(li_mail);
}

function fireEvent(element,event){
    if (document.createEventObject){
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}
Cufon.replace('.page-head, .page-head-alt, #featured-products h3');

