<?php
/**
* @version		1.0
* @package		Rokbox
* @copyright	Copyright (C) 2008 RocketTheme, LLC. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
*/


// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$_MAMBOTS->registerFunction( 'onAfterStart', 'botRokboxSystem' );

// looks for a reference to mootools in the 
if (!function_exists('mootoolsExists')) {
        function mootoolsExists() {
                global $mainframe;

                foreach($mainframe->_head['custom'] as $row) {
                        if (strpos($row,'mootools') > 0 && strpos($row,'.js') > 0) {
                                return true;
                        }
                }

                return false;
        }
}

function botRokboxSystem()
{
	global $database, $my, $mainframe, $mosConfig_live_site;
	
	$template_path = $mosConfig_live_site ."/templates/" . $mainframe->getTemplate(); 

	// load rokbox properties
	$query = "SELECT params"
	. "\n FROM #__mambots"
	. "\n WHERE element = 'rokbox'"
	. "\n AND folder = 'system'"
	;
	$database->setQuery( $query );
	$database->loadObject($mambot);
	
	$params = new mosParameters( $mambot->params );
	
	$theme = $params->get('theme', 'light');
	
	$rokboxJS = $mosConfig_live_site."/mambots/system/rokbox/rokbox.js";
	$remoteFolder = $mosConfig_live_site."/mambots/system/rokbox/themes";
	$localFolder = dirname($_SERVER['SCRIPT_FILENAME']) . "/mambots/system/rokbox/themes";
	if ($theme == 'custom') $theme = $params->get('custom-theme', 'sample');
	$config_exists = file_exists($localFolder . "/$theme/rokbox-config.js");
	
	$rokboxFix = false;
	$rokboxConfig = false;
	
	$rokbox = "<script type=\"text/javascript\" src=\"" . $rokboxJS . "\"></script>\r\n";
	
	$rokboxStyle = "<link href=\"" . $remoteFolder . "/$theme/rokbox-style.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n";
	// Load style for ie6 or ie7 if exist
	$browser = getBrowser();
	if ($browser == 7 || $browser == 6) {
		if (file_exists($localFolder . "/$theme/rokbox-style-ie$browser.php")) {
			$rokboxFix = "<link href=\"" . $remoteFolder . "/$theme/rokbox-style-ie$browser.php\" rel=\"stylesheet\" type=\"text/css\" />\r\n";
		}
		elseif (file_exists($localFolder . "/$theme/rokbox-style-ie$browser.css")) {
			$rokboxFix = "<link href=\"" . $remoteFolder . "/$theme/rokbox-style-ie$browser.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n";
		}
	}
	
	if ($params->get('custom-legacy', 0) == 1) {
		$rokboxConfig = loadManualConfiguration($theme, $params);
	} else {
		if ($config_exists) {
			$rokboxConfig = "<script type=\"text/javascript\" src=\"" . $remoteFolder . "/$theme/rokbox-config.js\"></script>\r\n";
		} else 
			$rokboxConfig = loadManualConfiguration($theme, $params);
	}
	
	if ($params->get('jslib', 1) && !mootoolsExists()) {
		$mainframe->addCustomHeadTag("<script src=\"$mosConfig_live_site/mambots/system/rokbox/mootools-release-1.11.js\" type=\"text/javascript\"></script>");
	}
	$mainframe->addCustomHeadTag($rokbox);
	$mainframe->addCustomHeadTag($rokboxStyle);
	if ($rokboxFix != false) $mainframe->addCustomHeadTag($rokboxFix);
	if ($rokboxConfig != false) $mainframe->addCustomHeadTag($rokboxConfig);
}

function getBrowser() 
{
	$agent = ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : false;
	$ie_version = false;
			
	if (eregi("msie", $agent) && !eregi("opera", $agent)){
		$val = explode(" ",stristr($agent, "msie"));
		$ver = explode(".", $val[1]);
		$ie_version = $ver[0];
		$ie_version = ereg_replace("[^0-9,.,a-z,A-Z]", "", $ie_version);
	}
	
	return $ie_version;
}

function loadManualConfiguration($theme, $params)
{
	$manual = "<script type=\"text/javascript\">\r\n";
	$manual .= "	if (typeof(RokBox) !== 'undefined') {\r\n";
	$manual .= "		window.addEvent('domready', function() {\r\n";
	$manual .= "			var rokbox = new RokBox({\r\n";
	$manual .= "				'className': '".$params->get('classname', 'rokbox')."',\r\n";
	$manual .= "				'theme': '".$theme."',\r\n";
	$manual .= "				'transition': Fx.Transitions.".$params->get('transition', 'Quad.easeOut').",\r\n";
	$manual .= "				'duration': ".$params->get('duration', 200).",\r\n";
	$manual .= "				'chase': ".$params->get('chase', 40).",\r\n";
	$manual .= "				'frame-border': ".$params->get('frame-border', 20).",\r\n";
	$manual .= "				'content-padding': ".$params->get('content-padding', 0).",\r\n";
	$manual .= "				'arrows-height': ".$params->get('arrows-height', 50).",\r\n";
	$manual .= "				'effect': '".$params->get('effect', 'quicksilver')."',\r\n";
	$manual .= "				'captions': ".$params->get('captions', 1).",\r\n";
	$manual .= "				'captionsDelay': ".$params->get('captionsDelay', 800).",\r\n";
	$manual .= "				'scrolling': ".$params->get('scrolling', 0).",\r\n";
	$manual .= "				'keyEvents': ".$params->get('keyEvents', 1).",\r\n";
	$manual .= "				'overlay': {\r\n";
	$manual .= "					'background': '".$params->get('overlay_background', '#000000')."',\r\n";
	$manual .= "					'opacity': ".$params->get('overlay_opacity', '0.85').",\r\n";
	$manual .= "					'duration': ".$params->get('overlay_duration', '200').",\r\n";
	$manual .= "					'transition': Fx.Transitions.".$params->get('overlay_transition', 'Quad.easeInOut')."\r\n";
	$manual .= "				},\r\n";
	$manual .= "				'defaultSize': {\r\n";
	$manual .= "					'width': ".$params->get('width', '640').",\r\n";
	$manual .= "					'height': ".$params->get('height', '460')."\r\n";
	$manual .= "				},\r\n";
	$manual .= "				'autoplay': '".$params->get('autoplay', 'true')."',\r\n";
	$manual .= "				'controller': '".$params->get('controller', 'true')."',\r\n";
	$manual .= "				'bgcolor': '".$params->get('bgcolor', '#f3f3f3')."',\r\n";
	$manual .= "				'youtubeAutoplay': ".$params->get('ytautoplay', 1).",\r\n";
	$manual .= "				'youtubeHighQuality': ".$this->params->get('ythighquality', 0).",\r\n";
	$manual .= "				'vimeoColor': '".$params->get('vimeoColor', '00adef')."',\r\n";
	$manual .= "				'vimeoPortrait': ".$params->get('vimeoPortrait', 0).",\r\n";
	$manual .= "				'vimeoTitle': ".$params->get('vimeoTitle', 0).",\r\n";
	$manual .= "				'vimeoFullScreen': ".$params->get('vimeoFullScreen', 1).",\r\n";
	$manual .= "				'vimeoByline': ".$params->get('vimeoByline', 0)."\r\n";
	$manual .= "			});\r\n";
	$manual .= "		});\r\n";
	$manual .= "	};\r\n";
	$manual .= "</script>\r\n";
	
	return $manual;
}

?>
