$(document).ready(function() {	
	/* Send External Links to New Window */
	$("a[rel=external]").attr("target","_blank");
	$('a.chat').click(function(){
    window.open(this.href,'','height=600, width=300,scrollbars=no');
    return false;
  });
	
	/* Font Replacement */
	Cufon.replace("ul#navigation h2 a, #main-column h3, #main-column .keypoint h4");
	
	/* Sidebar Default Form Value */
	$("form#sign-up-for-updates label").hide();
	$("form#retrieve-quote label").hide();
	$("form#save-quote label").hide();
	$("input#newsletter-name").defaultValue("Name");
	$("input#newsletter-email").defaultValue("Email");
	$("input#newsletter-company").defaultValue("Company");
	$("input#save-email-input").defaultValue("Email");
	$("input#retrieve-email-input").defaultValue("Email");
	$("input#retrieve-quote-input").defaultValue("Quote Number");
	
	/* Home Slideshow */
  $("ul#slideshow") 
    .before("<ul id='slideshow-controls'>") 
    .cycle({ 
      fx: "fade",
      timeout: 8000,
      pager: "#slideshow-controls",
      pagerAnchorBuilder: function(idx, slide) { 
        return '<li class="'+$(slide).attr('class')+'"><a href="#">'+(idx+1)+'</a></li>';
    }
  });
  $("ul#slideshow-controls li.one a").html("Experience");
  $("ul#slideshow-controls li.two a").html("Customer Service");
  $("ul#slideshow-controls li.three a").html("Printing Options");
	
	/* FAQ Toogles */
	$(".faq").children("p, ul").hide();
	$(".faq h4 a").addClass("closed");
  $(".faq h4 a").click(function() {
    $(this).parent().nextAll("p, ul").slideToggle("fast");
    $(this).toggleClass("closed");
    return false;
  });
  
  /* Team Toogles */
	$(".team").children("p").hide();
	$(".team").append("<p class='toggle'><a href='#'>Bio</a></p>");
  $(".team p.toggle a").toggle(function() {
			$(this).html("Close");
			$(this).addClass("close");
			$(this).parent().parent().children("p").not("p.toggle").slideDown("fast");
				}, function () {
			$(this).html("Bio");
			$(this).removeClass("close");
			$(this).parent().parent().children("p").not("p.toggle").slideUp("fast");
			return false;
	});
  
  /* Images Lightbox */
	$("a.image-zoom").fancybox({
    'overlayShow': false,
    'centerOnScroll': false
	});
	
	/* Contact Map Lightbox */
	$("a.iframe").fancybox({
    'overlayShow': false,
    'centerOnScroll': false,
		'width': 800,
    'height': 500
	});

	/* Hide Sections */
	$(".user-new-section, .user-existing-section, .billing-section, .payment-section, .shipping-section, .shipping-sub-section").hide();
	
	/* User Login Section Toggle */
  if ($("input.user-login-radio:checked").val() == "new-user") {
    $(".user-existing-section").hide();
    $(".user-new-section").show();
  }
  if ($("input.user-login-radio:checked").val() == "existing-user") {
    $(".user-new-section").hide();
    $(".user-existing-section").show();
  }
  $("input.user-login-radio").click(function() {
    if ($("input.user-login-radio:checked").val() == "new-user") {
      $(".user-existing-section").slideUp("fast");
      $(".user-new-section").slideDown("fast");
    }
    if ($("input.user-login-radio:checked").val() == "existing-user") {
      $(".user-new-section").slideUp("fast");
      $(".user-existing-section").slideDown("fast");
    }
  });
  
	/* Billing Section Toggle */
  if ($("input.billing-radio:checked").val() == "united-states-canada-mexico") {
    $(".billing-section:first").show();
  }
  if ($("input.billing-radio:checked").val() == "other") {
    $(".billing-section:last").show();
    $(".shipping-section:visible").hide();
    $(".shipping-section:last").show();
    $("#shipping-checkbox").hide();
    $("input#shipping-different-billing-input").attr("checked", false);
  }
  $("input.billing-radio").click(function() {
    if ($("input.billing-radio:checked").val() == "other") {
        $(".billing-section:first").slideUp("fast");
        $(".billing-section:last").slideDown("fast");
        $(".shipping-section:visible").slideUp("fast");
        $(".shipping-section:last").slideDown("fast");
        $("#shipping-checkbox").slideUp("fast");
        $("input#shipping-different-billing-input").attr("checked", false);
    }
    else {
        $(".billing-section:first").slideDown("fast");
        $(".billing-section:last").slideUp("fast");
        $(".shipping-section:last").slideUp("fast");
        $("#shipping-checkbox").slideDown("fast");
    }
  });
  
  /* Payment Section Toggle */
  if ($("input.payment-radio:checked").val() == "credit-card") {
    $(".payment-section").show();
  }
  $("input.payment-radio").click(function() {
    if ($("input.payment-radio:checked").val() == "credit-card") {
        $(".payment-section").slideDown("fast");
    }
    else {
        $(".payment-section").slideUp("fast");
    }
  });
  
  /* Shipping Section Toggle */
  if ($("input#shipping-different-billing-input:checked").val() == "y") {
    $(".shipping-section:first").show();
  }
  $("input#shipping-different-billing-input").click(function() {
    if ($(this).is(":checked")) {
      $(".shipping-section:first").slideDown("fast");
    }
    else {
      $(".shipping-section:first").slideUp("fast");
    }
  });
  
  /* Shipping Sub Section Toggle */
  if ($("input.shipping-radio:checked").val() == "united-states-canada-mexico") {
    $(".shipping-sub-section:first").show();
  }
  if ($("input.billing-radio:checked").val() == "other") {
    $(".shipping-sub-section:last").show();
  }
  $("input.shipping-radio").click(function() {
    if ($("input.shipping-radio:checked").val() == "other") {
        $(".shipping-sub-section:first").slideUp("fast");
        $(".shipping-sub-section:last").slideDown("fast");
    }
    else {
        $(".shipping-sub-section:first").slideDown("fast");
        $(".shipping-sub-section:last").slideUp("fast");
    }
  });
  
  /* Forgot Password Toggle */
  $("#my-account-forgot-password").hide();
  $("a#forgot-password").click(function() {
    $("#my-account-forgot-password").slideToggle("fast");
    return false;
  });
  
  
  // Display the correct number of upload fields
  if ($('#quotes-file-upload ').length > 0)
  {
    // Find how many upload fields to display
    var times = parseInt($('#sidebar p > strong').filter(function() {
      return $(this).html().match(/Number of Versions/);
    }).parent().html().replace(/<strong>.+<\/strong>/i, ''));
    
    // Copy the exsisting HTML
    var fieldset = $('#quotes-file-upload fieldset').clone();
    $('#quotes-file-upload fieldset').remove();

    // Create a new upload field for each time needed
    for (i = 1; i <= times; i++) {
      var current = fieldset.clone();
      var number = times - i + 1;
      $('#quotes-file-upload ').prepend(current);

      // Adjust HTML
      if ((i % 2) == 1) current.addClass('odd');
      current.find('h4 span').html(number);
      current.find('#upload-file-input-1').attr('id', 'upload-file-input-' + number).attr('name', 'upload-file-' + number);
      current.find('#upload-file-input-hidden-1').attr('id', 'upload-file-input-hidden-' + number).attr('name', 'upload-file-hidden-' + number);
      current.find('.file-upload-column a.button').attr('href', "javascript:$('#upload-file-input-" + number + "').uploadifyUpload();")
      current.find('#version-quantity-1').attr('id', 'version-quantity-' + number).attr('name', 'version-quantity-' + number);
      current.find('#upload-progress').attr('id', 'upload-progress-' + number);

      /* File Uploader */
      $("#upload-file-input-" +  number).uploadify({
    		'uploader'       : '/media/scripts/uploadify.swf',
    		'script'         : '/media/scripts/uploadify.php',
    		'folder'         : '/media/images/quotes/uploads',
    		'buttonImg'      : '/media/images/quotes/button.png',
    		'width'          : 86,
    		'height'         : 34,
    		'queueID'        : 'upload-progress-' + number,
    		'buttonText'     : 'Choose File',
    		'auto'           : false,
    		'multi'          : false,
        'onComplete'     : function(event,queueID,fileObj,response,data) {
          var id = $(event.target).attr('id').replace('upload-file-input-', '');
          $('#upload-file-input-hidden-' + id).val(response);
        }
    	});
    }
  }
  
  $('a.discount-policy').fancybox({
    'overlayShow': false,
    'centerOnScroll': false,
    'type' : 'iframe'
  });
});
