$(document).ready(function(){
 //$(document).pngFix(); 
  var elem = $(window);
  var golos = 0;
  var serviceScroll = 0;

  $("A").mousedown( function(){
    $(this).blur();
  }).focus( function(){
    $(this).blur();
  }).click( function(){
    $(this).blur();
  });

  //$("#block-info").css({ 'height' : elem.height()-125 });
  $("#block-service-move").css( {'margin-left' : '0px' } );

  $("#pobeda-1").css( {backgroundPosition: (( elem.width() - 982 ) / 2 - 20) + "px 0px"} );
  //$("#pobeda-1").animate({backgroundPosition: "(" + (( elem.width() - 982 ) / 2 - 20) + "px 0px)"}, {duration:500});
  $("#pobeda-2").css( {backgroundPosition: (( elem.width() + 982 ) / 2 ) + "px 0px"} );
  //$("#pobeda-2").animate({backgroundPosition: "(" + (( elem.width() + 982 ) / 2 ) + "px 0px)"}, {duration:500});

  $('*[name=content]').click(function () { 
    $('*[name=contentblock]').hide(); 
    var image = $(this).css("background-image");
    $(".gallery-container").html('');
    var flag = image.indexOf('i-min.gif');
    $('*[name=content]').css({ 'background-image' : 'url(/_page/img/i-plus.gif)' }); 
    if( flag == -1 ) {
      $("#contentblock" + $(this).attr("id") ).slideDown("slow"); 
      $(this).css({ 'background-image' : 'url(/_page/img/i-min.gif)' }); 
      if( gallery = $(this).attr('isgallery') ) { refreshGallery( gallery , 0, 0);   }
    }
  });
  $.get("/_page/phpfiles/_voting.php", {name: 1, itog: 1}, function(data){
     $("#gol-1").text(data); 
     golos = parseInt(data);
     $.get("/_page/phpfiles/_voting.php", {name: 3, itog: 1}, function(data){
        $("#gol-2").text(data);
        var golos2 = parseInt(data);
        $("#bg-gol1").css( {backgroundPosition: "0 82px"} );
        $("#bg-gol2").css( {backgroundPosition: "0 82px"} );
        $("#bg-gol1").animate({backgroundPosition:"(0px "+parseInt(82*(golos2/(golos+golos2)))+"px)"}, {duration:3000});
        $("#bg-gol2").animate({backgroundPosition:"(0px "+parseInt(82*(golos/(golos+golos2)))+"px)"}, {duration:3000});
     });
  });

//---
    $("li.roll").mouseover(function(){
      var bgpos = $(this).css( "backgroundPosition" ).split(" ");
      var xpos = parseInt( bgpos[0] ); 
      var ypos = parseInt( bgpos[1] ); 
      $(this).css( {backgroundPosition: xpos + "px 37px"} );
    }).mouseout(function(){
      var bgpos = $(this).css( "backgroundPosition" ).split(" ");
      var xpos = parseInt( bgpos[0] ); 
      var ypos = parseInt( bgpos[1] ); 
      $(this).css( {backgroundPosition: xpos + "px 0px"} );
    });
//---
    $("li.service").mouseover(function(){
      var bgpos = $(this).css( "backgroundPosition" ).split(" ");
      var xpos = parseInt( bgpos[0] ); 
      var ypos = parseInt( bgpos[1] ); 
      $(this).css( {backgroundPosition: xpos + "px 120px"} );
    }).mouseout(function(){
      var bgpos = $(this).css( "backgroundPosition" ).split(" ");
      var xpos = parseInt( bgpos[0] ); 
      var ypos = parseInt( bgpos[1] ); 
      $(this).css( {backgroundPosition: xpos + "px 0px"} );
    });
//---

  $("#left-arrow").click(function(e) {
    var $paneOptions = $('#block-service');
    serviceScroll -= 250; serviceScroll = ( serviceScroll < 0 ) ? 0 : serviceScroll;
    $paneOptions.stop().scrollTo( { top:0, left: serviceScroll } , 800 ); 
    $("#left-arrow").css( {backgroundPosition: "7px 0px"} );
    $("#right-arrow").css( {backgroundPosition: "-37px -120px"} );     
  });

  $("#right-arrow").click(function(e) {
    var $paneOptions = $('#block-service');
    serviceScroll += 250; serviceScroll = ( serviceScroll > 800 ) ? 800 : serviceScroll;
    $paneOptions.stop().scrollTo( { top:0,left : serviceScroll } , 800 ); 
    $("#left-arrow").css( {backgroundPosition: "7px 0px"} );
    $("#right-arrow").css( {backgroundPosition: "-37px -120px"} );     
  });
//---

  $("#service11").mousemove(function(e){
    var $paneOptions = $('#block-service');
    var elem = $(window);
    var xrightborder = elem.width()/2 + 450;
    var xleftborder = elem.width()/2 - 450;
    var x = e.pageX;
    if( x > xrightborder ) {
       $paneOptions.stop().scrollTo( { top:0,left:800} , 800 ); 
       $("#left-arrow").css( {backgroundPosition: "7px 0px"} );
       $("#right-arrow").css( {backgroundPosition: "-37px -120px"} );
    }
    else if( x < xleftborder ) {
       $paneOptions.stop().scrollTo( { top:0,left:0} , 800 );
       $("#left-arrow").css( {backgroundPosition: "7px -120px"} );
       $("#right-arrow").css( {backgroundPosition: "-37px 0px"} );
    }
    else {}
  });
    $('#block-service-move').bind('mousewheel', function(event, delta) {
          var dir = delta < 0 ? 700 : 0;
       if( delta < 0 ) {
         $('#block-service').stop().scrollTo( { top:0,left:800} , 800 );
         $("#left-arrow").css( {backgroundPosition: "7px 0px"} );
         $("#right-arrow").css( {backgroundPosition: "-37px -120px"} );
	} else {
         $('#block-service').stop().scrollTo( { top:0,left:0} , 800 );
         $("#left-arrow").css( {backgroundPosition: "7px -120px"} );
         $("#right-arrow").css( {backgroundPosition: "-37px 0px"} );
	}
       return false;
    });
 //---
    $("div.rate").mouseover(function(){
       $(this).animate({backgroundPosition:"(-82px 0px)"}, {duration:500});
       $("div#news-rate-caption").text($(this).attr("name"));
       var id = $(this).attr("id").replace("rate","");
       var offX = (id*20-20 > 600 ) ? id*20-20-275 : id*20-20;
       $("div#news-rate-caption"). css( { 'padding-left' : offX+"px", 'width':"300px" } );      
    }).mouseout(function(){
       $(this).animate({backgroundPosition:"(0px 0px)"}, {duration:500});
       //$("div#news-rate-caption").text(""));
    }).click(function(){
       window.location.href = $(this).attr("link");
    });
 //---
    $("a.crop-link").mouseover(function(e){
       $("#body").append('<div id="float-text" class="float-text"><div id="shadow"><div>'+$(this).attr("anons")+'</div></div></div>');
       var fWidth = $(this).attr("fw");
	var xshift = 300;
       if( fWidth ) {
		$("#float-text").css({ 'width' : fWidth + 'px' });
		xshift = parseInt(fWidth) + 20;
		$("#shadow").css({ 'width' : (fWidth-10)+'px' });
	}
       $("#shadow").dropShadow({left: 0, top: 0, opacity: 0.5, blur: 1});
       var floatX = ( elem.width() < e.pageX + xshift ) ? e.pageX - xshift : e.pageX+40;
       var floatY = ( elem.height() < e.pageY + $("#shadow").height() ) ? e.pageY - $("#shadow").height()/2 : e.pageY;
       $("#float-text").css( {'position':'absolute','top': floatY,'left':floatX,'z-index':'200' } );
    }).mouseout(function(){
       $(".float-text").remove();
    }).mousemove(function(e){
       var fWidth = $(this).attr("fw");
	var xshift = 320;
       if( fWidth ) {
		$("#float-text").css({ 'width' : fWidth + 'px' });
		xshift = parseInt(fWidth) + 20;
		$("#shadow").css({ 'width' : (fWidth-10)+'px' });
	}
       var floatX = ( elem.width() < e.pageX + xshift ) ? e.pageX - xshift : e.pageX+40;
       var floatY = ( elem.height() < e.pageY + $("#shadow").height() ) ? e.pageY - $("#shadow").height()/2 : e.pageY;
       $("#float-text").css( {'position':'absolute','top': floatY,'left':floatX,'z-index':'200' } );
    })

 //---
    $("img.user-pict").mouseover(function(e) {
       $(this).css('cursor', 'wait'); 
       var id = $(this).attr("id");
    // replace getProfile( $(this).attr("id"), 'mainpage', "userpict" );
    // need wait cursor

       $.get("/_page/phpfiles/_getprofile.php", { id: id, type: 'mainpage' }, function( data ) {
          $("div#float-text").remove();
          $("#center").append('<div id="float-text" class="float-text"><div id="shadow" style="width: 160px; height: 200px;"><div id="userpict"></div></div></div>');
          $("#userpict").html(data);
          var floatX = ( elem.width() < e.pageX + 200 ) ? e.pageX - 140 : e.pageX+20;
          $("#float-text").css( {'position':'absolute','top':e.pageY-40,'left':floatX,'z-index':'200' } );
          $("#shadow").dropShadow({left: 0, top: 0, opacity: 1.0, blur: 2});
          $("#" + id).css('cursor', 'default'); 
       });
    }).mouseout(function(){
       $(".float-text").remove();
    })
 //---
    $("td.graphrate").css({ 'backgroundPosition' : '-500px 50%' });
    $.each( $("td.graphrate"), function(n, val) { 
        $(this).animate({backgroundPosition:"("+( $(this).attr("rate")-500 )+"px 50%)"}, {duration:3000});
    });
 //---
    $("input.file").filestyle({ 
          image: "/images/choose-file.gif",
          imageheight : 22,
          imagewidth : 30,
          width : 200
    });
    $('*[name=mon-t]').click( function(){
       $("#mon-type").val( $(this).val() ) ;
    }); 
 //--

 // selector for stat checkbox 

    
//---------- end document loading --------------------------
});

