// JavaScript Document
var user_id, call_msg_timer;

$(document).ready(function() {
	$('#resoAddButton').click(function (e) {
		e.preventDefault();
		if(document.getElementById('resoForm')){
			document.getElementById('v_id').value='';
			for(var i=-5;i<6;++i) document.getElementById('v_val'+i).checked=false;
			document.getElementById('resotext').value='';
		}
		$('#resoAddDiv').modal({
			containerCss:{height:'300px',width:'600px'},
			onShow:function(){ ; },
			onClose:function(){ ; $.modal.close(); }
		});
	});
	$('#photoAddButton').click(function (e) {
		e.preventDefault();
		$('#photoAddDiv').modal({
			containerCss:{height:'600px',width:'600px'},
			onShow:function(){ swfuInit(); },
			onClose:function(){ swfu.stopUpload(); $.modal.close(); }
		});
	});
	$('#photoCoverButton').click(function (e) {
		e.preventDefault();
		$('#photoCoverDiv').modal({
			containerCss:{height:'600px',width:'600px'},
			onShow:function(){
				$('#photoCoverDiv').html('');
				$('img').each(function(){ if(this.id&&this.id.indexOf('photoListImage')==0){
					li=this.id.substring(14,this.id.length);
					$('#photoCoverDiv').append( ' <a href="#" onclick="loadPhotosForEdit(\'?action=cover&albomID='+albomID+'&coverID='+li+'\'); return false;"><img src="'+this.src+'"></a> ');
				}});
			}
		});
	});
	$('#reply').click(function (e) {
		e.preventDefault();
		user_id=$(this).attr('rel');
		$('#basicModal').modal({
			onShow:function(){sendMessages('getForm');},
			containerCss:{height:'220px'}
		});
	});

	var af=document.getElementById('auth_form');
	if(af!=undefined){
		$('#but_enter').click(function(){
			//af.style.display=(af.style.display=='')?'none':'';
			$(af).toggle("slow");
		});
		if($('#formError').html()!=null && $('#formError').html()!=''){
			//$(af).show("slow");
			af.style.display='';
		}
	}

	//Очистка полей при фокусе
	var clearFLDS=new Array();

	$(".helper").each(function(i){
		if(this.attributes['rel']){
			if( document.getElementById( this.attributes['rel'].value ) ){
				this.storeName=document.getElementById( this.attributes['rel'].value ).innerHTML;
			}else	this.storeName=this.attributes['rel'].value;
		}else if(this.value) this.storeName=this.value;
		else this.storeName=this.attributes['name'].value;
		if(this.value=='') this.value=this.storeName;
		clearFLDS.push(this);
	})
	$(".helper").focus(function(){
		if(this.value==this.storeName) this.value='';
	});
	$(".helper").blur(function(){
		if(this.value=='') this.value=this.storeName;
	});

	$("#regForm").submit(function() {
		for(var i=0; i<clearFLDS.length; i++){
			if(clearFLDS[i].value==clearFLDS[i].storeName)clearFLDS[i].value='';
		}
	});
	$("#profileForm").submit(function() {
		for(var i=0; i<clearFLDS.length; i++){
			if(clearFLDS[i].value==clearFLDS[i].storeName)clearFLDS[i].value='';
		}
	});

	//Фон от времени
	if(!$('#bodyItem').hasClass('wall_bg') && !users_bg){
		/*
		var bgimg='bg_2.jpg';
		var iDate = new Date();
		var H=iDate.getHours();
		if( H<9 || H>=18 ){ bgimg='bg_1.jpg'; }
		$('#bodyItem').css('background-image','url(/img/'+bgimg+')');
		*/
		/*$('#bodyItem').css('background-image','url(/img/sm_bg.jpg)');*/
        $('#bodyItem').css('background-image','url(/img/bg_annual.png)');
		$('#bodyItem').css('background-position','0 0');
	}

	if ($('#addComm2')) {
		$('#addComm2').hide();
	}
	$('#goComment').add('#addComm2').click(function() {
		$('#addComm2').hide();
		$('.commForm').hide();
		$('.commForm').css({'width':'', 'margin-left':''});
		$('.commForm').insertAfter("a[name='commForm']");
		$('#to_fld').val('');
		$('.commForm').show();
	});
	$("a[rel='addReply']").click(function() {
		if(toHide) {toHide.show(); toHide=false;}
		$('.commForm').hide();
		var myChosen = $(this).closest(".commItem");
		$(".commForm").appendTo(myChosen);
		$('.commForm').css({'width':'480px', 'margin-left':'70px'});
		$('.commForm').show();
		$('#addComm2').show();
		return false;
	});

	var toHide=false;
	$(".editComm").click(function() {
		$('.commForm').hide();
		//var myChosen = $(this).closest(".commText");
		var myChosen=$(this).parent().parent().siblings(".commText");

		if(toHide) toHide.show();
		toHide=$(myChosen).children(".commTextParagr");
		toHide.hide();
		$(".commForm").appendTo(myChosen);
		$('.commForm').css({'width':'100%', 'margin':'-20px 0 0 0'});
		$('.commForm').show();
		$('#addComm2').show();
		return false;
	});

	//Ноль комментариев
	$(".comments").each(function(){
		if(parseInt(this.innerHTML)=='0') this.style.display='none';
		});

	previewMessages([0,0]);
	call_msg_timer=setInterval('previewMessages([0,0]);', 30000);
	
	//BEGUN
	begun=setInterval('animateBegun();',30);
	$('#begun a').mouseover(function(){clearInterval(begun);}).mouseout(function(){begun=setInterval('animateBegun();',30);});
	//////////////////////
});

