// JavaScript Document
function zobraz_menu(id) {
    var blok = document.getElementById(id);
    if(blok){
        if(blok.style.display == "none") {
            blok.style.display = "block";
        } else if(blok.style.display == "block") {
            blok.style.display = "none";
        }	
    }
}

function addToCart(idp, pocetElementId) {
    var pocet = 1;
    if (document.getElementById(pocetElementId)) {
        pocet = parseInt($('#'+pocetElementId).val());
    }
    $.ajax({
        type: "POST",
        dataType: "json",
        url: _SERVER_PATH+"inc/ajax/cart.php?action=addToCart",
        data: "&idp="+parseInt(idp)+"&pocet="+pocet,
        success: function(msg) {
            alert(msg.message);
            $('#products_count').attr('innerHTML', ' '+msg.products_count+' ');
            $('#products_buy').css('display', 'inline');
            return false;
        }
    });
    return false;
}

function removeFirstOption(id)
{
  var elSel = document.getElementById(id);
  var i;
  elSel.remove(0);
}


function changeZpusobDoruceni() {
    var id = parseInt($('#druh_dopravy').val());
    if(id > 0) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: _SERVER_PATH+"inc/ajax/platba.php?action=getZpusobyDoruceni",
            data: "&idd="+id,
            success: function(msg) {
                var zpusoby_doruceni = msg.zpusoby_doruceni;
                $('#zpusob_doruceni').find('option').remove();
                $.each(zpusoby_doruceni, function () {
                    $('#zpusob_doruceni').append(
                        $('<option></option>').val(this.id).html(this.html)
                    );
                });
                /*
                //$('#zpusob_doruceni').attr('innerHTML', '');
                document.getElementById("zpusob_doruceni").innerHTML=msg.html_doruceni;
                $('#zpusob_doruceni').attr('innerHTML', msg.html_doruceni);
                if(!$.browser.msie) {
                    removeFirstOption("zpusob_doruceni");
                }
                //alert($('#zpusob_doruceni').attr('innerHTML'));
                document.getElementById("zpusob_doruceni").outerHTML=document.getElementById("zpusob_doruceni").outerHTML;
                */
                var zpusoby_platby = msg.zpusoby_platby;
                $('#zpusob_platby').find('option').remove();
                $.each(zpusoby_platby, function () {
                    $('#zpusob_platby').append(
                        $('<option></option>').val(this.id).html(this.html)
                    );
                });
                /*
                //$('#zpusob_doruceni').attr('innerHTML', msg.html_doruceni);
                document.getElementById("zpusob_platby").innerHTML=""+msg.html_platba;
                document.getElementById("zpusob_platby").outerHTML=document.getElementById("zpusob_platby").outerHTML;
                //$('#zpusob_platby').attr('innerHTML', msg.html_platba);
                */
                if(msg.show_info == 1) {
                    $('#info_box').attr('innerHTML', msg.text);
                    $('#zpusob_doruceni_p').css('display', 'none');
                    $('#zpusob_platby_p').css('display', 'none');
                } else {
                    $('#info_box').attr('innerHTML', '');
                    $('#zpusob_doruceni_p').css('display', 'block');
                    //$('#zpusob_platby_p').css('display', 'block');
                }
                return false;
            }
        });
    } else {
        $('#zpusob_doruceni_p').css('display', 'none');
        $('#zpusob_platby_p').css('display', 'none');
    }
    return false;
}

function changeZpusobPlatby() {
    var id = parseInt($('#zpusob_doruceni').val());
    if(id > 0) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: _SERVER_PATH+"inc/ajax/platba.php?action=getZpusobyPlatby",
            data: "&idd="+id,
            success: function(msg) {
                $('#zpusob_platby_p').css('display', 'block');

                var zpusoby_platby = msg.zpusoby_platby;

                $('#zpusob_platby').find('option').remove();

                $.each(zpusoby_platby, function () {
                    $('#zpusob_platby').append(
                        $('<option></option>').val(this.id).html(this.html)
                    );
                });
                /*
                document.getElementById("zpusob_platby").innerHTML=""+msg.html_platba;
                if(!$.browser.msie) {
                    removeFirstOption("zpusob_platby");
                }
                document.getElementById("zpusob_platby").outerHTML=document.getElementById("zpusob_platby").outerHTML;
                */
                //$('#zpusob_platby').attr('innerHTML', msg.html_platba);
                return false;
            }
        });
    } else {
        $('#zpusob_platby_p').css('display', 'none');
    }
    
    return false;
}

