
EnfoldWines = {}
EnfoldWines.ensureAncestorVisible = function(){
  $('.current_page_ancestor ul').css({
    display: "block",
    visibility: "visible"
  });
}

// FONT REPLACEMENT
// Replace all tags with cufon equivalant
Cufon.replace('h1,h2,h3,h4,h5,h6,#commentform label, fieldset legend, caption, .button, .shadowbox-link, .cufon');
//

// JQUERY
$(function(){
		   
	   // Clear searchform on click
		$("#searchform #s").click(function(){
			$(this).val("");   
		});
		
	
		// Dropdown Menu config
		$("ul.sf-menu").supersubs({ 
            minWidth:    2,   // minimum width of sub-menus in em units 
            maxWidth:    30,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish();  // call supersubs first, then superfish, so that subs are 
                         // not display:none when measuring. Call before initialising 
                         // containing tabs for same reason.
	
	
	// Softbutton = soft fade on hover, used for social media icons
	//$(".softbutton a").stop().fadeTo("fast", .55);
	
	var getGraySocial = function(img, gray)
	{
	  if(gray == undefined){
	    gray = true;
	  }
    var split = $(img).attr('src').replace('color/', '').split('/');
    var end = split.pop();
    if(gray){
      split.push('color');
    } else {
      var replace = $(img).attr('src').replace('color/', '');
      return replace;
    }
    split.push(end);
    
    return split.join('/');    
	}
	
	$(".softbutton a").hover(
		function(){
		  var a = $(this);
      var img = $(this).find('img');
      img.attr('src', getGraySocial(img, true));
      $(this).fadeTo("fast", 1.0, function(){
        img.attr('src', getGraySocial(img, true));
        $(a).fadeTo("fast", 1.00);
      });
		},
		function(){
	    var a = $(this);
      var img = $(this).find('img');
      $(this).fadeTo("fast", 1.00, function(){
        img.attr('src', getGraySocial(img, false));
        $(a).fadeTo("fast", 1.00);
      });
		}
	);  
	
	// Promo slider setup, inserted on when promo is in use to save bandwidth
	
	$('#promos').cycle({ 
		fx:     'fade', 
		speed:  1000, 
		timeout: 3000,
		pager: '#promo-nav',
		pause: 1,
		height: 'auto',
		pauseOnPagerHover: 1,
		fastOnEvent: 350,
		pagerAnchorBuilder: function(idx, slide) { 
			return "#promo-nav li:eq("+ idx +") a"; 
		}
	});
	
	// Fix applied so menu appears OVER cycled images
	// Reverse the default z-index stacking order of the elements
	/*
	var startIndex = 1500;
	$('div').each(function() {
		$(this).css('zIndex', startIndex);
		startIndex -= 5;
	});*/
	
	/*
	 * HTML Shadowbox popups
	 * <a rel="html-shadowbox" href="#shippingInfo">Shipping Info</a>
	 *
	 * <div id="shippingInfo">
	 *    Content Here...
	 *</div>
	 */
	
	Cufon.replace('.ui-dialog-title');
  $('a[rel="html-shadowbox"]').each(function(){
    var a = $(this);
    var hr = a.attr('href');

    if(hr.match(/^\#/)){
      var aTit = a.attr('title');
      $(hr).dialog(
        {
          autoOpen: false,
          title: aTit,
          position: 'center',
          width: 800,
          height: 600,
          modal: true
        }
      );
    }
    a.bind('click', function(){
      $(hr).dialog('open');
      return false;
    });
  }); 
  
/*
 * Dropdowns will stey down when they are the current page.
 */
$('.current_page_ancestor').addClass('sfHoverOn').unbind().find('ul').unbind();
EnfoldWines.ensureAncestorVisible();
$('ul.sf-menu li').bind('mouseenter', function(){
  EnfoldWines.ensureAncestorVisible();
});

/**
 * Widget last class
 */
 
$('.widget-area .widget:last').addClass('last');

/*
 * Winepage checkboxes
 */ 
 
var appendWineboxBtn = function(el, field, txt){
  if(undefined == txt){
    txt = 'Sign Up';
  }
  var html = [];
  html.push('<a href="#" class="button">' + txt + '</a><div style="display: none" class="ordered">Ordered</div>');
  html = html.join('');
  $(el).append(html).find('.button').bind('click', function(){
    var chk = $(field).attr('checked');
    if(chk){
      var chk = false;
    } else {
      var chk = true;
    }
    $(field).attr('checked', chk);
    $(this).parent().find('.ordered').toggle();
    $(this).toggleClass('toggle');
    return false;
  });
  
  var checkedNow = $(field).attr('checked');
  if(checkedNow){
    $(el).find('.ordered').show();
  }
  
  $(field).parent().hide();
}
 
var clubPage = $('#page-wineclub');
if(clubPage.length > 0){
  var firstEl = clubPage.find('input:first');
  var firstEl = firstEl.attr('id').split('_');
  
  //wine-box.one -> field_10
  //wine-box.two -> field_11
  //wine-box-three -> field_12
  appendWineboxBtn($('.wine-box.one'), '#bambino_field');
  appendWineboxBtn($('.wine-box.two'), '#padrino_field');
  appendWineboxBtn($('.wine-box.three'), '#taste_field');
  $('.wine-box.four').append('<span class="button">Sorry Sold Out</span>').find('.button').click(function(){
    return false;
  })
  
  Cufon.replace('.ordered');
}

$('.sendbutton, .backbutton, .resetbutton').each(function(){
  var txt = $(this).attr('value');
  var id = $(this).attr('id');
  var aID = 'for_' + id;
  
  $(this).hide().before('<a href="#" class="button" id="'+ aID  +'">' + txt + '</a>');
  
  $('#' + aID).bind('click', function(){
    $('#' + id).click();
    return false;
  });
});

});// end jquery
