var wayTo = "up";
var intervalID = null;

window.onload = function() {
    intervalID = setInterval('change_headbann_rand()', 5000);
    
    if( $.browser.msie )  {
        //$('#top_gallery_1 li').corner("tl 20px").corner("tr 20px").corner("bl 20px");
        //$('#top_gallery_2 li').corner("tl 20px").corner("tr 20px").corner("br 20px");
        //$('#top_gallery_3 li').corner("bl 20px").corner("br 20px");
        //$('#top_gallery_4 li').corner("bl 20px").corner("br 20px").corner("tr 20px");
        $('#head_menu_active a').corner("15px");
    }
    
	if (document.getElementById('head_zakaz')){
		$(document).ready(function(){
					$('.zakaz_inp_1').focus(function(){
					if (this.value==this.value) {var val=this.value; this.value='';}
						$('.zakaz_inp_1').blur(function(){
					if (this.value=='') {this.value=val; val='';}
				});
				});
				
			});		
		$(".head_zakaz_1").css("display","block"); 
		var sheight=document.getElementById('head_zakaz').scrollHeight;
		var zak_height=-(sheight-36)+'px';  // (sheight-36)
		document.getElementById('head_zakaz').style.top=zak_height;
					$(document).ready(function(){
                    //$(".head_zakaz_1").css("display", "block");
		        $('.head_zakaz_2').click(function(e){
                    var par = $(this).parent();
                    
                    if ( par.hasClass('opened') )
                        par.removeClass('opened').stop().animate({top:zak_height},{queue:false,duration:400});		                
                    else
                        par.addClass('opened').stop().animate({top:"0px"},{queue:false,duration:400});                        
                    
                    e.stopPropagation();
                    return false;
		        });
                
                $('.head_zakaz_2 input').unbind('click');                
            
			});
            				
		 }
        
	if (document.getElementById('top_gallery_1')) {
        /*		
	     $(document).ready(function(){
	        $(".top_gallery img").css({"display" : "none"});
	     });	
         
	 	  var dotex=$(".top_gallery img").length;
		  for (dot=0; dot < dotex; dot++){
		    $(".top_gallery li")[dot].style.background='url('+$(".top_gallery img")[dot].src+')';
		  }
         */
	      setTimeout('change_img_rand()',4000);              
		}      

}
 
function change_headbann_rand(){
    var container = $('#head_54');
    
    var cur_pos = parseInt(container.attr('rel'));
    var last_pos= $('.banner', container).length;    

    if (wayTo == "up"){
        if (cur_pos < last_pos){
            news_up();    
        } else {
            wayTo = "down";
            news_down();
        }
    } else {
        if (cur_pos > 1){
            news_down();              
        } else {
            wayTo = "up";
            news_up();  
        }        
    }
}
 
function news_up() {
    clearInterval(intervalID);
    var container = $('#head_54');
    
    var cur_pos = parseInt(container.attr('rel'));
    var last_pos= $('.banner', container).length;    

    if (cur_pos < last_pos){
        cur_pos = cur_pos+1;
        container.attr('rel', cur_pos);
        $('.banner:first', container).animate({"marginTop": "-=201px"}, "slow");
        
        if (cur_pos >= last_pos){
            $('#head_53').addClass('head_opac_1');
        } else {
            $('#head_53').removeClass('head_opac_1');
        }
        
        if (cur_pos <= 1){
            $('#head_52').addClass('head_opac_1');
        }else {
            $('#head_52').removeClass('head_opac_1');    
        }        
    } 
    
    intervalID = setInterval('change_headbann_rand()',5000);    
    return true;
}

function news_down() {
    clearInterval(intervalID);
    var container = $('#head_54');
    
    var cur_pos = parseInt(container.attr('rel'));
    var last_pos= $('.banner', container).length;    

    if (cur_pos > 1){
        cur_pos = cur_pos - 1;
        container.attr('rel', cur_pos);
        $('.banner:first', container).animate({"marginTop": "+=201px"}, "slow");
        
        if (cur_pos <= 1){
            $('#head_52').addClass('head_opac_1');
        }else {
            $('#head_52').removeClass('head_opac_1');    
        }        
        
        if (cur_pos >= last_pos){
            $('#head_53').addClass('head_opac_1');
        } else {
            $('#head_53').removeClass('head_opac_1');
        }        
    } 
    
    intervalID = setInterval('change_headbann_rand()',5000);
    return true;   
}

function change_img_rand(){
	var doc=document.getElementById('galleries');
	var doc1=doc.className.replace(/gal_pos_/, "");
	doc1=doc1-0;
    if (doc1=='0'){doc.className='gal_pos_1'; change_img(1,4,3,2); setTimeout('change_img_rand()',13000);}
    else if (doc1=='1'){doc.className='gal_pos_2'; change_img(3,1,4,2); setTimeout('change_img_rand()',16000);}
    else if (doc1=='2'){doc.className='gal_pos_3'; change_img(4,1,3,2); setTimeout('change_img_rand()',11000);}
    else if (doc1=='3'){doc.className='gal_pos_4'; change_img(1,3,2,4); setTimeout('change_img_rand()',8000);}
    else if (doc1=='4'){doc.className='gal_pos_5'; change_img(3,2,1,4); setTimeout('change_img_rand()',13000);}
    else if (doc1=='5'){doc.className='gal_pos_6'; change_img(2,3,1,4); setTimeout('change_img_rand()',16000);}
    else if (doc1=='6'){doc.className='gal_pos_7'; change_img(4,1,3,2); setTimeout('change_img_rand()',11000);}
    else if (doc1=='7'){doc.className='gal_pos_0'; change_img(3,2,1,4); setTimeout('change_img_rand()',8000);}
}

function change_img(fst,sec,third, frth) {	
	 $(document).ready(function(){
          $("#top_gallery_"+fst).animate({opacity:"0"}, 500); 
          setTimeout('change_li('+fst+')',500); 
          $("#top_gallery_"+fst).animate({opacity:"1"}, 500);
          
	      setTimeout(function(){$("#top_gallery_"+sec).animate({opacity:"0"}, 500); setTimeout('change_li('+sec+')',500); $("#top_gallery_"+sec).animate({opacity:"1"}, 500)},1000); 
	      setTimeout(function(){$("#top_gallery_"+third).animate({opacity:"0"}, 500); setTimeout('change_li('+third+')',500); $("#top_gallery_"+third).animate({opacity:"1"}, 500)},2000);
	      setTimeout(function(){$("#top_gallery_"+frth).animate({opacity:"0"}, 500); setTimeout('change_li('+frth+')',500); $("#top_gallery_"+frth).animate({opacity:"1"}, 500)},3000);	
	  });
}

function change_li(fst){
    $(document).ready(function(){
	    var div_height=$(document.getElementById('top_gallery_'+fst).getElementsByTagName('li')[0]).css("height");

        div_height=div_height.replace(/px/, "");
        div_height=div_height-0;
	    var li_lenght=document.getElementById('top_gallery_'+fst).getElementsByTagName('li').length;
	    var top=document.getElementById('top_gallery_'+fst).getElementsByTagName('ul')[0].style.top;
	    var matra=-((li_lenght*div_height)-div_height)+'px';
	    if (matra==top){
		    $(document.getElementById('top_gallery_'+fst).getElementsByTagName('ul')[0]).animate({top:"0px"}, -1000);
	    }
	    else {
            $(document.getElementById('top_gallery_'+fst).getElementsByTagName('ul')[0]).animate({"top": "-="+div_height+"px"}, -1000);
        }
    });
}
