/**
 * NASTAVENI
 */

var debuging = 'off';
var minWidth = 1024;
var minHeight = 800;
var shopNum = 1;
var newieDiv = '';
var actionBoxHeight = 0;

$(document).ready(function() {
    imagePreload(getImageDir()+'/images/ajax-loader-transparent.gif');
    imagePreload(getImageDir()+'/images/ajax-loader.gif');

    cufonInit();
    maxImageScaling();

    // validace formularu
    contactUs();
    newsletter();
    searchShop();

    tb_init('.thickbox');//pass where to apply thickbox
    imgLoader = new Image();// preload image
    imgLoader.src = tb_pathToImage;
    webDir = getImageDir()+'/';
    var tb_pathToImage = getImageDir()+"/images/ajax-loader.gif";

    if(readCookie('footerFixed') != null) moveFooter();

    /*if(navigator.appName == "Microsoft Internet Explorer") {
        $('#play-laskonka').css('display', 'none');
    }*/

    //shadowbox
    Shadowbox.init({
        handleOversize: "drag",
        displayNav: false,
        displayCounter: false,
        viewportPadding: 0
    });

/*if($('#what-new-button').hasClass('cufonize'))
    {
        moveFooterToBottom();
    }*/
})

/**
 * Funkce spusti zamenu fontu pomoci Cufonu
 */
function cufonInit()
{
    if(navigator.appName != "Microsoft Internet Explorer")
    {
        Cufon.replace('a.cufonize', {
            hover: true
        });
        Cufon.replace('cufonize a', {
            hover: true
        });
    }
    Cufon.replace(".cufonize");
    Cufon.now();
}

// FIXME pri resizeu to bude chtit poladit height
$(window).resize(function() {
    footerBottom();
    checkResize();
});

$(window).scroll(function() {
    checkFooterNavigation();
});

/**
 * Funkce vraci url stranek pro AJAX
 */
function getWebDir()
{
    var language = $('#actualLanguage').html();

    //return 'http://www.arkady-pankrac.dev/'+language;
    return 'http://www.arkady-pankrac.cz/'+language;
    //return 'http://www.smarcoms-dev.cz/arkady-pankrac/'+language;
}

/**
 * Funkce vraci url pro obrazky v AJAXu
 */
function getImageDir()
{
    //return 'http://www.arkady-pankrac.dev';
    return 'http://www.arkady-pankrac.cz';
    //return 'http://www.smarcoms-dev.cz/arkady-pankrac';
}

/**
 * Funkce k zobrazovani a skryvani divu
 */
function changeVisibility(id, effect)
{
    // pokud se jedna o vysouvaci box
    if($(id).hasClass('dark-box'))
    {
        // zneaktivneni jiz otevrenych boxu
        $('#footer-menu a').removeClass('active');
        if($(id).hasClass('bottom') && readCookie('footerFixed') != null) $('#footerNavigation a').addClass('hidden');

        var ids = $('.dark-box').get();
        for(i=0; i<$(ids).size(); i++)
        {
            // v cyklu najdeme vybrany div
            if($(ids[i]).hasClass(id.replace(/./, '')))
            {
                // pokud je box otevren ma tridu open
                if($(id).hasClass('open'))
                {
                    $(id).slideUp('slow', function() {
                        if($(id).hasClass('bottom') && readCookie('footerFixed') != null)
                        {
                            $('#footerNavigation a').removeClass('hidden');
                            $('#footerNavigation a').css('bottom', 57);
                        }
                        $(id).removeClass('open');
                        $(id.replace(/./, '#')).removeClass('active');
                    });
                }
                // pokud ne otevreme box
                else
                {
                    $(id).slideDown('slow', function() {
                        if($(id).hasClass('bottom') && readCookie('footerFixed') != null)
                        {
                            $('#footerNavigation a').removeClass('hidden');
                            $('#footerNavigation a').css('bottom', 196);
                        }
                        $(id).addClass('open');
                        $(id.replace(/./, '#')).addClass('active');
                    });
                }
            }
            // pokud ne
            else
            {
                // pokud se jedna o spodni box a paticka je fixne pozicovana
                if(readCookie('footerFixed') != null && $(ids[i]).hasClass('bottom'))
                {
                    $(ids[i]).css({
                        'position':'fixed',
                        'bottom':$('#footer').height(),
                        'display':'none'
                    });
                }
                else
                {
                    $(ids[i]).attr('style', '');
                }
            }
        }
    }
    else
    {
        $(id).slideToggle('slow', function() {});
    }
}

