// JavaScript Document
/* ----------------------------

Sublime thumbnail video switcher

Nabbed from: http://sublimevideo.net/demo

Ported from Prototype to $ 1.3.2 by Masni

---------------------------- */



var slideContainer = ".slide-container";
var controlsContainer = ".slide-controls";
var activeClass = "active";

var SublimeVideo = SublimeVideo || {};

SublimeVideo.isWebkitAnimationSupported = function() { // remember to cache the result
  return (typeof WebKitAnimationEvent === "object" || typeof WebKitAnimationEvent === "function");
};


var sublimeSlideShow = function(options) {

  initialize = function() {
	
	this.supportsWebkitAnimation = SublimeVideo.isWebkitAnimationSupported(); // to cache the result
    
    $(controlsContainer +" li").each(function(){
    	var thumb = $(this);
    	
		thumb.click(function(event){
			event.preventDefault();
			if (!thumb.hasClass(activeClass)) {
			  clickOnThumb(thumb.attr('id'));
			}
		});
	});
	
	this.load();
	
	
	};
  
  reset = function() {  

    // Hide the current active video
    $(slideContainer + " ."+activeClass).eq(0).removeClass(activeClass);

    // Get current active video and unprepare it
    sublimevideo.unprepare(this.activeVideoId); // we could have called unprepare() without any arguments, but this is faster
    
    // Deselect its thumb
    this.deselectThumb(this.activeVideoId);
  };
  
  clickOnThumb = function(thumbId) {
    
    // Basically undo all the stuff and bring it back to the point before js kicked in 
    this.reset();
    
    // Set new active video
    this.activeVideoId = thumbId.replace(/thumb_/,'');
    //alert(this.activeVideoId+ " Going to show");
	
    // Show it
    this.showVideo(this.activeVideoId);
   // alert(this.activeVideoId+ " showed");
	
    // PREPARE IT
    sublimevideo.prepare(this.activeVideoId);
   // alert(this.activeVideoId+ " Prepared");
	
    if (this.supportsWebkitAnimation)
    	SublimeVideo.fadeInSublimeVideosContainer(newVideo, 'slow');
	
	//$(".sublime_video_wrapper").attr("style","width: 890px; height: 412px; top: 90px; left: 189px; position: fixed;");
	//alert($(".sublime_video_wrapper").attr("style"));	
	//alert(this.activeVideoId+ " supportsWebkitAnimation");
	//$(".sublime_video_content").attr("style", 'background-color:#666666;');
	//$(".sublime_video_wrapper").attr("style", 'background-color:#FF0000;');
	//alert($(".sublime_video_wrapper").css("top"));
	//alert($(".sublime_video_content").css("top"));
  };
  
  load = function() {
  
    //if not the first time here
    if (this.activeVideoId) { 
    	this.reset();
    }
    
    this.activeVideoId = "video1";
    
    // Show first video
    var firstVideo = this.showVideo(this.activeVideoId);
    sublimevideo.prepare(firstVideo);
    
  };
  
  selectThumb = function(videoId) {
	var video_num	= parseInt(videoId.replace(/video/,''),10);
	$("#slide"+video_num).addClass(activeClass);
	$("#slide"+video_num+" span").addClass(activeClass);
  	//$("#thumb_"+videoId).addClass(activeClass).css("z-index", "100");
	$("#thumb_"+videoId).css("z-index", "100");
	//$("#"+videoId).css("margin-top", "-25px");
  };
  
  deselectThumb = function(videoId) {
    //$("#thumb_"+videoId).removeClass(activeClass).css("z-index", "0");
	var video_num	= parseInt(videoId.replace(/video/,''),10);
	$("#slide"+video_num).removeClass(activeClass);
	$("#slide"+video_num+" span").removeClass(activeClass);
	$("#thumb_"+videoId).css("z-index", "0");
	//alert($(".sublime_video_wrapper").css("top",parseInt($(".sublime_video_wrapper").css("top"))-20));
	
	//alert(parseInt($(".sublime_video_wrapper").css("top").replace("px","")));
	//alert(parseInt($(".sublime_video_content").css("top").replace("px","")));
	
  };
  
  showVideo = function(videoId) {  	
    var video = $("#"+videoId);

    // Show it 
    video.parent().addClass(activeClass);
        
    // Select its thumb
    this.selectThumb(videoId);
    return video;
  };
  
  handleAutoNext = function(endedVideoId) {
    var nextId = parseInt(endedVideoId.replace(/video/,''),10) + 1;
	//alert(nextId);
    if (nextId>1 && nextId<5) {
      	this.clickOnThumb('thumb_video'+nextId);
    }
	
  };
  
  
  initialize();
  
};


