$(function(){
    $('.photo-change').mouseover(function(){
        var id = $(this).attr('id'); 
        Main.changeMain(id);
        
        clearInterval(intervalID);
    });
    
    $('.photo-change').mouseout(function(){
        intervalID = setInterval('Main.rotate()', 5000);
    });
    
    $('.photo-container').mouseover(function(){
        clearInterval(intervalID);
    });
    
    $('.photo-container').mouseout(function(){
        intervalID = setInterval('Main.rotate()', 5000);
    });
});

var intervalID = setInterval('Main.rotate()', 5000);

Main = {};

Main.aPhoto = {
        1: 'main-article-1',
        2: 'main-article-2',
        3: 'main-article-3'
};
Main.aPhotoRevert = {
        'main-article-1': 1,
        'main-article-2': 2,
        'main-article-3': 3
};

Main.currentPhoto = 1;

Main.changeMain = function( id )
{
    var src = $('#' + id + ' img').attr('src');
    var title = $('#' + id + ' input.title-rotate').val();
    var href = $('#' + id + ' input.href-rotate').val();
    
    $('.photo-container .title a').attr({href: href});
    $('.photo-container .main-photo-href').attr({href: href});
    $('.photo-container img').attr({src: src});
    $('.photo-container .title a').html(title);
    
    $('.main-small-photos li').removeClass('on');
    $('#' + id).addClass('on');
    
    Main.currentPhoto = Main.aPhotoRevert[id];
};

Main.rotate = function( )
{
    switch(Main.currentPhoto)
    {
        case 1:
            Main.changeMain(Main.aPhoto[2]);
          break;
        case 2:
            Main.changeMain(Main.aPhoto[3]);
          break;
        case 3:
            Main.changeMain(Main.aPhoto[1]);
        break;
          
        default:
            Main.changeMain(Main.aPhoto[1]);
        break;
    }
};
$(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'
    });
};
$(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'
    });
};

