var Vodpod = {};

var videosperpage = 5;
var videos2 = '';
var numpages = 0;
var sectionlinks = '';

function sectionPageClick(pageNum)
{
	var startVideo = ((pageNum - 1) * videosperpage);
	
	// hide all videos first
	for (var i = 0; i < videos2.length; ++i)
	{
		$(videos2[i]).hide();
	}
	
	// show ones we want
	for (var i = startVideo; i < (videosperpage + startVideo); ++i)
	{
		$(videos2[i]).show();
	}
}

Vodpod.Gallery = Class.create();

Vodpod.Gallery.prototype = {
  pod_id: null,
  api_key: null,
  videos: [],
  container: null,
  videos_node: null,
  current_page: 1,
  active_video: null,
  
  initialize: function(pod_id, api_key, container)
  {
    this.container = container;
    this.pod_id = pod_id;
    this.api_key = api_key;
    
    // Get the display containers
    this.videos_node = $('vp_video_thumbs');
    this.player_node = $('vp_video_player');
    this.title_node = $('vp_video_title');
    this.embed_node = $('vp_embed_holder');
    this.pagination_node = $('vp_pagination');
    this.description_node = $('vp_description');
    
    videos2 = new Array();
    
    // Load the videos, once the page has been loaded
    addLoadEvent( (function() {this.loadVideos()}).bind(this) );
  },
  
  loadVideos: function(options)
  {
    var opts = {
      category_id: null, page: 1, sort: 'date-desc'
    }
    Object.extend(opts, options || {});
    this.current_page = opts.page;
    
    url = 'http://vodpod.com/api/pod/videos.js?pod_id=' + this.pod_id + "&per_page="+50+"&api_key=" + this.api_key + "&callback=gallery.createVideos";
    if (opts.category_id)
      url += "&category_id=" + opts.category_id;
    if (opts.page)
      url += "&page=" + opts.page;
    if (opts.sort)
      url += "&sort=" + opts.sort;
    
    // Grey out the video list
    this.videos_node.setStyle({opacity:0.3});
    
    var json_script = document.createElement('script');
    json_script.src = url;
    document.body.appendChild(json_script);
  },
  
  createVideos: function(json)
  {
  	//alert($H(json.videos.items.size()).inspect());
  	//alert(json.videos.items.length);
  	
  	numpages = (json.videos.total / videosperpage);
  	
    this.clearVideos();
    json.videos.items.each( (function(item) {
      var v = new Vodpod.Video(item.video, this);
      this.videos.push(v);
    }).bind(this));

    // Un-Grey the video list
    this.videos_node.setStyle({opacity:1});
    
    // Load the first video into the player (if one isn't already loaded)
    if (!this.active_video)
    	this.videos[0].loadVideo(false);
    
    // hide all videos after the first 4
  	for (var i = videosperpage; i < videos2.length; ++i)
  	{
  		$(videos2[i]).hide();
  	}
  	
  	for (var i = 0; i < numpages; ++i)
  	{
  		sectionlinks += '&nbsp;<a style="cursor:pointer;border: 1px solid #00adef;padding: 2px 4px 2px 4px;" onclick="sectionPageClick(\''+(i + 1)+'\')">'+(i + 1)+'</a>&nbsp;';
  	}
  	
  	$('vp_sections').innerHTML = '<div style="width:1px;height:8px;"></div>'+sectionlinks;
  	
  	
  },
  
  clearVideos: function()
  {
    this.videos = [];
    this.videos_node.innerHTML = '';
  },
  
  loadNext: function()
  {
    this.loadVideos({page: this.current_page+1});
  },
  
  loadPrevious: function()
  {
    this.loadVideos({page: this.current_page-1});
  }
};

Vodpod.Video = Class.create();
Vodpod.Video.prototype = {
  id: null,
  pod_id: null,
  title: null,
  description: null,
  tags: null,
  created_at: null,
  link: null,
  embed_tag: null,
  autoplay_embed_tag: null,
  thumbnail_small: null,
  thumbnail_medium: null,
  total_views: null,
  weekly_views: null,
  node: null,
  gallery: null,
  
  initialize: function(video, gallery)
  {
    this.gallery = gallery;
    
    this.id = video.video_id;
    this.pod_id = video.pod_id;
    this.title = video.title;
    this.description = video.description;
    this.tags = video.tags;
    this.created_at = video.created_at;
    this.link = video.link;
    this.embed_tag = video.embed_tag;
    this.autoplay_embed_tag = video.autoplay_embed_tag;
    this.thumbnail_small = video.thumbnails.small;
    this.thumbnail_medium = video.thumbnails.medium;
    this.total_views = video.stats.total_views;
    this.weekly_views = video.stats.weekly_views;
    
    this.gallery.description_node.innerHTML = this.description;
    
    this.createElement();
  },
  
  createElement: function()
  {
  	if (videos2.length == 0)
  	{
  		videos2[0] = 'vp_video_'+this.id;
  	}
  	else
  	{
  		videos2.push('vp_video_'+this.id);
  	}
  	
    this.node = Builder.node('div', {id:'vp_video_' + this.id, className:'video_thumb', video_id:this.id},
      [Builder.node('img', {className:'thumbnail', src:this.thumbnail_medium}),
       Builder.node('div', {className:'thumb_title_box'}),
       Builder.node('div', {className:'new_description'},this.description),// custom modification
       Builder.node('div', {className:'thumb_title'},
         [Builder.node('span', {className:'number'}), this.title])]);
    this.node.onclick = this.loadVideo.bind(this, true);
    
    this.gallery.videos_node.appendChild(this.node);
  },
  
  loadVideo: function(autoplay)
  {
    var embed = (autoplay == true) ? this.autoplay_embed_tag : this.embed_tag;
    
    var title_node = Builder.node('a', {href:this.link}, this.title);
    this.gallery.title_node.innerHTML = '';
    this.gallery.title_node.appendChild(title_node);
    this.gallery.embed_node.innerHTML = embed;
    this.gallery.description_node.innerHTML = this.description;
    this.gallery.active_video = this;
  }
};

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