/**
 * Funkce pro otevirani novinek v sekci Co je noveho
 */
function showNewie(id)
{
    if (newieDiv != id) $('.newie-box').hide(500);
    $(id).toggle(500);
    newieDiv = id;
}

/**
 * Funkce pro resize pozadi
 */
function maxImageScaling()
{
    jQuery('.bgmaximage').maxImage({
        isBackground: true,
        overflow: 'auto',
        position: 'absolute',
        maxAtOrigImageSize: false
    });
}

/**
 * Funkce pro resize pozadi, kdy je fixne pozicovano
 */
function maxImageScalingFixed()
{
    jQuery('.bgmaximage').maxImage({
        isBackground: true,
        overflow: 'auto',
        position: 'fixed',
        maxAtOrigImageSize: false
    });
}

/**
 * Funkce naanimuje paticku na spodek stranek
 */
function moveFooterToBottom()
{
    $('#footer').addClass('fixed');
    $(this).oneTime(3000, function() {
        $('#middle').height($('.bgmaximage').height());
        $('#footer').animate(
        {
            top: $(window).height()
        },
        1000,
        'linear',
        function() {
            $('#footer').removeClass('fixed');
        }
        )
    })
}

/**
 * Funkce prepocita pozici paticky podle velikosti pozadi
 */
function footerBottom()
{
    $('#middle').height($('.bgmaximage').height());
    $('.white-box').height($('#middle').height()-$('#footer').height()-151);
    $('.press-detail').height($('#middle').height()-37);
    $('.contact').height($('#middle').height()-57);
    $('.kid').css('top', $('#middle').height()-200);
    $('.diamond').css({
        'top' : '0',
        'height' : $('#middle').height()-$('#footer').height()
    });
    $('.event').css({
        'top' : '0',
        'height' : $('#middle').height()-$('#footer').height()
    });
    if($('.white-box').hasClass('shop-detail-action'))
    {
        debug('open');
        $('.textContent-box').height($('#middle').height()-338);
    }
    else
    {
        $('.textContent-box').height($('#middle').height()-180);
    }
    if($('.white-box').hasClass('shop-detail-map'))
    {
        $('#main-menu').addClass('hidden');
        $('.submenu').addClass('hidden');
    }

//debug('footerBottom -> height: '+$('#middle').height()+'px');
}

/**
 * Funkce zmeni status odkazu
 */
function changeActiveStatus(id)
{
    if($(id).hasClass('active'))
    {
        $(id).removeClass('active');
    }
    else
    {
        $(id).addClass('active');
    }
}

/**
 * Funkce zmeni text inputu
 */
function changeInputText(id, text)
{
    $(function(){
        if($(id).val() == text)
        {
            $(id).val('');
        }
        else
        {
            if($(id).val() == '') $(id).val(text);
        }
    })
}

/**
 * Debugovaci funkce
 * Funkce zapisuje do logu
 */
function debug($obj) {
    if (window.console && window.console.log) {
        window.console.log($obj);
    }
}

/**
 * Funkce pro zobrazeni detailu obchodu AJAXem
 */
function ajaxShopDetail(shopNameRobot)
{
    // IE6 FIX
    if(navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) <= 6)
    {
        return true;
    }

    $(function(){
        $('#shop-right').load(getWebDir()+'/obchody', {
            nameRobot: shopNameRobot
        }, function(response, status, xhr){
            if(status == 'success')
            {
                var shopId = $('#shopId').html();
                if($('.white-box').hasClass('all'))
                {
                    ajaxShopCategory(shopId);
                }

                $('.shop').removeClass('active');
                $('#shop'+shopId).addClass('active');
                footerBottom();
            }
            // debug
            if(debuging == 'on')
            {
                debug('AJAX RESPONSE:');
                debug('Status: '+status);
                debug('Server response: '+xhr.status+' '+xhr.statusText);
                debug('Script URL: '+getWebDir()+'/obchody');
                debug('nameRobot: '+shopNameRobot);
                debug('Response: '+response);
            }
        })
    })
}

/**
 * Funkce zobrazuje obchod na mape
 */
function ajaxShopDetailMap(shopId)
{
    $(function(){
        $('#shop-right').load(getWebDir()+'/obchody', {
            shopDetailId: shopId
        }, function(response, status, xhr){
            if(status == 'success')
            {
                footerBottom();
            }
            // debug
            if(debuging == 'on')
            {
                debug('AJAX RESPONSE:');
                debug('Status: '+status);
                debug('Server response: '+xhr.status+' '+xhr.statusText);
                debug('Script URL: '+getWebDir()+'/obchody');
                debug('shopDetailId: '+shopId);
                debug('Response: '+response);
            }
        })
    })

    return false;
}

