/*
	
	TelogisVideoPlayer
	
	A JavaScript class which creates and displays a popup video browser.
	
	Dependencies: jQuery, jQuery interface plugin, FlowPlayer
	
*/

function TelogisVideoPlayer(max_video, unique_key, vwidth, vheight) {
	var VideoPlayer = this;
	
	VideoPlayer.u = unique_key;
	VideoPlayer.dttop = 0;
	VideoPlayer.total_videos = 0;
	VideoPlayer.max_videos = max_video;
	VideoPlayer.video = new Array(max_video);
	VideoPlayer.vwidth = (typeof(vwidth) != 'undefined') ? vwidth : 520;
	VideoPlayer.vheight = (typeof(vheight) != 'undefined') ? vheight : 330;
	VideoPlayer.currentlyActive = false;
	
	$('a[name='+VideoPlayer.u+']').click(function(e) {
		e.preventDefault();
		VideoPlayer.display();
	});
	
	for (var i = 0; i < VideoPlayer.max_videos; i++) {
		VideoPlayer.video[i] = '';
	}
	
	VideoPlayer.writeHtml = function () {
		document.write('<div id="'+VideoPlayer.u+'_boxes" class="boxes"><div id="'+VideoPlayer.u+'_dialog" class="window"><a href="#" id="'+VideoPlayer.u+'_closelink" class="closelink">close</a><div id="'+VideoPlayer.u+'_video" class="video"><p id="'+VideoPlayer.u+'_vidtitle" class="vidtitle"></p><div class="player"><a href="" style="display:block;width:'+VideoPlayer.vwidth+'px;height:'+VideoPlayer.vheight+'px" id="'+VideoPlayer.u+'_player"></a></div><div id="'+VideoPlayer.u+'_selector" class="selector"><a href="#" id="'+VideoPlayer.u+'_up-arrow" class="up-arrow">Scroll Up</a><div id="'+VideoPlayer.u+'_sc_menu_wrapper" class="sc_menu_wrapper"><div class="sc_menu" id="'+VideoPlayer.u+'_videobox"></div></div><p><a href="#" id="'+VideoPlayer.u+'_down-arrow" class="down-arrow">Scroll Down</a></p></div><div id="'+VideoPlayer.u+'_caption" class="caption"><p id="'+VideoPlayer.u+'_captiontext" class="captiontext"></p></div></div></div><div id="'+VideoPlayer.u+'_mask" class="mask"></div></div>');
		VideoPlayer.loadVideos();
	}
	
	VideoPlayer.display = function (id) {
		if (is_ie6)  {
			// what to do if client is running IE6?
			return alert('Internet Explorer 6 does not support this video. We recommend using Internet Explorer 7 or higher or Firefox 2 or higher.');
		}
		
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$('#'+VideoPlayer.u+'_mask').css({'width':maskWidth,'height':maskHeight});
		$('#'+VideoPlayer.u+'_mask').css('opacity','0.0');
		$('#'+VideoPlayer.u+'_mask').show();
		$('#'+VideoPlayer.u+'_mask').fadeTo(1000,0.5);
		VideoPlayer.render();
		$('#'+VideoPlayer.u+'_dialog').show();
		$('#'+VideoPlayer.u+'_dialog').fadeTo(1000,1);
		$('#'+VideoPlayer.u+'_player').fadeTo(1000,1);
		
		if(typeof(id) == 'undefined') {
			id = 0;
		}
		VideoPlayer.showVid(id);
		
		VideoPlayer.currentlyActive = true;
	}
	
	VideoPlayer.showVid = function (id) {
		if (id < 0 || id >= VideoPlayer.max_videos) return false;
		if (VideoPlayer.video[id] == '') return false;
		$('#'+VideoPlayer.u+'_player').attr('href', VideoPlayer.video[id][0]);
		//$('#'+VideoPlayer.u+'_vidtitle').html(VideoPlayer.video[id][2]);
		//$('#'+VideoPlayer.u+'_captiontext').html(VideoPlayer.video[id][3]);
		for (i = 0; i < VideoPlayer.max_videos; i++) {
			if (VideoPlayer.video[i] != '') {
				if (i == id) {
					flowplayer(VideoPlayer.u+"_player", "/wp-content/themes/telogis/js/flowplayer/flowplayer-3.1.5.swf", {
						plugins: {
							// The controlbar is called "controls". By tweaking this, you can modify its look and feel
							controls: {

								// location of the controlbar plugin
								url: '/wp-content/themes/telogis/js/flowplayer/flowplayer.controls-3.1.5.swf',

								// display properties such as size, location and opacity
								left: 0,
								bottom: 0,
								//top: 0,
								opacity: 0.95,

								// styling properties (will be applied to all plugins)
								background: '#114455',// url(/my/custom/controls.png) no-repeat 3px 6px',
								backgroundGradient: 'low',

								// controlbar specific settings
								timeColor: '#980118',
								all: false,
								play: true,
								stop: true,
								scrubber: true,
								time: true,
								fullscreen: true,
								mute: VideoPlayer.video[id][4],
								volume: VideoPlayer.video[id][4],
								

								// tooltips (since 3.1)
								tooltips: {
									buttons: true,
									fullscreen: 'Enter fullscreen mode'
								}
							}
						}
					});
					$('a[target='+i+'][name='+VideoPlayer.u+'_vidlink]').attr('class', 'vidlink_selected');
				}
				else {
					$('a[target='+i+'][name='+VideoPlayer.u+'_vidlink]').attr('class', '');
				}
			}
		}
		return true;
	}
	
	// close video window functions

	VideoPlayer.closeVidWindow = function () {
		$('#'+VideoPlayer.u+'_dialog').hide();
		$('#'+VideoPlayer.u+'_mask').fadeTo(500, 0.0);
		setTimeout('$(\'#'+VideoPlayer.u+'_dialog\').hide();', 1000);
		VideoPlayer.currentlyActive = false;
	}
	
	VideoPlayer.loadVideos = function () {
		
		// if close image/link or mask are clicked
		$('#'+VideoPlayer.u+'_closelink').click(function (e) {
			e.preventDefault();
			VideoPlayer.closeVidWindow();
			setTimeout('$(\'#'+VideoPlayer.u+'_mask\').css({\'height\':\'0px\'});', 500);
		});
		
		$('#'+VideoPlayer.u+'_mask').click(function () {
			VideoPlayer.closeVidWindow();
			setTimeout('$(\'#'+VideoPlayer.u+'_mask\').css({\'height\':\'0px\'});', 500);
		});
		
		// scrollbar arrows
		$('#'+VideoPlayer.u+'_down-arrow').click(function(e){
			e.preventDefault();
			VideoPlayer.dttop += 65;
			if (VideoPlayer.dttop > (VideoPlayer.total_videos * 100)-315) VideoPlayer.dttop = (VideoPlayer.total_videos * 100)-315;
			$('#'+VideoPlayer.u+'_sc_menu_wrapper').scrollTop(VideoPlayer.dttop);
		})
		
		$('#'+VideoPlayer.u+'_up-arrow').click(function(e){
			e.preventDefault();
			VideoPlayer.dttop -= 65;
			if (VideoPlayer.dttop < 0) VideoPlayer.dttop = 0;
			$('#'+VideoPlayer.u+'_sc_menu_wrapper').scrollTop(VideoPlayer.dttop);
		})
		
		VideoPlayer.total_videos = 0;
		//$('#'+VideoPlayer.u+'_vidtitle').html('');
		//$('#'+VideoPlayer.u+'_captiontext').html('');
		$('#'+VideoPlayer.u+'_videobox').html('');
		$('#'+VideoPlayer.u+'_up-arrow').hide();
		$('#'+VideoPlayer.u+'_down-arrow').hide();
		return true;
	}
	
	VideoPlayer.getNextVideoID = function () {
		for (i = 0; i < VideoPlayer.max_videos; i++)
			if (VideoPlayer.video[i] == '')
				return i;
		return -1;
	}
	
	VideoPlayer.addVideo = function (url, imageurl, title, caption, showaudiobuttons) {
		if (typeof showaudiobuttons == 'undefined') {
			showaudiobuttons = true;
		}
		var id = VideoPlayer.getNextVideoID();
		if (id < 0 || url == '') return -1;
		VideoPlayer.video[id] = new Array(url, imageurl, title, caption, showaudiobuttons);
		$('#'+VideoPlayer.u+'_videobox').html($('#'+VideoPlayer.u+'_videobox').html() + '<div class="vidlink"><a href="#" target="' + id + '" title="' + title + '" name="'+VideoPlayer.u+'_vidlink"><img src="' + imageurl + '" alt="' + title + '"></a></div>');
		VideoPlayer.total_videos++;
		$('a[name='+VideoPlayer.u+'_vidlink]').unbind('click');
		$('a[name='+VideoPlayer.u+'_vidlink]').click(function (e) {
			e.preventDefault();
			VideoPlayer.showVid($(this).attr('target'));
		});
		return id;
	}
	
	VideoPlayer.removeVideo = function (id) {
		if (video[id] == '') return false;
		var oldhtml = $('#'+VideoPlayer.u+'_videobox').html();
		$('#'+VideoPlayer.u+'_videobox').html(oldhtml.replace('<div class="vidlink"><a href="#" target="' + id + '" title="' + video[id][2] + '" name="'+VideoPlayer.u+'_vidlink"><img src="' + video[id][1] + '" alt="' + video[id][2] + '"></a></div>', ''));
		VideoPlayer.video[id] = '';
		VideoPlayer.total_videos--;
		return true;
	}
	
	VideoPlayer.render = function () {
		var winH = $(window).height();
		var winW = $(window).width();
		var diaH = VideoPlayer.vheight + 60;
		var diaW = VideoPlayer.vwidth + 60;
		if (VideoPlayer.total_videos < 4) {
			$('#'+VideoPlayer.u+'_up-arrow').hide();
			$('#'+VideoPlayer.u+'_down-arrow').hide();
			if (VideoPlayer.total_videos < 2) {
				$('#'+VideoPlayer.u+'_selector').hide();
			} else {
				diaW += 120;
			}
		} else {
			$('#'+VideoPlayer.u+'_up-arrow').show();
			$('#'+VideoPlayer.u+'_down-arrow').show();
			$('#'+VideoPlayer.u+'_selector').show();
			diaW += 120;
		}
		
		$('#'+VideoPlayer.u+'_video').css({'height':(VideoPlayer.vheight)+'px','width':'100%'});
		$('#'+VideoPlayer.u+'_dialog').css({'height':diaH+'px','width':diaW+'px'});
		$('#'+VideoPlayer.u+'_dialog').css('left', winW / 2 - diaW / 2);
		$('#'+VideoPlayer.u+'_dialog').css('top',  winH * 0.5 - diaH / 2);
		
		$('#'+VideoPlayer.u+'_mask').css('z-index', '998');
		$('#'+VideoPlayer.u+'_dialog').css('z-index', '999');
		$('#'+VideoPlayer.u+'_player').css('z-index', '999');
	}
}
