$(function() {

	$('div.map_container').checkArea();
	popup = $('a.popup-frame').popupFrame();

	$('a.footer-menu_link').blink();

    $(".map_img").zoomMap();

//	if (true){
//			!$.cookie('metistr_banner')) {
//		$.cookie("metistr_banner", '1');
		$("#banner").set_position();
		$('#banner_close').click(function(){
			$("#banner").hide();
//		})
//	} else {
//		$('#banner').hide();
	});

	var bd= $("body");
    $(".button_top").click(function(){
        $(".buttons").toggle();
    });

    $(".buttons").click(function(){
          bd
            .removeClass("image-1 image-2 image-3")
            .addClass("image-"+this.id.split('but')[1]);
   			setInterval(function(){},200);
        window.print();
    });

    $("dl.g_photo dt").click(function(){
        $("img.g_title").css({display: "none"});
        $("dl.g_photo dd").removeClass("selected");
        k = this.id.split('g_item')[1];
        $("#g"+k).addClass("selected");
        $("dl.g_photo dt p").css({display: "none", cursor: "auto"});
        $("dl.g_photo dt div").css({border:"none"});
        $("span#g_caption").css({display: "block"});
        $("span.g_items").css({display: "none"});
        $("span#s"+k).css({display: "block"});

    });

          $(".g_photo a").fancybox({'titlePosition':'inside', 'overlayColor':'#000', 'overlayOpacity': '0.6'});

});



$.fn.checkArea = function() {
	var $t = this;
	if (!$t.length) return;
	var t = $t[0],
		width, offsetLeft, offsetTop, k;

	$(window).resize(function(){
		width = $t.width(),
		offsetLeft = $t.offset().left,
		offsetTop = $t.offset().top,
		k = 1100/width;
	}).resize();

	function setClass(string) {
		t.className = string;
	}


    var toView = $(".toView0");
    var  n  = 0;
        id = setInterval(function(){
              if (n == 0){
                n++;
                  toView.css({visibility: "hidden"});
             }else{
                 n--;
                  toView.css({visibility: "visible"});
              }
       } ,1000);


	return $t.mousemove(function(e){
      	var eX = ~~((e.pageX - offsetLeft)*k),
			eY = ~~((e.pageY - offsetTop)*k);

		if ((eX > 164 && eX < 374) && (eY > 331 && eY < 542)){
			setClass('map_container show-1');
             $(".div_toView").css({display: "block"});
        }
		else if ((eX > 415 && eX < 720) && (eY > 331 && eY < 542)){
			setClass('map_container show-2');
         $(".div_toView").css({display: "block"});
        }
		else if ((eX > 630 && eX < 956) && (eY > 543 && eY < 675)){
			setClass('map_container show-3');
         $(".div_toView").css({display: "block"});
        }
		else if ((eX > 157 && eX < 627) && (eY > 545 && eY < 743)){
			setClass('map_container show-4');
         $(".div_toView").css({display: "block"});
        }
		else if ((eX > 161 && eX < 767) && (eY > 125 && eY < 329)){
			setClass('map_container show-5');
         $(".div_toView").css({display: "block"});
        }
		else if ((eX > 173 && eX < 944) && (eY > 19 && eY < 120)){
			setClass('map_container show-6');
         $(".div_toView").css({display: "block"});
        }
		else if ((eX > 24 && eX < 155) && (eY > 124 && eY < 542)){
			setClass('map_container show-7');
         $(".div_toView").css({display: "block"});
        }
		else if ((eX > 724 && eX < 954) && (eY > 430 && eY < 542)){
			setClass('map_container show-8');
         $(".div_toView").css({display: "block"});
        }
		else if ((eX > 769 && eX < 959) && (eY > 124 && eY < 328)){
			setClass('map_container show-9');
         $(".div_toView").css({display: "block"});
        }
		else if ((eX > 972 && eX < 1093) && (eY > 6 && eY < 115)){
             $(".div_toView").css({display: "block"});
			setClass('map_container show-10')}
		else{
			setClass('map_container');
         $(".div_toView").css({display: "none"});
        }
	});
}

