var pbig_1_var = null;
var pbig_2_var = null;
var pbig_3_var = null;
var pbig_4_var = null;

var ppop_var = null;

var pbig_1_rec = new PlayerRec('id_video_00', 'pbig_1_var', 'false', 512, 404);
var pbig_2_rec = new PlayerRec('id_video_p1', 'pbig_2_var', 'false', 512, 404);
var pbig_3_rec = new PlayerRec('id_video_p2', 'pbig_3_var', 'false', 512, 404);
var pbig_4_rec = new PlayerRec('id_video_p3', 'pbig_4_var', 'false', 512, 404);

var ppop_rec = new PlayerRec('id_player', 'ppop_var', 'true', 375, 301);
var ppop_rec_tmp = new PlayerRec('id_player', 'ppop_var', 'true', 375, 301);

//////////////////////////////////////////////////////////////

function PlayerRec(thePlayerID, thePlayerVarName, isAutoStart, pWidth, pHeight) {
	this.player_id = thePlayerID;
	this.player_var_name = thePlayerVarName;
	this.autostart = isAutoStart;
	this.width = pWidth;
	this.height = pHeight;
	this.container_id = null;
}

//////////////////////////////////////////////////////////////

function createPlayer(thePlaceholderID, thePlayerVarName, theFile, isAutoStart, thePreview, pWidth, pHeight) {
	var flashvars = {
			file: theFile, 
			autostart: isAutoStart,
			image: thePreview
	}
	var params = {
			allowfullscreen: 'true', 
			autoplay: isAutoStart,
			allowscriptaccess: 'always',
			wmode: 'transparent'
	}
	var attributes = {
			id: thePlayerVarName,  
			name: thePlayerVarName
	}
	swfobject.embedSWF("/player.swf", thePlaceholderID, pWidth, pHeight, "7.0.0", '', flashvars, params, attributes);
}

//////////////////////////////////////////////////////////////

function playerReady(thePlayer) {
	if (thePlayer.id == pbig_1_rec.player_var_name) {
		window[pbig_1_rec.player_var_name] = window.document[thePlayer.id];
	}
	if (thePlayer.id == pbig_2_rec.player_var_name) {
		window[pbig_2_rec.player_var_name] = window.document[thePlayer.id];
	}
	if (thePlayer.id == pbig_3_rec.player_var_name) {
		window[pbig_3_rec.player_var_name] = window.document[thePlayer.id];
	}
	if (thePlayer.id == pbig_4_rec.player_var_name) {
		window[pbig_4_rec.player_var_name] = window.document[thePlayer.id];
	}
	if (thePlayer.id == ppop_rec.player_var_name) {
		window[ppop_rec.player_var_name] = window.document[thePlayer.id];
	}
}

//////////////////////////////////////////////////////////////

function CreateBigPlayer(thePlayerRec) {
	idStr = '#' + thePlayerRec.player_id;
	
	if ( $(idStr).length > 0 ) {
		videoFile = $(idStr).attr('href');
		previewPic = $(idStr).attr('rel');
		
		width = $(idStr).attr('width');
		height = $(idStr).attr('height');
		
		if (!(width && height)) {
			width = thePlayerRec.width;
			height = thePlayerRec.height;
		}
		
		autostart = $(idStr).attr('autostart');
		if (!autostart) {
			autostart = thePlayerRec.autostart;
		}
		//thePlayerRec.container_id
		playerContaiter = $(idStr).parent().attr('id');
		if (!playerContaiter) {
			$(idStr).parent().attr('id', thePlayerRec.player_id + '_container');
			thePlayerRec.container_id = playerContaiter = $(idStr).parent().attr('id');
		} else {
			thePlayerRec.container_id = playerContaiter;
		}
		createPlayer(thePlayerRec.container_id, thePlayerRec.player_var_name, videoFile, autostart, previewPic, width, height);
	} 
}

//////////////////////////////////////////////////////////////

function stopPlayer(thePlayerRec) { 
	if (window[thePlayerRec.player_var_name]) {
		playerVarName = thePlayerRec.player_var_name;
		msgStr = playerVarName + ".sendEvent('STOP')";
		setTimeout(msgStr, 0); 	
	}
}

//////////////////////////////////////////////////////////////

