<?php
/**
 
 * @author Valiano
 * @copyright (C) 2012 Unite CMS, All Rights Reserved. 
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 * */
defined('_JEXEC') or die('Restricted access');

define("UNITEGALLERY_TEXTDOMAIN", "unitegallery");

class UniteProviderFunctionsUG {
	
	private static $arrScripts = array();
	private static $arrStyles = array();
	private static $arrStylesInline = array();
	
	
    /**
     * init base variables of the globals
     */
    public static function initGlobalsBase() {

        $tablePrefix = DB_PRF;

        GlobalsUG::$table_galleries = $tablePrefix . GlobalsUG::TABLE_GALLERIES_NAME;
        GlobalsUG::$table_categories = $tablePrefix . GlobalsUG::TABLE_CATEGORIES_NAME;
        GlobalsUG::$table_items = $tablePrefix . GlobalsUG::TABLE_ITEMS_NAME;

        $pluginName = "unitegallery";
        
        GlobalsUG::$pathPlugin = ConfigCMS::$pathBase."unitegallery/";
        
        GlobalsUG::$path_media_ug = GlobalsUG::$pathPlugin . "unitegallery-plugin/";
                
        GlobalsUG::$path_base = realpath(ConfigCMS::$pathBase."/..")."/";
        
        GlobalsUG::$path_images = ConfigCMS::$pathImages;
        
        GlobalsUG::$path_cache = GlobalsUG::$pathPlugin . "cache/";
                
        GlobalsUG::$url_base = ConfigCMS::$urlSite;
        
        //dmp(ConfigCMS::$urlSite); exit();
        
        $urlAdmin = ConfigCMS::$urlBase;
        
        GlobalsUG::$urlPlugin = $urlAdmin.$pluginName."/";
        
        GlobalsUG::$url_component_client = "";
        
        GlobalsUG::$url_component_admin = $urlAdmin . "index.php?option=ug";
        
        GlobalsUG::$url_ajax = GlobalsUG::$url_component_admin.'&view=ajax';
		
        GlobalsUG::$url_ajax_front = ConfigCMS::$urlAjaxFront;
        
        GlobalsUG::$url_media_ug = GlobalsUG::$urlPlugin . "unitegallery-plugin/";
                
        GlobalsUG::$url_images = ConfigCMS::$urlImages;
        
       //GlobalsUG::printVars();
    }

    
    /**
     * add scripts and styles framework
     */
    public static function addScriptsFramework() {

        HelperUG::addStyle("jquery-ui.structure.min", "jui-smoothness-structure", "css/jui/new");
        HelperUG::addStyle("jquery-ui.theme.min", "jui-smoothness-theme", "css/jui/new");
        
        HelperUG::addScript("jquery-ui.min", "js","jquery-ui");
		
    }

    
    /**
     * 
     * register script
     */
    public static function addScript($handle, $url) {

        if (empty($url))
            UniteFunctionsUG::throwError("empty script url, handle: $handle");
        
        if(empty($handle))
        	$handle = UniteFunctionsUG::getRandomString(20);
        
        if(array_key_exists($handle, self::$arrScripts) == false)        
       		 self::$arrScripts[$handle] = $url;
    
    }

    
    /**
     *
     * register script
     */
    public static function addStyle($handle, $url) {

        if (empty($url))
            UniteFunctionsUG::throwError("empty style url, handle: $handle");
        
        if(empty($handle))
        	$handle = UniteFunctionsUG::getRandomString(20);
        
        if(array_key_exists($handle, self::$arrStyles) == false)
        	self::$arrStyles[$handle] = $url;
        
    }

    
    /**
     * add inline styles
     */
    public static function addStyleInline($style){
    	
    	if(!empty($style))
    		self::$arrStylesInline[] = $style;
    }
    
    
    /**
     * print inline styles
     */
    public static function printInlineStyles(){
    	
    	//print inline styles
    	if(!empty(self::$arrStylesInline)){
    	
    		echo "<style type='text/css'>"."\n";
    	
    		foreach(self::$arrStylesInline as $style){
    			echo $style."\n";
    		}
    	
    		echo "</style>"."\n";
    	}
    	
    	self::$arrStylesInline = array();
    }
    
    
    /**
     * print styles, then empty styles array
     */
    public static function printStyles(){
    	
    	foreach(self::$arrStyles as $style){
    		
    		echo "\n<link rel='stylesheet' type='text/css' href='" . $style . "'>\n";
    		
    	}
    	
    	self::$arrStyles = array();
    
    	self::printInlineStyles();
    }
    
    
    /**
     * print scripts then empty scripts array
     */
    public static function printScripts(){
    	
    	foreach(self::$arrScripts as $handle => $script){
    		
    		if(ConfigCMS::$isAdmin == true && $handle == "jQuery")
    			continue;
    		
    		echo "\n<script type='text/javascript' src='{$script}'></script>";
    	}
    	
    	self::$arrScripts = array();
    }
    
    
    /**
     *
     * sanitize data, in joomla no need to sanitize
     */
    public static function normalizeAjaxInputData($arrData) {
		
        return $arrData;
    }

    
    /**
     * put footer text line
     */
    public static function putFooterTextLine() {
        ?>
        &copy; <?php _e("All rights reserved", UNITEGALLERY_TEXTDOMAIN) ?>, <a href="http://codecanyon.net/user/valiano" target="_blank">Valiano</a>
        <?php
    }

    
    /**
     * add jquery include
     */
    public static function addjQueryInclude($app, $urljQuery = null) {
    	
    	if(ConfigCMS::$isAdmin == false)
    		HelperUG::addScript("jquery-1.11.1.min","js","jquery");
    	
    }


