﻿var sendToFriendOptions = {
	success : function(){
		$('#div-send-to-friend').html('Ваше письмо успешно отправлено другу');
	},
	beforeSubmit : validateSendToFriend
	
	/*function(){
		$('#div-send-to-friend').html('Ваше письмо отправляется...');
	}*/
}

function validateSendToFriend(formData, jqForm, options)
{
	var re1 = new RegExp("[a-z0-9\.\-_]+@[a-z0-9\.\-_]+", "i");
	
    var form = jqForm[0]; 
   /* if (!form.friend_name.value) { alert(' Поле имя друга не заполнено.'); return false;}*/ 
   /* if (!form.sender_name.value) { alert(' Поле имя отправителя не заполнено.'); return false;}*/
	if (!form.friend_email.value) { alert(' Поле email друга не заполнено.'); return false;} 
    if (!form.sender_email.value) { alert(' Поле email отправителя не заполнено.'); return false;} 	
    if (!re1.test(form.friend_email.value)) { alert(' Некоректное поле email друга.'); return false;} 
    if (!re1.test(form.sender_email.value)) { alert(' Некоректное поле email отправителя.'); return false;} 
    
	$('#div-send-to-friend').html('Ваше письмо успешно отправлено другу');
	return true;
}

function pinText(){
	document.getElementById("pricol-url-lb").style.display = "none";
	document.getElementById("img-upload-lb").style.display = "none";	
	document.getElementById("pricol-text-lb").style.display = "block";	
	document.getElementById("func-descript-lb").style.display = "none";
	document.getElementById("text-select").style.display = "inline";
	document.getElementById("video-select").style.display = "none";
	document.getElementById("foto-select").style.display = "none";
	document.getElementById("text-select").disabled = false;
	document.getElementById("video-select").disabled = true;
	document.getElementById("foto-select").disabled = true;
	document.getElementById("pin-image-text").style.display = "none";
}

function pinPhoto(){
	document.getElementById("pricol-url-lb").style.display = "none";
	document.getElementById("pricol-text-lb").style.display = "none";			
	document.getElementById("img-upload-lb").style.display = "block";	
	document.getElementById("func-descript-lb").style.display = "none";
	document.getElementById("text-select").style.display = "none";
	document.getElementById("video-select").style.display = "none";
	document.getElementById("foto-select").style.display = "inline";
	document.getElementById("text-select").disabled = true;
	document.getElementById("video-select").disabled = true;
	document.getElementById("foto-select").disabled = false;
	document.getElementById("pin-image-text").style.display = "block";
}
function pinVideo(){		
	document.getElementById("img-upload-lb").style.display = "none";
	document.getElementById("pricol-text-lb").style.display = "none";		
	document.getElementById("pricol-url-lb").style.display = "block";	
	document.getElementById("func-descript-lb").style.display = "block";
	document.getElementById("text-select").style.display = "none";
	document.getElementById("video-select").style.display = "inline";
	document.getElementById("foto-select").style.display = "none";
	document.getElementById("text-select").disabled = true;
	document.getElementById("video-select").disabled = false;
	document.getElementById("foto-select").disabled = true;
	document.getElementById("pin-image-text").style.display = "none";
}
function changeTops() {
	author = document.getElementById("author-top-a");
	pricol = document.getElementById("pricol-top-a");
    
	pricol_list = document.getElementById("top-list-pricol");
	author_list = document.getElementById("top-list-author");
	  	
	if (author.className=="active") {
		pricol.className = "active";	
		author.className = "activenone";
		pricol_list.style.display = "block";
		author_list.style.display = "none";	
	} else {
		author.className = "active";	
		pricol.className = "activenone";	
		author_list.style.display = "block";
		pricol_list.style.display = "none";	
	}
	return false;
}

function changeTabs(id)
{
	$("#posts-all").children("a").removeClass("active");
	$("#posts-text").children("a").removeClass("active");
	$("#posts-video").children("a").removeClass("active");
	$("#posts-photo").children("a").removeClass("active");
	$("#posts-top").children("a").removeClass("active");
	$(id).children("a").addClass("active");	
}

var load_block = false;