function viewProfile( id ) {
  $.get("/_page/phpfiles/_setprofile.php", { id: id }, function(data){
    if(data) window.location.href = "/prof/";
  });
}
function getProfile( id, type, area ) {
  $.get("/_page/phpfiles/_getprofile.php", { id: id, type:type }, function(data){
    $("#"+area).html(data);
  });
}
function voting( group,id ) {
  $.get("/_page/phpfiles/_voting.php", {'name': id, 'vot': group}, function(data){
    if( data == "-1" ) { 
	alert("Извините, голование по этому элементу закончилось!"); return false;
    }
    if( data == "0" ) { 
	alert("Мы уже учитывали Ваш голос:)"); return false;
    }
    if( data == "-1" ) { 
	alert("Пожалуйста, авторизуйтесь для голосования!"); return false;
    }
    $("#golcount-"+id).text(data);
    alert("Спасибо за Ваш голос!");
  });
}

function slideSwitch() {
    var $active = $('#slideshow IMG.active');
 
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
 
    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
 
    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );
 
 
    $active.addClass('last-active');
 
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
 
  $(function() {
    setInterval( "slideSwitch()", 5000 );
  });

function addComment( id, table ) {
  $.get("/_page/phpfiles/_addcomment.php", { id: id, table:table, comment: $("#comment"+id).val() }, function(data){
    window.location.reload();
  });
}