/**
 * Funkce zobrazi mapu obchodu
 */
function ajaxShopsMap(shopId)
{
    $('#shop-left').load(getWebDir()+'/mapa-obchodu', {
        showShopId: shopId
    }, function(response, status, xhr){
        if(status == 'success')
        {
            if($('.white-box').hasClass('all'))
            {
                $('.white-box').removeClass('all');
            }

            footerBottom();
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/mapa-obchodu');
            debug('showShopId: '+shopId);
            debug('Response: '+response);
        }
    })
}

/**
 * Funkce zobrazi kategorii obchodu
 */
function ajaxShopCategory(shopId)
{
    $('#shop-left').load(getWebDir()+'/obchody', {
        shopId: shopId
    }, function(response, status, xhr){
        if(status == 'success')
        {
            if($('.white-box').hasClass('all'))
            {
                $('.white-box').removeClass('all');
            }

            footerBottom();
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/obchody');
            debug('shopId: '+shopId);
            debug('Response: '+response);
        }
    })
}

/**
 * Funkce vypise kategorii obchodu z mapy obchodu
 */
function ajaxShopCategoryFromMap(shopId)
{
    $('#shop-left').load(getWebDir()+'/obchody', {
        shopNum: shopId
    }, function(response, status, xhr){
        if(status == 'success')
        {
            if($('.white-box').hasClass('all'))
            {
                $('.white-box').removeClass('all');
            }

            footerBottom();
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/obchody');
            debug('shopId: '+shopId);
            debug('Response: '+response);
        }
    })
}

/**
 * Funkce pro zobrazeni akce obchodu
 */
function ajaxShopAction(articleId, shopId)
{
    $('#shop-left').load(getWebDir()+'/obchody', {
        articleId: articleId,
        shop: shopId
    }, function(response, status, xhr){
        if(status == 'success')
        {
            if($('#main-menu').hasClass('hidden'))
            {
                $('#main-menu').removeClass('hidden');
                $('.submenu').removeClass('hidden');
            }

            if($('.white-box').hasClass('all'))
            {
                $('.white-box').removeClass('all');
            }


            footerBottom();
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/obchody');
            debug('articleId: '+articleId);
            debug('Response: '+response);
        }
    })
}

/**
 * Funkce pro zavirani AJAXovych oken
 */
function ajaxClose(id, type)
{
    // pokud se jedna o mapu obchodu
    if($('#main-menu').hasClass('hidden') && type == 'left')
    {
        var shopId = $('#showShopNum').html();
        if(shopId == '') shopId = shopNum;
        ajaxShopCategoryFromMap(shopId);

        $(id).css({
            'display' : 'none'
        });
        $('#main-menu').removeClass('hidden');
        $('.submenu').removeClass('hidden');
    }
    // zavreni akce obchodu
    else if($('.white-box').hasClass('shop-detail-action'))
    {
        var shopId = $('#shopId').html();
        ajaxShopCategory(shopId);
    }
    // zavreni ostatnich oken
    else
    {
        $(id).css({
            'display' : 'none'
        });
    }

    // zavreni detailu obchodu pri zavreni kategorie obchodu
    if(id == '.shop-list')
    {
        ajaxClose('.shop-detail', 'right');
        ajaxShowAllShops();
    }
}

/**
 * AJAXova funkce pro zobrazeni vsech obchodu
 */
function ajaxShowAllShops()
{
    $('#shop-left').load(getWebDir()+'/obchody', {
        ajaxAction: 'allShops'
    }, function(response, status, xhr){
        if(status == 'success')
        {
            if($('#main-menu').hasClass('hidden'))
            {
                $('#main-menu').removeClass('hidden');
                $('.submenu').removeClass('hidden');
            }

            $('.white-box').addClass('all');


            footerBottom();
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/obchody');
            debug('ajaxAction: allShops');
            debug('Response: '+response);
        }
    })
}

/**
 * Funkce pro zobrazeni akce centra
 */