$.fn.popupFrame = function () {
	var t = this;
	if (!t.length) return;

	var windowWidth = $(window).width();
//
//	var iframeWidth = windowWidth/2;
//	alert(iframeWidth)
//	var iframeHeight = (iframeWidth*3)/4;

	var iframeWidth = 800;
	var iframeHeight = 600;





	var template = $('<div class="popup_frame"><div class="popup_shadow popup_close-trigger"></div><div class="popup_body"><div class="popup_top"><div class="popup-bottom"></div></div><span class="popup_close-button popup_close-trigger">Закрыть (ESC)</span><div class="loader-content"><iframe frameborder="no" scrolling="no" width="'+iframeWidth+'" height="'+iframeHeight+'" src="pans/pan1.html" frameborder="0"></iframe></div></div></div>')
	var popupFrame = template.appendTo('body');
	var iframe = template.find('iframe');
	var closeTrigger = template.find('.popup_close-trigger');
	var popupBody = template.find('div.popup_body');

//	popupBody.css({
//		width: iframeWidth,
//		height: iframeHeight,
//		marginTop: -iframeHeight/2,
//		marginLeft: -iframeWidth/2
//	})


	template.height($(document).height())
	function show(url) {
		iframe[0].src = url;
		popupFrame.show()
	}

	function close() {
		popupFrame.hide()
	}

	t.click(function() {
		show(this.href);
		return false;
	});

	closeTrigger.click(close);

	$(document).keyup(function(e) {
		if (e.which === 27) close()
	});

	return {
		show:show,
		close:close
	}
}

$.fn.blink = function() {
	var t = this;
	if (!t.length) return;

	return t.each(function(){
		var t = $(this),
			blinker = t.children('i'),
			interval;

		function blink() {
			blinker.css({left:-50}).animate({left:200}, 500)
		}


		t.mouseenter(function() {
			interval = clearInterval(interval);
			blink();
			interval = setInterval(blink, 2000)
		})
		.mouseleave(function(){
			interval = clearInterval(interval);

		})

	})
}


// Движение по карте

var k,radius;
 $.fn.zoomMap = function() {
    var t = this;
	if (!t.length) return;

  radius = $('.radius');
    var n = 1;
        setInterval(function(){
            radius[0].className = 'point cadr'+n;
                n++;
            if (n == 5) n =1;
        }, 200);
	var $t = this;
	var
//            t = $t[0],
		width, oldK;
	$(window).resize(function(){
		width = $t.width();
//
		k = width/1875;
	}).resize();


     movie();
}




