function ajaxTextPreview(textId, save, divPreview){
    var text;
    if (BLOG_USE_TINYMCE && tinyMCE && (ed = tinyMCE.get(textId))) {
        text = ed.getContent();
    }
    else {
        text = $(textId).value;
    }
    JsHttpRequest.query(DIR_WEB_ROOT + '/include/ajax/textPreview.php', {
        text: text,
        save: save
    }, function(result, errors){
        if (!result) {
            msgErrorBox.alert('Error', 'Please try again later');
        }
        if (result.bStateError) {
            msgErrorBox.alert('Error', 'Please try again later');
        }
        else {
            if (!divPreview) {
                divPreview = 'text_preview';
            }
            if ($(divPreview)) {
                $(divPreview).set('html', result.sText).setStyle('display', 'block');
            }
        }
    }, true);
}


// для опроса
function addField(btn){
    tr = btn;
    while (tr.tagName != 'TR')
        tr = tr.parentNode;
    var newTr = tr.parentNode.insertBefore(tr.cloneNode(true), tr.nextSibling);
    checkFieldForLast();
}

function checkFieldForLast(){
    btns = document.getElementsByName('drop_answer');
    for (i = 0; i < btns.length; i++) {
        btns[i].disabled = false;
    }
    if (btns.length <= 2) {
        btns[0].disabled = true;
        btns[1].disabled = true;
    }
}

function dropField(btn){
    tr = btn;
    while (tr.tagName != 'TR')
        tr = tr.parentNode;
    tr.parentNode.removeChild(tr);
    checkFieldForLast();
}



function checkAllTalk(checkbox){
    $$('.form_talks_checkbox').each(function(chk){
        if (checkbox.checked) {
            chk.checked = true;
        }
        else {
            chk.checked = false;
        }
    });
}


function showImgUploadForm(){
    if (Browser.Engine.trident) {
        //return true;
    }
    if (!winFormImgUpload) {
        winFormImgUpload = new StickyWin.Modal({
            content: $('window_load_img'),
            closeClassName: 'close-block',
            useIframeShim: false
        });
    }
    winFormImgUpload.show();
    winFormImgUpload.pin(true);
    return false;
}

function hideImgUploadForm(){
    winFormImgUpload.hide();
}

var winFormImgUpload;


function ajaxUploadImg(value, sToLoad){
    sToLoad = $(sToLoad);
    var req = new JsHttpRequest();
    req.onreadystatechange = function(){
        if (req.readyState == 4) {
            if (req.responseJS.bStateError) {
                msgErrorBox.alert(req.responseJS.sMsgTitle, req.responseJS.sMsg);
            }
            else {
                sToLoad.insertAtCursor(req.responseJS.sText);
                hideImgUploadForm();
            }
        }
    }
    req.open(null, DIR_WEB_ROOT + '/include/ajax/uploadImg.php', true);
    req.send({
        value: value
    });
}

function ajaxUpdateTwit(sTwit){
    JsHttpRequest.query(DIR_WEB_ROOT + '/include/ajax/updateTwit.php', {
        twit: sTwit
    }, function(result, errors){
        if (!result) {
            msgErrorBox.alert('Error', 'Please try again later');
        }
        if (result.bStateError) {
            msgErrorBox.alert(result.sMsgTitle, result.sMsg);
        }
        else {
            msgNoticeBox.alert(result.sMsgTitle, result.sMsg);
        }
    }, true);
}