function actionDetail(action)
{
    // otevreni prvniho kroku
    if(action == 'open')
    {
        actionBoxHeight = $('.action-detail').height();
        $('#main-menu').attr('class', $('#menuClass').attr('rel'));
        $('.bgmaximage').attr('src', $('.bgAction').attr('src'));
        $('.bg').addClass('hidden');
        $('.bg-shadow').addClass('hidden');
        $('.left').addClass('navig-action');
        $('.left').removeClass('navig-action-2');
        $('.left .link').addClass($('#leftClass').attr('rel'));
        $('.right').addClass('navig-action');
        $('.right').removeClass('navig-action-2');
        $('.right .link').addClass($('#rightClass').attr('rel'));
        $('.action-bg').css({
            'background' : 'none'
        });
        $('.close-action').removeClass('hidden');
        $('#more').addClass('hidden');
        $('#more2').removeClass('hidden');

        $('.action-bg').removeClass('cursor');
        $('.action-bg').attr('onclick', '');
    }
    // oterveni druheho kroku
    else if(action == 'showDetail')
    {
        $('.white-box-akce').animate(
        {
            height: $('#middle').height()-20,
            top: 0
        },
        1000,
        'linear',
        function() {
            $('.postfix').removeClass('hidden');
            $('.gallery').removeClass('hidden');
            $('.fbPerex').addClass('hidden');
            $('#less').removeClass('hidden');
            $('#more2').addClass('hidden');
            var leftPosition = $('.action-bg').css('left').replace(/px/,"") * 1;
            var heightForPos = $('.action-detail').css('width').replace(/px/,"") * 1
            var leftPos = leftPosition + heightForPos + 160;
            $('.gallery').css('left', leftPos + 'px');
        }
        )
    }
    // zavreni druheho kroku
    else if(action == 'closeDetail')
    {
        if(!actionBoxHeight) actionBoxHeight = 211;
        $('.gallery').addClass('hidden');
        $('.white-box-akce').animate(
        {
            height: actionBoxHeight,
            top: 197
        },
        1000,
        'linear',
        function() {
            $('.postfix').addClass('hidden');
            $('#less').addClass('hidden');
            $('#more2').addClass('hidden');
            $('#more').removeClass('hidden');
            $('.fbPerex').removeClass('hidden');
        }
        )
    }
    // vraceni na vypis akci centra
    else
    {
        $('#main-menu').attr('class', '');
        $('.bgmaximage').attr('src', $('.bgDefault').attr('src'));
        $('.bg').removeClass('hidden');
        $('.bg-shadow').removeClass('hidden');
        $('.left').addClass('navig-action-2');
        $('.left').removeClass('navig-action');
        $('.left .link').removeClass($('#leftClass').attr('rel'));
        $('.right').addClass('navig-action-2');
        $('.right').removeClass('navig-action');
        $('.right .link').removeClass($('#rightClass').attr('rel'));
        var actionImage = $('.bgActionDefault').attr('src');
        $('.action-bg').css({
            'background' : 'url("'+actionImage+'")',
            'background-repeat' : 'no-repeat'
        });
        $('.gallery').addClass('hidden');
        $('.white-box-akce').animate(
        {
            height: actionBoxHeight,
            top: 197
        },
        1000,
        'linear',
        function() {
            $('.postfix').addClass('hidden');
            $('#less').addClass('hidden');
            $('#more2').addClass('hidden');
            $('.close-action').addClass('hidden');
            $('#more').removeClass('hidden');
            $('.fbPerex').removeClass('hidden');
        }
        )

        $('.action-bg').attr('onclick', "actionDetail('open'); return false;");
        $('.action-bg').addClass('cursor');
    }
}

/**
 * Funkce pro otaceni akci centra (kolotoc)
 */
function ajaxActions(step)
{
    // IE6 FIX
    if(navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) <= 6)
    {
        return true;
    }

    // cislo akce
    var actualPage = parseInt($('#actualPage').attr('rel'));
    if(step == 'next') actualPage = actualPage + 1;
    else actualPage = actualPage - 1;

    // kontrola zda je akce centra otevrena nebo ne
    if($('.bg-shadow').hasClass('hidden')) var boxDetail =  true;
    else boxDetail = false;

    // kontrola zda je oterven detail
    if($('.postfix').hasClass('hidden')) var open = false;
    else var open = true;

    // AJAXova funkce
    $('#akce').load(getWebDir()+'/akce-centra', {
        page: actualPage,
        detail: boxDetail
    }, function(response, status, xhr){
        if(status == 'success')
        {
            // pokud je akce otevrena
            if($('#more').hasClass('hidden'))
            {
                // nastaveni tridy menu
                $('#main-menu').attr('class', $('#menuClass').attr('rel'));
            }
            else
            {
                $('#main-menu').attr('class', '');
            }

            // nastaveni tridy pro zavreni akce
            $('.close-action').addClass($('#menuClass').attr('rel'));
            cufonInit();

            // pokud ma byt akce otevrena otevreme ji
            if(open == true)
            {
                actionDetail('open');
            }
            footerBottom();
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/akce-centra');
            debug('page: '+actualPage);
        }
    })

    return false;
}

