function mailForm()
{
  $('#mc_embed_signup').hide();
  $('.col2_2 a#form_dropdown').click(function(){
    
    if ($("#mc_embed_signup").is(":hidden"))
    {
      $('#mc_embed_signup').slideDown(800);
      setTimeout("$('.col2_2 h4 a').text(\"Close registration form\");",900);
      return false;
    }
    else
    {
      $('#mc_embed_signup').slideUp(800);
      setTimeout("$('.col2_2 h4 a').text(\"Register your interest to become a Community Blogger\");",900);
      return false;
    }
  });
}

//-------------------------------------------------------------------------------------

function equalHeight(group)
{
  var tallest = 0;
  group.each(function() {
   var thisHeight = $(this).height();
   if(thisHeight > tallest) {
     tallest = thisHeight;
   }
  });
  group.height(tallest);
}

//-------------------------------------------------------------------------------------

function clear_fields() {
  $.each($('#comment_form input'), function() {
    $(this).val('');
  });
}

//-------------------------------------------------------------------------------------

function slide()
{
  $('#slideshow .title').hide();
  $('#slideshow .caption').hide();
  
  $('#slideshow_ul').cycle({
  fx: 'fade',
  speed: 1000,
  timeout: 4500,
  next: '#next', 
  prev: '#prev',
  pause: 1,
  before: onBefore,
  after: onAfter
  });
  
  $('#profile_slideshow').cycle({
  fx: 'fade',
  speed: 1000,
  timeout: 4500,
  next: '#next', 
  prev: '#prev',
  pause: 1
  });
}

//-------------------------------------------------------------------------------------

function onBefore() {
  
  $('#slideshow_info .title').fadeOut(500);
  $('#slideshow_info .caption').fadeOut(500);
}

//-------------------------------------------------------------------------------------

function onAfter() {
  $('#slideshow_info .title').html($(this).attr("title"));
  $('#slideshow_info .title').fadeIn(500);
  
  var alt = this.alt;
  var exploded = alt.split('::');
  
  var caption = exploded[0];
  var credit = exploded[1];
  
  $('#slideshow_info .caption').html(caption);
  $('#slideshow_info .caption').fadeIn(500)
  
  if(credit != "")
  {
    $('#slideshow_credit .author').html("Photo Credit: " + credit);
  }
  else
  {
    $('#slideshow_credit .author').html("");
  }
}

//-------------------------------------------------------------------------------------

// This function corrects the borders of the subnav on interior pages

function correctBorders() {

  $(".interior .col1 #nav-sub li.here").prev().css('width', '220px');
  
  if($("#nav-sub:first-child").hasClass("here"))
  {
    $("#nav-sub:first-child").css('border-top', '#D6D6D6 solid 1px');
  }
  
  if ($(".interior").hasClass("directory") != true)
  {
    $(".interior .col1 #nav-sub li").each( function() {
      if($(this).height() > '35' && $(this).height() < '50')
      {
        $(this).addClass('double');
      }
    });
  }
  
}

//-------------------------------------------------------------------------------------

function getTabName() {
  
  text = $("#nav_main .here a").html();

  // alert(text);
  
  if ($("#breadcrumb a").hasClass("get_tab_name"))
  {
    $("#breadcrumb a.get_tab_name").html(text);
  }
}

//-------------------------------------------------------------------------------------

function labelify() {
  $(".labelify").labelify({ text: "label" });
}

//-------------------------------------------------------------------------------------

function joinForm() {
  
  $("#field_id_33").html("The first 20 words will be used as your description on the listings page");
  
  $("#field_id_33").focus( function() {
    $(this).html('');
  });
  
  w_limit = 20;
  
  if ($(".sec1").hasClass("basic"))
  {
    w_limit = 20;
  }
  else if ($(".sec1").hasClass("enhanced"))
  {
    w_limit = 70;
  }
  else if ($(".sec1").hasClass("premium"))
  {
    w_limit = 250;
  }
  
  limit_words(w_limit);

      
  // Section for additional link or file
  $("input[name='field_id_58']").change(function(){
  
    if($("input[name='field_id_58']:checked").val() == 'none')
    {
      $("#additional_image").css("display", "none");
      $("#additional_link").css("display", "none");
    }
    else if ($("input[name='field_id_58']:checked").val() == 'file')
    {
      $("#additional_image").css("display", "block");
      $("#additional_link").css("display", "none");
    }
    else if ($("input[name='field_id_58']:checked").val() == 'link')
    {
      $("#additional_image").css("display", "none");
      $("#additional_link").css("display", "block");
    }
    // End additional section
          
  });
}

//-------------------------------------------------------------------------------------

function limit_checkbox_selection() {
  $(".join.basic #categories input[type=checkbox]").click(function(){
    var checkboxes_to_allow = 1;
    
    if($('input[type=checkbox]:checked').length == (checkboxes_to_allow+1)){
      alert("You may only chose 1 category");
      return false;
    }
  });
  
  $(".join.enhanced #categories input[type=checkbox]").click(function(){
    var checkboxes_to_allow = 1;
    
    if($('input[type=checkbox]:checked').length == (checkboxes_to_allow+1)){
      alert("You may only chose 1 category");
      return false;
    }
  });
  
  $(".join.premium #categories input[type=checkbox]").click(function(){
    var checkboxes_to_allow = 3;
    
    if($('input[type=checkbox]:checked').length == (checkboxes_to_allow+1)){
      alert("You may only chose a maximum of 3 categories");
      return false;
    }
  });
}

