function isLeapYear(year)
{
  year = parseInt(year);
  if(year%4 == 0)
  {
    if(year%100 != 0)
    {
      return true;
    }
    else
    {
      if(year%400 == 0)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
  }
return false;
}

function convertToSlug(Text)
{
    return Text
        .replace(/[^A-Za-z0-9!.?:'" ]+/g,'')
        .replace(/ +/g,' ')
        ;
}

function populateDate()
{
  var maxDate = 28;
  var date = parseInt($('#dateSelect').val());
  var year = parseInt($('#yearSelect').val());
  var month = parseInt($('#monthSelect').val());
  var dateElement = $('#dateSelect');
  dateElement.attr('disabled', 'disabled');
  dateElement.empty();  
  if ((month == 4)||(month == 6)||(month == 9)||(month == 11))
  {
    for (i=1;i<31;i++)
    {
      dateElement.append('<option value="'+i+'">'+i+'</option>');
      maxDate = 30;
    }
  }
  else if (month == 2)
  {
    if (isLeapYear(year))
    {
      for (i=1;i<30;i++)
      {
        dateElement.append('<option value="'+i+'">'+i+'</option>');
        maxDate = 29;
      }
    }
    else
    {
      for (i=1;i<29;i++)
      {
        dateElement.append('<option value="'+i+'">'+i+'</option>');
        maxDate = 28;
      }
    }
  }
  else
  {
    for (i=1;i<32;i++)
    {
      dateElement.append('<option value="'+i+'">'+i+'</option>');
      maxDate = 31;
    }
  }
  if (date <= maxDate)
  {
    dateElement.val(date);
  }
  else
  {
    dateElement.val(maxDate);
  }
  dateElement.removeAttr('disabled');
  resetPreview();
}

function remainingChars()
{
  var numChars = $('#descriptionInput').val().length;
  var charsLeft = 35 - numChars;
  $('.remaining #charCount').empty();
  $('.remaining #charCount').append(charsLeft);
  if (charsLeft >= 0)
  {
    $('.remaining #charCount').removeClass('charStatusBad').addClass('charStatusGood');
  }
  else
  {
    $('.remaining #charCount').removeClass('charStatusGood').addClass('charStatusBad');
  }  
}

function sanitizeInput(){
	var sanitizedText = convertToSlug($('#descriptionInput').val());
	if (sanitizedText.length > 35){
	sanitizedText = sanitizedText.subStr(0, 35);
	}
	$('#descriptionInput').val(sanitizedText);
	remainingChars();
	resetPreview();
}

function resetPreview()
{
	var year = $('#yearSelect').val();
	var month = $('#monthSelect').val();
	var date = $('#dateSelect').val();
	if ($('#ampmSelect').val() == "am") {
		var hour = $('#hourSelect').val();		
	}
	else {
		if ($('#hourSelect').val() == "12"){
			var hour = 12;
		}
		else {
			var hour = Number($('#hourSelect').val())+12;
		}
	}
	var description = $('#descriptionInput').val();
	
	//alert(year+" "+month+" "+date+" "+hour+" "+description)
	
	$('#countdownPreview')[0].resetVariables(year,month,date,hour,description);
	updateEmbed();
}

function updateEmbed()
{
	var year = $('#yearSelect').val();
	var month = $('#monthSelect').val();
	var date = $('#dateSelect').val();
	if ($('#ampmSelect').val() == "am") {
		var hour = $('#hourSelect').val();		
	}
	else {
		if ($('#hourSelect').val() == "12"){
			var hour = 12;
		}
		else {
			var hour = Number($('#hourSelect').val())+12;
		}
	}
	var description = $('#descriptionInput').val();
	
	var embedCode = '<object ';
	embedCode += 'width='+$("#resizable").width()+' ';
	embedCode += 'height='+$("#resizable").height()+' ';
	embedCode += '<param name="movie" value="http://www.lost4815162342.com/flash/countdown.swf';
	embedCode += '?year='+year;
	embedCode += '&month='+month;
	embedCode += '&date='+date;
	embedCode += '&hour='+hour;
	embedCode += '&description='+escape(description);
	embedCode += '">';
	embedCode += '</param><param name="wmode" value="transparent"></param><embed src="http://www.lost4815162342.com/flash/countdown.swf';
	embedCode += '?year='+year;
	embedCode += '&month='+month;
	embedCode += '&date='+date;
	embedCode += '&hour='+hour;
	embedCode += '&description='+escape(description);
	embedCode += '" type="application/x-shockwave-flash" ';
	embedCode += 'width='+$("#resizable").width()+' ';
	embedCode += 'height='+$("#resizable").height()+' ';
	embedCode += 'wmode="transparent"></embed></object>';
	
	$('#embedTextArea').val(embedCode);

}

function selectAll(){
	$('#embedTextArea').select();
}

function newSize()
{
  var newHeight = $("#resizable").height();
  var newWidth = $("#resizable").width();
  $('#adjustedSize').empty();
  $('#adjustedSize').append(newWidth+' x '+newHeight);
}

function generateYears() {
	var d = new Date();
	var curYear = d.getFullYear();
	
	var yearOptions = '<option value="'+curYear+'">'+curYear+'</option>';
	yearOptions += '<option value="'+(curYear+1)+'" selected="selected">'+(curYear+1)+'</option>';
	yearOptions += '<option value="'+(curYear+2)+'">'+(curYear+2)+'</option>';
	
	return yearOptions;
}

$(document).ready(function()
{
	
	$('#yearSelect').append(generateYears());

   $('#monthSelect').change(populateDate);
   $('#yearSelect').change(populateDate);
   $('#descriptionInput').keyup(remainingChars);
   $("#resizable").resizable({ resize: newSize, aspectRatio: 2/1, maxWidth: 300, maxHeight: 150, minWidth: 80, minHeight: 40, stop: updateEmbed });
   
   $('#dateSelect').change(resetPreview);
   $('#hourSelect').change(resetPreview);
   $('#ampmSelect').change(resetPreview);
   
   $('#descriptionInput').blur(sanitizeInput);
   $('#embedTextArea').click(selectAll);
   
   var flashvars = {};
   	var params = { allowscriptaccess: "always", wmode: "transparent" };
   	var attributes = {
   		id: "countdownPreview",
   		name: "countdownPreview"   		
   	};
   
   swfobject.embedSWF("flash/customcountdown.swf", "previewFlash", "300", "150", "9.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
   
   remainingChars();
   updateEmbed();   

});
