/*-------- /js//box/vote.js --------*/

$(function(){
    $('.vote-submit a').live('click', function(){
        Vote.add();
        return false;
    });
    
    $('.vote-form').submit(function(){
        Vote.add();
        return false;
    });
    
    $('.show-result').live('click',function(){
        Vote.showBox('result');
        return false;
    });
    
    $('.show-vote').live('click',function(){
        Vote.showBox('vote');
        return false;
    });
});

function Vote(){
    
}

Vote.add = function ()
{
    var voteId = Ub.module('app').data.vote.voteId;
    var rowId = $('.vote-answer input:checked').val();
    var width = Ub.module('app').data.vote.width;
    
    $.ajax({ url: Ub.module('app').data.vote.url,
             type: 'post',
             data: { voteId: voteId,
                     rowId:  rowId,
                     width:  width}, 
             success: function(data)
             {
                 
                 if (data.status)
                 {
                     $('.vote-box').html(data.data.html);
                 }    
                 else
                 { 
                     alert(data.error);
                 } 
             },
             error: function(XMLHttpRequest, textStatus, errorThrown)
             {
                 alert(textStatus + "<br />" + errorThrown);
             },
             dataType: 'json'
    });
};

Vote.showBox = function (type)
{
    var voteId = Ub.module('app').data.vote.voteId;
    var width = Ub.module('app').data.vote.width;
    
    $.ajax({ url: Ub.module('app').data.vote.result,
             type: 'post',
             data: { voteId: voteId,
                     type:   type,
                     width:  width}, 
             success: function(data)
             {
                 if (data.status)
                 {
                     $('.vote-box').html(data.data.html);
                 }    
                 else
                 { 
                     alert(data.error);
                 } 
             },
             error: function(XMLHttpRequest, textStatus, errorThrown)
             {
                 alert(textStatus + "<br />" + errorThrown);
             },
             dataType: 'json'
    });
};

/*-------- /js//index/article.js --------*/

window.onload = function()
{
    if ( Ub.module('app').data.article.type == 'site' )
    {
        ODKL.init();
    }
    $('img.lightbox').lightbox();
};


/*-------- /js//index/comment.js --------*/

var Comment = {};

Comment.valid = true;
Comment.loggedIn = Ub.module('app').data.comment.loggedIn;

Comment.addComment = function( )
{
	var text = $('.com-area').val();
	
    var length = text.length;
    if ( length < 1 )
    {
    	alert('Вы не ввели текст комментария');
    	Comment.valid = false;
        return false;
    }
    
    if ( !Comment.loggedIn && length > 300 )
	{
    	alert('Для неавторизированных пользователей текст комментария ограничивается 300 символами.');
    	Comment.valid = false;
        return false;
	}
    
    if ( !Comment.loggedIn )
	{
    	var captchText = $('#captchaText').val();
    	
    	if (captchText == '')
		{
    		alert('Вы не ввели captcha');
        	Comment.valid = false;
            return false;
		}
	}
    
    $('.comment-form').submit();
};

Comment.addAnswer = function( parentId )
{
	$('.add-parentId').val(parentId);
	$('.com-title span.com-title-txt').html('Ответить на комментарий:');
	$('.cancel-answer').show();
	window.location = '#comment';
};

Comment.cancelAnswer = function( )
{
	$('.add-parentId').val('');
	$('.com-title span.com-title-txt').html('Оставить комментарий:');
	$('.cancel-answer').hide();
};

