function d(s) {
    console.log(s);  
}

function contact() {
    $('#representation .contacts div').css('display', 'none');
    $('#europe_map').css('display', 'block'); 
    
    // Po prejeti na nazev statu
    var states = $('#representation .left h3');
    states.mouseover(function() {
        changeMap($(this));
    });
    
    // Po prejeti na mapu
    var maps = $('#representation .right area');
    maps.mouseover(function() {
        changeMap($(this));
    });   
}

function changeMap(item) {
    var bgColor = '#ff6633';
    var state = item.attr('class');
    var itemState = $('#representation .left h3[class="' + state + '"]');
    
    // Zmena mapy
    $('#europe_map').attr('src','/images/contact/' + state + '.gif');
    
    // Zmena pozadi nazvu statu
    $('#representation .left h3').css('color', '#000');
    itemState.css('color', bgColor); 

    // Zmena kontaktu
    $('#representation .contacts div').css('display', 'none');
    $('#representation .contacts div[class="' + state + '"]').css('display', 'block'); 
    
}

(function($) {
    var cache = [];
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
            
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

$(function() {
    var states = $('#representation .left h3');
    $.each(states, function(index, value) { 
        var state = $(this).attr('class');
        jQuery.preLoadImages("/images/contact/"+state+".gif");
    });    
});

function footerCatalogCategory() {
    var width = $('.footer_catalog_category').width();
    if(width > 0)
        $('.footer_header li.products_footer').css('width', width);
    else
        $('.footer_header li.products_footer').css('width', 150);
    
    if($('#catalog').length > 0) {
        $('.footer_catalog_category li a').click(function() {
            var href = $(this).attr('href');
            if(href.substr(0,1) == '/')
                href = href.substr(1)
            
            window.location = location.protocol + '//' + location.host + '/' + href;
            location.reload();
        });
    }
}

/**
 * Prihlasovaci box
 */
function signInBox() {
    toogleBox($('.sign_in'), $('.sign_in_box'), $('.sign_in_box .close_signin_box a'), 'sign-in');
    toogleBox($('.user_menu a'), $('.edit_profil_box'), $('.close_edit_box a'), 'edit-profil-box');    
}

/**
 * Zobrazeni nebo skryti boxu
 */
function toogleBox (elShow, elBox, elClose, hash) {
    var duration = 400;
    
    elShow.click(function() {
        elBox.slideToggle(duration);
        
        if(hash == 'edit-profil-box') {
            if($('.user_menu').hasClass('user_menu_active'))
                setTimeout(function(){ $('.user_menu').removeClass('user_menu_active'); }, 300);
            else
                $('.user_menu').addClass('user_menu_active');
        } else {
            if($(this).hasClass('active'))
                setTimeout(function(){elShow.removeClass('active');}, 300);
            else
                $(this).addClass('active');    
        }
    });
    
    elClose.click(function() {
        elBox.slideUp(duration);
        closeBox(hash);
    });        
    
    var hashUrl = window.location.hash.slice(1);
    if(hashUrl == hash) {
        elBox.css('display', 'block');
        
        if(hash == 'edit-profil-box') 
            $('.user_menu').addClass('user_menu_active');
        else
            elShow.addClass('active');
    }
    
    $('body').click(function() {
        elBox.slideUp(duration); 
        closeBox(hash);
    });
    
    $('#user').click(function(event){
        event.stopPropagation();
    });
}

/**
 * Zavreni boxu
 */
function closeBox (hash) {
    if(hash == 'edit-profil-box') 
        setTimeout(function(){ $('.user_menu').removeClass('user_menu_active'); }, 300);    
    else
        setTimeout(function(){ $('.sign_in').removeClass('active');}, 300);
}

/**
 * Flashove zpravy
 */
function flashMessage() {
    var el = $('.flash_message');
    var height = $('.flash_message').innerHeight();
    var width = $('.flash_message').innerWidth();
    el.css({
        'top': '-' + height + 'px', 
        'left' : (830/2)-(width/2) + 'px', 
        'display' : 'block'
    });
    $('.flash_message').animate({
        'top': '+='+height+'px'
    }, 500);
    $('.flash_message').delay(4000).animate({
        'top': '-='+height+'px'
    }, 500);
}

jQuery(document).ready(function() {
    contact();
    footerCatalogCategory();
    signInBox();
    flashMessage();
});