function createPopUpPlayer(thePopUpRec, videoFile, previewPic) { 
	player_id = thePopUpRec.player_id;
	player_var_name = thePopUpRec.player_var_name;
	autostart = thePopUpRec.autostart;
	width = thePopUpRec.width;
	height = thePopUpRec.height;
	createPlayer(player_id, player_var_name, videoFile, autostart, previewPic, width, height);
}

//////////////////////////////////////////////////////////////
function deletePopUpPlayer() { 
	swfobject.removeSWF(ppop_rec.player_var_name);
	var tmp = document.getElementById('id_player_wrapper');
	if (tmp) { tmp.innerHTML = "<div id='id_player'>Video</div>"; }
}
//////////////////////////////////////////////////////////////

/// ON LOAD - jQUERY
$(document).ready(function(){
	CreateBigPlayer(pbig_1_rec);
	CreateBigPlayer(pbig_2_rec);
	CreateBigPlayer(pbig_3_rec);
	CreateBigPlayer(pbig_4_rec);
	
	$('.lnk_video').click(function(e) { 
		e.preventDefault();
		//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		videoFile = $(this).attr('href');
		previewPic = $(this).attr('rel');
		
		ppop_rec_tmp.autostart = ppop_rec.autostart;
		ppop_rec_tmp.width = ppop_rec.width;
		ppop_rec_tmp.height = ppop_rec.height;
		
		autostart = $(this).attr('autostart')
		if (autostart) {
			ppop_rec_tmp.autostart = autostart;
		}
		
		width = $(this).attr('width');
		height = $(this).attr('height');
		
		if (width && height) {
			ppop_rec_tmp.width = width;
			ppop_rec_tmp.height = height;
		}

		do_show_pop_up(ppop_rec_tmp);
		createPopUpPlayer(ppop_rec_tmp, videoFile, previewPic);
		
		//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	});

	$('#mask').click(function () {
		$(this).hide();
		deletePopUpPlayer();
	 	$("#id_pop_player").hide(); 
	});	

	$('.lnk_pop_up_close').click(function(e) { 
		e.preventDefault();
		do_close_pop_up();
	});
	/*
	$('#id_command').click(function(){
		stopPlayer(pbig_1_rec);
	});*/
});
/// END OF ON LOAD - jQUERY
//////////////////////////////////////////////////////////////

function do_show_pop_up(thePopUpPlayerRec) {
	stopPlayer(pbig_1_rec);
	stopPlayer(pbig_2_rec);
	stopPlayer(pbig_3_rec);
	stopPlayer(pbig_4_rec);
	calculateMask();
	$('#mask').fadeIn(10);
	$('#mask').fadeTo("fast", 0.6);
	
	width = thePopUpPlayerRec.width;
	height = 28 + parseInt(thePopUpPlayerRec.height);
	
	margin_left = Math.round((32 + parseInt(thePopUpPlayerRec.width)) / 2);
	
	$('#id_pop_player').css('width', width + 'px');
	$('#id_pop_player').css('height', height + 'px');
	$('#id_pop_player').css('margin-left', '-' + margin_left + 'px');
	
	$('#id_pop_player').show();
	do_center_pop_up(ppop_rec);
	$(window).bind('resize', onWindowResize);
	$(document).bind('keydown', onEsc);
}
//////////////////////////////////////////////////////////////
function calculateMask() {
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight});
}
//////////////////////////////////////////////////////////////
var onWindowResize = function(){ 
	calculateMask();
	do_center_pop_up(ppop_rec);
}
//////////////////////////////////////////////////////////////
var onEsc = function(event){ 
   if( event.which == 27) {  // escape, close box
     //alert(event);
	 $(document).unbind('keydown', onEsc);
	 do_close_pop_up();
   } 	
	
}
//////////////////////////////////////////////////////////////
function do_close_pop_up() {
	//stopPlayer(ppop_rec);
	deletePopUpPlayer();
	$('#mask').hide();
	$("#id_pop_player").hide();
	$(window).unbind('resize', onWindowResize);
}
//////////////////////////////////////////////////////////////
function do_center_pop_up(thePopUpPlayerRec) {
	var w = $(window);
	var top = (w.height()-$("#id_pop_player").height())/2 + (w.scrollTop()) + 'px';
	$('#id_pop_player').css('top', top);
	$('#id_pop_player').fadeIn(100); 
	

}
