function check_comment( obj, mov_id ) {
	if( $( '#username' ).val() == "" || $( '#comment_textarea' ).val() == "" ) {
		
		alert( "Напишите комментарий и назовитесь" );
		return;
	}
	if( $('#sendmail').checked && $( '#email' ).val() == "" ) {
		alert( "Оставьте свой e-mail" );
		return;
	}
	$.cookie('username', $('#username').val(), {expires: 30, path: '/'});
	$.cookie('email', $('#email').val(), {expires: 30, path: '/'});
	loadContent('post', '?module=view&ajax=1&mov_id='+mov_id+'&parent_id='+rep_comment_id, getFormData('comment_form'), 'view_comments' ); 
	obj.disabled=true;
}

function check_comment_lite( obj, mov_id ) {
	if( $( 'comment_textarea' ).value == "" ) {
		alert( "Напишите комментарий" );
		return;	
	}
	loadContent('post', '?module=view&ajax=1&mov_id='+mov_id+'&parent_id='+rep_comment_id, getFormData('comment_form'), 'view_comments' ); 
	obj.disabled=true;	
}

function update_title( title_id ) {
	
	$( "#img_title" + title_id ).html( $( "#title" + title_id ).val() );
}

//загрузка контента в div
function loadContent(method, url, inpData, divId, onLoadFunc, noLoading) {
  
	method = (''+method).length ? ''+method : 'post';
	url = ''+url;
	url = (url.length == 0 || url == 'undefined') ? 'index.php' : url;
	divId = ''+divId;
	divId = ((divId == 'undefined') ? 'contentDiv' : divId);
	
	if( divId != 'contentDiv' && isNaN(noLoading) )
		loading('block');
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.responseJS && divId != "null") {
				$('#' + divId).html(req.responseJS.content || "empty :'(");
				if( isNaN(noLoading) )
					loading('none');
			}
			
			var t = '' + req.responseJS.eval;
			if( t != 'undefined' ) {
				eval(req.responseJS.eval);
			}
			if( req.responseText != "null" ) {
				//$("debug").innerHTML = req.responseText;
			}
			if (''+onLoadFunc != 'undefined') {
				onLoadFunc();
			}
		}
	}
	
	req.caching = false;
	req.open(method, url, true);
	
	data = inpData;
	req.send(data);
}

//получение данных формы
function getFormData(formId) {
	
	var retval = [];
	var f = document.getElementById(formId);
	for (var i=0; i < f.length; i++) {

		elem = f[i];
		if (elem.type == 'checkbox') {

			if (elem['checked']) {
				var index = elem['name'];
				if (index[index.length - 2] == '[' 
				&&  index[index.length - 1] == ']')
				index = index.substr(0, index.length - 2);
				var tmp = ''+retval[index];
				if (tmp == 'undefined') {
					
					retval[index] = [];
				}
				retval[index][retval[index]['length']] = elem['value'];
			}
		} else if(elem.type == 'radio'){
			if( elem.checked )
				retval[elem['name']] = elem.value;
		} else if( elem.type == 'file' ) {
			retval[elem['name']] = document.getElementById(elem['id']);
		} else {
		
			retval[elem['name']] = elem['value'];
		}
	}
	return retval;
}

/*Comments rate*/

function cmin( comment_id, path ) {
	
	comm_num = parseInt($( '#cn'+comment_id ).text())-1;
	$( '#cn'+comment_id ).html(comm_num);
	var mclass =  comm_num>0?"mgreen":"mred";
	$( '#cn'+comment_id ).attr("class", mclass);

	//block plus-minus buttons
	$( '#cm'+comment_id ).html("<img class=\"minus_g\" src='/img/x.gif'>");
	$( '#cp'+comment_id ).html("<img class=\"plus_g\" src='/img/x.gif'>");
	loadContent( 'GET', '?module=view&ajax=1&path='+path+'&mode=rate_comm&mark=m&comment_id='+comment_id);
}

function cmax( comment_id, path ) {
	
	comm_num = parseInt($( '#cn'+comment_id ).text())+1;
	$( '#cn'+comment_id ).html( comm_num );
	var mclass =  comm_num>0?"mgreen":"mred";
	$( '#cn'+comment_id ).attr("class", mclass);
	
	//block plus-minus buttons
	$( '#cm'+comment_id ).html("<img class=\"minus_g\" src='/img/x.gif'>");
	$( '#cp'+comment_id ).html("<img class=\"plus_g\" src='/img/x.gif'>");
	loadContent( 'GET', '?module=view&ajax=1&path='+path+'&mode=rate_comm&mark=p&comment_id='+comment_id);
}

