function swichBlocks2(name1, name2) {
	document.getElementById(name1).style.display ='inline';
	document.getElementById(name2).style.display = 'none';
}
function switch_with_condition2 (name,width,height) {
	if ($$('photo_'+name).value!='') {
		swichBlocks2('file'+name, 'upload_'+name+'_field');
		$$('photo_'+name+'_img').src="fmanager/gallery/image/"+width+"x"+height+"/"+$$('photo_'+name).value;
	}
}
function delete_confirm(){
	return confirm("Уверены, что хотите удалить?");
}
function GetThis(type,T, U) {
	switch (type)  {
	case 'digg': 
		var targetUrl = 'http://digg.com/submit?phase=2&' + 'title=' + encodeURIComponent(T)+ '&url=' + encodeURIComponent(U);
		break;
	case 'technorati': 
		var targetUrl = 'http://technorati.com/faves?add=' + encodeURIComponent(U);
		break;
	case 'delicious': 
		var targetUrl = 'http://del.icio.us/post?title=' + encodeURIComponent(T)+  '&url=' + encodeURIComponent(U);
		break;
	case 'stumbleupon': 
		var targetUrl = 'http://www.stumbleupon.com/submit?title=' + encodeURIComponent(T)+  '&url=' + encodeURIComponent(U);
		break;
	case 'reddit': 
		var targetUrl = 'http://reddit.com/submit?title=' + encodeURIComponent(T)+ '&url=' + encodeURIComponent(U);		
		break;
	case 'facebook': 
		var targetUrl = 'http://www.facebook.com/sharer.php?' + 't=' + encodeURIComponent(T)+ '&u=' + encodeURIComponent(U);		
		break;
	case 'myspace': 
		var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&l=3&' + 't=' + encodeURIComponent(T)
		+ '&u=' + encodeURIComponent(U);
		break;
	}
	window.open(targetUrl);
}
function check_email(thisone) {
	if (!(/\w+@\w+\.[a-z]{2,4}/.test(thisone.value)) && (thisone.value!='')) {
		alert("Неправильный E-mail");
		thisone.select();
	}
}
function $$() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
		element = document.getElementById(element);
		if (arguments.length == 1)
		return element;
		elements.push(element);
	}
	return elements;
}

function addBookmark(url, title)
{
	if (!url) url = location.href;
	if (!title) title = document.title;

	//Gecko
	if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
	//IE4+
	else if (typeof window.external == "object") window.external.AddFavorite(url, title);
	//Opera7+
	else if (window.opera && document.createElement)
	{
		var a = document.createElement('A');
		if (!a) return false; //IF Opera 6
		a.setAttribute('rel','sidebar');
		a.setAttribute('href',url);
		a.setAttribute('title',title);
		a.click();
	}
	else return false;

	return true;
}

//<!--
//<![CDATA[
function menuhide(menunum)
{
	var currentmenu = document.getElementById("navbody" + menunum);
	currentmenu.style.visibility = 'hidden';
}

function menushow(menunum)
{
	var currentmenu = document.getElementById("navbody" + menunum);
	currentmenu.style.visibility = 'visible';
}
//]]>
//-->







function addBookmark(url, title)
{
	if (!url) url = location.href;
	if (!title) title = document.title;

	//Gecko
	if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
	//IE4+
	else if (typeof window.external == "object") window.external.AddFavorite(url, title);
	//Opera7+
	else if (window.opera && document.createElement)
	{
		var a = document.createElement('A');
		if (!a) return false; //IF Opera 6
		a.setAttribute('rel','sidebar');
		a.setAttribute('href',url);
		a.setAttribute('title',title);
		a.click();
	}
	else return false;

	return true;
}


function show_it(id) {

	var my_table=document.getElementById('hide');
	var el=document.getElementById(id);

	if (el.style.display=="none"){
		el.style.display="";
	}
	else
	{
		el.style.display="none";
	}

	//РЎРєСЂС‹РІР°РµРј РїСЂРµРґС‹РґСѓС‰СѓСЋ
	var hid_div=document.getElementById('temp');
	if (hid_div.value!=''){
		var last_div=document.getElementById(hid_div.value);
		last_div.style.display="none";
	}

	hid_div.value=id;

}



function select_submit(){
	document.forms['my'].submit();
}


function show_hide_form() {
	var rl=document.getElementById('reg-link');
	var login=document.getElementById('center');
	var reg=document.getElementById('reg-menu');
	if (rl.childNodes[0].textContent == "Р РµРіРёСЃС‚СЂР°С†РёСЏ") {

		alert(rl.innerHTML);
		rl.innerHTML = 'Р’С…РѕРґ';
	} else {
		reg.style.display="none";
		login.style.display="";
		alert(rl.innerHTML);
		rl.innerHTML = 'Р РµРіРёСЃС‚СЂР°С†РёСЏ';
	}
}