function photoView(photoUrl, recipeUserLogin, photoUserLogin, recipeId, recipeTitle) {
	if(recipeUserLogin != '') {
		recipeUserLogin = '&nbsp;(<a href="' + DIR_WEB_ROOT + '/profile/' + recipeUserLogin + '/" class="nick_kul">' + recipeUserLogin + '</a>)';
	}
	else {
		recipeUserLogin = '';
	}
	var photoModal = new Element('div', {'id': 'photoModal'});
	photoModal.grab(new Element('div', {'class': 'top', 'html': '<a href="#" class="close-block"></a>'}));
	photoModal.grab(new Element('p', {'class': 'prif', 'html': '<a href="' + DIR_WEB_ROOT + '/recipes/' + recipeId + '/">' + recipeTitle + '</a>' + recipeUserLogin}));
	photoModal.grab(new Element('p', {'html': '<span>Фото: </span><a href="' + DIR_WEB_ROOT + '/profile/' + photoUserLogin + '/" class="nick_kul">' + photoUserLogin + '</a>'}));
	photoModal.grab(new Element('p', {'class': 'content', 'html': '<img src="' + DIR_WEB_ROOT + '/uploads/images/' + photoUrl + '" id="mainPhoto">'}));
	var winPhoto = new StickyWin.Modal({
		content: photoModal,
		closeClassName: 'close-block',
        useIframeShim: false
    });
	winPhoto.show();
    winPhoto.pin(true);
}

function loginForm() {
	var lf = new Element('div', {'class': 'loginPopupForm', 'html': '														\
		<form method="post" action="http://cook.askwoman.ru/login/">                                                           \
		<div class="lfTop"><h2>Войти на сайт</h2><a href="#" class="lfClose close-block"></a></div>                      	\
		<p><span>Ещё не зарегистрированы?<a href="/registration/" class="reg">Зарегистрироваться</a></span></p>           	\
		<p><input name="login" type="text" class="lfIn in_bg11 li-popup" value="E-mail" onBlur="fLogin(this);" onFocus="ufLogin(this);"></p>                                                      	\
		<p class="in_ppass"><input name="password" type="password" class="lfIn in_bg22 pi-popup" value="Пароль" onBlur="fPassword(this);" onFocus="ufPassword(this);"><a href="/login/reminder/">Забыли пароль?</a></p>	\
		<p><input type="submit" value="" name="submit_login" class="log_enter"/><span><input type="checkbox" value="" checked="" class="checkbox" name="remember"/>Запомнить меня</span></p>                                               \
		</form>                                                                                                             \
	'});

	var winLf = new StickyWin.Modal({
		content: lf,
		closeClassName: 'close-block',
        useIframeShim: false
    });
}

function fLogin(t) {
	var el = document.getElementById('login-input');
	if(t) {el = t;}
	if(el.value == '') {el.value = 'E-mail'}
}

function ufLogin(t) {
	var el = document.getElementById('login-input');
	if(t) {el = t;}
	if(el.value == 'E-mail') {el.value = ''}
}

function fPassword(t) {
	var el = document.getElementById('password');
	if(t) {el = t;}
	if(el.value == '') {el.value = 'Пароль'}
}

function ufPassword(t) {
	var el = document.getElementById('password');
	if(t) {el = t;}
	if(el.value == 'Пароль') {el.value = ''}
}

function addNotifyNewRecipe() {
	if($('notify_new_recipe').get('class') == 'a__1') {iAdd = 1;} else {iAdd = 0;}
	JsHttpRequest.query(
    	DIR_WEB_ROOT+'/include/ajax/addNotifyNewRecipe.php',
        { iAdd: iAdd },
        function(result, errors) {
        	if (!result) {
                msgErrorBox.alert('Error','Please try again later');
        	}
            if (result.bStateError) {
            	msgErrorBox.alert(result.sMsgTitle,result.sMsg);
            } else {
            	
            	if(iAdd == 1) {
            		msgNoticeBox.alert(result.sMsgTitle,"Вы подписались на рассылку.");
            	$('notify_new_recipe_a').set('class', 'search_ras_d');
            	$('notify_new_recipe').set('class', 'a__2');
            	}
            	else {
            		msgNoticeBox.alert(result.sMsgTitle,"Вы отменили подписку на рассылку.");
            	$('notify_new_recipe_a').set('class', 'search_ras');
            	$('notify_new_recipe').set('class', 'a__1');
            	}
            }
        },
        true
     );
}
