/**
 * player class
 *
 * [2009-10-01 js] created
 *
 * $Rev: 480 $ Revision of last commit
 * $Author: jan $ Author of last commit
 * $Date: 2008-09-26 08:30:09 +0200 (Fr, 26 Sep 2008) $ Date of last commit
 */
var Player = (function() {
	
	var 
		// player
		otf = null;		
	
		function getClientHeight() {
				
			if (document.all) {
				
				var body = (document.compatMode && document.compatMode == "CSS1Compat") ? 
				           document.documentElement : 
				           document.body || null; 		
				           
				return body.clientHeight;		
			}
			             
			return window.innerHeight; 
		}
		
		function getPageOffsetY() {
						
			return document.all ? 
						 (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) :
						 window.pageYOffset;			
		}
		
		function initPlayer(file, img) {
			
      otf = new SWFObject(path.rel2root + "_videos_inc/mediaplayer.swf","player_onthefly","335","268","8");
      		
      otf.addVariable("file", (file.match(/.mp3$/) ? path.editorial_audio : path.editorial_video) + file);
      otf.addVariable("image", path.editorial_img + img);
      otf.addVariable("flashvars", "&config="+ path.rel2root +"_videos_inc/config.xml");
      otf.setAttribute("style", "position:relative;");
      otf.addParam("wmode","opaque");
      otf.addParam("fullscreen", "false");
      otf.addParam('allowfullscreen','false'); 
      otf.addParam('allowscriptaccess','always');      
      otf.write("otf_player");      			
		}
		
		function unloadPlayer() {
			
			if ($("#otf_player_main_box")) {
				$("#otf_player_main_box").remove();
			}			
			
			if (otf) {
				delete otf;
			} 			
		}
		
	return {

		play: function(file, img, _this, x, y) {
			
			// positions
			var posx = 0, posy = 0;		
			var pageOffsetY = 0;
			
			// otf player height
			var playerHeight = 287;
		
			// default positions based only on mouse coordinates
			posx = mouse.getx() + (!x ? 0 : 0 + x);
			posy = mouse.gety() + (!y ? (playerHeight * -1) : (playerHeight * -1) + y);			
			
			// positions based on current element
			if (typeof _this == "object") {
								
				var current_x = mouse.getx();
				var current_y = mouse.gety();
				
				// Note: perhaps the playerHeight/_this.offsetWidth should be removed for easier calculation
				posx = (current_x + x) - (current_x - $(_this).offset().left) + _this.offsetWidth;				
				posy = ((current_y + y) - (current_y - $(_this).offset().top)) - ( playerHeight );
			}
			
			// "Out of sight in Y-direction" - correction
			// difference between the player and the (scrolled)top page	(player: top-left corner)						
			var diffTop = getPageOffsetY() - posy;										
										
			if (diffTop > 0)	{					
				// + 10 for the distance between the border of the player and top page
				posy = posy + diffTop + 10;
			}				
			// difference between the player and the (scrolled)bottom page (player: top-left corner)							
			var diffBottom = (getPageOffsetY() + getClientHeight()) - (posy + playerHeight);
			
			if (diffBottom < 0) {
				// - 10 for the distance between the border of the player and bottom page
				posy = posy + diffBottom - 10;
			}			
		
			// unload player before a new one start
			this.close();
			
			// create html dom
			var html_player = $("<div id=\"otf_player_main_box\" style=\"position: absolute; top: "+ posy +"px; left: "+ posx +"px; width: 355px; border: 0px solid #A7A7A7; z-index: 99;\">"
			                  + "  <div style=\"position: absolute; top: 7px; right: 5px; width: 40px; background: #333; z-index: 200; color: #000; text-align: center; margin-top: -2px; padding-bottom: 3px; padding-left: 3px; cursor: pointer;\">"
			                  + "    <a onmouseover=\"$(this).css('color', '#E56606');\" onmouseout=\"$(this).css('color', '#FFF');\" style=\"color: #FFF; text-decoration: none;\" href=\"exit: player\" onclick=\"Player.close(); return false;\">exit »</a>"
			                  + "  </div>"
			                  + "  <div class=\"clearfix\" style=\"width: 355px;\">"
			                  + "  	 <div class=\"box-header black\">"
					              + "      <div class=\"lt\"></div>"
					              + "      <div class=\"ct\" style=\"width: 347px;\"></div>"
					              + "      <div class=\"rt\"></div>"
				                + "    </div>"
				                + "    <div class=\"content-area clearfix\" style=\"background: #333; height: 280px;\">"
				                + "   	 <div style=\"padding: 6px 10px;\" id=\"otf_player\"></div>"
				                + "    </div>"
											 	+ "  	 <div class=\"box-footer black\">"
											  + "	  	 <div class=\"lb\"></div>"
											  + "	     <div class=\"ct\" style=\"width: 347px;\"></div>"
											  + "	     <div class=\"rb\"></div>"
											  + "		 </div>"
				                + "  </div>"
			                  + "</div>");
						
			// append otf player to the body (easiest way ;)	
      html_player.appendTo("body");
      
    	// init player
    	initPlayer(file , img);   
		},
		close: function() {
			
			unloadPlayer();			
		}
	}		
})();

function playerReady(mplayer) {

	if ( mplayer["id"] == "player_onthefly") {

		$("#" + mplayer["id"]).get(0).sendEvent("PLAY","true");
	}
}