(function($){
	
	var SLIDER_DELAY = 5000;
	
	function onSLLoad(plugIn, userContext, sender) {
		window.status += plugIn.id + " loaded into " + userContext + ". ";
	}

	function onSLError(sender, args) {
		console.dir(args);
	}
	
	// Executed when te page is completely loaded
	$(function(){
		
		// Creates the content slider for "What Oracle might you believe"
		$('#mythsList').anythingSlider({
			startStopped    : false, // If autoPlay is on, this can force it to start stopped
			width           : 746,   // Override the default CSS width
			easing          : 'easeInOutQuint',
			buildNavigation : true,
			buildArrows		: true,
			pauseOnHover	: true,
			animationTime	: 1000,
			delay 			: 5000,
			autoPlay		: false,
			autoPlayLocked  : false,  // If true, user changing slides will not stop the slideshow
			resumeDelay     : SLIDER_DELAY, // Resume slideshow after user interaction, only if autoplayLocked is true (in milliseconds).
			stopAtEnd : true,
			onShowStop : function(e, slider){
				setTimeout(function(){
					if (slider.currentPage === slider.pages) { slider.gotoPage(1); }
				}, SLIDER_DELAY);
			}
		});
	
		$('#videoSelector').jcarousel({
			scroll : 4
		});
		
		$('#chatTermsLink').click(function(event){
			window.open($(this).attr('href'), 'chatTerms', 'height=400,width=400,scrollbars=1');
			event.preventDefault();
		});
			
		$('a.video-selector-item').click(function(event){
			var descr = $(this).find('span.description');
			
			var selected = $('#videoSelector').find('.video-selector-item-selected');
			var url = $(this).attr('href');
			var uuid = $(this).attr('uuid');
			
			selected.removeClass('video-selector-item-selected');
			$(this).addClass('video-selector-item-selected');
			
			var mainDescription = $('.mainVideoDescription').find('p');
			var facebookButton = $('.mainVideoDescription').find('iframe');
			
			facebookButton.remove();
			facebookButton = $('<iframe src="http://www.facebook.com/plugins/like.php?href='+url+'&amp;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;font=tahoma&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:200px; height:25px"></iframe>');
			
			$('.mainVideoDescription > div').append(facebookButton);
			
			$('.mainVideoDescription > div').fadeOut(function(){
				mainDescription.text(descr.text());
				$('.mainVideoDescription > div').fadeIn();
			})
			
			$('.mainVideoHolder').empty().html( Silverlight.createObject(
            	"http://www.microsoft.com/showcase/silverlight/player/1/player-en.xap",  // source
            	null,  // parent element
            	"slPlugin",  // id for generated object element
            	{
                	width: "474", 
					height: "266", 
					background: "#FF000000", 
                	enableHtmlAccess : "true",
					allowHtmlPopupwindow : "true",
					autoUpgrade : "true",
					version:"4.0.50401.0"
            	},
            	// See the event handlers in the full example.
            	{ onError: onSLError, onLoad: onSLLoad },
            	"Culture=en-US,Uuid="+uuid+",Autoplay=false,ShowMarketingOverlay=False,MiscControls=FullScreen;Detached,ShowMenu=False,Tabs=,VideoUrl="+url+",Mode=Player", 
            	"context"    // context helper for onLoad handler.
        	) );
			
			event.preventDefault();
		});
		
	});

	
})(jQuery);
