$(document).ready(function() {
    
    // анимируем облака
    var CURRCLOUD;
    function cloudOpen($cloud){
        var $cloudContent = $cloud.find(".content"),
            $fullInfo = $cloudContent.find(".full_info"),
            $shortInfo = $cloudContent.find(".short_info");
        $cloudContent.filter(":not(:animated)").animate({width: $fullInfo.width(), height: $fullInfo.height()}, 250);
        $shortInfo.css({"visibility":"hidden", "position":"absolute"});
        setTimeout(function(){
            $fullInfo.css({"display":"none", "visibility":"visible"}).fadeIn(100);
            return false;
        }, 150);
        return false;
    }
    function cloudClose(){
        var $cloudContent = CURRCLOUD.find(".content"),
            $fullInfo = $cloudContent.find(".full_info"),
            $shortInfo = $cloudContent.find(".short_info");
        $cloudContent.filter(":not(:animated)").animate({width: $shortInfo.width(), height: $shortInfo.height()}, 300);
        $fullInfo.fadeOut(100);
        setTimeout(function(){
            $shortInfo.css({"visibility":"visible", "position":"relative"});
            return false;
        }, 200);    
        return false;
    }
    $(".cl-open").click(function(){
        var $cloud = $(this).parent().parent().parent(),
            zIndex = 6;
        if(CURRCLOUD){
            zIndex = parseInt(CURRCLOUD.css("z-index")) + 1;
            if(CURRCLOUD.attr("id")!=$cloud.attr("id")) {cloudClose();}
        }
        $cloud.css({"z-index":zIndex});
        cloudOpen($cloud);
        CURRCLOUD = $cloud;
        return false;
    });
    $(".cl-close").click(function(){
        var $cloud = $(this).parent().parent().parent();
        cloudClose();
        return false;
    });
    
    // анимируем окно
    var SLIDERSTATUS;
    function slideWindow(cityname1, cityname2, direction){
        var $wrap = $(".wrap"),
            $cityWrap = $("#city_wrap"),
            $city1 = $cityWrap.find("." + cityname1),
            $city2 = $cityWrap.find("." + cityname2),
            wwidth = $(window).width(),
            sosni = (direction === 1) ? ((1920 - wwidth) / 2) : 0,
            speed = (1920 - wwidth) / 1.6;
        SLIDERSTATUS = direction;
        if ($.browser.msie) {
            $city2.hide();
            $wrap.filter(":not(:animated)").animate({"scrollLeft": sosni},speed, function(){
                $city1.css({"top":"-46px"}).show();
                return false;
            });
        } else {
            $wrap.filter(":not(:animated)").animate({"scrollLeft": sosni},speed);
            $city2.filter(":not(:animated)").fadeOut(400);
            $city1.filter(":not(:animated)").css({"top":"-46px"}).fadeIn(400);
            setTimeout(function(){
                $city1.css({"top":""});
                return false;
            }, 600);
        }
        return false;
    }
    $(".sosni").click(function(){
        slideWindow("krsk", "sosni", 1);
        return false;
    });
    $(".krsk").click(function(){
        slideWindow("sosni", "krsk", 0  );
        return false;
    });

    // реагируем на ресайз
    if( $(window).width() > 1790 ){$("#city_wrap").css({"display":"none"})}
    $(window).resize(function(){
        //var sosni = ((1920 - $(window).width()) / 2);
        if( $(window).width() > 1793 ){
            $("#city_wrap").css({"display":"none"})
        }else{
            $("#city_wrap").css({"display":"block"});
            //if(SLIDERSTATUS === 1){
            //    $(".wrap").scrollLeft(sosni);
            //}
        }
        return false;
    });
    
    
    // slider
    var $frame = $("#frame"),
        $film = $frame.find("ul"),
        $items = $film.find("li.slide"),
        $single = $items.filter(":first"),
        $pageNav = $(".page_nav"),

        currentPage = 1,   
        singleWidth = $single.outerWidth(),
        visible = 1, // сколько умещается во frame
        pages = Math.ceil($items.length / visible);

    function addEmpty(){
        // заполняем пробелы пустыми элементами
        var empty = $items.length % visible,
            html = "";
        if(empty !== 0) {
            for(var i=0; i < (visible - empty); i++){html += '<li class="empty"></li>';}
            $film.append(html);
            $items = $film.find("li");
        }
    }
    function clonLi(){
        // клонируем первую в конец, последнюю в начало
        $items.filter(":first").before($items.slice(-visible).clone().addClass("cloned"));
        $items.filter(":last").after($items.slice(0, visible).clone().addClass("cloned"));
        $(".cloned a").removeAttr("rel");
        $items = $film.find("li");
        $film.css("left", -singleWidth * visible); // ставим на начало
    }
    function goToPage(page){
        // ходим по страницам
        var dir = page < currentPage ? -1 : 1,
            n = Math.abs(currentPage - page),
            left = -singleWidth * dir * visible * n;

        $film.filter(":not(:animated)").animate({
            'left': "+=" + left
        },  400,  function(){
            if (page > pages){
                $film.css({"left": -singleWidth * visible});
                page = 1;
            }
            else if (page === 0) {
                $film.css({"left": -singleWidth * visible * pages});
                page = pages;
            }
            currentPage = page;

            $(".active").removeClass("active");
            $("#page_" + page).addClass("active");
        });
    }
    if (pages > 1) {
        addEmpty();
        clonLi();
        $("#prev").click(function(){
            goToPage(currentPage - 1);
            clearInterval(CLICKED);
            return false;
        });
        $("#next").click(function(){
            goToPage(currentPage + 1);
            clearInterval(CLICKED);
            return false;
        });

        CLICKED = setInterval(function(){
            goToPage(currentPage +1);
        }, 10000);
    } else {
        $("#prev, #next").hide();
    }


    $('#oneBitInsert_1, .onebit_mp3').click(function(){
        clearInterval(CLICKED);
        return false;
        });
    
    // всплывающее окошко
    $(".darkbox-shadow").css({"opacity":0.3});

    $('.more, .morepic').click(function() {
		var frame = $(".darkbox-frame"),
		    shadow = frame.find(".darkbox-shadow"),
		    close = frame.find(".ab-close"),
		    text = $(this).parents('.slide').find(".full_info"),
		    textTo = frame.find(".text");

        var destination = $(this).parent().offset().top;
		textTo.html(text.html());    
		frame.css({visibility: "visible"});
        function closer(){
            frame.css({visibility: "hidden"});
            return false;
        }
        shadow.click(closer);
        close.click(closer);
		$(document).keydown(function(e) {
			if(e.which==27) closer();
		});
		clearInterval(CLICKED);
		return false;
	});
});
