/* For the Preview Bubble Script: http://www.websnapr.com/previewbubble/ */
var bubbleImagePath = '/images/bg.png';

function loading() {
  Element.show('ajax-indicator');
}

function loaded() {
  Element.hide('ajax-indicator');
}

function show_status(message) {
  $('ajax-notice').innerHTML = message;
}

function disable(dform) {
  if (document.getElementById) {
    for (var sch = 0; sch < dform.length; sch++) {
      if (dform.elements[sch].type.toLowerCase() == "submit") {
        dform.elements[sch].disabled = true;
        dform.elements[sch].value = "Please wait...";
      }
    }
  }
  return true;
}

//  Displays and then fades a notice after a period of time.
AlertBox = Class.create() ;
AlertBox.prototype = {
  initialize: function(elementId,timeout) {
    this.elementId = elementId ;
    this.timeout = (timeout==undefined) ? 2 : timeout ;
  },

  show: function(text) {
    $(this.elementId).innerHTML = '<h1>' + text + '</h1>';
    new Effect.Appear(this.elementId, {duration: 0.3});
    if (this.timeout != 0) setTimeout(this.fade.bind(this), this.timeout * 1000);
  },

  fade: function() {
    new Effect.Fade(this.elementId, {duration: 0.3});
  }  
} ;

var Notice = new AlertBox('notice') ;

// Log Out Click
function oc(id) {
  url = '/friends/out_click/' + id;
  new Ajax.Request(url, {asynchronous:true, evalScripts:false});
}

refreshVideos = function() {
  loading();
  url = '/vspidered/home/refresh';
  new Ajax.Request(url, {asynchronous:true, evalScripts:true, onComplete: loaded});
}

loadGuideInitial = function() {
  $('videos').innerHTML = $('loading-html').innerHTML;
  url = '/channelsurfr/home/refresh?offset=' + current_offset;
  current_offset += 5;
  new Ajax.Request(url, {asynchronous:true, evalScripts:true, onComplete: loaded});
}

refreshGuide = function() {
  Element.hide('live-search');
  Element.hide('search-results');
  $('videos').innerHTML = $('loading-html').innerHTML;
  url = '/channelsurfr/home/refresh?offset=' + current_offset;
  current_offset += 5;
  new Ajax.Request(url, {asynchronous:true, evalScripts:true, onComplete: guideLoaded});
}

guideLoaded = function() {
  Element.show('live-search');
  Element.show('search-results');
  loaded();
}

vid_loaded = function() {
  Element.hide('video-loading');
  Element.show('vplayer');
}

vid_loading = function(id, delay) {
  current_item = id;
  Element.hide('vplayer');
  Element.hide('flag-item');
  Element.show('video-loading');
  setTimeout('vid_loaded()', delay);
	setTimeout('resetFlagCode()', delay + 1000);
}

flagItem = function(as) {
  if (current_item) {
    $('flag-item').innerHTML = "<font style='color: #ccc; size: .9em;'>item flagged -- thank you!</font>";
    url = '/flag/flag/' + current_item + '?flag=' + as;
    new Ajax.Request(url, {asynchronous:true, evalScripts:false});
  }
}

flagItemConfirm = function(as) {
  if (current_item) {
    if (confirm("Are you sure?\n\nPlease only flag videos that do not load after 10-20+ seconds, or that are unavailable / not embeddable.")) {
      $('flag-item').innerHTML = "<font style='color: #ccc; size: .9em;'>item flagged -- thank you!</font>";
      url = '/flag/flag/' + current_item + '?flag=' + as;
      new Ajax.Request(url, {asynchronous:true, evalScripts:false});
    }
  }
}

String.prototype.trim = function() {
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};

showLiveSearch = function() {
  new Effect.BlindDown('live-search', {queue: 'front'});
  new Effect.Highlight('inner-live-search', {queue: 'end', duration: '4'});
}

