﻿/*------------------------------------------------
/ Design for Webmaster.spb.ru
/ Date : 2010
------------------------------------------------*/




$(function(){ /* On Load */
	VK.init({apiId: 2626811});
	
	function hideLikeButtons()
	{
		$('.linkBox01').removeClass('active').next().removeClass('showBlock');
	}
	
	// header community block
	$('.linkBox01')
		.mouseover(function(){
			clearTimeout(window.hideLikeButtonsTimeoutId);
			$(this).addClass('active').next().addClass('showBlock');
		})
		.mouseout(function() {
			window.hideLikeButtonsTimeoutId = setTimeout(hideLikeButtons, 50);
		})
	;
	
	$('.comBlock').hover(
		function() {
			clearTimeout(window.hideLikeButtonsTimeoutId);
		},
		function() {
			window.hideLikeButtonsTimeoutId = setTimeout(hideLikeButtons, 50);
		}
	);
	
	
	$(window).scroll(function(){
		var maxH = $('.body').height() - $('.sidebarMenu').height() - 90;
		var scrollH = document.documentElement.scrollHeight;
		var scrollTop = window.document.documentElement.scrollTop || window.document.body.scrollTop; 
		
		var heightCoins = (maxH * (scrollH - scrollTop)) / scrollH;
		$('.snakeBlock:first').css('height', heightCoins + 'px');
	});
	$(window).scroll();
	
	// header slider
	$('#openHeaderSlider').click(function(){
		$('.headerSlider').slideToggle(400, function() {
			setCookie('headerSliderCssDisplayValue', $(this).css('display'), '', '/');
		});
		return false;
	});
	
	
	// hide text in form elements 
	$.fn.inputValue = function(){
		$(this)
			.focus(function() {
				if (this.value == this.title) {
					this.value = '';
					if ($(this).attr('id') == 'searchFormQuery') {
						$('.hideTextForm form .searchCancel').css('visibility', 'hidden');//hide();
					}
				}
			})
			.blur(function() {
				if (this.value == '') {
					this.value = this.title;
					if ($(this).attr('id') == 'searchFormQuery') {
						$('.hideTextForm form .searchCancel').css('visibility', 'hidden');//hide();
					}
				}
			})
			.blur()
		;
		if ($(this).attr('id') == 'searchFormQuery') {
			$('.hideTextForm form .searchCancel').css('visibility', 'hidden');//hide();
		}
	}
	
 	$('.hideTextForm, .hideTextForm input').inputValue();
 	
 	$('.hideTextForm input').keyup(function() {
 		if ($(this).attr('id') == 'searchFormQuery') {
	 		if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
	 			$('.hideTextForm form .searchCancel').css('visibility', 'hidden');//hide();
	 		}
	 		else {
	 			$('.hideTextForm form .searchCancel').css('visibility', 'visible');//show();
	 		}
 		}
 	});
	
 	$('.hideTextForm form .searchButton').click(function(){
 		$(this).parents('form:first').submit();
 		return false;
 	});
	
 	$('.hideTextForm form .searchCancel').click(function(){
 		$('#searchFormQuery').val($('#searchFormQuery').attr('title'));
 		$('.hideTextForm form .searchCancel').css('visibility', 'hidden');//hide();
 		return false;
 	});
 	
 	$('.hideTextForm form').submit(function(){
 		if ($('#searchFormQuery').val() != '' && $('#searchFormQuery').val() != $('#searchFormQuery').attr('title')) {
 			return true;
 		}
 		return false;
 	});
 	

	// special select for pager
	cuSel({
		changedEl: ".select-cusel",
		visRows: 10,
		scrollArrows: true
	});

	
	// close popup
	$('.popupBox .popupCancel, .popupLayer').click(function() {
		$('.popupLayer, .popupBox').hide();
		
		setCookie('commentFormText', null, new Date(0));
		setCookie('commentFormCommentsId', null, new Date(0));
		setCookie('otherLoginAction', null, new Date(0));

		//$('#guestFormComment').val('');
		//$('#guestFormCommentsId').val('');
		
		$('#authPopupWindow').removeData('window_open');
		
		//var $form = $('.commentForm').find('form');
		//$('input, textarea, button').removeAttr('disabled');
		//window.location.reload();
		
		return false;
	});
	$("#replyByMail_check").bind("change click", function () {
    if (this.checked){setCookie('replyByMail_check','1');setCookie('tmpReplyMail',$('#replyByMail_mail').attr('value'));}
    else{setCookie('replyByMail_check','');setCookie('tmpReplyMail','');}
    });
	// open popup comment
	$('.openComPopup').live('click', function() {
		if ($('#authPopupWindow').data('window_open')) {
			return false;
		}
		
		//$('.popupCen', $('#authPopupWindow')).html('');
		
		/*if ($(this).hasClass('loginAs')) {
			$(this).remove();
			
			$('#commentFormSubmit').addClass('openComPopup')
			$('#commentFormSubmit').html('Прокомментировать от имени...');
		}*/
		
		$('#authPopupWindow').data('window_open', true);
		
		if ($('#commentFormText').val() != '') {
			setCookie('commentFormText', $('#commentFormText').val());
			setCookie('commentFormCommentsId', $('#commentFormCommentsId').val());
            if($('#replyByMail').val()){
                setCookie('replyByMail',$('#replyByMail').val(),'','/');
                setCookie('replyByMail_check','1');
            }


			//$('#guestFormComment').val($('#commentFormText').val());
			//$('#guestFormCommentsId').val($('#commentFormCommentsId').val());
		}
		else {
			setCookie('otherLoginAction', 1);
		}

		/*
		$.get(
			'/auth/manager.php',
			{return_url: window.location.href},
			function(response) {
				$('.popupCen', $('#authPopupWindow')).html(response);
				
				$('.popupInnerContent', $('#authPopupWindow')).hide();
				
				$('#guest-login', $('#authPopupWindow')).find('a').click();
								
				$('#authPopupWindow').css({
					'top': $('#commentFormBox').offset().top - Math.abs(($('#commentFormBox').height() - $('#authPopupWindow').height()) / 2),
					'bottom': 'auto'
				});
				
				$('#guest-login-content').show();
				$('.popupLayer, .popupBox').show();
				$('#authPopupWindow').show();
			},
			'json'
		);
		*/
		
		//$('.popupCen', $('#authPopupWindow')).html(response);
		
		$('.popupInnerContent', $('#authPopupWindow')).hide();
		
		$('#guest-login', $('#authPopupWindow')).find('a').click();
						
		$('#authPopupWindow').css({
			'top': $('#commentFormBox').offset().top - Math.abs(($('#commentFormBox').height() - $('#authPopupWindow').height()) / 2),
			'bottom': 'auto'
		});
		
		$('#guest-login-content').show();
		$('.popupLayer, .popupBox').show();
		$('#authPopupWindow').show();
		
		return false;
		
		//var $form = $('.commentForm').find('form');
		//$('input, textarea, button').attr('disabled', 'disabled');
	});
	
	// hover in photogallery
	/*$('.scrollImg a img').hover(
		function () {
			$(this).animate({
				marginTop:5,
				marginLeft:5,
				width:120,
				height:70,
			}, 200);
		},
		function () {
			$(this).animate({
				marginTop:0,
				marginLeft:0,
				width:130,
				height:78
			}, 200);
		}
	);*/
	
	$('.popupMenuItem a').live('click', function() {
		$curActive = $('.popupMenu .active');
		$curActive.find('div').html('<a href="#" class="dashedLink">' + $curActive.find('div').html() + '</a>')
		$curActive.removeClass('active');
		
		$('.popupInnerContent').hide();
		$(this).parents('.popupMenuItem:first').addClass('active');
		$('#' + $(this).parents('.popupMenuItem:first').attr('id') + '-content').show();
		
		$(this).replaceWith($(this).html());
		return false;
	});

	
	/* комментарии */
	$('.commentsAnswerLink').click(function(){
		$('#commentFormCommentsId').val($(this).attr('id').replace(/comment-/, ''));
		$('#commentFormText').focus();
	});
	
	$('#commentFormText').keyup(function(){
		if ($(this).val() != '') {
			$('#commentFormSubmit').removeAttr('disabled');
			$('#guestFormSendButton').removeAttr('disabled');
		}
		else {
			$('#commentFormSubmit').attr('disabled', 'disabled');
			$('#guestFormSendButton').attr('disabled', 'disabled');
		}
	});
	
	$('#guestForm').submit(function() {
		var noError = true;
		if ($('#guestFormName').val() == '') {
			alert('Введите ваше имя');
			$('#guestFormName').focus();
			noError = false;
		}
		else if (!isValidEmail($('#guestFormEmail').val())) {
			if ($('#guestFormEmail').val() == '') {
				alert('Введите ваш e-mail');
			}
			else {
				alert('Неправильный формат e-mail');
			}
			$('#guestFormEmail').focus();
			noError = false;
		}
		
		return noError;
	});
	
	var realDeleteClick = function()
	{
		if(!confirm('Вы действительно хотите удалить этот комментарий и все ответы на него?'))
			return false;
			
		var $a = $(this);
		$.post(
			'/ajax/comment.php',
			{
				action: 'real_delete_comment',
				comment_id: parseInt($a.attr('id').replace('comment-', ''))
			},
			function(response) {
				if (response) {
					if (response.error) {
						alert(response.error);
						return false;
					}
					
					if (response.status)
					{
						window.location.reload();
					}
				}
			}
		);
		return false;
	}
	
	$('.commentsDeleteLink').click(function(){
		var $a = $(this);
		$.post(
			'/ajax/comment.php', //$a.attr('href'),
			{
				action: 'delete_comment',
				comment_id: parseInt($a.attr('id').replace('comment-', ''))
			},
			function(response) {
				if (response) {
					if (response.error) {
						alert(response.error);
						return false;
					}
					
					if (response.status && response.status == true) {
						$a.parents('.commentItem:first').find('.commentText').html('<font color="#888888"><i>комментарий удален администратором</i></font>');
						$a.unbind('click');
						$a.html('удалить насовсем');
						$a.click(realDeleteClick);
					}
				}
			}
		);
		return false;
	});
	
	
	$('.commentsRealDeleteLink').click(realDeleteClick);
		
	
	$('.headerSlider').css('display', getCookie('headerSliderCssDisplayValue'));

}); //  onLoad

function setCookie (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + encodeURIComponent(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function isValidEmail (email, strict)
{
	if (!strict) email = email.replace(/^\s+|\s+$/g, '');
	return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}
