jQuery(window).load(function(){
//    if (location.href.indexOf('#!') == -1) {
//	current_url=location.href.replace(ttHome,"").replace(/^\//, "");
//	if(current_url!=''){
//	    location.href = ttHome+"/#!/"+current_url;
//	    tt_ajax_call();
//	}
//    }
});
jQuery(document).ready(function(){
    jQuery('#navigation a, .entry-title a, .pager-list a, a.comment-count, #wp-calendar a, .entry-tags a, .author a, .recentcomments a:last-child, .recent-news-item a, .footer-bottom-menu a, li.menu-item a, li.cat-item a, li.page_item a, .author-link a, .comment-replay-link a, .entry-content a.more-link, form#commentform .form-submit input#submit, #commentform p a, a.ajax-link, #archives li a, .entry-image-slide a, .hover-content a, .widget_recent_entries a').live('click',function(e){
	if(!jQuery(this).hasClass('anchorLink')&&!e.ctrlKey&&!e.shiftKey&&jQuery(this).attr('target')!='_blank'){
	    if(jQuery(this).attr('href')!='#'){
		var link_url=jQuery(this).attr('href');
		if(!(link_url.indexOf('http') === 0)){
		    link_url='http://'+window.location.host+link_url;
		}
		if(!(link_url.indexOf(ttHome)===0)) return;
		var post_slug = link_url.replace(ttHome,"").replace(/^\//, "");
		if(post_slug!='')
		    jQuery.address.crawlable(true).value(post_slug);
		else{
		    window.location.hash='!';
		}
	    //tt_ajax_call();
	    }	    
	    return false;
	}
    });
    if(location.href.indexOf('#!')!=-1){
	tt_ajax_call();
    }else{
        showBody();
        theme_scripts();
        tt_init_custom_scripts();
    }
    jQuery(window).hashchange( function(e) {
	tt_ajax_call();
    });

    jQuery.validator.setDefaults({
	submitHandler:function(frm){
	    var data = jQuery(frm).serialize();
	    jQuery('#container_main').html('<div class="tt_ajax_loading"></div>');
	    showBody();
	    jQuery.post(jQuery(frm).attr('action'), data, function(response) {
		//alert(response);
		$retVal=jQuery(response).find('#container_main').children();
		jQuery('#container_main').html($retVal);
		showBody();
		theme_scripts();
		initScripts('body');
		tt_init_custom_scripts();
	    });
	    return false;
	}
    });
    jQuery('#searchform').live('submit',function(){
	var post_slug = '?s='+jQuery(this).find('#s').val();

	jQuery.address.crawlable(true).value(post_slug);
	return false;
    });
});

function tt_ajax_call(){
    jQuery('#container_main').css('height',jQuery('#container_main').height());
    jQuery('#container_main').animate({
	opacity:0
    },500,callAnimateEndedAjax);
    
}
function callAnimateEndedAjax(){
    jQuery('#container_main').css('opacity','1');
    jQuery('#container_main').html('<div class="tt_ajax_loading"><div class="ajax-loader"></div></div>');
    jQuery('#container_main > div').animate({
	opacity:1
    },100,dondogmaa);
    
}
var loaderProgress;
var currentState;
function dondogmaa(){
    jQuery.ajax({
	url:location.href.replace("#!/",""),
	beforeSend: function (thisXHR) {
	    currentState=thisXHR;
	    //ResultsWindow.LoadingStart();
	    loaderProgress=setInterval(function(){
		if (currentState.readyState > 2) {
		    var totalBytes = currentState.getResponseHeader('Content-length');
                    var dlBytes = currentState.responseText.length;
		    if(totalBytes>0){
			jQuery('#container_main .tt_ajax_loading').html("Downloading: " + Math.round((dlBytes / totalBytes) * 100) + "%");
		    }else{
			jQuery('#container_main .tt_ajax_loading').html("blabla");
		    }
                    //(totalBytes > 0) ? progressElem.html() : "Downloading: " + progressElem.html(Math.round(dlBytes / 1024) + "K");
		}
	    }, 5);
	},
	success: function(responce){
	    //ResultsWindow.LoadingEnd();
	    clearInterval(loaderProgress);
	    jQuery('#container_main').css('height','auto');
	    $retVal=jQuery(responce).find('#container_main').children();
	    jQuery('#container_main').html($retVal);
	    theme_scripts();
	    initScripts('body');
	    tt_init_custom_scripts();
	    showBody(100);
	    jQuery('body,html').animate({
		scrollTop:0
	    },'slow');
	}
    });
}