performSearch = function() {
  query = $('searchQuery').value.trim();
  if (query) {
//    alert("Query: '" + query + "'");
    Element.show('search-indicator');
    var url = '/channelsurfr/home/live_results?query=' + query;
    var target = 'search-results';
    new Ajax.Updater(target, url, {method: 'get', onSuccess: hideSearchIndicator});
  }
}

hideSearchIndicator = function() {
//  alert("Hiding search indicator");
  Element.hide('search-indicator');
  Element.show('search-results');
}

resetSearch = function() {
  new Effect.BlindUp('search-results');
  $('search-results').innerHTML = '';
  $('searchQuery').value = '';
}

resetFlagCode = function() {
  $('flag-item').innerHTML = $('flag-item-html').innerHTML;
  Element.show('flag-item');
}

scrollToTop = function () {
	window.scrollBy(0, -1000);
}

getEmbedCode = function(item_id) {
  Element.hide('embed-link-' + item_id);
  Element.show('embed-code-' + item_id);
  url = '/content/get_embed_code/' + item_id;
  new Ajax.Request(url, {asynchronous:true, evalScripts:true});
}

addToChannel = function(item_id) {
  url = '/video_channel/show_user_channels_inline/' + item_id;
  new Ajax.Request(url, {asynchronous:true, evalScripts:true});
}

rateItem = function(item_id, rating) {
  Element.hide('ajax-notice');
  url = '/rate/' + item_id + '?rating=' + rating;
  new Ajax.Request(url, {asynchronous:true, evalScripts:true});
}

updateCommentCount = function() {
  value = 'img src="/images/speech_bubble.gif"/ > &nbsp;<%= num_comments(@content_item) %>'
}

rateComment = function(comment_id, rating) {
  Element.hide('ajax-notice');
  url = '/comment/rate/' + comment_id + '?rating=' + rating;
  new Ajax.Request(url, {asynchronous:true, evalScripts:true});
}

toggleLinkCode = function() {
  Element.toggle('get-linkcode-link');
  Element.toggle('get-linkcode-code');
}

/* Submit Content Form */
toggleSubmitLink = function() {
  Element.toggle('link-submit-form');
  Element.toggle('submit-options');
}

toggleSubmitVideo = function() {
  Element.toggle('video-submit-form');
  Element.toggle('submit-options');
}

/* Yummy Upload Form */
toggleUpload = function() {
  Element.toggle('upload-form');
  Element.toggle('upload-options');
}

toggleGrab = function() {
  Element.toggle('grab-form');
  Element.toggle('upload-options');
}

/* Shoutbox */
toggleShoutEmail = function() {
  Element.toggle('shoutbox-toggle-link');
  Element.toggle('shoutbox-email-form');
}


function updateScore(id, increment_by) {
  if ($('score-' + id)) {
    cur_score = parseInt($('score-' + id).innerHTML);
    if (cur_score >= 0) {
      new_score = cur_score + increment_by;
      $('score-' + id).innerHTML = new_score;
    }
  }
}

function bookmarkUs(title, url){
  if (document.all)
    window.external.AddFavorite(url, title);
  else if (window.sidebar)
    window.sidebar.addPanel(title, url, "")
}


VideoChannelListing = Class.create();
VideoChannelListing.prototype = {
  initialize: function(base_url) {
    this.base_url        = base_url;
    this.video_channel_id = 0;

    if ($('channel-selector')) {
      new Form.Element.EventObserver('channel-selector', this.addToChannel.bind(this));
    }
  },
  
  addToChannel: function() {

    params = [];
    
    if ($('channel-selector')) {
      params.push(['channel_id', $('channel-selector').value]);
    }
    //alert("params : " + params);
    
    url_params = params.map(function(x){return x.join('=')}).join('&');
    
    url = this.base_url + '?' + url_params;
    //alert("URL: " + url);
    new Ajax.Request(url, {asynchronous:true, evalScripts:true});
  }

}

function CreateBookmarkLink() {

 title = window.title;
 url = window.location;

  if (window.sidebar) { // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
  } else if( window.external ) { // IE Favorite
    window.external.AddFavorite( url, title);
  } else if(window.opera && window.print) { // Opera Hotlist
    return true;
  }
}
