(function($) {
	$.fn.openCloseBlocks = function(blocks, options) {
		var defaults = { 
			speed: 'normal'
		}, 
		opts 		= $.extend(defaults, options),
  		togglers 	= $(this),
  		bls 		= $(blocks); if(!bls) return;
  		
  		togglers.each(function(index) {
  			$(this).click(function(e) {
  				e.preventDefault(); 			
  				$(bls[index]).slideToggle(opts['speed']);
  			});
  		});
	};
})(jQuery);


$('#show_login').openCloseBlocks($('#login-form'), {
	speed: 'fast'
});

$('#login-close').openCloseBlocks($('#login-form'), {
	speed: 'fast'
});

$('#show_iam').openCloseBlocks($('#iam-form'), {
	speed: 'fast'
});

$('#iam-close').openCloseBlocks($('#iam-form'), {
	speed: 'fast'
});

$('#change-password').openCloseBlocks($('#sub-form-cp'), {
	speed: 'fast'
});

$('#block-user').openCloseBlocks($('#sub-form-bl'), {
	speed: 'fast'
});

$('#delete-user').openCloseBlocks($('#sub-form-dl'), {
	speed: 'fast'
});

$('#add-mail').openCloseBlocks($('#sub-form-nm'), {
	speed: 'fast'
});

$('#description').openCloseBlocks($('#descr-close'), {
	speed: 'slow'
});

$('#description').openCloseBlocks($('#descr-open'), {
	speed: 'slow'
});

$('#description2').openCloseBlocks($('#descr-open'), {
	speed: 'slow'
});

$('#description2').openCloseBlocks($('#descr-close'), {
	speed: 'slow'
});

$('#comments').openCloseBlocks($('#comm-close'), {
	speed: 'slow'
});

$('#comments').openCloseBlocks($('#comm-open'), {
	speed: 'slow'
});

$('#comments2').openCloseBlocks($('#comm-open'), {
	speed: 'slow'
});

$('#comments2').openCloseBlocks($('#comm-close'), {
	speed: 'slow'
});


var er0 = "Введите логин"; //0
var er1 = "Логин и пароль не верны"; //0
var er12 = "Неверный пароль"; //0
var er2 = "А пароль-то где?"; //0
var er3 = "Ошибка =\\"; //0
var er4 = "Код неверный либо закончен срок его работы"; //0
var er5 = "А код где?"; //0
var er6 = "Слишком короткий =\\"; //0
var er7 = "Рука не поднимается =\\"; //0

var msg0 = "Ага, я записал."; //0
var msg1 = "Готово."; //0


function LogIn() {
    pass = $('#pass').val();
    login = $('#login').val();
	ShowIndicator();
	HideButton();
    if (login != "" ){
		$.ajax({
			url:'/ajax/login.html',
			type:'POST',
			data:'login=' + login + '&pass=' +pass,
			success: function(html){
				if(html == "1") { HideError(); location.reload();}
				if(html == "0") { ShowError(1); }
				if(html == "2") { ShowError(12); }
			}
		});
	}else{
		ShowError(0);
	}
}

function ChangeUserpic() {
}

function ChangePass() {
	pass = $('#pass').val();
	ShowIndicator();
	HideButton();
	if(pass != ""){
		$.ajax({
			url:'/ajax/changepass.html',
			type:'POST',
			data:'pass=' +pass,
			success: function(html){
				if(html == "2") { ShowError(6);}
				if(html == "1") { ShowMsg(0);}
				if(html == "0") { ShowError(3); }
			}
		});
	}else{
		ShowError(2);
	}
}


function AddMail() {
	nmail = $('#new-mail').val();
	ShowIndicator();
	HideButton();
	if(nmail != ""){
		$.ajax({
			url:'/ajax/addmail.html',
			type:'POST',
			data:'nmail=' +nmail,
			success: function(html){
				alert(html);
				if(html == "2") { ShowError(6);}
				if(html == "1") { ShowMsg(0);}
				if(html == "0") { ShowError(3); }
			}
		});
	}else{
		ShowError(2);
	}
}

function BlockUser() {
	block = $('#block').val();
	ShowIndicator();
	HideButton();
	if(block == 1){
		$.ajax({
			url:'/ajax/blockuser.html',
			type:'POST',
			data:'block=' +block,
			success: function(html){
				if(html == "1") { ShowMsg(1);}
				if(html == "0") { ShowError(7); }
			}
		});
	}else{
		ShowError(7);
	}
}

function DeleteUser() {
	del = $('#del').val();
	ShowIndicator();
	HideButton();
	if(del == 1){
		$.ajax({
			url:'/ajax/deleteuser.html',
			type:'POST',
			data:'del=' +del,
			success: function(html){
				if(html == "1") { ShowMsg(2);}
				if(html == "0") { ShowError(8); }
			}
		});
	}else{
		ShowError(8);
	}
}