//n, form1, form2, form5
//"письмо", "письма", "писем"
function pluralForm( nObj,nRes,f1,f2,f5,f0 ) {
	var n=document.getElementById(nObj).innerHTML;
	if( parseInt(n)==0 && f0) {
		document.getElementById(nObj).style.display='none';
		document.getElementById(nRes).innerHTML=f0;
		return true;
	}
	var rs=f5;
  n = Math.abs(n) % 100;
  n1 = n % 10;
  if (n1 > 1 && n1 < 5) rs=f2;
  if (n1 == 1) rs=f1;
  if (n > 10 && n < 20) rs= f5;
  //return form5;
  var tst=document.getElementById(nRes);
  if(tst!=undefined)tst.innerHTML=rs;
}

function win(url,awidth,aheight,wnd,no_scroll) {
	var scroll = (no_scroll) ? 'no':'yes';
	var x=(self.screen.width-awidth)/2; if (x<0) x=0;
	var y=(self.screen.height-aheight)/2; if (y<0) y=0;
	window.open(url,wnd,"width=" + awidth + ", height=" + aheight + ", replace=no, scrollbars=" + scroll + ", resize=no, left="+ x +", top=" + y);
}

function previewMessages(value) {
	clearInterval(call_msg_timer);
	//value - array - 0 => id_message; 1 => action
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
		if (req.readyState == 4) {
			call_msg_timer=setInterval('previewMessages([0,0]);', 30000);
			if(req.responseJS) {
				if(req.responseJS.countMes) {
					//document.getElementById('src').value;
					//$('#meesagePanel').style.display='none';
					if(req.responseJS.countMes==1) {
						$('#messagePanel').show();
						$('#messagePanel').html(req.responseJS.html);
					}
					else $('#messagePanel').hide();
				}
			}
			if(req.responseText!='') alert(req.responseText);
	    }
	}
	req.open(null, '/lib/previewMessages.php', true);
	req.send( { q: value } );
}

function sendMessages(value) {
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if(req.responseJS) {
				if(req.responseJS.basicModalContent) {
					$('#basicModal').html(req.responseJS.basicModalContent);
					$('#sendTo').attr('value',user_id);
				}
				if(req.responseJS.basicModalClose && req.responseJS.basicModalClose==1) setTimeout('$.modal.close();',2000);
			}
			if(req.responseText!='') alert(req.responseText);
	    }
	}
	req.open(null, '/lib/sendMessage.php', true);
	req.send( { q: value } );
}


var begun;
function animateBegun(){
	var margin=parseInt($('#begun').css('marginLeft'));

	if($('#begun').width()+margin+$('#begun').parent().width()<0){
		$('#begun').css({
			'marginLeft':'0px'
		});
	}
	else {
		$('#begun').css({
			'marginLeft':(margin-1)+'px'
		});
	}
}