function check_fill(){
	if (document.order.f.value=="" || document.order.phone.value=="" || document.order.email.value=="" || document.order.home.value=="") {
		alert ('Проверте правильность заполенения полей');} else {
			document.order.submit();
		}
}

function put_height(){
	//alert ('frame'+document.getElementById('content2').clientHeight);
	//alert ('body'+document.getElementById('content2').contentDocument.body.offsetHeight);
	//document.getElementById('content2').height=document.getElementById('content2').contentDocument.body.offsetHeight+20;
	document.getElementById('content2').height=document.getElementById('content2').contentWindow.document.body.offsetHeight+20;

}

function chooseMediaUrl(id,type, thumb_size_x, thumb_size_y){
	popupOpen('/fmanager/?type=url&ftype='+type+'&thumb_size_x='+thumb_size_x+'&thumb_size_y='+thumb_size_y+'&id='+id,'fmanager',672,512);
}
function popupOpen(url,name,width,height) {
	if (window.screenLeft && window.screenTop) {
		var left = window.screenLeft+Math.round(window.document.body.offsetWidth/2-width/2);
		var top = window.screenTop+Math.round(window.document.body.offsetHeight/2-height/2);
	} else {
		var left = window.screenX+Math.round(window.innerWidth/2-width/2);
		var top = window.screenY+Math.round(window.innerHeight/2-height/2);
	}
	nw = window.open(url, name, 'top='+top+',left='+left+',width='+width+',height='+height+',resizable=1,status=0, scrollbars=1');
	nw.focus();

}
//***************** slide-show on main  project **************************
current_block = 1;
function click_slide(id) {
	if (t !== undefined) {
		clearTimeout(t);
	}
	slide_show (id);
}

function slide_show (name) {
	for (i=1;i<=5;i++) {
		if ( $('#slide_control_'+i)) {
			$('#slide_control_'+i).css('font-weight','normal');
		}
	}
	if ($('#slide_control_'+name)) {
		$('#slide_control_'+name).css('font-weight','bold');
	}
	$('#slide_'+current_block).css('display','none');
	current_block = name;
	$('#slide_'+current_block).css('display','block');
	if ($$('slide_'+(name+1))) {
		name++;
	} else {
		name = 1;
	}
	t = setTimeout('slide_show('+name+')', 5000);
}
//***************** THE END **************************
// ****************** Calendar mouse events ****************
var nn6 = document.getElementById && !document.all;
var mouseElem;
function selectmouse(e) {
	var fobj = nn6 ? e.target : event.srcElement;
	mouseElem = fobj;
	return;
}

function hideTimer() {
	var fobj = mouseElem;
	if (! fobj) return;
	var area = false;
	for (i = 0; i < 10; i++) {
		if (fobj.parentNode) {
			if (fobj.parentNode.id && fobj.parentNode.id == "calTable") {
				area = true;
				break;
			} else {
				fobj = fobj.parentNode;
			}
		} else {
			clear_calendar();
			break
		}
	}
}
document.onmousemove = selectmouse;
setInterval("hideTimer()", 1000);

var onover = '';
var triangle = '';
function show_fb(who, state, pos) {

	clear_calendar();
	onover = who;
	var blockElement =	document.getElementById(who);
	//	var pos = parseInt(who.substring(5));
	//для других проэктов может понадобится формула . НО для этого нужно строку " blockElement.style.display = state;	" поствить выше условия
	blockElement.style.display = state;
	var xPos = blockElement.clientWidth;
	var pWidth =  blockElement.parentNode.clientWidth;
	//$('#'+who).css('margin-left','-'+(xPos-pWidth+6)+'px') ;

	if (pos > 4 || pos ==0) {
		//$('#'+who).css('margin-left','-233px');
		$('#'+who).css('margin-left','-'+(xPos-pWidth+6)+'px') ;
	}


}

function clear_calendar() {
	ono = document.getElementById(onover);
	if (ono != null) {
		ono.style.display = 'none';
		onover = '';
	}
}
// ****************** THE END ****************
// *********** pages template funcs ***************
pages_templates = Array();

max_templates = 20;
function update_templates_list () {
	$.getJSON('/modules/pages/templates_handler.php',{ajax : "true",type:'update_templates'}
	,function(data) {
		if (data['message']) {
			alert(data['message']);
		} else {
			alert ('Something wrong - no response from the server');
		}
	});
}