function ProfileUpdate(){
	prmail = $('#profile-mail').val();
	prname = $('#profile-name').val();
	pricq = $('#profile-icq').val();
	prbirth = $('#profile-birth').val();
	prskype = $('#profile-skype').val();
	prcity = $('#profile-city').val();
	prcountry = $('#profile-country').val();
	prlj = $('#profile-lj').val();
	prwork = $('#profile-work').val();
	prtwitter = $('#profile-twitter').val();
	prabout = $('#profile-about').val();
	prsite = $('#profile-site').val();
	prmail2 = $('#profile-mail2').val();
	document.getElementById("profile-update").innerHTML = "Подождите...";
		$.ajax({
			url:'/ajax/updateprofile.html',
			type:'POST',
			data:'prmail='+prmail+'&prmail2='+prmail2+'&prname='+prname+'&pricq='+pricq+'&prbirth='+prbirth+'&prskype='+prskype+'&prcity='+prcity+'&prcountry='+prcountry+'&prlj='+prlj+'&prwork='+prwork+'&prtwitter='+prtwitter+'&prabout='+prabout+'&prsite='+prsite,
			success: function(html){
				if(html == "ok") { $('#error').hide(); document.getElementById("profile-update").innerHTML = "Изменения сохранены.";}
				else{
					ShowErrors(html);
				}
			}
		});

}

function CheckCode() {
	code = $('#code').val();
	file_id = $('#file_id').val();
	file_id2 = $('#file_id2').val();
	name = $('#name').val();
	time = $('#time').val();
	if(code != ""){
		var newimg = document.getElementById("download-submit");
		newimg.src = "/img/ajax-loader.gif";
		$.ajax({
			url:'/ajax/checkcode.html',
			type:'POST',
			data:'code=' +code,
			success: function(html){
				if(html == "1") { 
					HideError();
					var codetext = document.getElementById("code-text");
					//HideForm(); 
					var codehtml = ShowLink(file_id, file_id2, code, name, time);
					codetext.innerHTML = codehtml;
					newimg.src = "/img/download-submit.png";
					}
				if(html == "0") { 
					ShowError(4); 
					//var newimg = document.getElementById("download-submit");
					newimg.src = "/img/download-submit.png";

					}
			}
		});
	}else{
		ShowError(5);
	}
}


function HideError() {
    $('#error').hide();
    $('#error2').hide();
    $('#error3').hide();
    $('#error4').hide();
    $('#error5').hide();
}

function HideMsg() {
    $('#msg').hide();
    $('#msg2').hide();
    $('#msg3').hide();
}

function ShowErrors(data){
	$('#error').html(data).slideDown();
}

function ShowError(code) {
	HideIndicator();
	ShowButton();
    HideError();
    HideMsg();
    switch (code) {
        case 0:
            $('#error').html(er0).slideDown();
        break;
        case 1:
            $('#error').html(er1).slideDown();
        break;
        case 12:
            $('#error').html(er12).slideDown();
        break;
        case 2:
            $('#error').html(er2).slideDown();
        break;
        case 3:
            $('#error').html(er3).slideDown();
        break;
        case 4:
            $('#error2').html(er4).slideDown();
        break;
        case 5:
            $('#error2').html(er5).slideDown();
        break;
        case 6:
            $('#error3').html(er6).slideDown();
        break;
        case 7:
            $('#error4').html(er7).slideDown();
        break;
        case 8:
            $('#error5').html(er7).slideDown();
        break;
    }
}

function ShowMsg(code) {
	HideIndicator();
	ShowButton();
    HideError();
    HideMsg();
    switch (code) {
        case 0:
            $('#msg').html(msg0).slideDown();
        break;
        case 1:
            $('#msg2').html(msg1).slideDown();
        break;
        case 2:
            $('#msg3').html(msg1).slideDown();
        break;
    }
}

function ShowLink(file_id, file_id2, code, name, time) {
	return '<a href="/vip/'+file_id2+'/'+code+'/'+time+'/'+file_id+'/'+name+'">Скачать файл</a><br />Используйте <a href="/html/Download Master 5.9.4.1257 Portable by zalivka.com.rar">Download Master</a> для скачивания.';
	var link = '<a href="/vip/'+file_id2+'/'+code+'/'+time+'/'+file_id+'/'+name+'">Скачать файл</a><br />Используйте <a href="/html/Download Master 5.9.4.1257 Portable by zalivka.com.rar">Download Master</a> для скачивания.';
	$('#download-link').html(link).slideDown();
}

function HideForm() {
    $('#download-form').hide();
}


function ShowIndicator() {
    $('#indicator').show();
}
function HideIndicator() {
    $('#indicator').hide();
}
function ShowButton() {
    $('#login-submit').show();
}
function HideButton() {
    $('#login-submit').hide();
}
function HideButton2() {
    $('#download-submit').hide();
}
function ShowButton2() {
    $('#download-submit').show();
}

function clearInput(inpt) {
    document.getElementById(inpt).value="";
	$(".profile label").overlabel();
}

