/**
 * @class UIController Will control event binding for user interface as and also change UI based on events.
 *
 * @author Gerardo Rodriguez - grodriguez@cmdagency.com
 * @created 01/24/2011
 */
function UIController()
{
	/*-------------------------------------------------
	 * Properties
	 *-------------------------------------------------*/
	var self = this;
	var appSettingsObj;
	
	/*-------------------------------------------------
	 * Faux Constructor
	 *-------------------------------------------------*/
	/**
	 * @private Will serve as our faux constructor.
	 */
	this.init = function()
	{
		
	}
	/*-------------------------------------------------
	 * Public Methods
	 *-------------------------------------------------*/
	/**
	 * @public Public method that initiates our setup.
	 * @param {Object} appSettingsObj The app settings object.
	 */
	this.setup = function( appSettingsObj )
	{
		self.appSettingsObj = appSettingsObj;
		self.bindEvents();
		self.setupPlugins();
	}
	/*-------------------------------------------------
	 * Private Methods
	 *-------------------------------------------------*/
	/**
	 * @private Private method that sets up our UI event binding & UI elements.
	 */
	this.bindEvents = function()
	{
		$('#navBar li a').live('click', function(e) {
			var currSelected = $(this).parent().index() + 1;
			$('.gallery').data('gallery').moveTo(currSelected);
		});
		/* bind the video link thumbnail to play the video by embedding the player using Microsoft's provided embed code */
		/* also stop the gallery from autoRotating */
		$('.videoPath').live('click', function(e) {
		  e.preventDefault();
		  var uuid = $(this).closest('.video').attr('title');
		  var path = $(this).attr('href');
		  $('.gallery').data('gallery').autoRotate('stop');
		  $(this).closest('.video').html('<div style=\'width:380px;height:214px\'><object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="380" height="214" id="silverlightControl_' + uuid + '"><param name="source" value="http://www.microsoft.com/global/en-us/showcase/RichMedia/player-en.xap"/><param name="enableHtmlAccess" value="true" /><param name="background" value="#FF000000" /><param name="allowHtmlPopupwindow" value="true" /><param name="windowless" value="true" /><param name="minRuntimeVersion" value="4.0.50401.0" /><param name="autoUpgrade" value="true" /><param name="windowless" value="true" /><param name="initParams" value="Culture=en-US,Uuid=' + uuid + ',Autoplay=true,MarketingOverlayText=Visit this video\'s website,ShowMarketingOverlay=true,MiscControls=FullScreen;Detached,ShowMenu=True,Tabs=Embed;Email;Share;Info,VideoUrl=http:/www.microsoft.com/en-us/showcase/details.aspx?uuid=' + uuid + ',Mode=Player" /><div class="noSilverlight"><a href="http://go.microsoft.com/fwlink/?LinkID=149156"><img src="http://www.microsoft.com/showcase/content/img/installSL.gif" alt="Get Microsoft Silverlight" width="380" height="auto" style="border-style: none"></a><a href="' + path + '"> View this video as a WMV</a></div><!-- <iframe src="http://www.microsoft.com/showcase/video.aspx?uuid=' + uuid + '&locale=en-US" border="0" width="100%" height="100%"/> --></object></div>');
		});
	}
	/**
	 * @private Private method that sets up our plugins.
	 */
	this.setupPlugins = function()
	{
		/* GALLERY PLUGIN SETUP BEGIN */
		$('.gallery').gallery({
			items: '.slide',
			itemsVisible: 1, /*# of items visible*/
			itemsIncrement: 1, /*# of items to move by*/
			controls: {
				prev: 'a.prev',
				next: 'a.next',
				count: 1
			}, /*Takes an obj with 'next' and 'prev' options as references to nav elements {next: '#feature .galleryControl.right',prev: '#feature .galleryControl.left'}*/
			clickable: false, /*Items are clickable and automatically move to the itemsOffset*/
			draggable: false, /* Gallery is draggable*/
			animate: true, /*Does the gallery animate on move*/
			animationDuration: 1000, /*Animation duration*/
			animationEasing: 'swing', /*Animation easing (using jquery easing plugin)*/
			autoRotate: true, /* set auto rotate to true*/
			autoRotateDuration: self.appSettingsObj.autoRotateDuration, /* override the auto rotation duration*/
			pagination: true, /* set whether we want pagination or not*/
			/* onMoveComplete: self.updateCountDisplay,*/
			onPaginationClick: function(gallery) {
				gallery.data('gallery').autoRotate(false);
				$('.video').each(function(){
				    /* get the uuid for the video and use it to pause the player */
    				var uuid = $(this).attr('title');
    				/* pause the player */
    				self.pausePlayer(uuid);
				});
			}
		});

		$('.gallery').data('gallery').moveTo(1);
		/* GALLERY PLUGIN SETUP END */
	}
	
	/**
	 * @private Private method to pause the silverlight plugin player object
	 */
    this.pausePlayer = function(uuid)
    {
        var player = self.getSlMediaPlayer(uuid);
        if (player != null)
        {
            player.Pause();
        }
    }
	/*-------------------------------------------------
	 * Event Handlers
	 *-------------------------------------------------*/

	/*-------------------------------------------------
	 * Getters/Setters
	 *-------------------------------------------------*/
    /**
     * @private Private method that gets the silverlight plugin player object
     */
    this.getSlMediaPlayer = function(uuid) 
    {
        var id = "silverlightControl_" + uuid;
        var slObject = document.getElementById(id);
        var player = null;
        
        if (slObject != null && slObject.Content != null) 
    	{
            player = slObject.Content.MediaPlayer;                
    	}
        
        return player;
    }

	/*-------------------------------------------------
	 * Faux Constructor Init
	 *-------------------------------------------------*/
	/**
	 * We need to call our faux constructor, since it won't actually run by itself.
	 */
	this.init();
}
