<?php  
	$vrus=array();$vrus_=array();$suff='';
	//ini_set('display_errors', 1);
	//error_reporting(E_ALL);
	
	if(isset($_POST["del_templ"])&&$_POST["del_templ"]=='Удалить'&& strpos($_POST['galtype'],'_user')!==false) {@unlink('templates/'.$_POST['galtype']);
		if(!isset($_GET['gallery_type']))@unlink("../upload/gs_foto/set/".$_GET['id']);
		$gt=substr($_POST['galtype'], 0, strrpos($_POST['galtype'], "_user"));
		header("Location: admin3.php?id=".$_GET['id']."&gallery_type=".$gt);
	} 
	if(isset($_POST["go_templ"])) {	 // здесь найдем свободный номер для пользовательской копии шаблона				
		if($_POST["go_templ"]=="сохранить шаблон" && isset($_POST['galtype'])&& $_POST['galtype']!='') {$suff='_user';
			if (strpos($_POST['galtype'],'_user')!==false ){
				$_POST['galtype']= substr($_POST['galtype'], 0, strpos($_POST['galtype'], "_user"));
			}	
			for ($i=0; $i++<9;) {
				if (!file_exists('templates/'.$_POST['galtype'].$suff.$i)) {break;}
			}$suff.=$i; 
			$_POST['galtype'].=$suff;
		}}	
		
		if (is_file("../upload/gs_foto/set/$_GET[id]") && !isset($_GET['gallery_type']))$template=file_get_contents("../upload/gs_foto/set/$_GET[id]");
		elseif(isset($_GET['gallery_type'])&& $_GET['gallery_type']!='') $template=file_get_contents("templates/".$_GET['gallery_type']);
		else {$template="NotDefined"; $vrus_comment=''; $set=array(); $set[3]='';}
		if ($template!="NotDefined") {
			$vrus_=explode('--',$template); 
			$vrus=explode('*:*',$vrus_[1]);
			$vrus_comment=isset($vrus[1]) ? $vrus[1] : '';
			
			$set_data=explode('//***',$template);
			$set=explode("'",$set_data[1]);
			if (isset($_POST['go_set']) || isset($_POST['go_templ'])) {
				
				$set_data_new="//***"."\r\n"." \$gVar=array('Тип галереи'=>'".trim($_POST['galtype'])."','Id галереи'=>'".$_GET['id']."'";
				
				$x=9;
				
				while (isset($_POST['field-'.$x])){
					$set_data_new.=	",'".$_POST['field-'.$x]."'=>'".trim($_POST[$x+2])."'";
					$x+=4;
				}
				$set_data_new.=");"."\r\n"."//***";
				$template=$set_data[0].$set_data_new.$set_data[2];
				
				
				if (isset ($suff) && $suff!='')
				{file_put_contents('templates/'.$_POST['galtype'],$template);}
				else {
				file_put_contents("../upload/gs_foto/set/$_GET[id]",$template);}
				
				$set_data[1]=$set_data_new;$set=explode("'",$set_data[1]);
			}
		}
		function lang_var($key) {require('lang.php');
			if (isset($lang[trim($key)]))  return  $lang[trim($key)];
			else return trim($key);
		} 
		function file_list($dir) {$file_array=array();
			if (is_dir($dir))
			{$d=$dir.'/*';
				$file_array=glob($d);
				if (!$file_array || $file_array===FALSE) $file_array=array();
			foreach ($file_array as $k=>$v) {$file_array[$k]=basename($v);}}
			return $file_array;
		}   
		$Galleries= file_list("templates"); 
		
