var videoPlayer = {

	movie: null, // to save video player object
	id: 'video-player', // video player id
	width: 352,
	height: 208,
	player_swf: '/swf/ns_player.swf', // location of the player
	
	playlist_url: null, // to save playlist url
	playlist_container: null, // to save playlist object
	playlist_container_id: '#playlist', // playlist object id
	playlist_scroll: 'div.video div.scroll-pane', // playlist scroll class
	playlist_length: 0, // save playlist length
	
	flashvars: null, // to save flash variables 
	video_title: null, // to save video title object
	
	pane: null, // to save scroll pane
	
	// select current video from the playlist
	selectVideo: function(n) {
		if(this.playlist_length < 2) return;
		// remove all seleted and select
		$('li', this.playlist_container).removeClass('video-selected');
		var obj = $('li:eq(' +n+ ')', this.playlist_container);
		obj.addClass('video-selected');
		// change video title
		this.video_title.html($('a',obj).attr('title'));
		// scroll to playlist item
		if(parseInt(n,10) > 2) {
			this.pane[0].scrollTo('#anchor-video-'+n);
		}
	},
	
	// get video player object
	getMovie: function() {
		if(this.movie==null) this.movie = (window[this.id]) ? window[this.id] : document[this.id];
	},
	
	// nextVideo video
	nextVideo: function() {
		this.getMovie();
		this.movie.nextVideo();
	},
	
	// previousVideo video
	previousVideo: function() {
		this.getMovie();
		this.movie.previousVideo();
	},
	
	// resumeVideo video
	resumeVideo: function() {
		this.getMovie();
		this.movie.resumeVideo();
	},

	// muteVideo video
	muteVideo: function() {
		this.getMovie();
		this.movie.muteVideo();
	},

	// unMuteVideo video
	unMuteVideo: function() {
		this.getMovie();
		this.movie.unMuteVideo();
	},	
	
	// open video
	openVideo: function(n) {
		this.getMovie();
		this.movie.openVideo(n);
	},	

	// pause video
	pauseVideo: function() {
		this.getMovie();
		this.movie.pauseVideo();
	},

	// play selected video
	playVideo: function(index) {
		this.getMovie();
		this.movie.playVideo(index);
	},
	
	// load flash video player
	loadPlayer: function() {
	
		var params = {
			scale: "noScale",
			salign: "tl",
			allowscriptaccess:"always",
			wmode: "transparent"
		};

		var attributes = {
			id: this.id,
			name: this.id
		};	
		
		swfobject.embedSWF(this.player_swf, this.id, this.width, this.height, "9.0.115", null, this.flashvars, params, attributes);
		
		// if unable to load player, show error msg
		var wrapper = $('#'+this.id);
		if( wrapper.attr('tagName') != 'OBJECT' ) {
			$('p.video-player-msg',wrapper).show();
		}
	
	},
	
	// start playlist
	onLoadJSON: function(htm) {
	 	// write playlist html
	   	this.playlist_container.html(htm);
	   	if(this.playlist_length > 1) {
	   	
			// video rollover	   	
		   	$('li a', this.playlist_container).hover(
		   		function(e) {
					$(this).addClass('video-over');
		   		},
		   		function(e) {
					$(this).removeClass('video-over');
		   		}
		   	);

		   	// apply playlist items click event
		   	$('li a', this.playlist_container).click(function(e) {
		   		e.preventDefault();
		   		var id = (' ' + this.href).split('#video-')[1];
				videoPlayer.playVideo(id);	   	
		   	});
		   	// start scroll pane
			videoPlayer.pane = $(this.playlist_scroll).jScrollPane({scrollbarMargin:0, showArrows:true, scrollbarWidth: 17});
		
		}
		// load flash video player
		this.loadPlayer();
	},
	
	// load playlist
	loadJSON: function() {
	
		// get playlist object from json
		$.getJSON(this.playlist_url,function(data){

			var htm = '';
			
			videoPlayer.playlist_length = data.playlist.videos.length;
			
			// if playlist has more than 1 video
			if(videoPlayer.playlist_length > 1) {
			
				// save each item html
				$.each(data.playlist.videos, function(i,video){
					htm+='<li><a onclick="omni_track(\'' +video.playlistTitle.split("'").join(' ')+ '\');" href="#video-'+ i +'" id="anchor-video-'+ i +'" class="video-first" title="' +video.screenTitle+ '"> \
							<img src="' +video.thumbnail.split('../').join('')+ '" alt="' +video.playlistTitle+ '" width="46" height="46" /> \
							<span class="title">' +video.playlistTitle+ '<br /></span>\
							<span class="subtitle">'+ video.description +'<br /></span>\
						</a></li>';
				});
			
			} else {
			
				var video = data.playlist.videos[0];
			
				htm+='<li> \
					<p class="video-title">'+video.playlistTitle+'</p> \
					<p class="video-description">'+video.description+'</p> \
				</li>';
				
				videoPlayer.video_title.html(video.screenTitle);
			
			}
			
          	videoPlayer.onLoadJSON(htm);
          	
      	});
	
	},

	// init video player object
	init: function(flashvars) {
	
		// save variables
		this.playlist_container = $(this.playlist_container_id);
		this.playlist_url = flashvars.playlist;
		this.flashvars = flashvars;
		this.video_title = $('#video-title');

		// load json
		this.loadJSON();

	}

};

// video changed handler. The flash video player calls this anytime the video state or index changes.
function videoChangedHandler(index) {
	videoPlayer.selectVideo(index);
};

// send email to friend
function sendMail(name,email,link,to){
	var obj = {};
	obj.name = name;
	obj.email = email;
	obj.link = link;
	obj.to = to;
	$.post("/jsps_hmr/home/templates/sendMail.jsp", obj, function(data) {
	});
};

// end of video signal (not used)
function endOfVideo(index) {
};


