//resize
$(function() {
  $(window).resize(function(){
    var w = $('#bottom').width();
	$('#move').width(w+"px");
	$('#move').css({left:"0px"});
	$('div.move_item').width(w+"px");
  });
});

//animate по стрелкам
$(function() {	
  $("a.arrow_right").click(function() {
	if ($.browser.msie && ($.browser.version < 7)) return true;  
    var delayHref = $(this).attr('href');
	add_rel = $(this).attr('rel');
	if (add_rel && delayHref) {
	  if (allpage[add_rel]['msg']) {
	    move_arrow_next(allpage[add_rel]['msg']); 
	  } else { 	  
		$.get(
		  delayHref,
		  {
		  },
		function(msg) {
		  if (allpage[add_rel]['msg']) allpage[add_rel]['msg'] = msg;
		  move_arrow_next(msg);	
		});
	  }  
	} else {
	  return true;
	}	
	return false;
  });

  $("a.arrow_left").click(function() {
	if ($.browser.msie && ($.browser.version < 7)) return true;
    var delayHref = $(this).attr('href');
    add_rel = $(this).attr('rel');
	if (add_rel && delayHref) {
	  if (allpage[add_rel]['msg']) {
	    move_arrow_prev(allpage[add_rel]['msg']); 	  
	  } else {		
	    $.get(
	      delayHref,
	      {
		  },
		function(msg) {
		  if (allpage[add_rel]) allpage[add_rel]['msg'] = msg;
		  move_arrow_prev(msg);
		});
	  }  
	} else {
	  return true;
	}	
	return false;
  });
});


function move_arrow_next(msg) {
var w = $('#p_'+current_id).width();
$("#p_"+current_id).width(w+"px");
$("#p_"+current_id).after(msg);
$("#p_"+add_rel).width(w+"px");

$("#page-right, #page-left").hide();

$('#move').css({width:w+w+"px"});
if (current_id == 'index') {
  if (!$('#bottom_main').hasClass('none'))  $('#bottom_main').addClass('none'); 
}
if (allpage[current_id]['bottombg'] == '1') 
 $('#bottom_work_page').attr("class", "none");

if ($("#"+current_id+"_swf").length) $('#'+current_id+'_swf').html('');

$("#move").animate({"left": "-="+w+"px"}, "slow", function() {
	$("#p_"+current_id).remove();
	$('#move').css({width:w+"px"});
	$('#move').css({left:"0px"});
	current_id = add_rel;
	current_taborder = allpage[current_id]['taborder'];
	
	if (current_id == 'index') {
	  if ($('#bottom_main').hasClass('none'))  $('#bottom_main').removeClass('none');
	  if (!$('#gohome').hasClass('none'))  $('#gohome').addClass('none');
	} else { 
	  if (allpage[current_id]['bottombg'] == '1') $('#bottom_work_page').attr("class", "bg_bottom_"+current_id);
	  if ($('#gohome').hasClass('none'))  $('#gohome').removeClass('none');
	}  
	
	if (allpage[current_id]['prev']) {	  
	  $("#page-left").attr('href',allpage[current_id]['prev']['url']);
	  $("#page-left").attr('rel',allpage[current_id]['prev']['id']);
	  if ($("#page-left").hasClass('none')) $("#page-left").removeClass('none');
	  $("#page-left").show();
	} else { 
	  if (!$("#page-left").hasClass('none')) $("#page-left").addClass('none');
	  $("#page-left").hide();
	}  
		
	if (allpage[current_id]['next']) {	  
	  $("#page-right").attr('href', allpage[current_id]['next']['url']);
	  $("#page-right").attr('rel', allpage[current_id]['next']['id']);
	  if ($("#page-right").hasClass('none')) $("#page-right").removeClass('none');
	  $("#page-right").show();
	} else {
	  if (!$("#page-right").hasClass('none')) $("#page-right").addClass('none');
	  $("#page-right").hide();
	}
	selMenu(current_id);
});
}
function move_arrow_prev(msg) {
var w = $('#p_'+current_id).width();
$("#p_"+current_id).width(w+"px");
$("#p_"+current_id).before(msg);
$("#p_"+add_rel).width(w+"px");
$('#move').css({width:w+w+"px"});
$('#move').css({left:-w+"px"});

$("#page-right, #page-left").hide();
if (current_id == 'index') {
  if (!$('#bottom_main').hasClass('none'))  $('#bottom_main').addClass('none');
}
if (allpage[current_id]['bottombg'] == '1')
  $('#bottom_work_page').attr("class", "none");


if ($("#"+current_id+"_swf").length) $('#'+current_id+'_swf').html('');

$("#move").animate({"left": "+="+w+"px"}, "slow", function() {
  $('#move').css({width:w+"px"});
  $('#move').css({left:"0px"});
  $("#p_"+current_id).remove();
  current_id = add_rel;
  current_taborder = allpage[current_id]['taborder'];
  
  if (current_id == 'index') {
	if ($('#bottom_main').hasClass('none'))  $('#bottom_main').removeClass('none');
	if (!$('#gohome').hasClass('none'))  $('#gohome').addClass('none');
  } else { 
	if (allpage[current_id]['bottombg'] == '1') $('#bottom_work_page').attr("class", "bg_bottom_"+current_id);  
    if ($('#gohome').hasClass('none'))  $('#gohome').removeClass('none');
  }
  
  if (allpage[current_id]['prev']) {	  
    $("#page-left").attr('href',allpage[current_id]['prev']['url']);
    $("#page-left").attr('rel',allpage[current_id]['prev']['id']);
	if ($("#page-left").hasClass('none')) $("#page-left").removeClass('none');
	$("#page-left").show();
  } else { 
    if (!$("#page-left").hasClass('none')) $("#page-left").addClass('none');
	$("#page-left").hide();
  }
  if (allpage[current_id]['next']) {	  
    $("#page-right").attr('href', allpage[current_id]['next']['url']);
	$("#page-right").attr('rel', allpage[current_id]['next']['id']);
	if ($("#page-right").hasClass('none')) $("#page-right").removeClass('none');
	$("#page-right").show();
  } else {
    if (!$("#page-right").hasClass('none')) $("#page-right").addClass('none');
	$("#page-right").hide();
  }
  selMenu(current_id);
});
}