function movie(){
var  one_point = $(".one_point");
     one_point.show();

	function putPoint(t){
		var left = t.left*k -23;
		var top = t.top*k-23;
		one_point.css({top: top, left: left});
	}

    // первая траектория
    radius.show();
    $({top:2836, left:300})
		.animate(
			{top:2020, left:850},

			{   duration: 3500,
				easing: "linear",
				step:function(a,b){
					putPoint(this);
				},
				complete:function() {
					 one_point.css({display: "block"})
				}
			 })

       .animate(
            {left: 1350, top: 1780},

            { duration: 2500,
               easing: "linear",
               step:function(a,b){
                        putPoint(this);
                    }
            })

       .animate(
            { left: "646", top: "1436"},

            {   duration: 2500,
                easing: "linear",
                step:function(a,b){
                       putPoint(this);
                    }
            })
       .animate(
            {left: 1118, top: 684},

            {   duration: 3200,
                easing: "linear",
                step:function(a,b){
                        putPoint(this);
                    }
    })
       .animate(
            {left: 1270, top: 704},
            {   duration: 800,
                easing: "linear",
                step:function(a,b){
                       putPoint(this);
                    },
                    complete:function() {
                        one_point.css({display: "none"});
                        $(".road").css("z-index", 0);
                }
       })
//
//вторая траектория
      .animate(
            {
                left: "1856", top: "536"},
            {
                duration: 100,
                easing: "linear",
                step:function(a,b){
                        putPoint(this);
                    },
                 complete:function() {
                        one_point.css({display: "block"});
                    }
            })
       .animate(
                {
                    left: "1020", top: "1776"},

                {
                    duration: 5200,
                    easing:"linear",
                    step:function(a,b){
                       putPoint(this);
                    }
                })
       .animate(
                {
                    left: "852", top: "1732"},

                {
                    duration: 800,
                    easing:"linear",
                    step:function(a,b){
                       putPoint(this);
                    }
                })
       .animate(
                {
                    left: "952", top: "1592"},

                {
                    duration: 900,
                    easing:"linear",
                    step:function(a,b){
                       putPoint(this);
                    }
                })
       .animate(
                {
                    left: "646", top: "1436"},

                {
                    duration: 1500,
                    easing:"linear",
                    step:function(a,b){
                       putPoint(this);
                    }
                })
       .animate(
                {
                    left: "1132", top: "672"},

                {
                    duration: 3200,
                    easing:"linear",
                    step:function(a,b){
                        putPoint(this);
                    }
                })
       .animate(
            {
                left: "1264", top: "748"},
            {
                duration: 800,
                easing: "linear",
                step:function(a,b){
                       putPoint(this);
                    },
            complete:function(){
            one_point.css({display: "none"});
        }})



    //третья траектория
            .animate(
                          {
                              left: 1024,top: 0},
//
                          {   duration: 100,
                              easing: "linear",
                               step:function(a,b){
                                 putPoint(this);
                              },

                               complete:function() {
                                   one_point.css({display: "block"});
                                    $(".road").css("z-index", 0);
                              }
                           })



                    .animate(
                          {
                              left: "740", top: "462"},

                          {
                                  duration: 2500,
                                  easing: "linear",
                                  step:function(a,b){
                                  putPoint(this);
                              }
                          })

                    .animate(
                          {
                              left: "1268", top: "454"},
                          {
                              duration: 2300,
                              easing: "linear",
                              step:function(a,b){
                                  putPoint(this);
                              }
                          })
                    .animate(
                          {
                              left: "1132", top: "672"},

                          {
                              duration: 1000,
                              easing:"linear",
                              step:function(a,b){
                                  putPoint(this);
                              }
                          })
                    .animate(
                          {
                            left: "1260", top: "748"},
                          {
                              duration: 800,
                              easing: "linear",
                              step:function(a,b){
                                 putPoint(this);
                              },
                              complete:function() {
                                   one_point.css({display: "none"});
                                   $(".road").css("z-index", 3);
                                   movie();
                              }

                          })

}
//Баннер
 $.fn.set_position = function(){
    var $t = this;
	if (!$t.length) return;

    var obj = new Array();
    var n = Number(2);
     for(i = 0; i < 14; i++){
        obj[i] = $("div#sc"+i);
     }

     var  left0, left1;
     $(window).resize(function(){
		width = $t.width();
	k = width/1100;
	}).resize();


i = 0;

	left0 = 0;
	var left00 = left0;
	 left1 = $t.width();
	var left11 = left1;
	var height = $t.height() - 179;
	var top = Math.random() * height;

	function check_top(){
		t = Math.random()*height;
		if (((t - 60) < top) && (top < (t + 60))||(t > height) ){
			check_top();
		}else{
			top = t;
		}
	}

	 ID = setTimeout(function(){
		check_top();
		startObj(i, top);
		i++;
	 if(i<14) setTimeout(arguments.callee, 7500);
	 else return;
	



	}, 400);


	function startObj(i,top){
		if ((i == 0)||(i == 1)){
			obj[i]
				.css({marginTop: "0 auto", width: "100%", top: "50%", textAlign: "center",display: "block", opacity:"0.0"})
				.animate(
				{	opacity: 0.9
				},
				{
					duration: 7000,
					easing: "linear",
					complete:function() {
						obj[i].css({display: "none"});
					}
				});
		}else{
			var width=obj[i].width();
			if ( i%2){
				left00 = left1;
				left11 = left0-width;
			} else{
				left00 = left0-width;
				left11 = left1;
			}

			obj[i]
				.css({left: left00,top: top, display: "block"})
				.animate(
				{
					left: left11
				},
				{
					duration: 13500,
					easing: "linear",
					complete:function() {
						obj[i]
							.css({display: "none"});
						if (i==13){
	//						$("#banner").css({display: "none"});
							i = 0;
							$("#banner").set_position();
						}
				}
			});
		}
	 }
}


