jQuery(window).load(function () {
	
	jQuery('#pane1').jScrollPane({showArrows:true, scrollbarWidth:14, scrollbarMargin:0});
	
	jQuery(".js-login").tValue({
      val: "Логин"
    });
	jQuery(".js-pass").tValue({
      val: "Пароль"
    });
	
  	/* schedule */
	if (jQuery('table.schedule').length > 0) {
		jQuery('table.schedule th:nth-child(2n),table.schedule td:nth-child(2n)').addClass('even');
		jQuery('table.schedule th:nth-child(6),table.schedule th:nth-child(7)').addClass('weekend');
		jQuery('table.schedule td:nth-child(1)').addClass('monday');
		jQuery('table.schedule td:nth-child(2)').addClass('tuesday');
		jQuery('table.schedule td:nth-child(3)').addClass('wednesday');
		jQuery('table.schedule td:nth-child(4)').addClass('thursday');
		jQuery('table.schedule td:nth-child(5)').addClass('friday');
		jQuery('table.schedule td:nth-child(6)').addClass('saturday');
		jQuery('table.schedule td:nth-child(7)').addClass('sunday');
	};
	

	jQuery("#schedule td dl").bind('mouseover', function(event) {
		jQuery(this).addClass("dl-hover");
	});
	jQuery("#schedule td dl").bind('mouseleave', function(event) {
		jQuery(this).removeClass("dl-hover");
	});
	
	jQuery(".preview, .channels-pic a").bind('mouseover', function(event) {
		jQuery(this).children('.bw').hide();
		jQuery(this).children('.color').show();
	});
	jQuery(".preview, .channels-pic a").bind('mouseleave', function(event) {
		jQuery(this).children('.color').hide();
		jQuery(this).children('.bw').show();
	});
	
	// Sort
	jQuery(".sort a").bind('mouseover', function(event) {
		jQuery(this).hide();
		jQuery(".sort-block").show();
	});
	jQuery(".sort-block").bind('mouseleave', function(event) {
		jQuery(this).hide();
		jQuery(".sort a").show();
	});
	jQuery(".sort-block li").bind('click', function(event) {
		jQuery(".sort-block").hide();
		jQuery(".sort a").show();
		var sort = jQuery(this).find("b").text();
		jQuery(".sort b").text(sort);
		// return false;
	});
	
	
	/* 18+ */
	jQuery('#f_adult').submit(function(){	
		var today = new Date();
		var today_year = today.getFullYear()-18;
		var user_year = jQuery('#f_adult_year').val();

		function tooold(){
			jQuery('.object-adult').removeClass('object-adult');
			jQuery('.adult').remove();
			jQuery.cookies.set('adult','18');
		};
		function tooyoung(){
			jQuery('#f_adult, .adult p.alert').remove();
			jQuery('.adult p.tooyoung').show();
			jQuery.cookies.set('adult','17');
		};

		if (today_year > user_year) {
			tooold();
		} else {
			tooyoung();
		};

		return false;
	});
	if (jQuery('.object-adult').length > 0) {
		var adult = jQuery.cookies.get('adult');
		if (adult == '18') {
			jQuery('.object-adult').removeClass('object-adult');
			jQuery('.adult').remove();
		} else if (adult == '17') {
			jQuery('#f_adult, .adult p.alert').remove();
			jQuery('.adult p.tooyoung').show();
		};
	};
	
	/* forum */
	jQuery('.answer-button').click(function(){
		jQuery('.forum-add-answer').insertAfter(jQuery(this).parents('.forum-in-m'));
		var txt = jQuery('.forum-add-answer textarea');
		txt.focus();
		txt.val('');
		var i = jQuery(this).parents(".forum-in-m").attr("id").replace('msg','');
		jQuery('#pid').attr("value", i)
		return false;
	});
	
	jQuery('.co-comment:even').addClass('co-odd');
	var com = jQuery('.co-comment').length;
	jQuery('#js-comments').text(com);
	
	/* comments */
	// jQuery('.co-container a.co-number').click(function() {
	// 	if (jQuery('.co-comment-block').length) {
	// 		jQuery('.co-add-comment').insertBefore('.co-comment-block:first').focus();
	// 	return false;
	// });
	
	jQuery('.co-answer a').live('click', function() {
		jQuery('#js-error').html('').show();
		
		var parent = jQuery(this).parent().parent();
	  	jQuery('.co-add-comment').insertAfter(parent);
		jQuery('.co-add-comment textarea').focus();
		
		jQuery('#js-pid').val(parent.parent().attr('pid'));
		
		return false;
	});
	
	jQuery('.co-add-com-button a').live('click', function() {
		jQuery('#js-error').html('').show();
		
		var parent = jQuery(this).parent();
	  	jQuery('.co-add-comment').insertAfter(parent);
		jQuery('.co-add-comment textarea').focus();
		
		jQuery('#js-pid').val(0);
		
		return false;
	});
	
	jQuery('.co-add-comment input').click(function() {
		var pp     = jQuery('#js-pid').val();
		var couser = jQuery('#js-username').attr('value');
		var cotext = jQuery('.co-add-comment textarea').val();
		var act    = jQuery('.co-add-comment').attr('action');
		
		if (cotext) {
			var parent = jQuery(this).parent();
			jQuery.ajax({
			    url: act,
				type: 'POST',
				data: ({ text: cotext, pid: pp }),
			    dataType : "json",                     
			    success: function (d) {
					if (d.id) {
						
						if( parent.prev().hasClass("co-add-com-button") ) {
							parent.prev().before('<div class="co-comment-block" pid="'+d.id+'"><div class="co-comment co-my"><div class="co-user"><a href="/profile/">'+couser+'</a><span>Сейчас</span></div><p>'+cotext+'</p><div class="co-answer"><a href="#">Ответить</a></div></div></div>');
						} else {
							parent.before('<div class="co-comment-block" pid="'+d.id+'"><div class="co-comment co-my"><div class="co-user"><a href="/profile/">'+couser+'</a><span>Сейчас</span></div><p>'+cotext+'</p><div class="co-answer"><a href="#">Ответить</a></div></div></div>');
						}
						jQuery('.co-add-comment textarea').val('');
						var com = jQuery('.co-comment').length;
						jQuery('#js-comments').text(com);
					} else if (d.error) {
						jQuery('#js-error').html(d.error).show();
					}
			    },
				error: function() {
					jQuery('#js-error').html('Сервер временно недоступен, попробуйте позже').show();
				}
			});
		} else {
			alert('Введите ваше сообщение');
		}
		

		return false;
	});


	jQuery('.co-number').click(function(){
		var current = jQuery(this).parent();
		if ( current.hasClass('co-hidden') ) {
			current.removeClass('co-hidden');
		} else {
			current.addClass('co-hidden');
		};
		return false;
	});
});