function ChangeNum(value){
	/* ААААААААА */
	var free = document.getElementById("freeone");
	var free2 = document.getElementById("freetwo");
	if(value==9) {
		free.style.display = 'block';
		free2.style.display = 'block';
	}else{
		free.style.display = 'none';
		free2.style.display = 'none';
	}

	
	var code_img = document.getElementById("code_img");
	var a1fuckingbilling = document.getElementById("a1-fucking-billing");
	var prefix = document.getElementById("prefix");
	var price_per_day = document.getElementById("price-per-day");
	code_img.src = "/code.png?id="+value;
	var d_window = document.getElementById("window");
	var code_text = document.getElementById("code_text");


	$.ajax({
	url:'/ajax/code.html',
	type:'GET',
	data:'id=' +value,
	success: function(html){
		html = html.split(':::');
		code_text.innerHTML = '<font class="small">'+html[0]+'</font>';
		if(html[1]==0) document.getElementById("tarif_link").style.display = 'none';
		else document.getElementById("tarif_link").style.display = '';
		a1fuckingbilling.href = html[1];
		prefix.innerHTML = html[2];
		price_per_day.innerHTML = html[3];
		if(html[4]=='1'){
			document.getElementById("dogovor_footer").style.display = 'block';
			document.getElementById("dogovor_item_1").style.display = 'block';
			/*document.getElementById("advertisment").style.display = 'block';*/
			document.getElementById("window").style.display = 'block';
		}else{
			document.getElementById("dogovor_footer").style.display = 'none';
			document.getElementById("dogovor_item_1").style.display = 'none';
			/*document.getElementById("advertisment").style.display = 'none';*/
			document.getElementById("window").style.display = 'none';
		}
	}
	});

	Set_Cookie('country', value, '', '/');
}
function checkRadio()
{
	if (document.getElementById("dogovor").checked == true)document.getElementById("submbtn").disabled = false;
	else document.getElementById("submbtn").disabled = true;
}


function AddFav(id){
	$.ajax({
	url:'/ajax/fav.html',
	type:'GET',
	data:'add=' +id,
	success: function(html){
		if(html == '1'){
			document.getElementById("fav-add").style.display = 'none';
			document.getElementById("fav-del").style.display = 'inline';
		}
	}
	});
}

function DelFav(id){
	$.ajax({
	url:'/ajax/fav.html',
	type:'GET',
	data:'del=' +id,
	success: function(html){
		if(html == '1'){
			document.getElementById("fav-add").style.display = 'inline';
			document.getElementById("fav-del").style.display = 'none';
		}
	}
	});
}

function DelFav2(id){
			DelFav(id);
			document.getElementById("f_"+id).style.display = 'none';
}

function smile(smile) {
	document.comm.comment.value += smile+" ";
} 

function openWindowImg(name,width,height)
{
  document.getElementById('ag_img').src = name;
  document.getElementById('ag_img').width = width;
  document.getElementById('ag_img').height = height;
    
  document.getElementById('windowImg').style.width = width+'px';
  document.getElementById('windowImg').style.height = height+'px';
    
  var left = getClientCenterX();
  var top = getClientCenterY()-80;

  document.getElementById('windowImg').style.left = (left-(width/2))+"px";
  document.getElementById('windowImg').style.top = (top-(height/2))+"px";
  document.getElementById('windowImg').style.display = 'block';
}

function closeWind()
{
  document.getElementById('windowImg').style.display = 'none'; 
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getBodyScrollTop()
{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
	return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getClientCenterX()
{
	return parseInt(getClientWidth()/2)+getBodyScrollLeft();
}

function getClientCenterY()
{
	return parseInt(getClientHeight()/2)+getBodyScrollTop();
}

$(function() { 
    var i = $('input').size() + 1;  
    $('a#add').click(function() { 
        $('<p><input type="file" name="pics[]"/></p>').animate({ opacity: "show" }, "slow").appendTo('#inputs'); 
        i++; 
    });  
    $('a#remove').click(function() {  
    if(i > 1) { 
        $('#inputs p input:last').remove(); 
        i--; 
    }  
    });  
    $('a.reset').click(function() {  
    while(i > 2) {  
        $('#inputs p input:last').remove();
        i--;  
    }  
    });  
	return false;
});  

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 * 30;
	}
	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 CheckMessage(){
	$.ajax({
	url:'/ajax/checkmessage.html',
	type:'GET',
	success: function(html){
		if(html == '1'){
			$('.new-message').show();
		}
	}
	});
}

function Vote(file_id, vote){
	$.ajax({
	url:'/ajax/vote.html',
	type:'POST',
	data:'file_id='+file_id+'&vote='+vote,
	success: function(html){
		if(html == '1'){
			alert('Спасибо за голос!');
			vote_t = parseInt($('#votes-sum').html());
			$('#votes-sum').html(vote_t+vote);
		}
	}
	});
}

function CommVote(comment_id, vote){
	$.ajax({
	url:'/ajax/cvote.html',
	type:'POST',
	data:'comment_id='+comment_id+'&vote='+vote,
	success: function(html){
		if(html == '1'){
			alert('Спасибо!');
		}
	}
	});
}