function ScrollDyn( id, shift ) {
  $.get("/_page/phpfiles/_session.php", { id: shift, session: 'dyndata'+id }, function(data){
    window.location.reload();
  });
}

function showRating( max, rating ) {
  $.each(rating, function(i, val) {
    var rateHeight = parseInt(val/max*120)+10;
    $("#rate"+(rating.length-i)).animate({ height: rateHeight }, 1500 );
  });
}

function openFloatForm( id ) {
    var xy = getScrollXY();
    var elem = $(window);
    var floatX = ( elem.width() - 350 ) / 2;
    var floatY =  (elem.height() / 2 - 200) + xy[1] ;
    $("#form-"+id).css( {'position':'absolute','top': floatY,'left':floatX,'z-index':'200' } );
    $("#form-shadow").dropShadow({left: 10, top: 10, opacity: 0.2, blur: 0});
    $("#form-"+id).css({'visibility':'visible'});
}


function closeFloatForm( id ) {
     $("#form-" + id ).css({'visibility':'hidden'});
}

function getScrollXY() {
       var scrOfX = 0, scrOfY = 0;
       if( typeof( window.pageYOffset ) == 'number' ) {
               //Netscape compliant
               scrOfY = window.pageYOffset;
               scrOfX = window.pageXOffset;
       } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
               //DOM compliant
               scrOfY = document.body.scrollTop;
               scrOfX = document.body.scrollLeft;
       } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
               //IE6 Strict
               scrOfY = document.documentElement.scrollTop;
               scrOfX = document.documentElement.scrollLeft;
       }
       return [ scrOfX, scrOfY ];
}



function URLDecode(str){
    str2 = "";
    for(k=0;k<str.length;k++){
        str2 += '%' + chr[str.charAt(k)];
    }
    return str2;
}


function GP(x)
{
	if(x)	document.location.href = x;
	else	alert("страница отсутствует!");
}


document.onkeyup = function(e){ 	
return;
	if (e == null) { // ie
		//keycode = event.keyCode;
	} else { // mozilla
		//keycode = e.which;
	}
	if( keycode == 13 && document.getElementById("searchfld").value != "поиск" ) {
		search();
	}
	if( keycode == 13 && document.getElementById("searchfld1") ) {
		search();
	}
};



function ShowHide(idname)
{
 if (document.getElementById(idname).style.display == "none")
 {
  document.getElementById(idname).style.display = "block";
 }
 else
 {
  document.getElementById(idname).style.display = "none";
 }
}


function fixPNG(element, type)
{
  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/serv/1x1.gif";
      }
    }
    else 
    {
      src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
      if (src)
      {
        src = src[1];
        element.runtimeStyle.backgroundImage="none";
      }
    }
	type = (type == 1) ? "crop" : "scale";
  if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='"+type+"')";
  }
}

