﻿Type.registerNamespace("ViagogoAjax");

ViagogoAjax.TabbedBehavior = function(element)
{
	ViagogoAjax.TabbedBehavior.initializeBase(this, [element]);

	// fields
	this._tabIds;
	this._contentIds;
	this._tabURLs;
	this._activeIndex;
	this._rotateTimeout;

	this._previousIndex = null;

	// private fields
	this._initialized = false;
	this._contentContainer;
	this._hiddenContainer;

	// delegates
	this._tabMouseOverHandler = Function.createDelegate(this, this._onTabMouseOver);
	this._tabMouseOutHandler = Function.createDelegate(this, this._onTabMouseOut);
	this._activeContentMouseOverHandler = Function.createDelegate(this, this._onActiveContentMouseOver);
	this._activeContentMouseOutHandler = Function.createDelegate(this, this._onActiveContentMouseOut);
	this._tabClickHandler = Function.createDelegate(this, this._onTabClick);
	this._rotateTimerTickHandler = Function.createDelegate(this, this._onRotateTimerTick);

	// timers
	this._rotateTimer;
}

ViagogoAjax.TabbedBehavior.prototype =
{
	// public methods

	// event handlers
	_onTabMouseOver : function(args)
	{
		this._stopRotator();
		var tabIndex = 0;
		
		var element = this._getParentTab(args.target);
		
		for(; tabIndex < this._tabIds.length; tabIndex++)
		{
			if(this._tabIds[tabIndex] == element.id)
			{
				break;
			}
		}
		if(tabIndex < this._tabIds.length)
		{
			this.set_activeIndex(tabIndex);
		}
		
	},
	
	_onTabMouseOut : function(args)
	{
		this._startRotator();
	},
	
	_getParentTab: function(childElement)
	{
		while(childElement !== document)
		{
			if(childElement.isTab == true)
			{
				break;
			}					
			childElement = childElement.parentNode;	
		}
		
		return childElement;
	},
	
	_onTabClick : function(args)
	{
	   this._stopRotator();
		var activetab;
		var tabIndex = 0;
		
		var element = this._getParentTab(args.target);
		
		for(; tabIndex < this._tabIds.length; tabIndex++)
		{
			if(this._tabIds[tabIndex] == element.id)
			{
				activetab = tabIndex;
				break;
			}
		}
	   
	   var url = this._getTabURL(activetab);
	   
	   if(url != null)
	   {
			window.location = url;
		}
	},

	_onActiveIndexChanged : function()
	{
		this._updateActiveContent();
		this._updateActiveTab();
	},
	
	_onRotateTimerTick : function()
	{
		this._moveNext();
		this._rotateTimer.set_interval(this._rotateTimeout);
	},
	
	_onActiveContentMouseOver : function()
	{
		this._stopRotator();
	},
	
	_onActiveContentMouseOut : function()
	{
		this._startRotator();
	},

	// private methods
	_updateActiveContent : function()
	{
		var content = $get(this._getContentId(this._activeIndex, "content"));
		if (content)
			this._contentContainer.innerHTML = content.innerHTML;
	},
	
	_updateActiveTab : function()
	{
		var tab = null;
		// previously active content
		if (this._previousIndex != null)
		{
			tab = $get(this._getTabId(this._previousIndex));
			if (tab)
			{
				Sys.UI.DomElement.addCssClass(tab, "go_inactive");
				Sys.UI.DomElement.removeCssClass(tab, "go_active");
				tab.innerHTML = $get(this._getContentId(this._previousIndex, "inactive")).innerHTML;
			}
		}

		// currently active content
		tab = $get(this._getTabId(this._activeIndex));
		if (tab)
		{
			Sys.UI.DomElement.addCssClass(tab, "go_active");
			Sys.UI.DomElement.removeCssClass(tab, "go_inactive");
			tab.innerHTML = $get(this._getContentId(this._activeIndex, "active")).innerHTML;
		}
	},
	
	_getContainerId : function(postfix)
	{
		return $get(this.get_element().id + "_" + postfix);
	},
	
	_getContentId : function(index, postfix)
	{
		 return this._contentIds[index] + "_" + postfix;
	},
	
	_getTabURL : function(index)
	{
		return this._tabURLs[index];
	},
	
	_getTabId : function(index)
	{
		return this.get_element().id + "_Tab" + index;
	},
	
	_startRotator : function()
	{
		if(this._rotateTimeout > 0)
			this._rotateTimer.set_enabled(true);
	},
	
	_stopRotator : function()
	{
		this._rotateTimer.set_enabled(false);
	},
	
	_moveNext : function()
	{
		var newIndex = this._activeIndex + 1;
		if(newIndex >= this._tabIds.length)
		{
			newIndex = 0;
		}
		this.set_activeIndex(newIndex);
	},

	// initialization methods
	_initializeContainerFields : function()
	{
		this._contentContainer = this._getContainerId("contentContainer");
		this._hiddenContainer =  this._getContainerId("hiddenContainer");
	
	},
	
	_enrolListeners : function()
	{
		for(var tabIndex = 0; tabIndex < this._tabIds.length; tabIndex++)
		{
			$addHandlers($get(this._tabIds[tabIndex]), {mouseover:this._tabMouseOverHandler, mouseout:this._tabMouseOutHandler, click:this._tabClickHandler});
		}
		$addHandlers(this._contentContainer, {mouseover:this._activeContentMouseOverHandler, mouseout:this._activeContentMouseOutHandler});
	},
	
	_initializeTimer : function()
	{
		this._rotateTimer = new Sys.Timer();
		this._rotateTimer.set_interval(this._rotateTimeout);
		this._rotateTimer.add_tick(this._rotateTimerTickHandler);
	},
	
	_initializeTabFields : function()
	{
		for(var tabIndex = 0; tabIndex < this._tabIds.length; tabIndex++)
		{
			var tabElement = $get(this._tabIds[tabIndex])
			 tabElement.isTab = true;		
		}
	},

	// constructors and destructors
	initialize : function()
	{
		ViagogoAjax.TabbedBehavior.callBaseMethod(this, "initialize");

		this._initializeContainerFields();
		this._enrolListeners();
		this._initializeTimer();
		this._initializeTabFields();
		this._initialized = true;
		this._startRotator();
	},

	dispose : function()
	{
		ViagogoAjax.TabbedBehavior.callBaseMethod(this, "dispose");
	},

	// accessors

	// tabIds
	get_tabIds : function()
	{
		return this._tabIds;
	},
	set_tabIds : function(value)
	{
		if(this._tabIds != value)
		{
			this._tabIds = value;
			this.raisePropertyChanged("tabIds");
		}
	},
	
	//tabUrls
	get_tabURLs : function()
	{
		return this._tabURLs;
	},
	set_tabURLs : function(value)
	{
		if(this._tabURLs != value)
		{
			this._tabURLs = value;
			this.raisePropertyChanged("tabURLs");
		}
	} ,

	// contentIds
	get_contentIds : function()
	{
		return this._contentIds;
	},
	set_contentIds : function(value)
	{
		if(this._contentIds != value)
		{
			this._contentIds = value;
			this.raisePropertyChanged("contentIds");
		}
	},

	// activeIndex
	get_activeIndex : function()
	{
		return this._activeIndex;
	},
	set_activeIndex : function(value)
	{
		if(this._activeIndex != value)
		{
			this._previousIndex = this._activeIndex;
			this._activeIndex = value;
			if(this._initialized == true)
			{
				this._onActiveIndexChanged();
			}
		}
	},

	// rotateTimeout
	get_rotateTimeout : function()
	{
		return this._rotateTimeout;
	},
	set_rotateTimeout : function(value)
	{
		if(this._rotateTimeout != value)
		{
			this._rotateTimeout = value;
			this.raisePropertyChanged("rotateTimeout");
		}
	}
}
ViagogoAjax.TabbedBehavior.registerClass("ViagogoAjax.TabbedBehavior", Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();