Comment.addPoint = function( type, commentId )
{
	shadow.show({zIndex: 5});
    ajaxLoader.show(); 
    
    $.ajax({ url: Ub.module('app').data.comment.pointUrl,
             type: 'post',
             data: { commentId: commentId,
    				 type: type}, 
             success: function(data)
             {
                 shadow.hide();
                 ajaxLoader.hide();
            
                 if (data.status)
                 {
                	 if (type == 'minus')
                	 {
                		 $('#comment-' + commentId + ' .comment-minus span').html('(' + data.data.minus + ')');
                	 }
                	 else
                	 {
                		 $('#comment-' + commentId + ' .comment-plus span').html('(' + data.data.plus + ')');
                	 }
                 }    
                 else
                 { 
                     alert('Вы уже оставили свое мнение об этом комментарии');
                 } 
             },
             error: function(XMLHttpRequest, textStatus, errorThrown)
             {
                 shadow.hide(); ajaxLoader.hide();
                 alert('Произошла ошибка на сервере.');
             },
             dataType: 'json'
    });
};

$(function(){
	$('.comment-submit a').live('click', function(){
		Comment.addComment();
        return false;
    });
	
	$('.cancel-answer a').live('click', function(){
		Comment.cancelAnswer();
        return false;
	});
});

/*-------- /js//html/header.js --------*/

$(function(){
    $('.search input.input').focus(function(){
        if ( $('.search input.input').val() == 'Поиск...' )
        {
            $('.search input.input').val('');
        }
    });
    
    $('.search input.input').blur(function(){
        if ( $('.search input.input').val() == '' )
        {
            $('.search input.input').val('Поиск...');
        }
    });
    
    $('.enter').click(function(){
        Header.showLoginBox();
        return false;
    });
    
    $('.enter-comment').click(function(){
    	Header.reload = true;
        Header.showLoginBox();
        return false;
    });
    
    $('#login-box .close-box a').live('click', function(){
        Header.closeLoginBox();
    });
    
    $('#login-box .login-button a').live('click', function(){
        Header.login();
    });
    
    $('#login-box .login-form').live('submit', function(){
        Header.login();
        return false;
    });
    
    // пошук автокомпліт
    $(".search-input").autocomplete(Ub.module('app').data.header.searchautocomplete, {
        minChars: 2
        }
    );
});

Header = {};

Header.reload = false;

Header.showLoginBox = function()
{
    shadow.show({zIndex: 5});
    $('#login-box').fadeIn(100);
    
    $('#login-box').css({
            'left' : '50%',
            'top' : '50%',
            'margin-top': '-60px',
            'margin-left': '-120px'
    });

};

Header.closeLoginBox = function()
{
    shadow.hide();
    $('#login-box').fadeOut(100);
};

Header.strpos = function( haystack, needle, offset)
{ 		 
	var i = haystack.indexOf( needle, offset );
	return i >= 0 ? i : false;
};

Header.login = function()
{
    var login = $('#login-box .login').val();
    var pass = $('#login-box .pass').val();
    
    if ( login == '' )
    {
    	alert('Вы не ввели логин');
    	return false;
    }
    
    if ( pass == '' )
    {
    	alert('Вы не ввели пароль');
    	return false;
    }
    
    shadow.show({zIndex: 5});
    ajaxLoader.show(); 
    
    $.ajax({ url: Ub.module('app').data.header.auth,
             type: 'post',
             data: { login: login,
                     pass:  pass}, 
             success: function(data)
             {
            	 shadow.hide();
                 ajaxLoader.hide();
                    	 
                 if (data.status)
                 {
                     var html = '<a href="' + data.data.cabinetUrl + '">' + data.data.auth.login + '</a>'
                                 + '<a href="' + Ub.module('app').data.header.logout + '">Выйти</a>';
                     
                     $('.header-registration').html(html);
                     
                     Header.closeLoginBox();
                     
                     if ( Header.reload 
                    		 || 
                    	  Header.strpos(window.location.href, '/contact', 0)
                    	  	 ||
                    	  	Header.strpos(window.location.href, '/registration', 0))
                	 {
                    	 window.location.reload();
                	 }
                 }    
                 else
                 { 
                     alert(data.error);
                 }
             },
             error: function(XMLHttpRequest, textStatus, errorThrown)
             {
                 shadow.hide(); ajaxLoader.hide();
                 alert(textStatus + "<br />" + errorThrown);
             },
             dataType: 'json'
    });
};