function loading( mode ) {
	
	$( '#loading' ).display = mode;
	if(mode=='block') {
	
		$('#loading').css('top', getBodyScrollTop()+getClientHeight()/2 + 'px');
	}
}
function getBodyScrollTop(){
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}
function getClientHeight(){
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
/*Search functions*/
function sFocus(inObj, isFocus) {
	if( isFocus && inObj.value == 'Найти классное видео' )
		inObj.value = '';
	if( !isFocus && inObj.value == '' )
		inObj.value = 'Найти классное видео';
}

function findVideo() {
	var searchStr = $( '#inputSearch' ).val();
	if( searchStr == '' || searchStr == 'Найти классное видео' )
		alert( 'Введите строку поиска' );
	else {
		window.location = '/search/'+searchStr;
	}
}
function enterPress(ev) {
	if( ev.keyCode == 13 )
		findVideo();
}
/*Comments*/
var rep_comment_id = 0;
function reply_button( comment_id ){
	if( rep_comment_id != 0 ) {
		$('#reply'+rep_comment_id).hide();
		$('#reply'+rep_comment_id).html('');
	}
	rep_comment_id=comment_id;
	$('#reply'+comment_id).html( $('#comm_form_div').html() );
	$('#reply'+comment_id).show();
	$('#comm_form_div').hide();
	$('#comment_textarea').focus();
}

function show_comm_form() {

	if( rep_comment_id != 0 ) {
		$('#reply'+rep_comment_id).style.display = 'none';
		$('#reply'+rep_comment_id).innerHTML = '';
		$('#comm_form_div').style.display = '';
		rep_comment_id = 0;
	}
	$('#comment_textarea').focus();
}

function loadReg() {
	loadContent( 'get', '?module=register&mode=load&ajax=1', '', 'regForm', showRegForm );
}
function loadLogin() {
	loadContent( 'get', '?module=register&mode=loadLogin&ajax=1', '', 'regForm', showRegForm );
}
function doLogin() {
	loadContent( 'get', '?module=register&mode=enter&ajax=1&url='+location.href, getFormData('enterForm'), 'regForm' );
}
function unLogin() {
	loadContent( 'get', '?module=register&mode=unlogin&ajax=1url='+location.href );
}
function loginEnter(ev) {
	if( ev.keyCode == 13 )
		doLogin();
}

function reg() {
	if( !loginOk ) alert( 'Введите корректный логин' );
	if( !passOk ) alert( 'Введите правильный пароль' );
	if( !emailOk ) alert( 'Введите корректный email' );
	if( loginOk && passOk && emailOk )
		loadContent( 'get', '?module=register&mode=register&ajax=1', getFormData('registerForm'), 'regForm' );
}

function upprof() {	
	/*$('#registerForm').ajaxSubmit({
		scriptCharset: "win-1251",
		contentType: "application/json; charset=win-1251", 
		sucsess:function(json){alert(json)}
	});*/
	loadContent( 'post', '?module=register&mode=update&ajax=1', {form: document.getElementById('registerForm')}, 'regForm' );
}

function delav() {
	loadContent( 'get', '?module=register&mode=delav&ajax=1', {}, 'regForm' );
}

function showRegForm() {
	$('#regForm').show();
	$('body').css('overflow', 'hidden');
	$('#regForm').css('top', getBodyScrollTop()+250 + 'px');
}
function closeRegForm() {
	$('#regForm').hide();
	$('body').css('overflow', 'auto');
}

//check all for login form
loginOk = false;
passOk = false;
emailOk = false;

function checkAll( element ) {
	val = $('#'+element).val();
	loadContent( 'get', '?module=register&ajax=1&mode='+element, {'value':val}, element+'Check', '', 1 );
}
function checkPass() {
	
	if( $('pass1').value == $('pass2').value && $('pass1').value != "" ) {
		$('passCheck').innerHTML = "<font color=green><b>Ok</b></font>";
		passOk = true;
	}
	else {
		$('passCheck').innerHTML = "<font color=red>Не совпадают</font>";
		passOk = false;
	}
}

function getPageCoords(element) {
  var coords = {x : 0, y : 0};
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}

function profile(user_id) {

	loadContent( 'get', '?module=register&ajax=1&mode=profile&user_id='+user_id, '', 'regForm', showRegForm );
}