var flag=new Array();
var top_img=1;
var w=screen.width;
var h=screen.height;

var bottom_direct=-1;

function bottomMove() {
    //var left=$("#bottom_ban_inner").offset().left;
    var left=$("#bottom_ban_inner").css("left");
    if(left=='auto') {
	left=$("#bottom_ban_inner").offset().left;
	left=left*1+bottom_direct*1;
    } else {
	left=left.substr(0, left.length-2)*1+bottom_direct*1;
    }
    
    $("#bottom_ban_inner").css("left", left);
    if(left<-75) {
	bottom_direct=1;
    }
    if(left>1) {
	bottom_direct=-1;
    }
    setTimeout("bottomMove()", 50);
}

function hideMenu() {
    for(i in flag) {
        if(!flag[i]) {
            $("#"+i).animate({opacity: 0});
            $("#"+i).css({display: "none"});
        }
    }
}

function hideMenuAll() {
    $(".under_menu").animate({opacity: 0}, 100);
    $(".under_menu").css({
        "display": "none"
    });
    for(i in flag) {
        flag[i]=0;
    }
}

function changeTime() {
    var CurrentDate = new Date();
    
    var time=CurrentDate.getHours()+':'+((CurrentDate.getMinutes()<10)?'0'+CurrentDate.getMinutes():CurrentDate.getMinutes())+':'+((CurrentDate.getSeconds()<10)?'0'+CurrentDate.getSeconds():CurrentDate.getSeconds());
    $("#time1").html(time);
    
    var hour2=CurrentDate.getHours()-1;
    if(hour2<0) hour2+=24;
    var time=hour2+':'+((CurrentDate.getMinutes()<10)?'0'+CurrentDate.getMinutes():CurrentDate.getMinutes())+':'+((CurrentDate.getSeconds()<10)?'0'+CurrentDate.getSeconds():CurrentDate.getSeconds());
    $("#time2").html(time);
    
    var hour2=CurrentDate.getHours()-9;
    if(hour2<0) hour2+=24;
    var time=hour2+':'+((CurrentDate.getMinutes()<10)?'0'+CurrentDate.getMinutes():CurrentDate.getMinutes())+':'+((CurrentDate.getSeconds()<10)?'0'+CurrentDate.getSeconds():CurrentDate.getSeconds());
    $("#time3").html(time);
    
    $("#time4").html(time);
    
    var hour2=CurrentDate.getHours()-4;
    if(hour2<0) hour2+=24;
    var time=hour2+':'+((CurrentDate.getMinutes()<10)?'0'+CurrentDate.getMinutes():CurrentDate.getMinutes())+':'+((CurrentDate.getSeconds()<10)?'0'+CurrentDate.getSeconds():CurrentDate.getSeconds());
    $("#time5").html(time);
    
    var hour2=CurrentDate.getHours()+4;
    if(hour2>24) hour2-=24;
    var time=hour2+':'+((CurrentDate.getMinutes()<10)?'0'+CurrentDate.getMinutes():CurrentDate.getMinutes())+':'+((CurrentDate.getSeconds()<10)?'0'+CurrentDate.getSeconds():CurrentDate.getSeconds());
    $("#time6").html(time);
    
    var hour2=CurrentDate.getHours()+3;
    if(hour2>24) hour2-=24;
    var time=hour2+':'+((CurrentDate.getMinutes()<10)?'0'+CurrentDate.getMinutes():CurrentDate.getMinutes())+':'+((CurrentDate.getSeconds()<10)?'0'+CurrentDate.getSeconds():CurrentDate.getSeconds());
    $("#time7").html(time);
    
    setTimeout("changeTime()", 1000);
}

function changeTopImg() {
    if(top_img==3) top_img=0;
    
    $("#header_img_"+top_img).fadeIn(2000);
    var hidded_img=(top_img==0)?2:top_img-1;
    $("#header_img_"+hidded_img).fadeOut(1000);
    top_img++;
    setTimeout("changeTopImg()", 3000);
}

function movePopUp() {
    var st=document.documentElement.scrollTop;
    var sl=document.documentElement.scrollLeft;
    var login_form=$('#login_form');
    login_form.css("top", (w-login_form.height())/2+st-250);
    login_form.css("left", (h-login_form.width())/2+sl);
}