function change_env(val) {
	if (val == 'y') {
		$('#wysiwyg').show();
		$('#template').hide();
		unload_temaplates();
	} else {
		$('#template').show();
		$('#wysiwyg').hide();
		load_temaplates();
	}
}

function recount () {
	$('#tamplates_container').children().each(function (i){
		$(this).attr('position',''+(i));
	});
}

function intval (data) {
	tmp = parseInt(data * 1);
	if(isNaN(tmp) || !isFinite(tmp)){
		return 0;
	}
	return tmp;
}

can_add_template = true;
can_operate_block = true;
function operate_block (obj, type) {
	if (!can_operate_block) {
		alert ('Wait a moment please');
		return;
	}
	last_pos = $('#tamplates_container').children().length;
	num = intval(($(obj).parent().parent().attr('position')));
	page_id = ($(obj).parent().parent().attr('page'));
	template_id = ($(obj).parent().parent().attr('template'));
	switch (type)  {
		case 'delete':
		can_operate_block = false;
		num = ($(obj).parent().parent().parent().attr('position'));
		page_id = ($(obj).parent().parent().parent().attr('page'));
		template_id = ($(obj).parent().parent().parent().attr('template'));
		$.getJSON("/modules/pages/templates_handler.php",{ajax : "true", type:'delete', template_id : template_id, page_id : page_id, position_num:num},function (data, textStatus) {
			if (textStatus=='success') {
				add_editor(num,'remove');
				$(obj).parent().parent().parent().parent().children().each(function (i){
					if (i>num) {
						$(this).attr('position',''+(i-1));
					}
				});
				pages_templates[num] = null;
				$(obj).parent().parent().parent().remove();
				can_operate_block =true;
			}
		});
		//delete_block(id,pos);
		break;
		case 'up':
		if (num>0) {
			can_operate_block = false;
			add_editor(num,'remove');
			add_editor((num-1),'remove');
			$.getJSON("/modules/pages/templates_handler.php",{ajax : "true", type:'move', page_id : page_id, position_num:num,position_num2:(num-1)},function (data, textStatus) {
				if (textStatus=='success') {
					$(obj).parent().parent().prev().attr('position',''+(num));
					$(obj).parent().parent().attr('position',''+(num-1)).insertBefore($(obj).parent().parent().prev());
					add_editor((num-1),'add');
					add_editor(num,'add');
					can_operate_block =true;
				}
			});
		}
		break;
		case 'down':
		if (num<(last_pos-1)) {
			can_operate_block = false;
			add_editor(num,'remove');
			add_editor((num+1),'remove');
			$.getJSON("/modules/pages/templates_handler.php",{ajax : "true", type:'move', page_id : page_id, position_num:num,position_num2:(num+1)},function (data, textStatus) {
				if (textStatus=='success') {
					$(obj).parent().parent().next().attr('position',''+(num));
					$(obj).parent().parent().attr('position',''+(num+1)).insertAfter($(obj).parent().parent().next());
					add_editor(num,'add');
					add_editor((num+1),'add');
					can_operate_block =true;
				}
			});
		}
		default:
		break;
	}
}
templates_temp_content='';
function unload_temaplates() {
	add_editor(1,'uninit');
	$('#insert').unbind('click');
	templates_temp_content = $('#tamplates_container').html();
	$('#tamplates_container').empty();
	tinyMCE.execCommand('mceAddControl',false,'ta_text');
	can_add_template = false;
	can_operate_block = false;
}

