var isloading=false;
var first=true;
var forward=false;
var mainmenu=false;
var oldhref="";
var submenu=false;


function parseScript(_source) {
		var source = _source;
		var scripts = new Array();
		
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
			
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
		
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(ex) {
				// do what you want here when a script fails
			}
		}
		
		// Return the cleaned source
		return source;
	}
	
	    
function scrolls(){
	//$('.scroll-pane').jScrollPane({showArrows:true,    dragMaxHeight:43,  scrollbarOnLeft:false , reinitialiseOnImageLoad:true });

	fleXenv.initByClass("flexcroll");
	$('.flexcroll img').load(function(){
		fleXenv.updateScrollBars();
	});		
}
	    	
function ajax(href){
if(first){first=false;if(href==""){return;}}


if(href==""){ href=window.location.pathname+window.location.search;}
	isloading=true;
	
	
	
	
var u="http://"+window.location.host+"/"+href;
	$.ajax({
		url: u,
		context: document.body,
		beforeSend: function( data ){
		},
		error:  function(){
			isloading=false;
			first=false;
			forward=false;
			alert("ошибка запроса");
		},
		success: function(data){
			if(oldhref=="/"||href=="/"){

    if(BrowserDetect.browser!="Explorer")	    {
				$('#wrap_in').attr("id","oldwrap_in");	
				$('#oldwrap_in').hide("fade",{}, 500,function (){
					var contentdiv = $(document.createElement('div'));					
					contentdiv.attr("id","wrap_in");					
					contentdiv.css("display","none");									
					$('#oldwrap_in').after(contentdiv);	
					$('#wrap_in').show(1000,
					function (){  
	                    $("#side_menu").fadeIn(1000); isloading=false;scrolls();
					});

					$('#wrap_in').html($('#wrap_in',data).html());				
					parseScript(data);				
					links($('a').not("[href^='#'],[href^='javascript:'],[href^='http://'],[href^='ftp://'], [class*='nonajax']"));				                       
					$(this).remove();
				});
}else{
$('#wrap_in').html($('#wrap_in',data).html());	
}

			}else
		
			if(submenu && $("#middle_right").length>0 && $('#middle_right',data).length>0){
			
				//$('#oldmiddle_right').remove();					
				//$('#middle_right').css("position","absolute");					
				//$('#middle_right').attr("id","oldmiddle_right");					
				//var contentdiv = $(document.createElement('div'));					
				//contentdiv.attr("id","middle_right");					
				//$('#oldmiddle_right').after(contentdiv);	
			
				$('#middle_right').html($('#middle_right',data).html());				
				parseScript(data);				
				links($('#middle a').not("[href^='#'],[href^='javascript:'],[href^='http://'],[href^='ftp://'], [class*='nonajax']"));				                       

				//$('#oldmiddle_right').hide("fade",{}, 1000,function (){$(this).remove();});	
				//$('#middle_right').show("fade",{}, 1000,function (){isloading=false;});
			
						
				var image = $(document.createElement('img'));		
				image.load(function(){	
				var oldimg=$('#middle_left img');
					oldimg.before(image);
					oldimg.hide("fade",{}, 1000,function (){$(this).remove();});		
				});
				image.attr("src",$('#middle_left img',data).attr("src"));
scrolls();
				
				
				
			//$('#middle_left img').attr("src",$('#middle_left img',data).attr("src"));
			
				$("#side_menu a").each(function(index) {
					if(getpagename(this.href) == href){
							this.className = "active";
					}
					else{
							this.className = "";
					}
				});
				isloading=false;
			
			}else{
			
				$('#oldmiddle').remove();					
				$('#middle').css("position","absolute");					
				$('#middle').attr("id","oldmiddle");					
				var contentdiv = $(document.createElement('div'));					
				contentdiv.attr("id","middle");					
				$('#oldmiddle').after(contentdiv);	
			
				$('#middle').html($('#middle',data).html());	
				$('#bottom').html($('#bottom',data).html());				
				parseScript(data);				
				links($('#middle a').not("[href^='#'],[href^='javascript:'],[href^='http://'],[href^='ftp://'], [class*='nonajax']"));				                       

				$('#oldmiddle').hide("slide",{direction:mainmenu? (forward?"down":"up"):(forward?"left":"right")}, 1000,function (){$(this).remove();});	
				$('#middle').show(
					"slide",
					{direction:mainmenu? (forward?"up":"down"):(forward?"right":"left")}, 
					1000,
					function (){  
	                                     $("#side_menu").fadeIn(1000); 
						isloading=false;
scrolls();

					}
				);
			}
			forward=false;
			oldhref=href;
		}
	});
}

	
function getpagename(href){


var url = href.replace(/(^http:\/\/)/, '').replace(window.location.host+"/", '');
	if(url.match(/\?/)){   
		url=window.location.href.replace(window.location.host+"/", '').replace(/(^.*#)/, '').replace(/%2F/g, '/').replace(/%3F[^\n]*(\n|$)/g,'')+url.replace(/(^.*\?)/, '?');
	}
	return url;
}

function links(area){  
	area.click(function(){
		if(isloading==false){  
			forward=true;
			$.history.load(getpagename(this.href));
		}
		return false;
	});
				
}
	
$(document).ready(function(){

$("#snow").click( function(event) {
  event.preventDefault();
});


	$("#side_menu").fadeIn(2000); 
	 BrowserDetect.init();	


if(oldhref==""){ oldhref=window.location.pathname+window.location.search;}

	links($('a').not(" [href^='#'], [href^='javascript:'],[href^='http://'], [class*='nonajax']"));


    $.history.init(function(url){ 
		if(isloading)return false;
		if(forward){
			if(url.lastIndexOf("/",url.length-2)==-1)mainmenu=true;else mainmenu=false;
		}else{
			if(oldhref.lastIndexOf("/",oldhref.length-2)==-1)mainmenu=true;else mainmenu=false;
		}

		if(!mainmenu&&url.substr(0,  url.indexOf('/'))==oldhref.substr(0,  oldhref.indexOf('/'))){
			
				submenu=true;}
			else 
				submenu=false;
		
		ajax(url);
	});
});

