<?php
//============== ajax functions =====================================$site_url = get_option('siteurl');
	function saveFilesa(){
		echo "alert('save file')";
		die;
	}
	function readFiles(){
	}
	function deleteFiles(){
	}
	function saveRecords(){
	}
	function readRecords(){
	}
	function htmlResponce(){
	}
//============== common functions =====================================
function strlef($s1, $s2) {
	if (strpos($s1, $s2)>0){
		$values = substr($s1, 0, strpos($s1, $s2));
	}else{
		$values = $s1;
	}
	return  $values;
}

//====================== get current URL ====================================
function siteSelfURL($WithQuery,$secureSocket) {
	$s ='';
	$s = empty($_SERVER["HTTPS"]) ? "" : ($_SERVER["HTTPS"] == "on") ? "s" : "";
	$protocol = strlef(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
	if($WithQuery){
		if($secureSocket){
			return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
		}else{
			return str_replace('https','http',$protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']);
		}
	}else{
		if($secureSocket){
			return strlef($protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'],'?');
		}else{
			return strlef(str_replace('https','http',$protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']),'?');
		}
	}
}

//====================== get the skin name from the url ======================== 
function extractSkinName($url){
	Global $wpdb;
	$skinN = $url;
	$blog__id = 0;
	
	//==== check if url is mapped ===========	
	$skinN = substr($skinN, 7, strlen ($skinN)-1);
	$endIndex = strpos($skinN, '/', 0);
	$skinN = substr($skinN, 0, $endIndex);
	$blog__id = $wpdb->get_var("SELECT `blog_id` FROM  wp_domain_mapping WHERE `domain` = '".$skinN."'",0,0);

	if(strpos($url, 'ttp://dwdining.', 0)){
		return 'dwdining';
	}
	if ($blog__id > 0){
		//=== get blog url from database  if it is mapped ===
		$skinN = $wpdb->get_var("SELECT `domain` FROM  wp_blogs WHERE `blog_id` = '".$blog__id."'",0,0);				
		$skinN = strlef($skinN, '.dwdining.');		
	}else{		
		$skinN = strlef($skinN, '.dwdining.');		
	}	
	return $skinN;
}

//====================== constract the css html ===============================
function load_cssFile(){
	global $cssFilePath;
	echo '<link rel="stylesheet" type="text/css" href=" ' . $cssFilePath . '" media="screen, projection" />'. "\r\n";
}

//====================== load css files ======================================
function loadCss($skinName){
	global $cssFilePath;
	//=== load css ===	
	$cssFilePath = CUSTOM_DIR_LOC.'/skins/'.$skinName.'/'.$skinName.'.css';		
	if (!empty($cssFilePath))  {
		add_action('wp_head', 'load_cssFile');
	}else{
		echo '<div> skin css not available </div>';
	}		
}
	
//====================== hook add new blog function ======================================= 
function create_skin_folder (){	
	global $finalPath;	
	//=== get new site name =====  
	$siteName = $_POST['blog']['domain'];	
	$newFolderPath = $finalPath.'\\skins\\'.$siteName.'\\images';
	$newCssFileNamePath = $finalPath.'\\skins\\'.$siteName.'\\'.$siteName.'.css';
	$newTplFileNamePath = $finalPath.'\\skins\\'.$siteName.'\\'.$siteName.'_functions.php';
	
	// === create folder and files if they dont exist ===
	if(!(is_readable($newTplFileNamePath))){
		$dirMade = mkdir($newFolderPath, 0777, true);
		if ($dirMade){
			$fh = false;
			$fh = fopen($newCssFileNamePath, "w+");
			if($fh==false){	
				die("unable to create css file");
			}else{
				$stringData = "/*\n";
				$stringData .= "File:\t\t custom.css\n";
				$stringData .= "Description:\t\t Custom styles for Thesis\n";
				$stringData .= "\n";
				$stringData .= "BASIC USAGE:\n";
				$stringData .= "\n";
				$stringData .= "If you have enabled the custom stylesheet in the Thesis options panel, the <body> tag \n";
				$stringData .= 'will be appended with the "custom" class, like so: <body class="custom">. You can use \n';
				$stringData .= 'the "custom" class to override *any* CSS declarations contained in the style.css file.\n';
				$stringData .= "\n";
				$stringData .= "For example, if you wish to change the default link color to green, you would add the \n";
				$stringData .= "following declarations to this file:\n";
				$stringData .= "\n";
				$stringData .= "\t\t .custom a, .custom a:visited { color: #090; }	<--- This makes links green\n";
				$stringData .= "\t\t .custom a:hover { color: #00f; }	<--- This makes links blue when you mouse over them\n";
				$stringData .= "\n";
				$stringData .= "WHY THIS WORKS:\n";
				$stringData .= "\n";
				$stringData .= 'By using the "custom" class, you are creating more specific CSS declarations for HTML\n';
				$stringData .= "elements. CSS styling is applied through rules of specificity, and because declarations\n";
				$stringData .= "prepended with .custom are more specific, they get applied when the page is rendered!\n";
				$stringData .= "\n";
				$stringData .= "More information about styling your Thesis installation using this file can be found\n";
				$stringData .= "in the User's Guide:\n";
				$stringData .= "\t\t http://diythemes.com/thesis/rtfm/customizing-thesis-with-customcss/\n";
				$stringData .= "*/\n";
				fwrite($fh, $stringData);
				fclose($fh);
			}
			$fh = fopen($newTplFileNamePath, "w+");
			if($fh==false){
				die("unable to create php file");
			}else{
				$stringData  = "<?php\n";
				$stringData .= "//=========== the following lines are required so that the sub theme can work  =====================================\n";
				$stringData .= "remove_action('thesis_hook_header', 'thesis_default_header');\n";
				$stringData .= "remove_action('thesis_hook_before_header', 'thesis_nav_menu');\n";
				$stringData .= "remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');\n";
				$stringData .= "remove_action('thesis_hook_404_title', 'thesis_404_title');\n";
				$stringData .= "remove_action('thesis_hook_404_content', 'thesis_404_content');\n";
				$stringData .= "remove_action('thesis_hook_footer', 'thesis_attribution');\n";
				$stringData .= "//=========== end of lines ================================================================================\n";
				$stringData .= "?>\n";
				fwrite($fh, $stringData);
				fclose($fh);
			}
		}
	}
}
add_action('wpmu_new_blog', 'create_skin_folder');

function delete_skin_folder ($blog_id){
	Global $wpdb, $finalPath;
	//=== get to be deleted site name ===== needs custom table to work	debugger($blogz);
	$siteName = $wpdb->get_var("SELECT `domain` FROM  wp_blogs WHERE `blog_id` = '".$blog_id."'",0,0);				
	$siteName = strlef($skinN, '.dwdining.');		
	$deleteFolderPath = $finalPath.'\\skins\\'.$siteName;	
}
add_action('delete_blog', 'delete_skin_folder');

function voimitJavaScripts(){
	global $initialPath;
	//print out the sack ajax library	
	wp_enqueue_script( 'skinscripts', '/wp-content/plugins/skin-editor/skin_js.js', null, null, false );
}
add_action('init', 'voimitJavaScripts');
?>