/*---------------------------------------------------------------------------------------------------------*/
//# JPGMAG Namespace
/*---------------------------------------------------------------------------------------------------------*/

var JPGMAG =
{
    countWords: function(field)
  {
    var text = field.value + " ";
    text = text.replace(/^[^A-Za-z0-9]+/gi,"");
    do {
        text = text.replace("  "," ");
    } while (text.search("  ")>-1);
    text = text.replace(/[^A-Za-z0-9]+/gi," ");
    wordCount = text.split(' ').length-1;
    return wordCount; 
  },/*countWords*/
  
    checkStoryLength: function()
  {
    
    var storyText = $('storytext');
    storyText.onkeyup = function()
    {
      var words = $('wordsSoFar');
      var minCount = $('minCount').value;
      var maxCount = $('maxCount').value;
      
      var wordCount = JPGMAG.countWords(this);
      if(this.value.length==0){
      	words.innerHTML = '0';
      }else{
        words.innerHTML = wordCount;
      }
      
      if(wordCount>=minCount) $('charleft').innerHTML = '<img src="/images/WriteCool.gif" alt="Cool!" />';
      if(wordCount<minCount) $('charleft').innerHTML = '';
      if(wordCount>maxCount) $('charleft').innerHTML = '<img src="/images/WriteTooMuch.gif" alt="Too Much!" />';
    }
    storyText.onkeyup();
  },/*checkDescriptionLength;*/

    checkShortLength: function()
  {

    var fld = $('shortdesc');
    fld.onkeyup = function()
    {
      var words = $('shortWordsSoFar');
      var wordCount = JPGMAG.countWords(this);
      //mx.innerHTML = (250 - this.value.length) + ' left';
      if(this.value.length==0){
          words.innerHTML = '0';
      } else {
          words.innerHTML = wordCount;
      }
      if(wordCount>25) $('shortWordsLeft').innerHTML = '<img src="/images/WriteTooMuch.gif" alt="Too Much!" />';
      if(wordCount<26 && wordCount>0) $('shortWordsLeft').innerHTML = '<img src="/images/WriteCool.gif" alt="Cool!" />';
      if(wordCount==0) $('shortWordsLeft').innerHTML = '';
    }
    fld.onkeyup();
  },/*checkDescriptionLength;*/
  
      switchEmail: function()
  {
  

    $('emailLink').onclick = function()
    {
    
      $('voting').style.display = 'none';
      $('emailAFriend').style.display = 'block';
     
      return false;
    }
    $('cancelEmail').onclick = function()
    {
      $('emailAFriend').style.display = 'none';
      $('voting').style.display = 'block';
      return false;
    }

  },/*switchEmail;*/
  
    selectThemeView: function()
  {
    var submitStory = $('submittedCheck');
    submitStory.onclick = function()
    {
      var themeLabel = ($('themeLabelBox'))?$('themeLabelBox'):$('themeLabelBoxShowing');
      var themeSelector = ($('themeSelectBox'))?$('themeSelectBox'):$('themeSelectBoxShowing');
      if(submitStory.checked==1){
          themeLabel.style.display = 'block';
          themeSelector.style.display = 'block';
      }else{
          themeLabel.style.display = 'none';
          themeSelector.style.display = 'none';
          $('themeSelect').options[0].selected=1;
      }
    }
  },/*selectThemeView*/
  
    submitPreview: function()
  {

    var messagePreview = $('emailMessage');
    messagePreview.onkeyup = function()
    {
      var mx = $('preview');
      mx.innerHTML = this.value;
      if(this.value.length>=500)this.value = this.value.substr(0,499);
    }
    messagePreview.onkeyup();
  },/*submitPreview;*/
 
    removeInputClues: function()
  {
    var toName = $('toNameField');
    toName.onfocus = function()
    {
      this.value='';
      this.onfocus='';
    }
    var toEmail = $('toEmailField');
    toEmail.onfocus = function()
    {
      this.value='';
      this.onfocus='';
    }
    
  },/*removeInputClues;*/
  
    categorySelectSort: function()
  {
  	var selector=$('storytypeselect');
  	selector.onchange = function()
  	{
  	  location.href=selector.options[selector.selectedIndex].value
  	}
  }/*categorySelectSort;*/
 }
  
ElementReady.chkDomId('storytext',JPGMAG.checkStoryLength);
ElementReady.chkDomId('shortdesc',JPGMAG.checkShortLength);
ElementReady.chkDomId('emailLink',JPGMAG.switchEmail);
ElementReady.chkDomId('submittedCheck',JPGMAG.selectThemeView);
ElementReady.chkDomId('preview',JPGMAG.submitPreview);
ElementReady.chkDomId('toNameField',JPGMAG.removeInputClues);
ElementReady.chkDomId('storytypeselect',JPGMAG.categorySelectSort);

window.onload = function() { ElementReady.cleanUp(); };