/**
 * Zmena tridy Carouselu
 */
function changeCarouselClass(button, action)
{
    if(!$('.bg').hasClass('hidden'))
    {
        // zapnuto
        if(action == 'on')
        {
            $('.'+button+' .link').addClass($('#'+button+'CarouselClass').attr('rel'));
        }
        // vypnuto
        else
        {
            $('.'+button+' .link').removeClass($('#'+button+'CarouselClass').attr('rel'));
        }
    }
}

/**
 * Funkce pozicuje prostredni obrazek akce centra
 */
function positionCalc()
{
    var left = ($(window).width()/2)-378;
    $('.action-bg').css('left', left);
}

/**
 * Funkce pro spolupraci flashove mapy obchodu a AJAXu
 */
function shopinfo(id){
    shopNum = id;
    ajaxShopDetailMap(id);
}

/**
 * Funkce pro fultextove vyhledavani
 */
function fulltext()
{
    // ziskani promennych
    var search = $('#searchinput').attr('value');

    if(search == 'hledej na webu') search = $('#searchtext-big').attr('value');

    // otevreni okna s vysledky vyhledavani
    $('.search-box').css('display', 'block');

    // vlozeni loaderu
    $('#searchResults').html('<div class="loader-box"><img src="'+getImageDir()+'/images/ajax-loader.gif" alt="nahrávám..." class="loader" /></div>');

    // spusteni ajaxu
    $('#searchResults').load(getWebDir()+'/vyhledavani', {
        searchtext: search
    }, function(response, status, xhr){
        if(status == 'success')
        {
            if($('#searchResults').height() < 718) $('#searchResults').height(718);
            $('#searchtext-big').attr('value', search);
            $('#searchtext-big').focus();
            $('#searchinput').attr('value', 'hledej na webu');
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/vyhledavani');
            debug('searchtext: '+search);
            debug('response: '+response);
        }
    })
}

/**
 * Funkce pro zobrazeni souteze
 */
function showCompetition(action)
{
    // otevreni
    if(action == 'open')
    {
        $('.event').css({
            'top' : '0',
            'height' : $('#middle').height()-$('#footer').height()
        });
        $('.action-detail').addClass('hidden');
        if(!$('.gallery').hasClass('hidden'))
        {
            $('.gallery').addClass('hidden');
            $('.gallery').addClass('wasOpen');
        }
        $('.event').slideToggle('slow', function() {});
    }
    // zavreni
    else
    {
        $('.event').slideToggle('slow', function() {
            $('.action-detail').removeClass('hidden');
            if($('.gallery').hasClass('wasOpen'))
            {
                $('.gallery').removeClass('wasOpen');
                $('.gallery').removeClass('hidden');
            }
        });
    }

}

/**
 * Funkce pro zobrazeni souteze
 */
function showDiamond(action)
{
    // otevreni
    if(action == 'open')
    {
        $('.action-detail').addClass('hidden');
        if(!$('.gallery').hasClass('hidden'))
        {
            $('.gallery').addClass('hidden');
            $('.gallery').addClass('wasOpen');
        }
        $('#diamond-photos').slideToggle('slow', function() {});
    }
    // zavreni
    else
    {
        $('#diamond-photos').slideToggle('slow', function() {
            $('.action-detail').removeClass('hidden');
            if($('.gallery').hasClass('wasOpen'))
            {
                $('.gallery').removeClass('wasOpen');
                $('.gallery').removeClass('hidden');
            }
        });
    }

}

/**
 * Funkce pro zobrazeni souteze
 */
function showDiamondVideo(action)
{
    // otevreni
    if(action == 'open')
    {
        $('.action-detail').addClass('hidden');
        if(!$('.gallery').hasClass('hidden'))
        {
            $('.gallery').addClass('hidden');
            $('.gallery').addClass('wasOpen');
        }
        $('#diamond-video').slideToggle('slow', function() {});
    }
    // zavreni
    else
    {
        $('#diamond-video').slideToggle('slow', function() {
            $('.action-detail').removeClass('hidden');
            if($('.gallery').hasClass('wasOpen'))
            {
                $('.gallery').removeClass('wasOpen');
                $('.gallery').removeClass('hidden');
            }
        });
    }

}

/**
 * Funkce pro zobrazeni partneru akce diamond race
 */
