var editor_opened = "";
var filter_count = 0;
var filter_types = new Array();
var filter_options = new Array();
var filter_values1 = new Array();

function FCKeditor_OnComplete( editorInstance )
{
    editorInstance.Events.AttachEvent( 'OnFocus', DoSomething ) ;
}

function DoSomething( editorInstance )
{
	ajaxcall('set_edited');
}


function ajaxcall(funcstr,param1,param2) {
jQuery(document).ready(function() {
	switch (funcstr) {
		case ('showparams') : 			
				if (editor_opened != "") {		
					oEditor = FCKeditorAPI.GetInstance('details_ukr');
					$("#t_items_field_details_ukr_"+param2).attr("value", oEditor.GetHTML());				
				}
				
				$('div.editors').hide("normal");	
				$('div.anotherfields').hide("normal");	
				$('div.editors').html("");
				if (editor_opened != param2) {
					$('#'+param1).show("normal");	
					$('#anotherfields_'+param2).show("normal");						
					editor_opened = param2;
					$('#'+param1).load("orderproc.php", {get_editor : 1, id : param2});
					} else {
					editor_opened = "";
					}
				break;	
		case ('get_tree') : 
				var my_text1 = $("#treenodetextukr").attr("value");
				$("#maintree").load("orderproc.php", {text_ukr: my_text1, get_tree : 1});		
				break;
		case ('add_filter') : 
				param1 = $("#filter_value1_add").attr("value");
				if (IsNumeric(param1) && (param1 != "")) {
					param1 = IsNumeric(param1);
					filter_count = (+filter_count) + 1;
					my_type = $("#filter_type_add").attr("value");
					my_option = $("#filter_option_add").attr("value");
					my_value1 = $("#filter_value1_add").attr("value");
					Set_Cookie('filter_count', filter_count, '', '/', '', '' );				
					Set_Cookie('type_'+(+filter_count), my_type, '', '/', '', '' );				
					Set_Cookie('option_'+(+filter_count), my_option, '', '/', '', '' );				
					Set_Cookie('value1_'+(+filter_count), my_value1, '', '/', '', '' );				
					$('#apply_filter').show("normal");	
					Show_Filters();
				}			
				break;
		case ('remove_filter') : 				
					if (filter_count > 0) {
						for (i = param1; i < filter_count; i++) {
							my_type = Get_Cookie("type_"+(i+1));
							my_option = Get_Cookie("option_"+(i+1));
							my_value1 = Get_Cookie("value1_"+(i+1));						
							Set_Cookie('type_'+(+i), my_type, '', '/', '', '' );				
							Set_Cookie('option_'+(+i), my_option, '', '/', '', '' );				
							Set_Cookie('value1_'+(+i), my_value1, '', '/', '', '' );										
						}
						
						Delete_Cookie('type_'+filter_count, '/', '');		
						Delete_Cookie('option_'+filter_count, '/', '');		
						Delete_Cookie('value1_'+filter_count, '/', '');		
						
						filter_count = (+filter_count) - 1;						
						Set_Cookie('filter_count', filter_count, '', '/', '', '' );				
					}
					$('#apply_filter').show("normal");	
					Show_Filters();
					
				break;
		case ('apply_filter') : 	
					document.location.reload();
					$('#apply_filter').hide("normal");						
				break;
		}
	});
}

function Show_Filters () {
	if (Get_Cookie( 'filter_count' )) {
		filter_count = Get_Cookie( 'filter_count' );
		str = "";
		dimension = "";
		for (i = 0; i < filter_count; i++) {
			if (Get_Cookie("type_"+(i+1))) {
			my_type = Get_Cookie("type_"+(i+1));
			my_option = Get_Cookie("option_"+(i+1));
			my_value1 = Get_Cookie("value1_"+(i+1));
			switch (my_type) {
				case ('byprice'):
				str = str + "Ціна ";
				dimension = "грн.";
				break;
				case ('byw'):
				str = str + "Ширина ";
				dimension = "см.";
				break;
				case ('byh'):
				str = str + "Висота ";
				dimension = "см.";
				break;
				case ('byd'):
				str = str + "Глибина ";
				dimension = "см.";
				break;
			}
			switch (my_option) {
				case ('greater'):
					str = str + "більше " + my_value1 +" "+dimension;
				break;
				case ('less'):
					str = str + "менше " + my_value1 + " "+dimension;
				break;
			}
			} else {
			 filter_count = i;
			}
		str = str +	'<img src="/images/minus.gif" align="top" style="cursor: pointer;" onclick="ajaxcall(\'remove_filter\','+(i+1)+')">';
		str = str + "<br>";			
		}
		if (str != '') {
			$("#filters").html(str);
			}
	};
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( j = 0; j < a_all_cookies.length; j++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[j].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}			

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/*
<script type="text/javascript">
// remember, these are the possible parameters for Set_Cookie:
// name, value, expires, path, domain, secure
Set_Cookie( 'test', 'it works', '', '/', '', '' );
if ( Get_Cookie( 'test' ) ) alert( Get_Cookie('test'));
// and these are the parameters for Delete_Cookie:
// name, path, domain
// make sure you use the same parameters in Set and Delete Cookie.
Delete_Cookie('test', '/', '');
( Get_Cookie( 'test' ) ) ? alert( Get_Cookie('test')) : 
alert( 'it is gone');
</script>
*/

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (k = 0; k < sText.length && IsNumber == true; k++)  { 
		Char = sText.charAt(k); 
		if (ValidChars.indexOf(Char) == -1) { IsNumber = false;  }
	}
	return IsNumber;
}