    /**
     * add position settings (like shortcode) based on the platform
     */
    public static function addPositionToMainSettings($settingsMain){
    	
    	//$settingsMain = new UniteSettingsAdvancedUG();
    	
        $textGenerate = __("Generate code", UNITEGALLERY_TEXTDOMAIN);
        $descShortcode = __("Insert this code to your web page, to the header and body parts accordingly", UNITEGALLERY_TEXTDOMAIN);
        $style = "width:500px;height:80px;";
        
        $settingsMain->addTextArea("phpcode", "", __("Front End PHP Code", UNITEGALLERY_TEXTDOMAIN), array("description" => $descShortcode, "readonly" => true, "class" => "input-alias input-readonly", "style"=>$style, "addtext" => "&nbsp;&nbsp; <a id='button_generate_shortcode' class='unite-button-secondary' >{$textGenerate}</a>"));
        
        return($settingsMain);
    }

    
    /**
     * add tile size related settings
     */
    public static function addTilesSizeSettings($settings) {

        $arrSizes = array();
        $arrSizes["medium"] = "Medium (max width - 300)";
        $arrSizes["large"] = "Large (max width - 768)";
        $arrSizes["full"] = "Full";

        $params = array(
            "description" => __("Tiles thumbs resolution. If selected 'Large', The thumbnails will be generated on the first gallery output", UNITEGALLERY_TEXTDOMAIN)
        );

        $settings->addHr();

        $settings->addSelect("tile_image_resolution", $arrSizes, "Tile Image Resolution", "medium", $params);

        return($settings);
    }

    /**
     * get thumb width by size name
     */
    public static function getThumbWidth($sizeName) {

        switch ($sizeName) {
            case "medium":
                return(GlobalsUG::THUMB_WIDTH);
                break;
            case "large":
                return(GlobalsUG::THUMB_WIDTH_LARGE);
                break;
            default:
                UniteFunctionsUG::throwError("Wrong thumb size");
                break;
        }
    }

    /**
     * print custom script
     */
    public static function printCustomScript($script, $hardCoded = true) {

        echo "<script type='text/javascript'>{$script}</script>";
    }
	
    /**
     * replace dialog shortcode
     */
    public static function dialogShortcodeReplace(){
    	
    	$objCategories = new UniteGalleryCategories();
    	$arrCats = $objCategories->getCatsShort("component");
    	$htmlSelectCats = UniteFunctionsUG::getHTMLSelect($arrCats, "", "id='ds_select_cats'", true);
    	
    	?>
    	
			<div id="dialog_shortcode" class="unite-inputs" title="<?php _e("Generate Shortcode",UNITEGALLERY_TEXTDOMAIN)?>" style="display:none;">	
				<br><br>
				
				<div class="mbottom_5">
					<?php _e("Generate gallery front end code for different categories",UNITEGALLERY_TEXTDOMAIN)?>:
				</div>
				
				<br>
							
				<textarea id="ds_shortcode" class="ug-textarea-generate input-readonly"></textarea>
				
				<div class="vert_sap20"></div>

				<div class="mbottom_5">				
				<?php _e("Select category below", UNITEGALLERY_TEXTDOMAIN)?>:
				</div>
				
				<?php echo $htmlSelectCats?>
				<div class="vert_sap20"></div>
				
			</div>
    	
    	
    	<?php 
    }
    
}
?>