jQuery(function( $ ){
$('#contents').hide();
//$('#dinamic').hide();

$('#textStatus').show();

$.preload( '#hidden_images img', {//the first argument is a selector to the images
		onRequest:request,
		onComplete:complete,
		onFinish:finish,
		placeholder:'/images/placeholder.jpg',//this is the really important option
		//notFound:'../images/notfound.jpg',//optional image if an image wasn't found
		threshold: 1 //'2' is the default, how many at a time, to load.
	});
    function update( data ){
		$('#textStatus_done').html( data.done );
		$('#textStatus_total').html( '/'+data.total );
		$('#textStatus_loaded').html( data.loaded );
		$('#textStatus_failed').html( data.failed );
	};
	function complete( data ){
		update( data );
		$('#textStatus_next').html( 'none' );//reset the "loading: xxxx"
		$('#textStatus_loaded').html( data.image );
	};
	function request( data ){
		update( data );
		$('#textStatus_next').html( data.image );//set the "loading: xxxx"
	};
	function finish(){
        restore_background();
        $('#textStatus').hide();
	};
function restore_background() {
$('#contents').show();
//$('#dinamic').show();
$('#head').css( { backgroundImage: 'url(images/header.jpg)'} );
$('#plashka_plato_ukok').css( { backgroundImage: 'url(images/plato_ukok_bg.jpg)'} );
$('#plashka_gora_bogdo').css( { backgroundImage: 'url(images/gora_bogdo_bg.jpg)'} );
$('#plashka_tury_kavkaza').css( { backgroundImage: 'url(images/tury_kavkaza_bg.jpg)'} );
$('#plashka_volga').css( { backgroundImage: 'url(images/volga_bg.jpg)'} );
$('#plashka_digoria2').css( { backgroundImage: 'url(images/digoria2_bg.jpg)'} );
$('#plashka_kalmikia').css( { backgroundImage: 'url(images/kalmikia_bg.jpg)'} );
$('#plashka_digoria').css( { backgroundImage: 'url(images/digoria_bg.jpg)'} );
};

$("#plato_ukok").load("text/plato_ukok.txt");
$("#gora_bogdo").load("text/gora_bogdo.txt");
$("#tury_kavkaza").load("text/tury_kavkaza.txt");
$("#volga").load("text/volga.txt");
$("#digoria2").load("text/digoria2.txt");
$("#kalmikia").load("text/kalmikia.txt");
$("#digoria").load("text/digoria.txt");



$("#dinamic").hide();

function restore_underline(){
  $("#team").css( {textDecoration: 'underline' } );
  $("#count_point").css( {textDecoration: 'underline' } );
  $("#world_map").css( {textDecoration: 'underline' } );
  $("#horison_of_events").css( {textDecoration: 'underline' } );
  $("#at_fire").css( {textDecoration: 'underline' } );
	};

	$(".plashka").click(function(){
		$(this).next("p").slideToggle("slow")
		.siblings("p:visible").slideUp("slow");
    $("#dinamic").slideUp("slow");
    restore_underline();    
	});
  
	$(".accordion .close").click(function(){
		$(this).parent().slideToggle("slow");
    restore_underline();
	});

	$("#team").click(function(){
    restore_underline();
    $(this).css( {textDecoration: 'none' } );
		$(".accordion p").slideUp("slow");
    $("#dinamic").slideUp("slow");
    $("#dinamic_p").load("text/komanda.txt");
    $("#dinamic").slideDown("slow");
  });
  
  $("#count_point").click(function(){
    restore_underline();
    $(this).css( {textDecoration: 'none' } );
		$(".accordion p").slideUp("slow");
    $("#dinamic").slideUp("slow");
    $("#dinamic_p").load("text/tochka.txt");
    $("#dinamic").slideDown("slow");
  });
  
  $("#world_map").click(function(){
    restore_underline();
    $(this).css( {textDecoration: 'none' } );
		$(".accordion p").slideUp("slow");
    $("#dinamic").slideUp("slow");
    $("#dinamic_p").load("text/karta_mira.txt");
    $("#dinamic").slideDown("slow");
  });
  
	$("#horison_of_events").click(function(){
    restore_underline();
    $(this).css( {textDecoration: 'none' } );
		$(".accordion p").slideUp("slow");
    $("#dinamic").slideUp("slow");
    $("#dinamic_p").load("text/gorizont.txt");
    $("#dinamic").slideDown("slow");
  });
  
  $("#at_fire").click(function(){
    restore_underline();
    $(this).css( {textDecoration: 'none' } );
		$(".accordion p").slideUp("slow");
    $("#dinamic").slideUp("slow");
    $("#dinamic").slideDown("slow");
    $("#dinamic_p").load("text/koster.txt");
  });
  
});

function my_open_window(name, url, left, top, width, height, toolbar, menubar, statusbar, scrollbar, resizable)
{
  toolbar_str = toolbar ? 'yes' : 'no';
  menubar_str = menubar ? 'yes' : 'no';
  statusbar_str = statusbar ? 'yes' : 'no';
  scrollbar_str = scrollbar ? 'yes' : 'no';
  resizable_str = resizable ? 'yes' : 'no';

  cookie_str = document.cookie;
  cookie_str.toString();

  pos_start  = cookie_str.indexOf(name);
  pos_end    = cookie_str.indexOf('=', pos_start);

  cookie_name = cookie_str.substring(pos_start, pos_end);

  pos_start  = cookie_str.indexOf(name);
  pos_start  = cookie_str.indexOf('=', pos_start);
  pos_end    = cookie_str.indexOf(';', pos_start);
  
  if (pos_end <= 0) pos_end = cookie_str.length;
  cookie_val = cookie_str.substring(pos_start + 1, pos_end);
  if (cookie_name == name && cookie_val  == "done")
    return;

  window.open(url, name, 'left='+left+',top='+top+',width='+width+',height='+height+',toolbar='+toolbar_str+',menubar='+menubar_str+',status='+statusbar_str+',scrollbars='+scrollbar_str+',resizable='+resizable_str);
  }
