var lastDig = [];
lastDig['ztype'] = '';
lastDig['num'] = '';
function clear_temp_dig()
{
	$.ajax({
		url: "/ajax/temp_digs.php", 
		data: {
			act: 'clear'
		}, 
		type: "GET", 
		dataType: "html", 
		success: function(){}
	});
}
function save_temp_dig(id, vote, type)
{ 
	if(id > 0)
	{
		if(type == 'dig')
		{
			$.ajax({
				url: "/ajax/temp_digs.php", 
				data: { 
					act:  'push',
					id:   id,
					vote: vote,
					type: type
				}, 
				type: "GET", 
				dataType: "html",
				success: function(){}			
			});
		}
		else
		{
			$.ajax({
				url: "/ajax/temp_digs.php", 
				data: { 
					act:  'push',
					id:   id,
					vote: vote['vote'],					
					story: vote['story'],
					dir: vote['dir'],					
					type: type
				}, 
				type: "GET", 
				dataType: "html",
				success: function(){}			
			});
		}
	}
}
function my_dig(Ztype,id)
{  
    if(!logined)
    {
        if(phpSelf=='view_page.php')
        {
            $('#login_f').toggle();
        }
        else
        {
        	$('#icq-add-msg').html('Чтобы ваш голос засчитался,');
        	show_icq(0);
        	digs = $('#num_digs'+id).html();
        	vote = 0;
        	
        	if(lastDig['num'] == ''  && lastCDig['num'] != 0)
        		lastDig['num'] = digs;
        	
            if( (Ztype=='-') )
            {
                $('#up_b_'+id).removeClass('up_active').addClass('up');
                if(lastDig['ztype'] != '+')
                {
                	$('#down_b_'+id).addClass('down_active');
                	$('#num_digs'+id).html(lastDig['num'] - 1);
                	vote = -1;
                }                
                else
                	$('#num_digs'+id).html(lastDig['num']);
            }
            else
            {
                $('#down_b_'+id).removeClass('down_active').addClass('down');
                if(lastDig['ztype'] != '-')
                {
                	$('#up_b_'+id).addClass('up_active');
                	$('#num_digs'+id).html(lastDig['num'] - 1 + 2);           	
                	vote = 1;
                }
                else
                	$('#num_digs'+id).html(lastDig['num']);
            } 
            lastDig['ztype'] = Ztype;
            if($('#num_digs'+id).html() == '')
            	$('#num_digs'+id).html('0');
                        
            save_temp_dig(id, vote, 'dig');
        }
        return;
    }

    if( Ztype=='-')
    {
 	   str = $('#num_digs'+id).html()*1 - 1;           
	   if(!str) str = '0';           
	   
       if($('#up_b_'+id).hasClass('up_active'))
       {
    	   $('#up_b_'+id).removeClass('up_active').addClass('up');
           $('#num_digs'+id).html(str);
       }
       else if(!$('#down_b_'+id).hasClass('down_active'))
       {
           $('#down_b_'+id).removeClass('down').addClass('down_active');
           $('#num_digs'+id).html(str);           
       }       
    }
    else
    {
  	   str = $('#num_digs'+id).html()*1 + 1;           
	   if(!str) str = '0'; 
	   
       if($('#down_b_'+id).hasClass('down_active'))
       {
     	  $('#down_b_'+id).removeClass('down_active').addClass('down');
          $('#num_digs'+id).html(str);    	   
       }
       else if(!$('#up_b_'+id).hasClass('up_active'))
       {
           $('#up_b_'+id).removeClass('up').addClass('up_active');
           $('#num_digs'+id).html(str);           
       } 
    }    
    
	 $.ajax({
	   url: '/ajax/dig.php',
	   data:{
	       i: id,
	       type: Ztype
	       }, 
	   type: 'GET',
	   dataType: "json",  
	    success: function(data,code,n)
	    { 
	        if(data.status==='0')
	        {
	            location.href = '/login.php';
	            return;
	        }
	        
            if($('#num_digs'+id).html()!=data.digs)
            {
                if( (Ztype=='-')&&data.status==3)
                {
                    $('#up_b_'+id).removeClass('up_active').addClass('up');
                    $('#down_b_'+id).addClass('down_active');
                }
                else if(data.status==5)
                {                                          
                    $('#down_b_'+id).removeClass('down_active').addClass('down');
                    $('#up_b_'+id).addClass('up_active');
                }
                else
                {
                   $('#down_b_'+id).removeClass('down_active').addClass('down'); 
                   $('#up_b_'+id).removeClass('up_active').addClass('up');
                }
            }
            $('#num_digs'+id).html(data.digs);	        
	    }
	     
	 });   
 
}

function add_podpiska(cat_id,img_id)
{
 if(!logined)
 {
     show_icq();
     return;
 }   
 $.ajax({
   url: '/ajax/podpiska.php',
   data:{
       i: cat_id
   },
   type: 'GET',
   dataType: "json",  
    success: function(data,code)
    {                                    
        if(data.status=='0')
            alert('Невозможно подписаться. Обратитесь к администратору');
        else if(data.status=='1')
        {
            
            $('#a_'+cat_id).attr('my_title','Отказаться от подписки');    
            $('#'+img_id).removeClass('add_rem_add').addClass('add_rem_delete');
              
            
        }
        else if(data.status=='2')
        {
            
            $('#a_'+cat_id).attr('my_title','Подписаться');
            
            $('#'+img_id).removeClass('add_rem_delete').addClass('add_rem_add');  
        }
    }
     
 });    
}

$(function(){
 k = Get_Cookie('last_click'); 
  if(phpSelf!='story.php')
  {
  $('table[rel=table]').each(function()
  {
  
      if(k==$(this).attr('lang'))
        {
             
            $('table[rel=table]').removeClass('table_dashed');
            $(this).addClass('table_dashed');
            Set_Cookie('last_click','');
        }
  });  
  }
  else Set_Cookie('last_click','');
  
  $('a[rel=story_url]').each(function()
  {
     this.onclick = function()
     {
         Set_Cookie('last_click',$(this).attr('href')); 
         id = $(this).attr('id');                           
         location.href = '/view_page.php?story_id='+id.substr(8,20);
         return false;
     } 
  });
});

function Set_Cookie( name, value)  
{
var today = new Date();
today.setTime( today.getTime() );
expires = 10 * 1000 * 60 * 60 * 24;
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" );
}
 
function Get_Cookie( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{return null;}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
} 

function windowHeight() {
var de = document.documentElement;

return self.innerHeight || ( de && de.clientHeight ) || document.body.clientHeight;
}

// Определение ширины видимой части страницы
function windowWidth() {
var de = document.documentElement;

return self.innerWidth || ( de && de.clientWidth ) || document.body.clientWidth;
} 