//Обработка нажатий на пункты меню и другие ссылки
$(function() {	
  $("a.ataborder").click(function() {
	  return aclick($(this));
  });
});

function aclick(obj) {
  if ($.browser.msie && ($.browser.version < 7)) return true;	
  var delayHref = obj.attr('href');
	add_rel = obj.attr('rel');
	if (add_rel && delayHref && allpage[add_rel]) {
		var movetaborder = Number(allpage[add_rel]['taborder']);
		if (isNaN(Number(current_taborder))) current_taborder = 0;
		if (isNaN(movetaborder)) movetaborder = 999;
		
		if (allpage[add_rel]['msg']) {
		  if (movetaborder > current_taborder) move_arrow_next(allpage[add_rel]['msg']); 
		  if (movetaborder < current_taborder) move_arrow_prev(allpage[add_rel]['msg']);
		} else { 	  
		  $.get(
		    delayHref,
		    {
		    },
		  function(msg) {
		    if (allpage[add_rel]['msg']) allpage[add_rel]['msg'] = msg;
		    if (movetaborder > current_taborder) move_arrow_next(msg); 
			if (movetaborder < current_taborder) move_arrow_prev(msg);
		  });
		}
	} else {
	  return true;
	}
  return false;
}  

function selMenu(selid) {
  if (selid == 'index') {
    $("#menu a").each(function() {
	  if ($(this).hasClass('menu_current')) $(this).removeClass('menu_current');
	  var child = $(this).attr('id');
	  if ($("#s"+child).length) {
		$("#s"+child+" a").each(function() {
		  if ($(this).hasClass('smenu_current')) $(this).removeClass('smenu_current');	
		}); 	  
	  } 
	});	  
  } 	  
  if ($("#menu"+selid).length) {
    $("#menu a").each(function() {
	  if ($(this).hasClass('menu_current')) $(this).removeClass('menu_current');
	  var child = $(this).attr('id');
	  if ($("#s"+child).length) {
		$("#s"+child+" a").each(function() {
		  if ($(this).hasClass('smenu_current')) $(this).removeClass('smenu_current');	
		}); 	  
	  } 	  
	});
    
    if ($("#menu"+selid).attr('parent')) {
      var parent = $("#menu"+selid).attr('parent');
      $('#'+parent).addClass('menu_current');
      $("#menu"+selid).addClass('smenu_current');
    } else {
      $("#menu"+selid).addClass('menu_current');	
    }	
  }
}
