$(document).ready(function() {

  /* Open External Links in New Window */
	$("a[rel=external]").attr('target','_blank');
	
  /* Main Navigation Font Replacement */
	$("ul#navigation li a").addClass("cufon")
	Cufon.replace("ul#navigation li a.cufon span");
	
	/* Connect Menu Hover */
	$("#header ul#alt-navigation li#connect").hover(function() {
      $(this).find("ul").hide();
      $(this).find("ul").slideDown(100);
    }, function () {
		  $(this).find("ul").slideUp(100);
		return false;
	});
	
	/* Newsletter Lightbox */
	$("li#connect a.newsletter").fancybox({
    'centerOnScroll': true,
    'overlayOpacity': 0.5,
    'overlayColor':	'#000',
    'titleShow': false,
    'padding': 20,
    'autoDimensions': false,
    'width': 230,
    'height': 230,
    'scrolling': 'no'
	});
	
	/* Green Tips Lightbox */
	$("a.iframe").fancybox({
    'centerOnScroll': true,
    'overlayOpacity': 0.5,
    'overlayColor':	'#000',
    'titleShow': false,
    'padding': 20,
    'autoDimensions': false,
    'width': 230,
    'height': 330,
    'scrolling': 'no'
	});
	
	/* Tabs */
	$(".tabs").tabs({
    cookie: {}
  });
	
	/* Sidebar Form Labels and Input Values */
	$("#sidebar form label").hide();
	$("input#newsletter-name-input").defaultValue("Name");
	$("input#newsletter-email-input").defaultValue("Email");
	
	/* Sidebar Collapsable */
	$("#sidebar .collapsable").hide();
	$("#sidebar h5 a").addClass("collapsed");
	$("#sidebar h5 a").toggle(function() {
			$(this).removeClass("collapsed");
			$(this).parent().nextAll("#sidebar .collapsable").slideDown(100);
				}, function () {
		  $(this).addClass("collapsed");
			$(this).parent().nextAll("#sidebar .collapsable").slideUp(100);
			return false;
	});
	
	/* Our Drinks Slideshow */
	$("ul#slideshow") 
    .before("<ul id='slideshow-navigation'>Flavors<ul></ul></ul>", "<a id='slideshow-previous' href='#'>Previous</a>", "<a id='slideshow-next' href='#'>Next</a>")
    .cycle({ 
      fx: "scrollHorz",
      timeout: 8000,
      pause: 1,
      pauseOnPagerHover: 1,
      cleartypeNoBg: true,
      prev: "#slideshow-previous",
      next: "#slideshow-next",
      pager: "#slideshow-navigation ul",
      activePagerClass: "current",
      pagerAnchorBuilder: function(idx, slide) { 
        return '<li><a href="#">'+$(slide).find('h3').text()+'</a></li>';
    }
  });
	$("#feature ul#slideshow-navigation").hover(function() {
      $(this).find("ul").hide();
      $(this).find("ul").slideDown(100);
    }, function () {
		  $(this).find("ul").slideUp(100);
		return false;
	});
  
  /* Get MADE Locations */
  $('#select-state').change(function() {
    var json = $(this).find('option:selected').attr('data-cities').replace(/\'/g, '"');
    var object = jQuery.parseJSON(json);
    object = object.sort(function(a, b) {
      return (a.name.charCodeAt(0) - b.name.charCodeAt(0));
    });
    
    var shown = new Array();
    var html = '<option value="">Select City</option>';
    jQuery.each(object, function(index, city) {
      if (jQuery.inArray(city.handle, shown) == -1) {
        shown.push(city.handle);
        html += '<option value="' + city.handle + '">' + city.name + '</option>';
      }
    });
    $('#select-city').html(html);
  });
  
  if ($('#select-state').val() != '') {
    $('#select-state').change();
    $('#select-city').val(window.location.pathname.split('/')[3]);
  }
  
  $('#find-made').click(function() {
    var state = $('#select-state option:selected').attr('value');
    var city = $('#select-city option:selected').attr('value');
    if (city != '' && state != '') {
      window.location = '/get-made/' + state + '/' + city;
    } else if (state !='' ) {
      window.location = '/get-made/' + state;
    }
  });
  
  /* Get MADE Event Regions */
  $("#select-region").change(function() {
    var region = $("#select-region option:selected").attr("value");
    if (region != "") {
      window.location = "/get-made/events/region/" + region + "/";
    }
  });
  
  /* Trade Merchandise */
  $("form#merchandise .section:last").hide();
  $("<a class='view-modify' href='#' title='View/Modify'>View/Modify</a>").appendTo("form#merchandise .section:first");
  $("form#merchandise .section:first a.view-modify").hide();
  $("<a class='button' href='#' title='Next Step'>Next Step</a>").appendTo("form#merchandise .section:first fieldset");
  $("form#merchandise .section:first a.button").click(function() {
    $("form#merchandise .section:first fieldset").slideUp(200);
    $("form#merchandise .section:last").delay(200).slideDown(200);
    $("form#merchandise .section:first a.view-modify").show();
    $("form#merchandise .section:first h4").addClass("disabled");
		return false;
	});
	$("form#merchandise .section:first a.view-modify").click(function() {
    $(this).hide();
    $("form#merchandise .section:last").slideUp(200);
    $("form#merchandise .section:first fieldset").delay(200).slideDown(200);
    $("form#merchandise .section:first h4").removeClass("disabled");
		return false;
	});
  
  /* Trade FAQ */
  $("h4.question").next(".section").hide();
  $("h4.question").wrapInner("<a href='#' />");
  $("<span></span>").prependTo("h4.question a");
  $("h4.question").addClass("hidden");
  $("h4.question a").toggle(function() {
      $(this).parent().removeClass("hidden");
      $(this).parent().next(".section").slideDown(100);
    }, function () {
		  $(this).parent().addClass("hidden");
      $(this).parent().next(".section").slideUp(100);
		return false;
	});
  
  /* Contact Form Recipients */
  if (window.location.hash == "#charley") {
    $("select#select-recipient option[value='Charley Snell']").attr('selected', 'selected');
  }
  if (window.location.hash == "#frank") {
    $("select#select-recipient option[value='Frank Javech']").attr('selected', 'selected');
  }
  if (window.location.hash == "#denny") {
    $("select#select-recipient option[value='Denny Cohlmia']").attr('selected', 'selected');
  }
  if (window.location.hash == "#allison") {
    $("select#select-recipient option[value='Allison Cash']").attr('selected', 'selected');
  }
   
});