/*######  Выпадающее меню ############*/
    $(document).delegate('#morecatspointer', 'click', function(){$('#morecats').show();});
    $(document).delegate('#morecats', 'mouseover', function(){overCats = true;});
    $(document).delegate('#morecats', 'mouseleave', function(){overCats = false;});
    $(document).delegate('#morecatspointer', 'mouseleave', function(){setTimeout("if(!overCats) $('#morecats').hide();", 300)});
/*####################################*/

/*######  Выпадающее меню myaccount ############*/
    $(document).delegate('#malistpointer', 'click', function(){$('#malist').show();});
    $(document).delegate('#malist', 'mouseover', function(){overMaList = true;});
    $(document).delegate('#malist', 'mouseleave', function(){overMaList = false;setTimeout("if(!overMaList) $('#malist').hide();", 300);});
    $(document).delegate('#malistpointer', 'mouseleave', function(){setTimeout("if(!overMaList) $('#malist').hide();", 300)});
/*####################################*/
    
    function show_stats(obj, uid)
    {
        if ( ! $('ul.user-menu').length)
        {
            $.get('/profile_getstats_i'+uid+'.html', 
                function(data){
                    $(obj).after(data);
                }
            );
        }
        else
        {
            $('ul.user-menu').remove();
        }
        
    }
    