?>
<!DOCTYPE html><!-- HTML5 -->
<html lang="ru-RU" dir="ltr">
	<head>
		<title>Настройки</title>
		<meta charset="utf-8" />
		<!--[if IE]><meta http-equiv="ImageToolbar" content="False" /><![endif]-->
		<link rel="stylesheet" type="text/css" href="../../style/reset.css" media="screen,print" />
		<link rel="stylesheet" type="text/css" href="../../sg_cm/sg_cm/style/cap2.css" media="screen" />
	</head>
	<body onload="
	window.onscroll = function() {
	document.getElementById('rgb').style.display ='none';}" >
		<div id=rgb style="display:none; position:fixed;z-index:10;margin-left:30px; overflow:visible; "> 
			<img 
			onclick='obj=document.getElementById("rgb");obj.style.display="none";'
			src="../../sg_cm/sg_images/imClose.png" style="position: absolute; right: -12px; top: -12px; cursor: pointer; display: block; width: 24px; height: 24px;">
			<script type="text/javascript">
				function doSelectFx()
				{optSel=document.getElementById('ssTransition');
					var selected_eff=optSel.options[optSel.selectedIndex].text;selected_eff_val=optSel.options[optSel.selectedIndex].value;
					if (selected_eff_val!='' && optSel.options[optSel.selectedIndex].value!='null' ){obj_field=document.getElementById('fx_names');obj_field.value+=', '+selected_eff;
					obj_field=document.getElementById('codes');obj_field.value+=', '+selected_eff_val;}}
					
					function doTransitions() { optSel=document.getElementById('ssTransition');
						var text_fx = document.getElementById('fx_names').value;
						arr_effects= text_fx.split(',');
						var i;var j;count=arr_effects.length; 	var arr_transitions = '';
						for (var i = 0; i < count ; i++) {
							var v=arr_effects[i].trim(); 
							for (var j = 0; j < optSel.length; j++) {
								var t=optSel[j].text.trim(); 
								if (v==t) {var text_index=j;}
							}
							arr_transitions+=optSel[text_index].value+',';
						}
						document.getElementById('codes').value=arr_transitions
					}
					
					function doMenu(item, name) {fname=name; obj=document.getElementById(item); if (obj.style.display=="none") { obj.style.display="block"; } else { obj.style.display="none";} }							
					v=new Array('00','33','66','99','CC','FF');
					v1=new Array('90','A8','C0','D8','E8','transparent','00','18','30','48','68','78');
					function dw(z)
					{document.write(z)}
					function dw1(z)
					{document.write(z)}
					function showit(i,j,k)
					{ var mycol='#'+v[i]+v[j]+v[k];
						document.getElementsByName(fname)[0].value=mycol;
						document.getElementsByName(fname)[0].style.backgroundColor=mycol;
						if (i<6 && j<4 && k<6) document.getElementsByName(fname)[0].style.color='white';
						else document.getElementsByName(fname)[0].style.color='black';
					}
					function showit1(i,j,k)
					{ 
						if (v1[i]!='transparent') {var mycol='#'+v1[i]+v1[j]+v1[k];} 
						
						else	{var mycol=v1[i];}
						document.getElementsByName(fname)[0].value=mycol;
						document.getElementsByName(fname)[0].style.backgroundColor=mycol;
						if (v1[i]<91 && v1[i]==v1[j] && v1[i]==v1[k] ) document.getElementsByName(fname)[0].style.color='white'; 
						else document.getElementsByName(fname)[0].style.color='black';
					}
					function MakeTable()
					{
						dw('<table style=" background-color:white; border: double 4px; border-collapse:separate; border-spacing:1px; cursor:default;" width=564px>')
						for(i=0; i<v.length; i++)
						{dw('<tr>')
							for(j=0; j<v.length; j++) {
								for(k=0; k<v.length; k++) {
									var t=v[i]+v[j]+v[k]; if (t=='000000') t='черный'; 	if (t=='FFFFFF') t='белый';  
								dw('<td style=\' padding:0px; background-color:#'+v[i]+v[j]+v[k]+';\' onclick=showit('+i+','+j+','+k+') title="'+t+'">&nbsp;</td>')}}
								var t=v1[i]+v1[i]+v1[i]; if (t=='000000') t='черный'; 	if (t=='FFFFFF') t='белый';  
								if(i != 5) {
								dw1('<td style=\' padding:0px; background-color:#'+v1[i]+v1[i]+v1[i]+';\' onclick=showit1('+i+','+i+','+i+') title="'+t+'">&nbsp;</td>')}
								else {dw1('<td style=\' padding:0px; width:14px; text-align:center; background-color:'+v1[i]+';\' onclick=showit1('+i+','+i+','+i+') title="прозрачный">░</td>')}
								
								var m=i+6;var t=v1[m]+v1[m]+v1[m];	if (v1[m]=='00') t='черный'; 	if (v1[m]=='FF') t='белый'; 
								dw1('<td style=\' padding:0px; background-color:#'+v1[m]+v1[m]+v1[m]+';\' onclick=showit1('+m+','+m+','+m+') title="'+t+'">&nbsp;</td></tr>')
						}
						dw('</table>')
					}
					MakeTable()
			</script>  
		</div>
		<form  method=post >
			<table class=tab1>
				<tr><td>
					<div style="position:fixed;width:92% ">
						<input title="Сохранить настройки галереи" type=submit value=сохранить id=button name=go_set >
						<input title="Сохранить как пользовательский шаблон" type=submit value="сохранить шаблон" id=button2 name=go_templ style=" background-color:white;color:#0066CC; background-image:none" >
						<? if (strpos($set[3],'_user')!==false || (isset($_GET['gallery_type'])&&strpos($_GET['gallery_type'],'_user')!==false)) {?>
							<button class="knob b2"  TYPE="submit" NAME="del_templ"  title="Удалить этот шаблон" VALUE="Удалить" onclick="return confirm('Вы хотите удалить этот шаблон?')" 
							><img src="../sg_images/del.png" title="Удалить этот шаблон" alt="Удалить" style="vertical-align: baseline">
							</button><?}?>
					</div>
				</td></tr>
				<tr><td> 
					<table class=tab3>
						<tr>
							<td> Тип галереи<br />
								<? echo $vrus_comment; $vrus=array();$vrus_=array();?>
							</td>
							<td> <SELECT onchange="var gtype=this.options[this.selectedIndex].value;var hr=window.location.href+'&gallery_type='+gtype; window.location=hr;"
								style="width:200px; height: 24px; vertical-align: bottom; padding=0" NAME="galtype"  SIZE="1">
									<OPTION  VALUE="" >Не определён</OPTION>
									<?php foreach ($Galleries as $k=>$v) {?>
										<OPTION  VALUE="<?php echo $v.'"';
											if ($v==$set[3]) {echo 'selected';}
										?> >
										<?php $vrus_arr=file('templates/'.$v); 
											$vrus_=explode('--',$vrus_arr[0]);
											if(strpos($v, "_user")!==false) $usr_suff=substr($v,strpos($v, "_user"));else $usr_suff='';
										$vrus=explode('*:*',$vrus_[1]);echo $vrus[0].$usr_suff; ?>
									</OPTION><?php } ?></SELECT><br />
									</td>
									</tr>
									<? $x=9;
										if (strpos($set[3],'jssor')!==false && is_file('include/Jssor_effects')){ ?>
										<tr>
										<td><? echo lang_var($set[$x]);?> <input  name="<? echo 'field-'.$x;?>" type="hidden"  value="<? echo $set[$x];?>">
										</td>
										<td><? require('include/Jssor_effects');?>
											<input id="codes" name="<? echo $x+2;?>" type="hidden"  value="<? echo $set[$x+2];$x+=4;?>"><p><BR />
											<textarea onchange="doTransitions();" rows=5 id="fx_names" name="<? echo $x+2;?>" style="width:200px; resize:both"><? echo $set[$x+2];?></textarea></p>
											<input  name="<? echo 'field-'.$x;?>" type="hidden"  value="<? echo $set[$x];?>">
										</td>
									</tr>	
								<? $x+=4;}?>
								<? while (isset($set[$x])) {?>
									<tr>
										<td> <? echo lang_var($set[$x]);?> <input  name="<? echo 'field-'.$x;?>" type="hidden"  value="<? echo $set[$x];?>"></td>
										<td> <input 
											<? $x2=$x+2;?> name="<? echo $x2;?>" type="text" size=10 value=<? echo '"'.$set[$x2].'"';if ((stripos ($set[$x], 'color')!==false || stripos (lang_var($set[$x]), '/color/')!==false)&& stripos (lang_var($set[$x]), '/no_color/')===false) echo "onclick=\"this.select();document.getElementsByName('".$x2."')[0].style.backgroundColor=this.value;; doMenu('rgb','".$x2."');\"
												onchange=\"document.getElementsByName('".$x2."')[0].style.backgroundColor=this.value;\">";
											else echo ">";?></td>
										</tr>
									<? $x+=4;}?>
									
								</table></td></tr>
								
					</table>
				</form>
				</body>
			</html>
			
			
				