function load_temaplates() {
	tinyMCE.execCommand('mceRemoveControl',false,'ta_text');
	last_pos = $('#tamplates_container').children().length;
	page_id =  intval ($('#inserter').attr('page'));
	$('#insert').bind('click', function(type){
		if (!can_add_template) {
			alert ('Wait a little please');
			return;
		}
		last_pos = $('#tamplates_container').children().length;
		count = intval($('#templates_count').val());
		var template_id = intval ($('#inserter').val());
		if (count ==0) {
			alert ('1 block will be inserted');
			count =1;
		}
		if (template_id<=0 ) {
			alert ('Please select Type from the list');
		} else if ((last_pos+count)>max_templates) {
			alert ('Cannot add more than '+max_templates+' blocks');
		} else {
			can_add_template = false;
			$.getJSON("/modules/pages/templates_handler.php",{ajax : "true",type:'insert',template_id : template_id, page_id : page_id, count:count,last_pos:last_pos},function (data, textStatus) {
				if (textStatus=='success') {
					for (i = last_pos,j=0; i<(last_pos+count);i++,j++) {
						pages_templates[i] = data[j];
						$('#tamplates_container').append('<div id="block_'+i+'"   position="'+i+'" page="'+pages_templates[i]['page_id']+'" template="'+pages_templates[i]['template_id']+'"><div class="topnav">Sort Order: <a href="javascript:;"  onclick="operate_block(this,\'up\')">UP <img src="image/arr_up.gif" width="11" height="10" /></a> <a href="javascript:;" onclick="operate_block(this,\'down\')">DOWN <img src="image/arr_down.gif" width="11" height="10" /></a> </div><div class="win"><div   style="min-height: 300px;" >'+pages_templates[i]['content']+'</div><div align="right"><input align="texttop" type="button" style="margin: 3px 0px;" class="input_button"  onclick="operate_block(this,\'delete\')" value="Delete"/></div></div></div>');
						add_editor(i, 'add');
					}
					recount();
					can_add_template = true;
				}
			});
		}
	});
	if (templates_temp_content=='') {
		can_add_template = false;
		$.getJSON("/modules/pages/templates_handler.php",{ajax : "true",type:'get_content', page_id : page_id},function (data, textStatus) {
			for (i = last_pos,j=0;data[i];i++,j++) {
				pages_templates[i] = data[j];
				$('#tamplates_container').append('<div id="block_'+i+'"  position="'+i+'" page="'+pages_templates[i]['page_id']+'" template="'+pages_templates[i]['template_id']+'"><div class="topnav">Sort Order: <a href="javascript:;"  onclick="operate_block(this,\'up\')">UP <img src="image/arr_up.gif" width="11" height="10" /></a> <a href="javascript:;" onclick="operate_block(this,\'down\')">DOWN <img src="image/arr_down.gif" width="11" height="10" /></a> </div><div class="win"><div   style="min-height: 300px;" >'+pages_templates[i]['content']+'</div><div align="right"><input align="texttop" type="button" style="margin: 3px 0px;" class="input_button"  onclick="operate_block(this,\'delete\')" value="Delete"/></div></div></div>');
			}
			add_editor(1,'init');
			recount();
			can_add_template = true;
		});
	} else {
		$('#tamplates_container').append(templates_temp_content);
		recount();
		can_add_template = true;
		add_editor(1,'init');
	}
	can_operate_block = true;
}

function add_editor (i, mode) {
	if ('null'== tinyMCE) {
		tinyMCE.init({
			// General options
			mode : "specific_textareas",
			theme : "advanced",
			plugins : "safari,inlinepopups,spellchecker,paste,media,fullscreen,secondrow",
			// Theme options
			theme_advanced_buttons1 : "bold,italic,strikethrough,|,bullist,numlist,blockquote,|,justifyleft,justifycenter,justifyright,|,link,unlink,|,spellchecker,fullscreen,sr_adv",
			theme_advanced_buttons2 : "formatselect,underline,justifyfull,forecolor,|,pastetext,pasteword,removeformat,|,charmap,|,outdent,indent,|,undo,redo",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal: false,
			spellchecker_languages:"+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv",
			dialog_type:"window",

			entities:"38,amp,60,lt,62,gt",
			accessibility_focus: true,
			editor_deselector : "no_editor",
			tab_focus:":prev,:next",

			// Drop lists for link/image/media/template dialogs
			//		template_external_list_url : "/tinymce/lists/template_list.js",
			//		external_link_list_url : "/tinymce/lists/link_list.js",
			//		external_image_list_url : "/tinymce/lists/image_list.js",
			//		media_external_list_url : "/tinymce/lists/media_list.js",

			// Example content CSS (should be your site CSS)
			content_css:"/tinymce/editorarea.css"
		});
	} else {
		if ( mode=='add') {
			$('#tamplates_container>div[position="'+i+'"] textarea').each(function(){
				tinyMCE.execCommand('mceAddControl', false, this.name);
			});
		} else if ( mode=='remove') {
			$('#tamplates_container>div[position="'+i+'"] textarea').each(function(){
				tinyMCE.execCommand('mceRemoveControl', false, this.name);
			});
		} else if (mode=='init') {
			$('#tamplates_container textarea').each(function(){
				tinyMCE.execCommand('mceAddControl', false, this.name);
			});
		} else if (mode=='uninit') {
			$('#tamplates_container textarea').each(function(){
				$(this).html(tinyMCE.get(this.name).getContent())	;
				tinyMCE.execCommand('mceRemoveControl', false, this.name);
			});
		}
	}

}
$(document).ready(function() {
	if ('undefined'!==typeof(tinyMCE)) {
		$.getScript("/engine/js/tinymce_editor.js");
	}
});