//-------------------------------------------------------------------------------------

function limit_words(limit) {

	var minWords = 0;
	var maxWords = limit;
	
	var numWords = jQuery.trim($('#field_id_33').val()).split(' ').length;
	
	if($('#field_id_33').val() === '') {
		numWords = 0;
	}
	
	if(numWords > maxWords && maxWords != 0) {
		$('#charleft').addClass('error');
	} else {
		$('#charleft').removeClass('error');	
	}
	
	$(".count").each(function() {

		$('#charleft').html('<strong>' + numWords + '</strong> Words (Maximum of ' + maxWords + ')');
		if(minWords > 0) {
			$(this).siblings('.wordCount').addClass('error');
		}

		$(this).bind('keyup click blur focus change paste', function() {
			numWords = jQuery.trim($(this).val()).split(' ').length;
			if($(this).val() === '') {
    		numWords = 0;
    	}
    	
			$('#charleft').children('strong').text(numWords);

			if(numWords < minWords || (numWords > maxWords && maxWords != 0)) {
				$('#charleft').addClass('error');
			} else {
				$('#charleft').removeClass('error');	
			}
		});
	});
  
}

//-------------------------------------------------------------------------------------

function validate_form() {
    
  $(".join #submit_button").click( function() {

    var validity = $(".join #entryform").valid();
    var acceptval = false;
    var taval = false;
    
    $("#errordiv #errorul").children().each(function() {
      $(this).remove();
    });
    
    $("#errordiv").css("display","none");
    
    // Manually add error message underneath acceptance
    if ( $("input[name='field_id_39']").is(':checked'))
    {
      $(".accepterr").remove();
        
      acceptval = true;
    }
    else
    {  
      if ($(".accepterr").is('*'))
      {
      }
      else
      {
        $("#options").after('<p class="accepterr error">You must accept the application before proceeding</p>');
      }
      
      acceptval = false;
    }
    // end acceptance validation
    
    // Manually check description isn't too long
    var wordl = 0;
    var wordc = jQuery.trim($('#field_id_33').val()).split(' ').length;
    
    /////////
    
    if ($(".sec1").hasClass("basic"))
    {
      wordl = 20;
    }
    else if ($(".sec1").hasClass("enhanced"))
    {
      wordl = 70;
    }
    else if ($(".sec1").hasClass("premium"))
    {
      wordl = 250;
    }
    
    ////////
    
    if($('#field_id_33').val() === '') {
  		wordc = 0;
  	}
    
    if (wordc > wordl)
    {
      taval = false;
    }
    else
    {
      taval = true;
    }
    
    // end wordcount validation
      
    if ( validity == true && acceptval == true && taval == true)
    {
      if ($('#p_field_id_32').css('display') == 'block')
      {
        urlvalue = $('#field_id_32').val();
        urllength = $('#field_id_32').val().length;
        sevenchars = urlvalue.substr(0,7);
        
        if (sevenchars == 'http://')
        {
          newval = urlvalue.substr(7,urllength);
          $('#field_id_32').val(newval);
        }
      }
      
      if ($('#p_field_id_60').css('display') == 'block')
      {
        urlvalue2 = $('#field_id_59').val();
        urllength2 = $('#field_id_59').val().length;
        sevenchars2 = urlvalue2.substr(0,7);
        
        if (sevenchars2 == 'http://')
        {
          newval = urlvalue2.substr(7,urllength2);
          $('#field_id_59').val(newval);
        }
      }
      else
      {
        $('#field_id_59').html();
      }
      
      $(".join #entryform").submit();
    }
    else
    {
      if (validity == false)
      {
        $('<li>You missed out some required fields</li>').appendTo("#errordiv #errorul");
      }
      if (taval == false)
      {
        $('<li>The description is too long for your chosen membership type</li>').appendTo("#errordiv #errorul");
      }
      if (acceptval == false)
      {
        $('<li>You must acknowledge your acceptance</li>').appendTo("#errordiv #errorul");
      }
      
      $("#errordiv").css("display", "block");
      $("body").scrollTop(300);
    }
      
  });

}

//-------------------------------------------------------------------------------------

function lightbox(){
  $('a.lightbox').lightBox();
}

//-------------------------------------------------------------------------------------

function set_photo_credits() {
  
  if (!($("#wrapper-inner").hasClass("index")))
  {
    $('img').each(function(){

      if ($(this).attr("alt").indexOf("::") >= 0)
      {
        var photo_alt = $(this).attr("alt");
        var exploded = photo_alt.split('::');
        var photo_credit = exploded[1];

        $(this).after('<p class="photocredit">Photo credit: ' + photo_credit + '</p><div class=\"clearfix\"></div>');
        $(this).addClass("pushcredit");
        $(this).attr("alt", exploded[0]);
      }
    });
  }
}

//-------------------------------------------------------------------------------------

function validate_comment_form()
{
  $('#comment_form button').click( function() {
    
    $('#comment_form').valid();
  });
}

//-------------------------------------------------------------------------------------

$(document).ready(function() {
  //mailForm();
  // clear_fields();
  slide();
  equalHeight($("#footer #columns div"));
  equalHeight($(".bloggers ul#authors li.equal"));
  equalHeight($(".sec2.four-col .equal li"));
  equalHeight($(".sec2.four-col .equal"));
  equalHeight($("#image_gallery .equal"));
  correctBorders();
  set_photo_credits();
  getTabName();
  labelify();
  joinForm();
  lightbox();
  limit_checkbox_selection();
  validate_form();
  validate_comment_form();
});