function showDiamondPartners(action)
{
    // otevreni
    if(action == 'open')
    {
        $('.action-detail').addClass('hidden');
        if(!$('.gallery').hasClass('hidden'))
        {
            $('.gallery').addClass('hidden');
            $('.gallery').addClass('wasOpen');
        }
        $('#diamond-partners').slideToggle('slow', function() {});
    }
    // zavreni
    else
    {
        $('#diamond-partners').slideToggle('slow', function() {
            $('.action-detail').removeClass('hidden');
            if($('.gallery').hasClass('wasOpen'))
            {
                $('.gallery').removeClass('wasOpen');
                $('.gallery').removeClass('hidden');
            }
        });
    }

}

/**
 * Funkce kontroluje vypneni formulare souteze
 */
function checkForm()
{
    $("#competitionForm").validate({
        onsubmit: true,
        submitHandler: function(form) {
            // spusteni ajaxu
            $('#competitionForm').load(getWebDir()+'/soutez', {
                competition: $('input[name="competition_id"]').attr('value'),
                vote:        $('input[name="fanswer"]').attr('value'),
                name:        $('input[name="fname"]').attr('value'),
                surname:     $('input[name="fsurname"]').attr('value'),
                phone:       $('input[name="fphone"]').attr('value'),
                email:       $('input[name="femail"]').attr('value'),
                city:        $('input[name="fcity"]').attr('value')
            }, function(response, status, xhr){
                if(status == 'success')
                {
                    $('#competitionForm').html('<span class="success">Vaše odpověď byla uložena.</span>')
                }

                // debug
                if(debuging == 'on')
                {
                    debug('AJAX RESPONSE:');
                    debug('Status: '+status);
                    debug('Server response: '+xhr.status+' '+xhr.statusText);
                    debug('Script URL: '+getWebDir()+'/soutez');
                    debug('response: '+response);
                }
            })
        }
    });
}

function searchShopMenu(id)
{
    $('#footer').addClass('fixed');
    $('.bottom').css({
        'position':'fixed',
        'bottom':$('#footer').height()
    });
    $('#footerNavigation a').removeClass('up');
    $('#footerNavigation a').addClass('down');

    if($('div[style*="display: block;"]').hasClass('bottom'))
    {
        $('#footerNavigation a').css('bottom', 196);
    }
    else
    {
        $('#footerNavigation a').css('bottom', $('#footer').height());
    }

    $(id).slideDown('slow', function() {

        $('#footerNavigation a').removeClass('hidden');
        $('#footerNavigation a').css('bottom', 196);

        $(id).addClass('open');
        $(id.replace(/./, '#')).addClass('active');
    });

    // cookie
    createCookie('footerFixed', true, 1);
}

function moveFooter()
{
    if($('#footerNavigation a').hasClass('up'))
    {
        $('#footer').addClass('fixed');
        $('.bottom').css({
            'position':'fixed',
            'bottom':$('#footer').height()
        });
        $('#footerNavigation a').removeClass('up');
        $('#footerNavigation a').addClass('down');

        if($('div[style*="display: block;"]').hasClass('bottom'))
        {
            $('#footerNavigation a').css('bottom', 196);
        }
        else
        {
            $('#footerNavigation a').css('bottom', $('#footer').height());
        }

        // cookie
        createCookie('footerFixed', true, 1);
    }
    else
    {
        $('#footer').removeClass('fixed');
        $('.bottom').css({
            'position':'',
            'bottom':0
        });
        $('#footerNavigation a').removeClass('down');
        $('#footerNavigation a').addClass('up');
        $('#footerNavigation a').css('bottom', 0);

        // smazani cookie
        eraseCookie('footerFixed');
    }
}

function checkFooterNavigation()
{
    if($('div[style="display: block;"]').hasClass('bottom'))
    {
        var bottomOpen = 'open';
        var scrollHeight = $(window).scrollTop()+$(window).height()-$('#footer').height()-$('#header').height()-$('.dark-box').height()-6;
        var windowHeight = $('#middle').height()-$('.dark-box').height()-220;
    }
    else
    {
        var bottomOpen = 'close';
        var scrollHeight = $(window).scrollTop()+$(window).height()-$('#footer').height()-$('#header').height()-6;
        var windowHeight = $('#middle').height();
    }

    if(scrollHeight >= windowHeight) $('#footerNavigation a').addClass('hidden');
    else if($('#footerNavigation a').hasClass('hidden')) $('#footerNavigation a').removeClass('hidden');

    if(debuging == 'on')
    {
/*debug('bottom: '+bottomOpen);
        debug('scroll height: '+scrollHeight);
        debug('window height: '+windowHeight);*/
}
}

