var Load_Timeout = 10000; // seconds
var Current_Destination = 'main-body';
var timeout;
var start = 'yes';
var site = location.host;




function checkAnchorOnload()
	{

	var urlPathName = location.pathname;
	var urlAnchor = location.hash;

	if(start == 'yes')
		{
			
		if(urlPathName == "/" && urlAnchor == "") 
			{
			menuGo("intro");
			
			}
		else if(urlAnchor != "" && urlAnchor.indexOf("#") != -1)
			{
			$(document).ready(function(){menuGo(urlAnchor.replace("\#", ""));});
				}
		start = '';
		}
	else if(start == '')
		{
		if(urlPathName == "/") 
			{
			if(urlAnchor == "" || (urlAnchor == "#" && navigator.appName == 'Microsoft Internet Explorer')) 
				{
				menuGo("http://"+site+"/");
				}
			else if(urlAnchor.indexOf("#") != -1) 
				{
				menuGo(urlAnchor.replace("\#", ""));
				}
			}
		}
	}


function historyHandler() {
	var stateVar = "nothin'", displayDiv = $("#main-column");
	
	this.getNewHistoryState = function(currentState) {
		var newVal = currentState;
		unFocus.History.addHistory(newVal);
	};
	
	this.historyListener = function(historyHash) {
		stateVar = historyHash;
		// при каждом изменении истории мы будем
		// выдавать соответствующий контент
		checkAnchorOnload();
	};
	unFocus.History.addEventListener('historyChange', this.historyListener);
	
	this.historyListener(unFocus.History.getCurrent());
};

var historyVar = new historyHandler();

function menuGo(destination){
	$('body').animate({scrollTop:0}, 'slow'); 
	if (Current_Destination+"%20active" == destination) {return false;}
	document.title = ($('a[href$="/'+destination+'"]').attr("title")+" — Продам Слона");
	$("#loader").fadeIn();
	$("#main-column").ready(function(){
		$(".active").removeClass("active");
		Current_Destination = destination;
		$('a[href$="'+destination+'"]').parent().addClass("active");
			$.get("/"+destination,function(data){
			//      $.get('/co/',{a:'get_response',page:destination},function(response){
					$("#main-column").html(data);
			//		$("#loader").fadeOut();	
			//		 pageTracker._trackPageview("/"+destination );	
			//			
			//	});
			
			
			
			});
		});


	return false;
}

function menuBack(){
	if (Menu_History.length > 0) {
		destination = Menu_History.shift();
		
		$(".active").removeClass("active");
		$("li." + destination).addClass("active");
		
		$("#loader").fadeIn();
		clearTimeout(timeout);
		$("#main-column").load("/" + destination, function(){
			$("#loader").fadeOut();
	
			
		});
		if(Menu_History.length==0){$("#back-button").attr("disabled","disabled");} 
	}
}



function saveHistory(obj){
	destination = obj.attr("href").replace("\/","");
	historyVar.getNewHistoryState(destination);

}

function sendResponse(email,name,text,page){
	$.post("/co/",{a:'add_response',text:text,email:email,name:name,page:page},function(){
		$('#ans_text').val("");
		if ($("ul.reponse").children.length==1){$("#no_answers").remove()}
		$("ul.response").append("<li><strong>"+name+"</strong>: <em>"+text+"</em></li>");
		$('#answers').animate({bottom:'-225px'}, 50);
		$('#thankyou').fadeIn();
		setTimeout(function(){$('#thankyou').fadeOut()},1500);
		
	});	
	
}

