var player = null;

function playerReady(obj) {
	player = document.getElementById(obj.id);
}

function videoViewer(flvFile, playlist) {

	var flvWidth = 600;
	var flvHeight = 492;
	
	if(playlist == 1){
		var flvWidth = 700;
		var flvHeight = 492;
	}

		// Hime some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, select').css({ 'visibility' : 'hidden' });
	$('body').append('<div id="jquery-overlay"></div><div id="jquery-videobox"><div id="videobox-container"><div id="videobox-container-inner"><div id="video"></div><div id="videobox-footer"><a href="#" id="videobox-btn-close"><img src="templates/yt_industries/img/videobox-btn-close.gif"></a></div></div></div></div>');
	
	var arrPageSizes = _getPageSize();
	var arrPageScroll = _getPageScroll();

		// Style overlay and show it
	$('#jquery-overlay').css({
		backgroundColor: '#000',
		opacity:         0.8,
		width:           arrPageSizes[0],
		height:	         arrPageSizes[1]
	}).fadeIn();
	
		// Calculate top and left offset for the jquery-brightbox div object and show it
	$('#jquery-videobox').css({
		top:  arrPageScroll[1] + (arrPageSizes[3] / 10),
		left: arrPageScroll[0]
	}).show();
	
	$('#videobox-container').css({
		width:  flvWidth + 20,
		height: flvHeight + 57
	});
	
		// Assigning click events in elements to close overlay
	$('#videobox-btn-close').click(function() {
		_finish();
		return false;								
	});
	
		// Enable keyboard navigation
	_enable_keyboard_navigation();
	
		// If window was resized or scrolled, calculate the new overlay dimensions
	//$(window).resize(_resize());
	//$(window).scroll(_resize());
	
	$(window).resize(function() {
		var arrPageSizes = _getPageSize();
		var arrPageScroll = _getPageScroll();
		
			// Style overlay and show it
		$('#jquery-overlay').css({
			width:  arrPageSizes[0],
			height: arrPageSizes[1]
		});
		
			// Calculate top and left offset for the jquery-brightbox div object and show it
		$('#jquery-videobox').css({
			top:  arrPageScroll[1] + (arrPageSizes[3] / 10),
			left: arrPageScroll[0]
		});
	});
	
	$(window).scroll(function() {
		var arrPageSizes = _getPageSize();
		var arrPageScroll = _getPageScroll();
		
			// Style overlay and show it
		$('#jquery-overlay').css({
			width:  arrPageSizes[0],
			height: arrPageSizes[1]
		});
	});
	
	

	if(playlist == 1){
		var flashvars = {
			'file': flvFile,
      'playlist':            'right',
      'playlistsize':        '180',
			'skin': 'modieus.zip'
		};
	}else{
		var flashvars = {
			'file': flvFile,
			'autostart': true,
			'skin': 'modieus.zip'
		};
		
	}
	var params = {
    'allowfullscreen':    'true',
    'allowscriptaccess':  'always',
    'bgcolor':            '#ffffff'
	};
	var attributes = {
    	id: 'player1',  
    	name: 'player1'
    }
    
swfobject.embedSWF('player.swf', 'video', flvWidth, flvHeight, '9.0.0', 'false', flashvars, params, attributes);
//swfobject.embedSWF(flvFile, 'video', flvWidth, flvHeight, '9.0.0', 'player.swf', flashvars, params, attributes);	
	$('#videobox-footer').fadeIn('slow');
	
	return false;
}


function _resize() {

	var arrPageSizes = _getPageSize();
	var arrPageScroll = _getPageScroll();
	
		// Style overlay and show it
	$('#jquery-overlay').css({
		width:  arrPageSizes[0],
		height: arrPageSizes[1]
	});
	
		// Calculate top and left offset for the jquery-brightbox div object and show it
	$('#jquery-videobox').css({
		top:  arrPageScroll[1] + (arrPageSizes[3] / 10),
		left: arrPageScroll[0]
	});
}

/**
 * Remove jQuery lightBox plugin HTML markup
 *
 */
function _finish() {
	$('#jquery-videobox').remove();
	$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
		// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, select').css({ 'visibility' : 'visible' });
}

/**
 * Enable a support to keyboard navigation
 *
 */
function _enable_keyboard_navigation() {
	$(document).keydown(function(objEvent) {
		_keyboard_action(objEvent);
	});
}

/**
 * Disable the support to keyboard navigation
 *
 */
function _disable_keyboard_navigation() {
	$(document).unbind();
}

/**
 * Perform the keyboard actions
 *
 */
function _keyboard_action(objEvent) {
		// To ie
	if ( objEvent == null ) {
		keycode = event.keyCode;
		escapeKey = 27;
		// To Mozilla
	} else {
		keycode = objEvent.keyCode;
		escapeKey = objEvent.DOM_VK_ESCAPE;
	}
		// Get the key in lower case form
	key = String.fromCharCode(keycode).toLowerCase();
		// Verify the keys to close the ligthBox
	if ( ( key == 'x' ) || ( keycode == escapeKey ) ) {
		_finish();
	}
}

/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function _getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function _getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