if(!$.browser.mobile){
   var xhr = undefined;
   var overCats;
   var overMaList;
   var back = false;
   var arHistory = Array();
   var updateAjaxOnMain = true;
   
   var sucstory;
   function pushHistory(url) {
    arHistory[arHistory.length] = url;
   }
/*####################################*/
function backButton() {
    if(arHistory[0]) {
        back = true;
        reloadContentURL(arHistory[arHistory.length-1]);
        if (typeof history.pushState === 'undefined' || ($.browser.safari && $.browser.version == '533.18.1')){
            window.location.hash = arHistory[arHistory.length-1];
        } else {
            window.history.pushState('', '', arHistory[arHistory.length-1]);
        }
        arHistory.splice(arHistory.length-1);
        
    } else {
        window.history.back();
    }
}   
/*####################################*/


    function progress_href(href) {
        nhref = href.replace(window.location.host, '');
        if(nhref[0] != '/')
            nhref = '/' + nhref;
        return nhref;
    }
    
    function get_host() {
        return 'http://'+window.location.host.replace('/','').replace('http:','');
    }
/*#####################################*/
$(document).ready(function(){

    $(document).delegate('.pagination select','change',function(){
    	var curloc = window.location.toString();
    	if(curloc.indexOf('#') != -1) {
    		xhr = $.get(curloc.substring(curloc.indexOf('#')+1), $('.pagination form').serialize(),
    			function(data) {
    				$('#ajaxblock').html(' '+data);
    				xhr=undefined;
    				//updateFormsWithoutAction();
    		},'html');
    	}
    	else {
    		xhr = $.get(curloc, $('.pagination form').serialize(),
    			function(data) {
    				$('#ajaxblock').html(' '+data);
    				xhr=undefined;
    		},'html');
    	}
    });
    
   	$(document).delegate('a[rel=like]','click', function(e){
		var __href = $(this).attr('href');
		if(__href && __href.indexOf('?') == 0) {
			e.preventDefault();
			$.get(get_path()+__href);
			$(this).addClass('selected').removeAttr('href');
		}
	});
    
});
/*#####################################*/
function updateFormsWithoutAction() {
	/* Forms without action*/
	$('form:not([action]), form[action=""]').not('#comment_form').attr('action',get_path());
}
/*#####################################*/


/*#####################################*/

function reloadContent() {
    if(back){
       return false; 
    }
    updateAjaxOnMain = false;
    if(sucstory)
        clearTimeout(sucstory);
	if(curuser.statusInterval) {
		clearInterval(curuser.statusInterval);
		curuser.statusInterval = undefined;
	}
    
    //$("#layer, #ajaxlayer").fadeIn(500);
    
    if(xhr != undefined) {
        xhr.abort();
    }
    
	var curloc = window.location.toString();
	if(curloc.indexOf('#') != -1) {
		xhr = $.get(curloc.substring(curloc.indexOf('#')+1), {nc:Math.random()},
			function(data) {

				$('#ajaxblock').html(' '+data);
                $('#loginbox, #logohead').show();
                if(window.inlinePlayer)
                    inlinePlayer.refresh();
                //$("#layer, #ajaxlayer").fadeOut(100);
				xhr=undefined;
		},'html');
	}
	else {
		xhr = $.get(curloc,{nc:Math.random()},
			function(data) {
				$('#ajaxblock').html(' '+data);
                $('#loginbox, #logohead').show();
                if(window.inlinePlayer)
                    inlinePlayer.refresh();
                //$("#layer, #ajaxlayer").fadeOut(100);
				xhr=undefined;
		},'html');
	}
}

function reloadContentURL(url) {
    updateAjaxOnMain = false;
    if(sucstory)
        clearTimeout(sucstory);
        
    if(window.soundManager)
    {
        //soundManager.stopAll();
        soundManager.reboot();
    }
	if(curuser.statusInterval) {
		clearInterval(curuser.statusInterval);
		curuser.statusInterval = undefined;
	}
    //$("#layer, #ajaxlayer").fadeIn(500);
    
    if(xhr != undefined) {
        xhr.abort();
    }
    
	xhr = $.get(url, {nc:Math.random()},
		function(data) {
		    $('#loginbox, #logohead').show();
			$('#ajaxblock').html(' '+data);
            //$("#layer, #ajaxlayer").fadeOut(100);
			xhr=undefined;
            back = false;
	},'html');
}

function get_path()
{
    if(window.location.hash)
		path = window.location.hash.replace('#','');
	else
		path = window.location.pathname+window.location.search;
		
		//path_arr = path.split('?');
	return progress_href(path);//path_arr[0].toString();
}

function get_twitter_path()
{
    return get_host() + get_path();
}

/*######### redirect if is hash #############*/
if(window.location.hash)
{
	reloadContent();
}

    /*if ('onhashchange' in window) {*/
    if ( !$.browser.msie && 'onhashchange' in window) {
    	
    	window.onhashchange = function() {
    		reloadContent();
    	};
    }
    else {
    	setInterval(function(){
    		if(__hash != window.location.hash.replace('#','')) {
    			reloadContent();
    			__hash = window.location.hash.replace('#','');
    		}
    	},1);
    }

/*#####################################*/

/*#####################################*/
if (typeof history.pushState === 'undefined' || ($.browser.safari && $.browser.version == '533.18.1')) { //нет html5

 
    var __hash = '';
    

    /*#####################################*/
    $(document).ready(function(){
        
    $(document).delegate('.pagination select','change',function(){
    	var curloc = window.location.toString();
    	if(curloc.indexOf('#') != -1) {
    		xhr = $.get(curloc.substring(curloc.indexOf('#')+1), $('.pagination form').serialize(),
    			function(data) {
    				$('#ajaxblock').html(' '+data);
    				xhr=undefined;
    				//updateFormsWithoutAction();
    		},'html');
    	}
    	else {
    		xhr = $.get(curloc, $('.pagination form').serialize(),
    			function(data) {
    				$('#ajaxblock').html(' '+data);
    				xhr=undefined;
    				//updateFormsWithoutAction();
    		},'html');
    	}
    });	
    relinks();
    });
/////////
    
   
    /*#####################################*/
    

    
    /*#####################################*/
    function relinks (){
    	$(document).delegate('a:not([rel=redirect]):not([rel=like])','click', function(e){
    		var __href = $(this).attr('href');
    		if( __href && 
    			__href.toLowerCase().indexOf('javascript:') == -1 && 
    			__href.indexOf('#') == -1 && 
    			__href.indexOf('?') != 0 && 
    			((__href.indexOf('http')==0 && __href.indexOf('http://'+window.location.host.replace('http://','')) == 0) || __href.indexOf('http') == -1)
    			) {
    			e.preventDefault();
                pushHistory(get_path());
    			var href = $(this).attr('href');
    				oldurl = window.location.toString();
    				newurl = href.replace('http://'+window.location.host.replace('http://',''), '').replace(window.location.hash,'').replace('#','');
    				window.location.hash = newurl;
    			$('.navigation a[class=active]').removeClass('active');
    		}
    		else if(__href && __href.indexOf('?') == 0) {
    			e.preventDefault();
    			window.location.hash = get_path()+__href;
    		}
    	});
    }
    
    /*#####################################*/
    
    	$.keyboard('ctrl+aleft', function(e){
    		var __href = $('.pagination a.prev').attr('href');
    		newurl = __href.replace('http://'+window.location.host.replace('http://',''), '').replace(window.location.hash,'').replace('#','')
    		window.location.hash = newurl;
    	});
    
    	$.keyboard('ctrl+aright', function(e){
    		var __href = $('.pagination a.next').attr('href');
    		newurl = __href.replace('http://'+window.location.host.replace('http://',''), '').replace(window.location.hash,'').replace('#','')
    		window.location.hash = newurl;
    	});
    	
}
else { //есть html5
    var __path = get_path();
    $(document).ready(function(){
        

        /*#####################################*/
        function relinks (){
        	$(document).delegate('a:not([rel=redirect]):not([rel=like])','click', function(e){
        		var __href = $(this).attr('href');
        		if( __href && 
        			__href.toLowerCase().indexOf('javascript:') == -1 && 
        			__href.indexOf('?') != 0 && 
        			((__href.indexOf('http')==0 && __href.indexOf('http://'+window.location.host.replace('http://','')) == 0) || __href.indexOf('http') == -1)
        			) {
        			e.preventDefault();
                    pushHistory(get_path());
        			var href = $(this).attr('href');
    				oldurl = window.location.toString();
    				newurl = href.replace('http://'+window.location.host.replace('http://',''), '').replace(window.location.hash,'').replace('#','');
    				history.pushState('', '', newurl);
                    
        			
                    if(__path != get_path())		
        			 $('.navigation a[class=active]').removeClass('active');
        		}
        		else if(__href && __href.indexOf('?') == 0) {
        			e.preventDefault();
        			history.pushState('', '', get_path()+__href);
                    reloadContent();
        		}
        	});
        }
        
        relinks();
    	setInterval(function(){
    		if(__path != get_path()) {
    			reloadContent();
    			__path = get_path();
    		}
    	},1);
        
        $.keyboard('ctrl+aleft', function(e){
    		var __href = progress_href($('.pagination a.prev').attr('href'));
    		history.pushState('', '', get_host()+__href);
    	});
    
    	$.keyboard('ctrl+aright', function(e){
    		var __href = progress_href($('.pagination a.next').attr('href'));
    		history.pushState('', '', get_host()+__href);
    	});
        
    });
}
}