/**
 * Funkce pro vytvoreni cookie
 */
function createCookie(name,value,hours) {
    if (hours) {
        var date = new Date();
        date.setTime(date.getTime()+(hours*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

/**
 * Funkce pro precteni cookie
 */
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

/**
 * Funkce pro smazani cookie
 */
function eraseCookie(name) {
    createCookie(name,"",-1);
}

/**
 * Funkce hlida resize okna
 */
function checkResize()
{
    if($(window).width() <= minWidth)
    {
        $('#header').width(minWidth);
        $('#footer').width(minWidth);
        $('.dark-box').width(minWidth);
    }
    else
    {
        $('#header').width('100%');
        $('#footer').width('100%');
        $('.dark-box').width('100%');
    }
}

/**
 * Funkce pro pridani mailu do databaze
 */
function newsletter()
{
    $('#newsletterForm').validate({
        onsubmit: true,
        submitHandler: function(form) {
            // spusteni ajaxu
            $('#newsletterText').load(getWebDir()+'/novinky-na-email', {
                email: $('input[name="newsletter_email"]').attr('value')
            }, function(response, status, xhr){
                $('#newsletterText').html('<span class="success">'+response+'</span>')
                $('input[name="newsletter_email"]').attr('value', 'váš e-mail');

                // debug
                if(debuging == 'on')
                {
                    debug('AJAX RESPONSE:');
                    debug('Status: '+status);
                    debug('Server response: '+xhr.status+' '+xhr.statusText);
                    debug('Script URL: '+getWebDir()+'/novinky-na-email');
                    debug('response: '+response);
                }
            })
        }
    });
}

/**
 * Funkce pro odebrani mailu z databaze
 */
function delNewsletter()
{
    $('#cancelNewsletterForm').validate({
        onsubmit: true,
        submitHandler: function(form) {
            // spusteni ajaxu
            $('#newsletterText').load(getWebDir()+'/zrusit-newsletter', {
                delEmail: $('input[name="newsletter_email"]').attr('value')
            }, function(response, status, xhr){

                if(response == 0) var text = 'Zadaný e-mail nebyl v databázi pro zasílání novinek nalezen.';
                else var text = 'Váš e-mail byl odstraněn z databáze pro zasílání novinek.';

                $('#newsletterText').html('<span class="success">'+text+'</span>')
                $('input[name="newsletter_email"]').attr('value', 'váš e-mail');

                // debug
                if(debuging == 'on')
                {
                    debug('AJAX RESPONSE:');
                    debug('Status: '+status);
                    debug('Server response: '+xhr.status+' '+xhr.statusText);
                    debug('Script URL: '+getWebDir()+'/zrusit-newsletter');
                    debug('response: '+response);
                }
            })
        }
    });
}

/**
 * Funkce pro odeslani kontaktniho formulare
 */
function contactUs()
{
    $('#contactForm').validate({
        onsubmit: true,
        submitHandler: function(form) {
            // spusteni ajaxu
            $('#mResult').load(getWebDir()+'/napiste-nam', {
                name:           $('input[name="cname"]').attr('value'),
                email:          $('input[name="cemail"]').attr('value'),
                message:        $('textarea[name="cmessage"]').attr('value'),
                emailTemplate:  $('input[name="emailTemplate"]').attr('value')
            }, function(response, status, xhr){
                if(response == "ok")
                {
                    $('#mResult').html('<span class="success">Vaše zpráva byla úspěšně odeslána.</span>');
                    $('input[name="cname"]').attr('value', 'vaše jméno');
                    $('input[name="cemail"]').attr('value', 'váš e-mail');
                    $('textarea[name="cmessage"]').attr('value', 'vaše zpráva');
                }
                else
                {
                    $('#mResult').html('<span class="error">Při odeslání došlo k chybě.</span>');
                }

                // debug
                if(debuging == 'on')
                {
                    debug('AJAX RESPONSE:');
                    debug('Status: '+status);
                    debug('Server response: '+xhr.status+' '+xhr.statusText);
                    debug('Script URL: '+getWebDir()+'/napiste-nam');
                    debug('response: '+response);
                }
            })
        }
    });
}

/**
 * Vyhledavani obchodu
 */
function searchShop()
{
    $("#searchShopForm").validate({
        submitHandler: function(form) {
            form.submit();
        }
    });
}

/**
 * Funkce pro preload obrazku
 */
function imagePreload(imgUrl)
{
    preload_image_object = new Image();
    preload_image_object.src = imgUrl;
}

/**
 * Funkce pro zobrazeni sluzby centra (AJAX)
 */
function showService(pageNameRobot)
{
    $('.about').load(getWebDir()+'/o-centru', {
        nameRobot: pageNameRobot
    }, function(response, status, xhr){
        if(status == 'success')
        {
            cufonInit();
            $('.service').removeClass('active');
            $('#'+pageNameRobot).addClass('active');
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/o-centru');
            debug('nameRobot: '+pageNameRobot);
            debug('response: '+response);
        }
    })
}

/**
 * Funkce pro zavreni sluzbu centra
 * Funkce vrati zpet text o centru
 */
function serviceClose()
{
    var defaultPage = $('#defaultPage').text();

    $('.about').load(getWebDir()+'/o-centru', {
        nameRobot: defaultPage,
        ajaxAction: 'close'
    }, function(response, status, xhr){
        if(status == 'success')
        {
            cufonInit();
            $('.service').removeClass('active');
        }
        // debug
        if(debuging == 'on')
        {
            debug('AJAX RESPONSE:');
            debug('Status: '+status);
            debug('Server response: '+xhr.status+' '+xhr.statusText);
            debug('Script URL: '+getWebDir()+'/o-centru');
            debug('nameRobot: '+pageNameRobot);
            debug('response: '+response);
        }
    })
}

function facebookAPIinit(language)
{
    if(language == 'cz') {
        var localize = 'cs_CZ';
    }
    else if(language == 'en') {
        var localize = 'en_US';
    }
    else if(language == 'de') {
        var localize = 'de_DE';
    }


    window.fbAsyncInit = function() {
        FB.init({
            appId: '141350219220962',
            status: false,
            cookie: true,
            xfbml: true
        });
    };
    (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
        '//connect.facebook.net/'+localize+'/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    }());
}

function shareFacebook(url, title, description) {
    var share = {
        method: 'stream.share',
        u: url
    };

    FB.ui(share, function(response) {

        });
}

function flashPlayer(videoUrl) {
    /*var Wo = $(window).width() - 40;
    var Ho = $(window).height() - 40 - 48;*/

    var Wo = $('#middle').width();
    var Ho = $('#middle').height();

    var H = Math.floor(Ho/9)*9;
    var W = Math.round(H*16/9);

    if(W > Wo){
        W = Math.floor(Wo/16)*16;
        H = Math.round(W/16*9);
    }

    if(W > 1024) {
        H = Math.floor(H*(1024/W));
        W = 1024;
    }

    Shadowbox.open({
        content: getImageDir()+"/flash/player.swf",
        player: "swf",
        title: false,
        width: W, //$(window).width(),
        height: H + 48, //$(window).height(),
        overlayColor: '#0B1113',
        options: {
            flashVars: {
                videourl:videoUrl
            },
            flashParams: {
                menu: false,
                wmode: "window"
            }
        }
    });

    $('#sb-container').css({
        'position' : 'absolute',
        'top' : '65px',
        'height' : ($('#middle').height()+5)
    });

    $('#sb-overlay').css('background-color','#0B1113');

    if($('#footer').hasClass('fixed')) {
        $('#sb-container').height($(window).height()-116);
    }
}

function closeVideo(){
    Shadowbox.close();
}
function shareVideo(){
    Shadowbox.close();
    if($('#diamond-photos').hasClass('diamond')) {
        var share = {
            method: 'stream.share',
            u: 'http://www.arkady-pankrac.cz/cz/akce-centra/diamond-race'
        };

        FB.ui(share, function(response) {

        });
    }
    else {
    FB.ui(
    {
        method: 'stream.publish',
        message: '',
        attachment: {
            name: 'Jak vzniká módní fotografie?',
            caption: 'Arkády Pankrác - Jak vzniká módní fotografie',
            description: (
                'Zajímá Vás, jak se fotil vizuál pro Arkády? Sledujte exkluzivní video ze zákulisí.'
                ),
            href: 'http://www.arkady-pankrac.cz/cz?utm_campaign=laskonka&utm_medium=facebook&utm_source=website',
            media: [{
                'type': 'image',
                'src': 'http://www.arkady-pankrac.cz/images/laskonka-fb.jpg',
                'href': 'http://www.arkady-pankrac.cz/cz?utm_campaign=laskonka&utm_medium=facebook&utm_source=website'
            }]
        },
        action_links: []
    },
    function(response) {
        /*if (response && response.post_id) {
            alert('Post was published.');
        } else {
            alert('Post was not published.');
        }*/
        }
        );
    }
}
