$(document).ready(function() {
	var videoCenter = new QCTVideoCenter();
	
	videoCenter.init( $.url.param("videoId") );

});
 
 
var QCTVideoCenter = function() {
	
	// CLASS VARS
	var isDebug = true;
	var defaultVideoId;
	var selectedThumb;
	var videotopplayer;
	var video_title_a;
	var video_date;
	var video_description;
	var STR_WATCH_NOW = "Watch Now";
	var STR_NOW_PLAYING = "Now Playing";
	var HTML_NO_FLASH = "<div class='no_flash'><p>Javascript must be enabled and the Flash plugin must be installed to view these videos.</p><p>Download the <a href='http://get.adobe.com/flashplayer/'>Flash plugin</a>.</p></div>";
//	var VIDEO_DIR = 'http://www.qctconnect.com/images/videocenter-content/';
//	var PLAYER_DIR = 'http://www.qctconnect.com/includes/flash/';
	var HOST = window.location.protocol + "//" + window.location.host;
	var VIDEO_DIR = HOST;
	var PLAYER_DIR = HOST + '/includes/flash/';

var clipboard;
	
	// INIT 
	// Called only on page load
	var init = function(videoId) {
	
		// Retrieve common DOM elements
		videotopplayer = $('#videotopplayer');
		video_title_a = $('.videoinfo .title a');
		video_date = $('.videoinfo .date');
		video_description = $('.videoinfo .description');
	
		// Assign click handlers to all the video thumbs
		$('.videothumb').click(function() {
			showVideo( $(this).attr('id'), true );
			return false;
		});
		
		// Assign click handler to embed code input field
		$("#videotopembed").click(function () {
			$(this).select();
			return false;
		});
		
		// Set up the clipboard, part swf and part JS
		// http://code.google.com/p/zeroclipboard/
		ZeroClipboard.setMoviePath( '/includes/js/ZeroClipboard.swf' );
		clipboard = new ZeroClipboard.Client();
		clipboard.setHandCursor( true );
		clipboard.addEventListener('mouseOver', onClipboardMouseOver);
		clipboard.glue( 'copy_to_clipboard' );

		// Assign click handler to more videos link
		$("#more_videos_link").click( function() {
			//$('#videogroup_more').css({opacity:'0'}).animate({opacity:'100'}, 1000);
			$(this).slideUp("slow");
		 	$("#more_videos").slideDown("slow");
			return false;
		});
		
		// Get or set the id of the video to begin playing
		if (videoId === undefined ) {
			defaultVideoId = $('.videothumb:first').attr('id');
		} else {
			defaultVideoId = videoId;
		}
		

		// Make sure defaultVideoId exists on page		
		if ($('#' + defaultVideoId).length)
		{
			
		}
		else
		{
			// Video not found
			
		}
		
		
		// Begin playing video
		showVideo(defaultVideoId, false);
	}
	this.init = init;
	
	// SHOW VIDEO
	// Displays video and video info
	// Needs valid video id
	var showVideo = function(vId, isScroll) {
		
		// Set selected thumb var
		selectedThumb = $('#' + vId);
		
		// De-select any currently playing thumbs
		$('.playing').each(function(i) {
						
			if ( $(this).attr('id') != $(selectedThumb).attr('id') ) {
				$(this).attr('class', 'videothumb');
				$('.btn span', $(this)).text(STR_WATCH_NOW);
			}
		});
		
		// Stop and remove currently playing movie
		$(videotopplayer).html('');
		
		// Select thumb and update button
		$(selectedThumb).attr('class', 'videothumb playing');
		$('.btn span', $(selectedThumb)).text(STR_NOW_PLAYING);
		
		// Show the video info
		$('.videoinfo .content').css({visibility:'visible'});
		
		// Set the video text info
		$(video_title_a).text( $('.title', $(selectedThumb)).text() );
		$(video_date).text( $('.date', $(selectedThumb)).text() );
		$(video_description).text( $('.description', $(selectedThumb)).val() );
		
		// Embed Flash video player
		var poster = $('.poster', $(selectedThumb)).val();
		var video = $('.video', $(selectedThumb)).val();

		// Create embed code
		var embedWidth = 360;
		var embedHeight = 223;
		var embedId = 'qctVideo1';
		var playerPath = PLAYER_DIR + 'qualcomm-video-player.swf';
		var videoPath = VIDEO_DIR + video;
		var imagePath = VIDEO_DIR + poster;
		//var videoPath = video;
		//var imagePath = poster;

		var code = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='" + embedWidth + "' height='" + embedHeight + "' id='" + embedId + "'>  <param name='wmode' value='transparent'><param name='allowFullScreen' value='false' /><param name='movie' value='" + playerPath + "' /><param name='play' value='false' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><param name='FlashVars' value='video=" + videoPath + "&autoPlay=false&img=" + imagePath + "' /><embed src='" + playerPath + "' play='false' quality='high' bgcolor='#ffffff' width='" + embedWidth + "' height='" + embedHeight + "' name='" + embedId + "' FlashVars='video=" + videoPath + "&autoPlay=false&img=" + imagePath + "' allowFullScreen='false' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object>";
		
		$('#videotopembed').val(code);
		
		// Create Flash embed object
		var autoPlay = (isScroll) ? ('true') : ('false');
		//var flashObj = { src: PLAYER_DIR + 'qct_video_player.swf', width: 640, height: 380, flashvars: { img: VIDEO_DIR + poster, video: VIDEO_DIR + video, autoPlay: autoPlay } };
		var flashObj = { src: PLAYER_DIR + 'qualcomm-video-player.swf', width: 640, height: 380, wmode: 'transparent', flashvars: { img: poster, video: video, autoPlay: autoPlay } };
				
		// Embded the flash video player
		// Embed after animated scrolling if requested
		// Safari can only scroll 'body', others 'html'
		
		if (isScroll == true)
		{
			var targetOffset = $('#videotopplayer').offset().top;
			targetOffset = targetOffset - 20;
			if($.browser.safari) {
				$('body').animate({scrollTop: targetOffset}, 400, function(){
					embedFlash(flashObj);
				});
			} else {
				$('html').animate({scrollTop: targetOffset}, 400, function(){
					embedFlash(flashObj);
				});
			}			
		}
		else
		{
			embedFlash(flashObj);
		}
				
	}
	this.showVideo = showVideo;
	
	
	var embedFlash = function(flashObj)
	{
		$('#videotopplayer').flash(flashObj,{ version: 8 }, null, function(){
			// If no Flash installed, show no Flash message:
			$('#no_flash').css({display:'block'});
		});
	}
	this.embedFlash = embedFlash;
	
	
	var onClipboardMouseOver = function() {
		clipboard.setText( document.getElementById("videotopembed").value );
	}
	this.onClipboardMouseOver = onClipboardMouseOver;
	
	
}

