$(document).ready(function() {
    // tooltip
    if (document.getElementById("sluzby")) {
        $("#sluzby div.advantage").cluetip({splitTitle: ' | ', positionBy: 'mouse'});
    }

    if (document.getElementById("temata")) {
        $("#temata div.menu_box").cluetip({splitTitle: ' | ', positionBy: 'mouse'});
    }

    if (document.getElementById("i_country")) {
        $("#i_country").change(function () {
            var val = $(this).val();

            if (228 == val) {
                // CR
                unmaskPhones();
                setMaskPhonesCS();
            } else if (182 == val) {
                // SR
                unmaskPhones();
                setMaskPhonesSK();
            }
        });
    }

    if (document.getElementById("i_c_country")) {
        $("#i_c_country").change(function () {
            var val = $(this).val();
            
            if (228 == val) {
                // CR
                unmaskPhones();
                setMaskPhonesCS();
            } else if (182 == val) {
                // SR
                unmaskPhones();
                setMaskPhonesSK();
            }
        });
    }

    var preloadText = 'Náhled se načítá...';

    if ('cs' != NWLANG) {
        preloadText = 'Loading zoom...';
    }
    
    var options = {
        zoomWidth: 687,
        zoomHeight: 350,
        xOffset: 0,
        yOffset: 20,
        position: 'bottom',
        preloadText: preloadText,
        title: false
    };

    $('#imagedetail-thumbnail a.img_main').jqzoom(options);

/*
    if (document.getElementById('imagedetail-full')) {
        $('#buy').imageDetail('imagedetail-thumbnail', 'imagedetail-full', '433x252', '2400');
    }

    $('div.offer a.product-ico').mouseover(function() {
        $(this).find('span.zoom').css('display', 'block');
    });

    $('div.offer a.product-ico').mouseout(function() {
        $(this).find('span.zoom').hide();
    });

    $('span.zoom').mouseover(function() {
        $(this).parent().find('span.preview-container').css('display', 'block');
        var width = $(this).parent().find('span.preview-container').find('img').width();
        $(this).parent().find('span.preview-container').css('width', width);
    });

    $('span.zoom').mouseout(function() {
        $(this).parent().find('span.preview-container').hide();
    });

    function callZoom() {
        $('#imagedetail-thumbnail a.img_main img').load(function () {
            // aktualizace dynamickeho zoomovani
            if (document.getElementById('imagedetail-full')) {
                $('#buy').imageDetail('imagedetail-thumbnail', 'imagedetail-full', '433x252', '2400');
            }
        });
    }
*/

    $('#photos a.other-photo').click(function() {
        var top_href = $('#imagedetail-thumbnail a.img_main').attr('href');
        var bottom_href = $(this).attr('href');
        var top_id = top_href.match(/\/upload\/images-cache\/([0-9]+)\/[0-9]+\.jpg/);
        var bottom_id = bottom_href.match(/\/upload\/images-cache\/([0-9]+)\/[0-9]+\.jpg/);

        if (top_id[1] && bottom_id[1]) {
            // nahrazeni horniho odkazu
            $('#imagedetail-thumbnail a.img_main').attr('href', bottom_href);

            // nahrazeni spodniho odkazu
            $(this).attr('href', top_href);

            // nahrazeni horni fotky
            $('#imagedetail-thumbnail a.img_main img').attr('src', '/upload/images-cache/' + bottom_id[1] + '/433x325.jpg');

            // nahrazeni spodni fotky
            $(this).find('img').attr('src', '/upload/images-cache/' + top_id[1] + '/120.jpg');

//             // .load netusim proc nefungovalo :-/
//             setTimeout(callZoom, 200);
        }

        return false;
    });
});

// {{{ number_format
function number_format( number, decimals, dec_point, thousands_sep ) {
    // Format a number with grouped thousands
    //
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
    // +       version: 809.522
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}// }}}

function formaPrice(price)
{
    return number_format(price, 0, ',', ' ')
}

function updateTotalPrice(base_price, material, varianta, moreni, rost, zebrik, price_sufix)
{
    // pricteni ceny za material
    if (document.getElementById(material)) {
        material = document.getElementById(material).value;

        if (materials[material]) {
            base_price += materials[material];
        }
    }

    // pricteni ceny za variantu
    if (document.getElementById(varianta)) {
        varianta = document.getElementById(varianta).value;

        if (variants[varianta]) {
            base_price += variants[varianta];
        }
    }

    // pricteni ceny za moreni
    if (document.getElementById(moreni)) {
        moreni = document.getElementById(moreni).value;

        if (morenis[moreni]) {
            base_price += morenis[moreni];
        }
    }

    // pricteni ceny za rost
    if (document.getElementById(rost)) {
        rost = document.getElementById(rost).value;

        if (rosty[rost]) {
            base_price += rosty[rost];
        }
    }
    
    // pricteni ceny za zebrik
    if (document.getElementById(zebrik)) {
        zebrik = document.getElementById(zebrik).value;

        if (zebriks[zebrik]) {
            base_price += zebriks[zebrik];
        }
    }

    // formatovani ceny
    base_price = formaPrice(base_price);

    $("#endprice").text(base_price + ',- ' + price_sufix);
}

function checkMaterial(material, text)
{
    material = document.getElementById(material);

    if (!material) {
        return true;
    }

    material = material.value;

    if ('' == material) {
        alert(text);
        return false;
    } else {
        return true;
    }
}

// rychlost animace vlastnosti
var speedAnimate = 4000;
var indexProperty = 0;

// animace fotek
var property_animate;
var property_hover = false;

$(document).ready(function() {
    if (document.getElementById("loga")) {
        // animace vlastnosti
        property_animate = setTimeout(function() { runAnimate(); }, speedAnimate);

        // stop / start animace vlastnosti
        $("#loga div").hover(
            function() {
                // zastaveni animace, pokud na ni uzivatel najel mysi
                clearTimeout(property_animate);
                property_hover = true;
            },
            function() {
                // znovuspusteni animace, pokud na ni uzivatel uz neni mysi
                property_animate = setTimeout(function() { runAnimate(); }, speedAnimate);
                property_hover = false;
            }
        );
    }
    
    $('.btn_doporucit .dop_headline').click(function() {
        if ($('.btn_doporucit .dop_form').hasClass('hidden')) {
            $('.btn_doporucit .dop_form').removeClass('hidden');
        } else {
            $('.btn_doporucit .dop_form').addClass('hidden');
        }
    });

    $('.btn_doporucit .dop_close').click(function() {
        $('.btn_doporucit .dop_form').addClass('hidden');
    });
});

function runAnimate()
{
    $("#loga #logo"+indexProperty).fadeOut("slow", function () {
        indexProperty = indexProperty + 1;

        $("#loga div").addClass("non-visible");

        if (!document.getElementById("logo"+indexProperty)) {
            indexProperty = 0;
        }

        $("#loga #logo"+indexProperty).removeClass("non-visible");
        $("#loga #logo"+indexProperty).fadeIn("normal");

        if (!property_hover) {
            property_animate = setTimeout(function() { runAnimate(); }, speedAnimate);
        }
    });
} 