$(document).ready(function(){
	$('#btn-search').click(function(){
		if ($('#search-text').get(0).value.length<1) { alert("Поле поиска пустое пустое или текст менее 1-го символа");return false;}
		return true;
	});
	
	
	$('#send-btn').click(function(){

		if ($('#name').get(0).value.length<3) { alert('Поле "Название" пустое или менее 3х символов');return false;}
		if ($('#tags-list').get(0).value.length<3) { alert('Поле "Теги" пустое или менее 3х символов');return false;}
		
		if ($('#radio-text').attr('checked')) {
			if ($('#pricol-text').get(0).value.length<10) { alert('Поле "Описание" пустое или менее 10-ти символов');return false;}			
		}		
		if ($('#radio-video').attr('checked')) {
			if ($('#pricol-url').get(0).value.length<32) { alert('Некоректный url ролика');return false;}
		}		
		if ($('#radio-image').attr('checked')) {
			fl = false;
			for (i=0;i<10;i++) {
				if ($('#images__0').get(0).value.length>4) {
					fl = true;
					break;
				}
			}
			if (!fl) { alert('Не выбрано ни одного изображениe для загрузки');return false;}
		}
		
		return true;
	});
	
	$('#comment-send-btn').click(function(){
		if ($('#comment-send-text').get(0).value.length>=3) return true;
		alert('Комментарий пустой или менее 3 символов!');
		return false;
	});
	
	/*$('a').click(function(){			   
			   url = $(this).attr('href');
			   if (url.match(/^\/index\/exit\/$/)) return true;
			   if (url.match(/#$/)) return false;
			   if (url == '/'){
				   changeTabs("#posts-all");
				   url = '/index/index/';
			   }
			   if (url.match(/^\/\w+\/$/)){
				   $(this).removeClass("active");
				   changeTabs("#"+$(this).attr("class"));
				   url+='index/';
			   }
			   showCenterByUrl(url+'mode/ajax/');
			   return false;
	});*/
	
	applyCommentClicks();
	checkAuth();
	
	$("#login-box").hide();
	$("#ref-enter").click(function(){
		$("#login-box").toggle("slow");
		return false;
	});
	$("#fl-btn-close").click(function(){
		$("#login-box").hide("slow");
		return false;
	});

	$('#form-send-to-friend').ajaxForm(sendToFriendOptions);
		src = "";
	if(getCookie('g') == '1'){
		src = "g_1";
	}else if(getCookie('g') == '0'){
		src = "g_0";
	}
	$(".banners-zhlobanners").append("<li><frame id='a5fa83e6' name='a5fa83e6' src='http://da.ukr.net/www/delivery/afr.php?zoneid=17&amp;source="+src+"' framespacing='0' frameborder='no' scrolling='no' width='1' height='1'><a href='http://da.ukr.net/www/delivery/ck.php?n=ad4e2197' target='_blank'><img src='http://da.ukr.net/www/delivery/avw.php?zoneid=17&amp;n=ad4e2197&amp;source="+src+"' border='0' alt='' /></a></iframe></li>");
});

function applyCommentClicks(){	
	if (document.getElementById("unique") && $("#unique").attr("value")!="nocomments")
		$("#comment-add-bar").hide();
	$("#comment-add").click(function(){
		$("#comment-add-bar").toggle();
		return false;
	});		
}

function showCenterByUrl(url){
	/*var column = document.getElementById("central-column");*/
	document.getElementById("central-column").innerHTML = '<div style="border:1px solid red">Подождите, пожалуйста, идет загрузка...</div>';
	var column = document.getElementById("central");
	/*load_block = true;*/
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("get", url, true);
	oXmlHttp.onreadystatechange = function(){	
		/*load_block = false;*/
		if(oXmlHttp.readyState == 4) {			
			if(oXmlHttp.status < 400){
				column.innerHTML = oXmlHttp.responseText;
				window.scroll(0,0);
				applyCommentClicks();
				$("#central a").click(function(){
				   /*if (load_block) return false;*/
				   url = $(this).attr('href');
				   if (url.match(/^\/index\/exit\/$/)) return true;
				   if (url.match(/#$/)) return false;
				   if (url == '/') url = '/index/index/';
				   if (url.match(/^\/\w+\/$/)) url+='index/';
				   showCenterByUrl(url+'mode/ajax/');
				   return false;
			   });
			   $('#form-send-to-friend').ajaxForm(sendToFriendOptions);
			} else{
				column.innerHTML = "Ошибка: #"+oXmlHttp.status;
			}
		}
	}
	oXmlHttp.send(null);
	return false;
}




function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function isAuthUser() {
	if (getCookie("video_auth")==1) return true;
	return false;
}

function checkAuth()
{
	if(isAuthUser()){
		toggleAuth(true);
	}else{
		toggleAuth(false);
	}
}

function toggleAuth(isAuth)
{
	if(isAuth){
		$('#authorize').hide();
		$('#unauthorize').show();
	}else{
		$('#authorize').show();
		$('#unauthorize').hide();
	}
	return true;
}

function showAuthBox(id)
{
	$("#login-box").show("slow");
	return false;
}

function rate_ajax(obj)
{
	ratingAJAX(obj);
	alert('Ваш голос принят. Спасибо.'); 
	return false;
}