function getWinSize() {
    var myWidth = 0, myHeight = 0;
    
    if( typeof( window.innerWidth ) == 'number' ) {
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if(document.documentElement && (document.documentElement.clientWidth ||
                                           document.documentElement.clientHeight )) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && (document.body.clientWidth ||
                                 document.body.clientHeight ) ) {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    
    return {
        width: myWidth,
        height: myHeight
    }
}

var lastOpacity = '.3';
var typingTimeout, mapColorTimeout;
var currSlide;

function showHelp(){
    $('#help').show();
    return false;
}

function setActive(e){
    currSlide = e;
    $('#map a.slider-goto').removeClass('selected');
    $('#map a.slider-goto[href=' + e + ']').addClass('selected');
    
    function setMapColor(){
        var newClass = e.split('#');
        var lastClass = $('#map').attr('class');
        newClass = newClass[1];
        $('#map').removeClass(lastClass);
        $('#map').addClass(newClass);
    }
    
    window.clearInterval(mapColorTimeout);
    mapColorTimeout = window.setTimeout(function() {
        setMapColor();
    }, 500);
}

function setLocation(newLocation){
    var cleanedLocation = new String(window.location).split('#')
    cleanedLocation = cleanedLocation[0];
    window.location = cleanedLocation + newLocation;
}

$(document).ready(function(){
    
    var e = new String(window.location).split('#');
    if(e.length == 1) {
        e = $('#start-link').attr('href');
    } else {
        e = '#' + e[1];
    }
    
    setActive(e);
    
    $('#map').mouseover(function(){
        $('#help').hide();
        //$('#show-help').text('аЁаНаОаВаА аНаЕ аНаАб�аЛаИ? o_O');
    });
});

var left = false;
var right = false;

$(document).keydown(function(e){
    //console.log(e);
    var gotoElement;
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    var el = $('a.slider-goto[href=' + currSlide + ']');
	switch(key) {
		case 37: 
            var prevElement = $(el).prev();
            gotoElement = $(prevElement).attr('href');
            
            if (!$(prevElement).prev().length){
                left = false;
                right = true;
            } else {
                left = true;
            }
        break;
		
        case 39: 
            var prevElement = $(el).next();
            gotoElement = $(prevElement).attr('href');
            
            if (!$(prevElement).next().length){
                left = true;
                right = false;
            } else {
                right = true;
            }
		break;
        
        case 38: 
            if(left || !right){
                gotoElement = $('#map a.slider-goto[href=' + currSlide + ']').parent().prev().children().next().attr('href');
            } else {
                gotoElement = $('#map a.slider-goto[href=' + currSlide + ']').parent().prev().children().attr('href');
            }
        break;
		
		case 40:
            if(left || !right){
                gotoElement = $('#map a.slider-goto[href=' + currSlide + ']').parent().next().children().next().attr('href');
            } else {
                gotoElement = $('#map a.slider-goto[href=' + currSlide + ']').parent().next().children().attr('href');
            }
        break;
	}


    //var gotoElement = $(this).attr('href');
    var gotoTop = $(gotoElement).offset().top;
    var gotoLeft = $(gotoElement).offset().left;
        
    $('html, body').animate({scrollTop: gotoTop, scrollLeft: gotoLeft}, 500);
    setActive(gotoElement);
    window.clearInterval(typingTimeout);
    typingTimeout = window.setTimeout(function() {
        setLocation(gotoElement);
    }, 1000);    
});

jQuery.fn.slider = function() {
    var windowSize = getWinSize();
    var lastSize = {
        width: 0,
        height: 0
    };
    
    var elementCounter = 0;
    
    $('a.slider-goto').click(function(){
        var gotoElement = $(this).attr('href');
        var gotoTop = $(gotoElement).offset().top;
        var gotoLeft = $(gotoElement).offset().left;
        
        $('html, body').animate({scrollTop: gotoTop, scrollLeft: gotoLeft}, 500);
        setActive(gotoElement);
        window.clearInterval(typingTimeout);
        typingTimeout = window.setTimeout(function() {
            setLocation(gotoElement);
        }, 1000);
    });
    
    return this.each(function(){
        ++elementCounter;
        
        $(this).css({
            'top': lastSize.height,
            'left': lastSize.width,
            'width': windowSize.width,
            'height': windowSize.height
        });
        
        lastSize.width += windowSize.width;
        
        if(elementCounter == 2 ) {
            lastSize.height = windowSize.height;
            lastSize.width = 0;
        }
        
    });
}