jQuery(
  function( $ )
  {      
    //MENU***************************************************
    $("#menu SPAN").each(function(i){
      flyId = i + 1;               
      itemId = flyId;
      
      if (flyId>5 && flyId>0) {    
        while (flyId>5) {
          flyId = flyId - 5;
        }
      }  

      switch (flyId) {
        case 1:
            flyHtml = $('<object width="56" height="59" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"><param name="movie" value="/flash/fly1.swf"><param name="wmode" value="transparent"><embed src="/flash/fly1.swf" width="56" height="59" wmode=transparent type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>');
        break;
        case 2:
            flyHtml = $('<object width="66" height="69" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"><param name="movie" value="/flash/fly2.swf"><param name="wmode" value="transparent"><embed src="/flash/fly2.swf" width="66" height="69" wmode=transparent type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>');
        break;
        case 3:
            flyHtml = $('<object width="77" height="74" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"><param name="movie" value="/flash/fly3.swf"><param name="wmode" value="transparent"><embed src="/flash/fly3.swf" width="77" height="74" wmode=transparent type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>');
        break;
        case 4:
            flyHtml = $('<object width="44" height="47" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"><param name="movie" value="/flash/fly4.swf"><param name="wmode" value="transparent"><embed src="/flash/fly4.swf" width="44" height="47" wmode=transparent type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>');
        break;
        case 5:
            flyHtml = $('<object width="52" height="49" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"><param name="movie" value="/flash/fly5.swf"><param name="wmode" value="transparent"><embed src="/flash/fly5.swf" width="52" height="49" wmode=transparent type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>');
        break;
      }

      if (itemId%2!=0) {
        $(this).css("text-align", "right");        
        $(flyHtml).css("margin-left", ($(this).width() + 56)*-1 + "px");
      } else {
        $(this).css("margin-left", "20px");
        $(flyHtml).css("margin-left", "-5px");
      }
      
      flyHtml.appendTo("#menuItem" + itemId);                                    
    });    
    
    $("#menu SPAN").dropShadow({
      left:0,
      top:0,
      blur:2,
      opacity:0.2
    });
	
	jQuery.each(jQuery.browser, function(i, val) {
      
        if((i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9") || (i=="opera" && jQuery.browser.version.substr(0,1)=="9") || (i=="safari" && jQuery.browser.version.substr(0,1)=="5")){
		 $("#menu SPAN").next().css("left", "250px");
		 $("#menu SPAN").next().css("margin-top", "40px");

        }
        
    });



    $("#menu SPAN").mouseover(function() {
      $(this).css("text-decoration","underline");
      $(this).css("background","#f6d1c4");
    });    
    
    $("#menu SPAN").mouseout(function() {  
      $(this).css("text-decoration","none");
      $(this).css("background","white");
    });
    //MENU***************************************************

    $("#bg").css({"background":"white url('/images/bg" + randomNumber(1,5) + ".jpg') center top no-repeat"});
    
    $("*").ready(function(){
      $(document).pngFix();
      $(".mainpage #bg").fadeIn(1000, function(){
         $(".mainpage .content *").fadeIn(2000);
         $(".mainpage #body").animate({
              height: 'toggle', opacity: 'toggle'
         }, 1200);
         $(".mainpage #footer").fadeIn(1000);
      });
    });
  }
);


function randomNumber (m,n)
{
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}


var timerID = null;
var timerShowID = null;

function stopTimer() {
	if (timerID != null) {
		clearTimeout(timerID);
	}
	if (timerShowID != null) {
		clearTimeout(timerShowID);
	}
}

function openMenu(id) {
	if (timerID != null) {
		clearTimeout(timerID);
	}
	if (timerShowID != null) {
		clearTimeout(timerShowID);
	}
	closeMenu(id);
    document.getElementById(id).style.visibility="visible";
    document.getElementById(id).style.display="block";
}

function closeMenu(id){
	if (timerID!=null){
		clearTimeout(timerID);
	}
	document.getElementById(id).style.visibility="hidden";
    document.getElementById(id).style.display="none";
}

function timeoutClose(id, timer) {
	if (timerID != null) {
		clearTimeout(timerID);
	}
    timerID = setTimeout("closeMenu("+id+")", timer);
}

function openwin(myurl, mx, my, name) {
	if (name == null) {
		name = "popup";
	}
	nn4 = (document.layers) ? true : false;
	ie4 = (document.all) ? true : false;
	wx = Math.round((screen.width - mx) / 2);
	wy = Math.round((screen.height - my) / 2);
	wpos = (nn4) ? "screenX=" + wx + ",screenY=" + wy : "left=" + wx + ",top=" + wy;
	var a = window.open(myurl, name, "resizable=yes, menubar=no,toolbars=no,scrollbars=yes,width=" + mx + ",height=" + my + "," + wpos);
	a.focus();
}

function openimg(myurl, mx, my, name) {
	if (a != null) {
		a.close();
	}
	mx = mx + 30;
	my = my + 57;
	if (name == null) {
		name = "Image";
	}
	nn4 = (document.layers) ? true : false;
	ie4 = (document.all) ? true : false;
	wx = Math.round((screen.width - mx) / 2);
	wy = Math.round((screen.height - my) / 2);
	wpos = (nn4) ? "screenX=" + wx + ",screenY=" + wy : "left=" + wx + ",top=" + wy;
	var a = window.open(myurl, name, "resizable=no, menubar=no,toolbars=no,scrollbars=no,width=" + mx + ",height=" + my + "," + wpos);
	a.resizeTo(mx, my);
	a.focus();
}


function showHide(subitm, itm) {
	var active = document.getElementById("a" + itm.id);
	if (subitm.style.display == "none") {
		subitm.style.display = "block";
	} else {
		subitm.style.display = "none";
	}
}

/*
* Открывает всплывающее окно точно по размеру картинки
*/
function openPreview (url, width, height) {
    var x = window.open("", 'preview', 'scrollbars=no,width='+width+',height='+height+',toolbar=no,status=no,menubar=no,links=no,resizable=no');
    x.document.write('<html><head><title>Просмотр изображения</title></head>'+
                       '<body topmargin="0" leftmargin="0"><img src="'+url+'"' +
                       '></body></html>');
    x.document.close();
    x.focus();
}

function fixPNG(element)
{
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{
		var src;

		if (element.tagName=='IMG')
		{
			if (/\.png$/.test(element.src))
			{
				src = element.src;
				element.src = "/img/empty.gif";
			}
		}
		else
		{
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			if (src)
			{
				src = src[1];
				element.runtimeStyle.backgroundImage="none";
			}
		}

		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
}