﻿
Type.registerNamespace("TitanCast");

// Constructor
TitanCast.RelatedVideos = function(element) {
	
	TitanCast.RelatedVideos.initializeBase(this, [element]);
	
	// Private variables

	this._itemCount = 0;
	this._itemWidth = 177;
	this._itemsPerPage = 2;
	this._currentPage = 0;
	this._maxPage = 0;
	this._inTransition = false;
	this._nextButtonStyle = "relatedVideoNextButton";
	this._nextButtonDisabledStyle = "relatedVideoNextButtonDisabled";
	this._prevButtonStyle = "relatedVideoPrevButton";
	this._prevButtonDisabledStyle = "relatedVideoPrevButtonDisabled";

	this._nextButton = null;
	this._prevButton = null;
	this._innerBox = null;
		
	// Public methods
  
	// Delegates
	this._nextClickDelegate = null;
	this._prevClickDelegate = null;
}

TitanCast.RelatedVideos.prototype = {

	// property accessors.
	
	get_itemWidth: function() {
      return this._itemWidth;
  },
  set_itemWidth: function(value) {
      this._itemWidth = value;
  },
  
  get_itemsPerPage: function() {
      return this._itemsPerPage;
  },
  set_itemsPerPage: function(value) {
      this._itemsPerPage = value;
      this._calculateMaxPage();
  },
  
  get_itemCount: function() {
      return this._itemCount;
  },
  set_itemCount: function(value) {
			Sys.Debug.trace('value: ' + value);
      this._itemCount = value;
      this._calculateMaxPage();
  },
  

	// Initialize instance resources
 	initialize: function() 
	{
		var element = this.get_element();
		
		if (!element.tabIndex) element.tabIndex = 0;
		
		// Attach to child elements;
		this._nextButton = $get('rvnb', element);
		this._prevButton = $get('rvpb', element);
		this._innerBox = $get('rvbi', element);
				
		// this ensures that the method runs in the context of the object
		//if (this.completeTransition === null) this.completeTransition = Function.createDelegate(this, this._completeTransition);
		
		if(this._nextButton != null && this._nextClickDelegate === null)
		{
			this._nextClickDelegate = Function.createDelegate(this, this._nextClickHandler);
			Sys.UI.DomEvent.addHandler(this._nextButton, 'click', this._nextClickDelegate);
		}
		
		if(this._prevButton != null && this._prevClickDelegate === null)
		{
			this._prevClickDelegate = Function.createDelegate(this, this._prevClickHandler);
			Sys.UI.DomEvent.addHandler(this._prevButton, 'click', this._prevClickDelegate);
		}
		
		this._toggleButtons();
		
		TitanCast.RelatedVideos.callBaseMethod(this, 'initialize');

		Sys.Debug.trace('Initialize: ' + element.id);
		
	}, 
   	
	// Release instance resources before control is disposed.
  dispose: function() 
  {		
    var element = this.get_element();

		//if(this.completeTransition)
		//	delete this.completeTransition;
			
		if (this._nextClickDelegate) 
    {
			Sys.UI.DomEvent.removeHandler(this._nextButton, 'click', this._nextClickDelegate);
      delete this._nextClickDelegate;
    }	

		if (this._nextClickDelegate) 
    {
			Sys.UI.DomEvent.removeHandler(this._nextButton, 'click', this._nextClickDelegate);
      delete this._nextClickDelegate;
    }	

		TitanCast.RelatedVideos.callBaseMethod(this, 'dispose');
		
		Sys.Debug.trace('Dispose: ' + element.id);
  },
    
	_nextClickHandler: function(event) 
	{
		this._doTransition(true);
	},
	
	_prevClickHandler: function(event) 
	{
		this._doTransition(false);
	},
	
	_doTransition: function(forward)
	{
		if(this._inTransition)
			return;
		
		if(forward && this._currentPage >= this._maxPage)
			return;
			
		if(!forward && this._currentPage <= 0)
			return;	
			
		this._inTransition = true;	
		
		var newPage = this._currentPage;
		
		if(forward)
			newPage++;
		else
			newPage--;
			
		var page_width = 	this._itemWidth * this._itemsPerPage;
		var start_left = this._currentPage * page_width
		var end_left = newPage * page_width;
	
		var rv_tween = new Tween(this._innerBox.style,'left',Tween.regularEaseIn,-start_left,-end_left,.5,'px');
		rv_tween.onMotionFinished = Function.createDelegate(this, function() { this._completeTransition(newPage); });
		rv_tween.start();
	},
	
	_completeTransition: function(newPage)
	{
		this._currentPage = newPage; 
		this._inTransition = false; 
		this._toggleButtons();
	},
	
	_toggleButtons: function ()
	{	
		this._nextButton.disabled = (this._currentPage < this._maxPage) ? false : true;
		this._nextButton.className = this._nextButton.disabled ? this._nextButtonDisabledStyle : this._nextButtonStyle;
		this._prevButton.disabled = (this._currentPage > 0) ? false : true;
		this._prevButton.className = this._prevButton.disabled ? this._prevButtonDisabledStyle : this._prevButtonStyle;
	},
	
	_calculateMaxPage: function()
	{
		if(this._itemCount > 0 && this._itemsPerPage > 0)
		{
			this._maxPage = Math.floor(this._itemCount / this._itemsPerPage) - 1;
			if(this._itemCount % this._itemsPerPage > 0) this._maxPage++;
		}
		else
			this._maxPage = 0;
	}
	
	
}

TitanCast.RelatedVideos.registerClass('TitanCast.RelatedVideos', Sys.UI.Control);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