jQuery(document).ready(function() {
	
	if ( Get_Cookie( 'filter_count' ) ) {
		filter_count = Get_Cookie( 'filter_count' );
		Show_Filters();
	};

//jQuery(document).ready(function() {
	$("a.fancy_group").fancybox({
		'overlayOpacity': 0.8,
		'hideOnContentClick': false,
		'callbackOnShow': function() {
			text=$('#fancy_title_main div').html();
			var ap1 = text.indexOf("::")+2;
			var ap2 = text.indexOf("::",ap1+1);			
			if ((ap1 != 0) && (ap2 != 0)) {
				aurl = text.substr(ap1, ap2-ap1);
				text = text.replace("::"+aurl+"::", '<a href="' + aurl + '>Перейти</a>');				
				$('#fancy_title_main div').html(text);
			}			
		}
	}); 

/*
	var esel = new Object();
	esel.big_image = escape('http://www.vsi-mebli.ua/banners/IMG2.png');

	// ====== Ссылка на вашу страницу с информацией о проекте ======
	esel.ad_url = escape('http://www.vsi-mebli.ua/interesting');
	// ===============================================
	
	esel.small_path = 'http://www.vsi-mebli.ua/banners/esel_s.swf'; 
	esel.small_image = escape('http://www.vsi-mebli.ua/banners/IMGs.jpg'); 
	esel.big_path = 'http://www.vsi-mebli.ua/banners/esel_b.swf';
	
	// Do NOT Change anything under this line---------------FINISHED----STOP----------------------------------------------------------
	esel.small_width = '100'; esel.small_height = '100'; esel.small_params = 'ico=' + esel.small_image; esel.big_width = '650'; esel.big_height = '650'; esel.big_params = 'big=' + esel.big_image + '&ad_url=' + esel.ad_url; 
	function sizeup987(){
		document.getElementById('eselcornerBig').style.top = '0px';
		document.getElementById('eselcornerSmall').style.top = '-1000px';
	}
	function sizedown987(){
		document.getElementById("eselcornerSmall").style.top = "0px";
		document.getElementById("eselcornerBig").style.top = "-1000px";
	}
	esel.putObjects = function () {
	document.write('<div id="eselcornerSmall" style="position:absolute;width:'+ esel.small_width +'px;height:'+ esel.small_height +'px;z-index:9999;right:0px;top:0px;">');
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
	document.write('codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"');
	document.write(' id="eselcornerSmallObject" width="'+esel.small_width+'" height="'+esel.small_height+'">');
	document.write(' <param name="allowScriptAccess" value="always"/> ');
	document.write(' <param name="movie" value="'+ esel.small_path +'?'+ esel.small_params +'"/>');
	document.write(' <param name="wmode" value="transparent" />');
	document.write(' <param name="quality" value="high" /> ');
	document.write(' <param name="FlashVars" value="'+esel.small_params+'"/>');
	document.write('<embed src="'+ esel.small_path + '?' + esel.small_params +'" name="eselcornerSmallObject" wmode="transparent" quality="high" width="'+ esel.small_width +'" height="'+ esel.small_height +'" flashvars="'+ esel.small_params +'" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
	document.write('</object></div></script>');
	document.write('<div id="eselcornerBig" style="position:absolute;width:'+ esel.big_width +'px;height:'+ esel.big_height +'px;z-index:9999;right:0px;top:0px;">');
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
	document.write('codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"');
	document.write(' id="eselcornerBigObject" width="'+ esel.big_width +'" height="'+ esel.big_height +'">');
	document.write(' <param name="allowScriptAccess" value="always"/> ');
	document.write(' <param name="movie" value="'+ esel.big_path +'?'+ esel.big_params +'"/>');
	document.write(' <param name="wmode" value="transparent"/>');
	document.write(' <param name="quality" value="high" /> ');
	document.write(' <param name="FlashVars" value="'+ esel.big_params +'"/>');
	document.write('<embed src="'+ esel.big_path + '?' + esel.big_params +'" id="eselcornerBigEmbed" name="eselcornerBigObject" wmode="transparent" quality="high" width="'+ esel.big_width +'" height="'+ esel.big_height +'" flashvars="'+ esel.big_params +'" swliveconnect="true" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
	document.write('</object></div>');
	//setTimeout('document.getElementById("eselcornerBig").style.top = "-1000px";',1000);
	}
	esel.putObjects();
*/
//});
	

});