$(document).ready(function() {
    $('#laguagesSwitcher').dialog({
        autoOpen: false,
        show: 'blind',
        hide: 'explode',
        modal: true
    });
    $("#languages a").click(function(event) {
        $('#laguagesSwitcher').dialog('open');
        event.preventDefault();
    });
    if(document.getElementById('new_registration_link')) {
        $('#new_registration_link').toggle(function() {
            $("#new_registration_div").animate({ height: 'show', opacity: 'show' }, 'slow');
	},function(){
            $("#new_registration_div").animate({ height: 'hide', opacity: 'hide' }, 'slow');
	});
        $('#forgotten_password_link').toggle(function() {
            $("#forgotten_password_div").animate({ height: 'show', opacity: 'show' }, 'slow');
	},function(){
            $("#forgotten_password_div").animate({ height: 'hide', opacity: 'hide' }, 'slow');
	});
        $("#login_form").validate({
            errorLabelContainer: $("#login_form div.error_container")
	});
        $("#registration_form").validate({
            errorLabelContainer: $("#registration_form div.error_container")
	});
        $("#forgotten_password_form").validate({
            errorLabelContainer: $("#forgotten_password_form div.error_container")
	});
    } else {
        if(document.getElementById('registration_form')) {
            $("#registration_form").validate({
                errorLabelContainer: $("#registration_form div.error_container")
            });
        }
    }
    if(document.getElementById('form_doruceni')) {
        $("#form_doruceni").validate({
            errorLabelContainer: $("#form_doruceni div.error_container")
        });
    }
    if(document.getElementById('reklamace_form')) {
        $("#reklamace_form").validate({
            errorLabelContainer: $("#reklamace_form div.error_container")
	});

        var options = {
            'maxCharacterSize': 90,
            'originalStyle': 'originalTextareaInfo',
            'warningStyle' : 'warningTextareaInfo',
            'warningNumber': 80,
            'displayFormat' : _WORDS_LEFT
        };
        $('#popis_zavady').textareaCount(options);
        $('#baleni').textareaCount(options);
    }
    if(document.getElementById('opravy_form')) {
        $("#opravy_form").validate({
            errorLabelContainer: $("#opravy_form div.error_container")
	});
    }
    if(document.getElementById('opravy_form_step_2')) {
        $("#opravy_form_step_2").validate({
            errorLabelContainer: $("#opravy_form_step_2 div.error_container")
	});
    }
    if(document.getElementById('reklamace_form_step_2')) {
        $("#reklamace_form_step_2").validate({
            errorLabelContainer: $("#reklamace_form_step_2 div.error_container")
	});
    }
    if (document.getElementById('rizikova_mista')) {
        $("#rizikova_mista").validate({
            errorLabelContainer: $("#rizikova_mista div.error_container")
	});
    }
    
    // Je to kontaktni formular
    if(document.getElementById('d_checkbox')) {
        $('#d_checkbox').click(function() {
            if($('#d_checkbox').attr('checked')) {
                $("#jina_adresa_div").animate({ height: 'show', opacity: 'show' }, 'slow');
            } else {
                //$('#d_jmeno').val('');
                //$('#d_ulice').val('');
                //$('#d_mesto').val('');
                //$('#d_psc').val('');
                $("#jina_adresa_div").animate({ height: 'hide', opacity: 'hide' }, 'slow');
            }
	});
        $("#form_kontakt").validate({
            errorLabelContainer: $("#form_kontakt div.error_container")
	});
    }
    if(document.getElementById('carousel')) {
        $('#carousel').carouFredSel(
            {
                visibleItems		: 3,
                scrollItems		: 1,
                scrollItemsButtons	: 1,
                scrollEffect		: 'swing',
                scrollSpeed		: 1600,
                direction		: 'left',
                next			: $('#carouselPrevButt'),
                prev			: $('#carouselNextButt'),
                pauseDuration		: 5000,
                pauseOnHover		: true
            }
        );
    }
    if (document.getElementById('leftMenu')) {
        $("#leftMenu").treeview({
            animated: "fast",
            collapsed: true,
            unique: false,
            persist: "location"
	});
    }

    if (document.getElementById('banner')) {
        // redefine Cycle's updateActivePagerLink function
        $.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
            $(pager).find('a').removeClass('active')
                .filter('a:eq('+currSlideIndex+')').addClass('active');
        };

        $('#banners').cycle({
            timeout: 8000,
            speed:  1600,
            pager:  '.bannerSwitchersHolder',
            pagerAnchorBuilder: function(idx, slide) {
                return '<a href="#" class="bannerSwitcher">' + (idx+1) + '</a>';
            }
        });
    }

});
