// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var upload_id;

function set_video_view_type() {
  var type = $F('view_type');
  $('video_list').removeClassName('grid');
  $('video_list').removeClassName('list');
  $('video_list').addClassName(type);
  expire = new Date();
  expire.setTime(expire.getTime() + 3600000*24*30); // 30 days
  document.cookie = "view_type="+type+";expires="+expire.toGMTString();
}

function validate() {
	id = upload_id;
	if ( !$('video_title').present()) {
		alert('Please include a title!');
		return;
	}else {
    $('video_title').value = $('video_title').value.strip();
  }
	if ( !$('video_description').present()) {
		alert('Please include a description!');
		return;
	} else {
    $('video_description').value = $('video_description').value.strip();
  }
	Element.update('upload_errors','');
	Element.update('results','');
	//UploadProgress.monitor(id);
	//$('file_form').action = "/files/upload?upload_id="+id;
	//Element.hide('file_form');
	Element.show('load_file');
}

function trim(s) {
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ') {	l++; }
	while(r > l && s[r] == ' ') {	r-=1;	}
	return s.substring(l, r+1);
}

function completeUpload(success, newid) {
  UploadProgress.finish();
  if (success) {
    Element.update('upload_form', '<p>Thanks for submitting your video to REELcomp! You will be notified when your video is available on the site.</p>')
  } else {
    upload_id=newid;
    Element.hide('load_file');
    Element.show('file_form');
    Element.update('upload_errors','<div class="errorExplanation" id="errorExplanation"><h2>Errors prohibited this video from being uploaded</h2><p>Please verify that all fields are not empty and that the video matches the requirements listed below.</p></div>');
    
  }
}

function addMoreTags() {
  var list = $('tag_fields'), copyFrom = list.down();
  var newNode = copyFrom.cloneNode(true);
  // var close = document.getElementsByClassName('remove-file', newNode)[0]; 
  // Event.observe(close, 'click', function(e) { 
  //   Event.findElement(e, 'p').remove(); 
  //   e.preventDefault();
  // });
  // close.show();
  list.appendChild(newNode);
}


/*
http://missinghref.com/archive/2006/08/29/1322.aspx
*/
var TextCounter = Class.create();
TextCounter.prototype = {
    initialize: function(textareaid, inputid, maxLength) {
        this.maxLength = maxLength;
        this.textarea = $(textareaid);
        this.input = $(inputid);

        this.input.value = maxLength;
        this.input.readonly = true;
        this.input.disabled = true;

        Event.observe(this.textarea, 'keyup', this.checkChars.bindAsEventListener(this));
        Event.observe(this.textarea, 'keydown', this.checkChars.bindAsEventListener(this));

        this.checkChars();
    }, 

    checkChars: function(e) {

        var includeBreaksInCount = false; // false = don't count a return (\r or \n) in the count.
        var charCount = this.textarea.value.length;
        var breaks = 0;

        if (!includeBreaksInCount) {
            var lines = this.textarea.value.split('\n');
            breaks = lines.length;

            // check for /r at the end of the lines (IE)
            for (var i=0; i<lines.length; i++) {
                var line = lines[ i ];                
                if (line.charCodeAt(line.length-1) == 13)
                    breaks++;
            }
            
        }
        
        // check if over limit
        if ((charCount-breaks) > this.maxLength) {            
            this.textarea.value = this.textarea.value.substring(0, (this.maxLength + breaks) );
        }
        
        // update counter
        if (this.input) {
            if ((charCount-breaks) > this.maxLength) {
                this.input.value = 0;
            } else {
                this.input.value = (this.maxLength + breaks) - charCount;
            }        
        }
    }
}





function showPaginationLoader() {
  $('pagination-loading').show();
  $('pagination-area').hide();
}
function checkForPagination() {
  if ($('pagination-loading') && $('pagination-area')) {
    Event.addBehavior.reassignAfterAjax = true;
    Event.addBehavior({
      'div.pagination a' : Remote.Link({ onLoading: showPaginationLoader, onComplete: anchorComment })
    })
  }
}
function anchorComment() {
  window.location.href='#comment_display';
}
function newComment() {
  $('comment').value=''; 
  new Effect.Pulsate($('comments').firstDescendant()); 
  anchorComment();
  checkForPagination();
}

function showPlayerLoader() {
  
}

function checkForAjaxPlayer() {
  if ($('ajax_player')) {
    Event.addBehavior.reassignAfterAjax = true;
    $$('a.video_link').each(function(x) {
      // alert(x.attributes['video'].value);
      var video_url = "/videos/view_ajax/"+x.attributes['video'].value;
      new Remote.Link(x, { onLoading: showPlayerLoader, url: video_url });
    });
  }
}

Event.observe(window,'load',checkForPagination);
Event.observe(window,'load',checkForAjaxPlayer);