function hideMsgBox() {
    $("#msg_box").slideUp();
}

$(document).ready(function() {
    setTimeout("changeTopImg()", 3000);
    setTimeout("changeTime()", 1000);
    
    /*$("a.gal").fancybox({
        'overlayShow': true 
    });*/
    
    $(".news_img").fancybox({
        'overlayShow': true 
    });
    
    $(".top_menu a").mouseover(function() {
        if($(this).parent().attr("class")!='under_menu') {
            var id=$(this).attr('class');
            if(flag[id]!=1) {
                hideMenuAll();
                
                flag[id]=1;
                
                var top=$(this).offset().top;
                var left=$(this).offset().left;
                
                $("#"+id).css({
                    "display": "block",
                    "z-index": "2000",
                    "left": $(".top_menu").offset().left+"px"
                });
                $("#"+id).animate({
                    opacity: 0.85
                });
            }
        }
    });
    $(".top_menu a").mouseout(function() {
        var id=$(this).attr('class');
        flag[id]=0;
        
        setTimeout(hideMenu, 1000);
    });
    
    $(".under_menu a").mouseover(function() {
        var id=$(this).parent().attr('id');
        flag[id]=1;
    });
    $(".under_menu a").mouseout(function() {
        var id=$(this).parent().attr('id');
        flag[id]=0;
        setTimeout(hideMenu, 1000);
    });
    
    $(".under_menu").mouseover(function() {
        var id=$(this).attr('id');
        flag[id]=1;
    });
    $(".under_menu").mouseout(function() {
        var id=$(this).attr('id');
        flag[id]=0;
        setTimeout(hideMenu, 1000);
    });
    
    $("#login_form_show").click(function() {
        var st=document.documentElement.scrollTop;
        var sl=document.documentElement.scrollLeft;
	
	$("#blockui").show();
	$("#blockui").css({
	    width: w,
	    height: h,
	    top: 0,
	    left: 0
	});
	
        var login_form=$('#login_form');
        login_form.css({
	    top: ((w-login_form.height())/2+st-250),
	    left: ((h-login_form.width())/2+sl)
	});
        login_form.show();
	
        return false;
    });
    
    $("#login_form_show2").click(function() {
        var st=document.documentElement.scrollTop;
        var sl=document.documentElement.scrollLeft;
	
	$("#blockui").show();
	$("#blockui").css({
	    width: w,
	    height: h,
	    top: 0,
	    left: 0
	});
	
        var login_form=$('#login_form');
        login_form.css({
	    top: ((w-login_form.height())/2+st-250),
	    left: ((h-login_form.width())/2+sl)
	});
        login_form.show();
	
        return false;
    });
    
    $("#blockui").click(function() {
	$("#login_form").hide();
	$(this).hide();
	return false;
    });
    
    $("#login_form input.lp").focus(function() {
	$(this).css("backgroundColor", "#ffff00");
    });
    
    $("#login_form input.lp").blur(function() {
	$(this).css("backgroundColor", "#fff");
    });
    
    if($("#msg_box").html()!='') {
	$("#msg_box").fadeIn(1500);
        setInterval('hideMsgBox()', 5000);
    }
    
    $("#ipay_buy_news").click(function() {
        var id=$(this).attr('class');
        JsHttpRequest.query(
            '/ajax.php',
            {
                "id": id
            },
            function(result, errors) {
                $("#debug").html(errors);
                if (result) {
                    $("#ipay_form").html(result["form"]);
                    $("#ipay_frm").submit();
                }
            },
            true
        );
        return false;
    });
    
    
    if($("#graph1").text()=='Loading graph...') {
	
	var myChart = new JSChart('graph1', 'line');
	myChart.setDataArray(myData);
	myChart.setTitle('');
	myChart.setTitleColor('#f00');
	myChart.setTitleFontSize(8);
	myChart.setAxisNameX('');
	myChart.setAxisNameY('');
	myChart.setAxisColor('#000');
	myChart.setAxisValuesColor('#000');
	myChart.setAxisValuesFontSize(6);
	myChart.setAxisPaddingLeft(50);
	myChart.setAxisPaddingRight(30);
	myChart.setAxisPaddingTop(24);
	myChart.setAxisPaddingBottom(45);
	myChart.setGraphExtend(true);
	myChart.setGridColor('#dddddd');
	myChart.setLineWidth(1);
	myChart.setLineColor('#ff8330');
	myChart.setSize(260, 160);
	myChart.setShowXValues(false);
	myChart.setBackgroundImage('/img/graph.png');
	for(i in myTooltips) {
	    myChart.setTooltip(myTooltips[i]);
	}	
	myChart.draw();
	
	//$("#graph1_title").text("Мука пшеничная");
	$("#graph1").css("top", "-20px");
	$("#graph1_title").css({
	    "z-index": "100",
	    "top": "+5px"
	});
    }
    
    if($("#graph2").text()=='Loading graph...') {
	var myChart2 = new JSChart('graph2', 'line');
	myChart2.setDataArray(myData);
	myChart2.setTitle('');
	myChart2.setTitleColor('#f00');
	myChart2.setTitleFontSize(8);
	myChart2.setAxisNameX('');
	myChart2.setAxisNameY('');
	myChart2.setAxisColor('#000');
	myChart2.setAxisValuesColor('#000');
	myChart2.setAxisValuesFontSize(6);
	myChart2.setAxisPaddingLeft(50);
	myChart2.setAxisPaddingRight(30);
	myChart2.setAxisPaddingTop(24);
	myChart2.setAxisPaddingBottom(45);
	myChart2.setGraphExtend(true);
	myChart2.setGridColor('#dddddd');
	myChart2.setLineWidth(1);
	myChart2.setLineColor('#ff8330');
	myChart2.setSize(260, 160);
	myChart2.setBackgroundImage('/img/graph.png');
	myChart2.draw();
	
	$("#graph2_title").text("Мука пшеничная");
	$("#graph2").css("top", "-20px");
	$("#graph2_title").css({
	    "z-index": "100",
	    "top": "+5px"
	});
    }
    
    if($("#graph_delta").text()=='Loading graph...') {
	var myChart = new JSChart('graph_delta', 'line');
	myChart.setDataArray(myData);
	myChart.setTitle('');
	myChart.setTitleColor('#f00');
	myChart.setTitleFontSize(8);
	myChart.setAxisNameX('');
	myChart.setAxisNameY('');
	myChart.setAxisColor('#000');
	myChart.setAxisValuesColor('#000');
	myChart.setAxisValuesFontSize(6);
	myChart.setAxisPaddingLeft(84);
	myChart.setAxisPaddingRight(15);
	myChart.setAxisPaddingTop(19);
	myChart.setAxisPaddingBottom(18);
	myChart.setGraphExtend(true);
	myChart.setGridColor('#dddddd');
	myChart.setLineWidth(1);
	myChart.setLineColor('#ff8330');
	myChart.setSize(600, 350);
	myChart.setShowXValues(false);
	myChart.setBackgroundImage('/img/graph2.png');
	for(i in myTooltips) {
	    if(myTooltips[i]) {
		myChart.setTooltip(myTooltips[i]);
	    }
	}	
	
	myChart.draw();
    }
    
    $(".tnwed_link").click(function() {
	var id=this.id;
	var src=$(".img"+id).attr("src");
	if(src=='/img/plus.png') {
	    $("."+id).show();
	    $(".img"+id).attr("src", "/img/minus.png");
	} else {
	    $("."+id).hide();
	    $(".img"+id).attr("src", "/img/plus.png");
	}
	
		
	return false;
    });
    
    $(".listing2").dataTable({
	"oLanguage": {
	    "sLengthMenu": "Отображать _MENU_ записей на страницу",
	    "sZeroRecords": "К сожалению, ничего не найдено",
	    "sInfo": "Показаны записи от _START_ до _END_ из _TOTAL_",
	    "sInfoEmtpy": "Показаны записи от 0 до 0 из 0",
	    "sInfoFiltered": "(отобрано из _MAX_ записей0)"
	}
    });
    
    $("#calendar_year").change(function() {
	top.location.href=$(this).val();
    });
    
    $(".video_news_selectbox").change(function() {
	if($(this).val()!='') {
	    var v=$(this).val();
	    $(".video_news_selectbox").val('');
	    $(this).val(v);
	    $("#video_news_form").submit();
	}
    });
});