function installOpenSearch() {
  if((typeof window.external=="object")&&((typeof window.external.AddSearchProvider=="unknown")||(typeof window.external.AddSearchProvider=="function"))) {
    window.external.AddSearchProvider("http://www.hulu.com/opensearch.xml");
  } else {
    alert("This plugin requires a browser that supports OpenSearch.  Browsers that support OpenSearch include Firefox, Chrome and Internet Explorer.");
  }
}

function launchLivePlayer(event) {
  var url = 'http://www.hulu.com/live/' + event;
  var full = window.open(url, "live_player_window", "toolbar=0,status=0,resizable=1,scrollbars=0,width=916,height=520");
  if (full) {
    if (navigator.userAgent.indexOf("Chrome") == -1)
      full.moveTo(0,0);
    full.focus();  
  }
  else {
    alert("Oops - your browser may be blocking this pop-up. Please disable your pop-up blocker before continuing.");
  }
}

function toggle_comments(el) {
  if(el.hasClassName('open')) {
    el.removeClassName('open');
    el.up('div').next('.comments-area').hide();
  } else {
    el.addClassName('open');
    el.up('div').next('.comments-area').show();
  }
}

function image_toggle_over(el) {
  var img = $(el);
  var extension_rx = /(\.[^.]+)$/;
  if (!img.src.match(/-disabled/) && img.src.match(extension_rx)) {
    var extension = RegExp.$1;
    if (!img.src.match(/-hover/))
      img.src = img.src.replace(extension_rx, '-hover' + extension);
    else
      img.src = img.src.replace(extension_rx, extension);
    if (img.src.match(/spottypes/))
      img.style.cursor = "default";
    else
      img.style.cursor = "pointer";
  } else {
    img.style.cursor = "default";
    img.alt = "";
  }
}

function image_toggle_out(el) {
  var img = $(el);
  var extension_rx = /(\.[^.]+)$/;
  if (!img.src.match(/-disabled/) && img.src.match(extension_rx)) {
    if (img.src.match(/-hover/))
    img.src = img.src.replace(/-hover/, '');
  }
}

function validate_comment(el) {
  var form = el.up('form');
  
  if(form["author"].value.replace(/^\s*|\s*$/g,'') == "") {
    form.down('span.comment-error').innerHTML = "Please enter your name."
    form["author"].focus();
    return false;
  } else if(form["email"].value.replace(/^\s*|\s*$/g,'') == "") {
    form.down('span.comment-error').innerHTML = "Please enter your email address."
    form["email"].focus();
    return false;
  } else if(form["email"].value.replace(/^\s*|\s*$/g,'').length < 6) {
    form.down('span.comment-error').innerHTML = "Please enter a valid email address."
    form["email"].focus();
    return false;
  } else if(!validate_email(form["email"].value.replace(/^\s*|\s*$/g,''))) {
    form.down('span.comment-error').innerHTML = "Please enter a valid email address."
    form["email"].focus();
    return false;
  } else if(form["comment"].value.replace(/^\s*|\s*$/g,'') == "") {
    form.down('span.comment-error').innerHTML = "Please enter a comment."
    form["comment"].focus();
    return false;
  }
  
  form.submit();

  return true;
}

function trim(str) {
	while(str.charAt(0) == ' ') {
		str = str.substring(1);
	}
	while(str.charAt(str.length - 1) == ' ') {
		str = str.substring(0, str.length - 1);
	}
	
	return str;
}


function validate_email(to) {	
	var at = "@";
	var dot = ".";
	var emails = to.split(',');
	
	for(i = 0; i < emails.length; i++) {
		var email = trim(emails[i]);
		var lat = email.indexOf(at);
		var lemail = email.length;
		var ldot = email.indexOf(dot);
		
		if(email.indexOf(at) == -1)
			return false;
		
		if(email.indexOf(at) == -1 || email.indexOf(at) == 0 || email.indexOf(at) == lemail)
			return false;
		
		if(email.indexOf(dot) == -1 || email.indexOf(dot) == 0 || email.indexOf(dot) == lemail)
			return false;
		
		if(email.indexOf(at, (lat + 1)) != -1)
			return false;
		
		if(email.substring(lat - 1, lat) == dot || email.substring(lat + 1, lat + 2) == dot)
			return false;
		
		if(email.indexOf(dot, (lat + 2)) == -1)
			return false;
		
		if(email.indexOf(" ") != -1)
			return false;		
	}//end for
	
	return true;
}


function append_date(el) {
  dropdown = el.getElementsBySelector('select')[0];
  el.action = el.action + dropdown[dropdown.selectedIndex].value;
}
  
function toggle_visible(el) {
  if (typeof($) == 'undefined') return;
  var img = $(el);
  if (!img) return;
  img.style.visibility = "visible";
}

function toggle_invisible(el) {
  if (typeof($) == 'undefined') return;
  var img = $(el);
  if (!img) return;
  img.style.visibility = "hidden";
}

// Remember/capture focus events so we can rewrite the doc title
// after IE messes with it
var TitleHack = {
  originalDocumentTitle: document.title,
  fixOnFocus: function(el) {
    Event.observe(el, "focus", function() {
      if (document.title != TitleHack.originalDocumentTitle)
        document.title = TitleHack.originalDocumentTitle;
    });
  }
};

Object.extend(Hash.prototype, (function () {
  function toQueryPair(key, value) {
    if (Object.isUndefined(value)) return key;
    return key + '=' + encodeURIComponent(String.interpret(value));
  }

  return {
    toSortedQueryString: function() {
      return this.keys().sort().inject([], function(results, key) {
        var values = this.get(key);
        key = encodeURIComponent(key);

        if (values && typeof values == 'object') {
          if (Object.isArray(values))
            return results.concat(values.map(toQueryPair.curry(key)));
        }
        else {
          results.push(toQueryPair(key, values));
        }

        return results;
      }.bind(this)).join('&');
    }
  };
})());

function navigate_to(form) {
  form = $(form);
  var select = form.getElementsBySelector('select')[0];
  if (select) form.action = select[select.selectedIndex].value;
}
