$(document).ready(function() {
    // overirani odkazu v novem okne
    $("a[rel^='newWindow']").attr('target','_blank');
    
    // Notice the use of the each method to gain access to each element individually
    $('a.qtip').each(function()
    {
        // Create image content using websnapr thumbnail service
        var content = '<iframe src="';
        content += $(this).attr('href');
        content += '" />';

        // Setup the tooltip with the content
        $(this).qtip(
        {
            content: content,
            position: {
                corner: {
                    tooltip: 'bottomMiddle',
                    target: 'topMiddle'
                }
            },
            style: {
                tip: true, // Give it a speech bubble tip with automatic corner detection
                name: 'light',
                width: 500,
                height: 210
            }
        });
    });

    /*$('a.lightbox').nyroModal();*/
    $(".yox").yoxview({
        lang: 'cs'
    });
    $("a[href$='jpg']").yoxview({
        lang: 'cs'
    });
  
    /*** ajax hash url **/
    // save AJAX state .ajaxhash
    var processAjax = true;
    var domain = 'http://' + document.location.host;
    var clicked = false;

    $("a.ajaxhash").live("click", function (event) {
        url = this.href.substring(domain.length, this.href.length);
        $.bbq.pushState('#!' + url);
        processAjax = false;
        event.preventDefault();
        clicked = true;
        $.get(this.href);

        // zobrazení spinneru a nastavení jeho pozice
        $("#ajax-spinner").show().css({
            position: "absolute",
            left: 307,
            top: 210
        });

    });

    // reload page from ajax state
    $(window).bind("hashchange", function(e) {
        if (!clicked) {
            loaded = false;
            url = '';
            for(key in $.bbq.getState()) {

                if ($.bbq.getState()[key] == '') {
                    url = key.substring(1, key.length);
                } else if (url == '') {
                    url = key.substring(1, key.length) + '=' + $.bbq.getState()[key];
                } else {
                    url = url + '&' + key.substring(0, key.length) + '=' + $.bbq.getState()[key];
                }
            }

            if (!processAjax || url != '') {
                if (url == undefined) {
                    $.get(location.href);
                } else {
                    $.get(domain + url);
                }
            }
            processAjax = false;
            loaded = true;

            if (!processAjax && !loaded) $.get(location.href);
        }
        clicked = false;

    });
    // load ajax state
    $(window).trigger( "hashchange" );

    $('a').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).addClass('external');

    /*** menu ***/
    $('#menu ul li').mouseover(function() {
        $(this).stopTime("menu_close");
        $(this).oneTime(30, "menu_open", function() {
            $(this).children('ul').slideDown(100);
        });
    });
  
    $('#menu ul li ul li').mouseover(function() {
        $(this).parent().parent().find('a').first().addClass('hover');
    });
  
    $('#menu ul li ul li').mouseout(function() {
        $(this).parent().parent().find('a').first().removeClass('hover');
    });
  

    $('#menu ul li').mouseout(function() {
        $(this).parent().parent().find('a').removeClass('hover');
        $(this).stopTime("menu_open");
        $(this).oneTime(150, "menu_close", function() {
            $(this).children('ul').slideUp(50);
        });
    });
  
  
    /*** vyhledávání ***/
  
    $('.q').bind('click', function() {
        if ($(this).val() == 'Vyhledat konkrétní vůz') {
            $(this).val('');
        } 
        return false;
    });
    
    //odstranění popisku inputu
    $('.q').bind('blur', function() {
        if ($(this).val() == '') {
            $(this).val('Vyhledat konkrétní vůz');
        }
        return false;
    });


    /*** ajaxové formuláře ***/

    $(".onchange").live('change', function () {
        $(this).parent().parent().ajaxSubmit();
        return false;
    });

    // odeslání na formulářích
    $("#list_header_filters form, form.ajaxsubmit").live('submit', function () {
        $(this).ajaxSubmit();
        return false;
    });
  
    $("#frmfiltersForm-order_by, #frmfiltersForm-cars_per_page").live('change', function() {
        $(this).parent().parent().ajaxSubmit();
        return false;
    });

    // odeslání pomocí tlačítek
    $("#list_header_filters form :submit, form.ajaxsubmit :submit").live('click', function () {
        $(this).ajaxSubmit();
        return false;
    });


    $(function () {
        // vhodně nastylovaný div vložím po načtení stránky
        $('<div id="ajax-spinner"></div>').appendTo("#content_right").ajaxStop(function () {
            // a při události ajaxStop spinner schovám a nastavím mu původní pozici
            $(this).hide().css({
                position: "fixed",
                left: "50%",
                top: "50%"
            });
        }).hide();
    });

    // zajaxovatění odkazů
    $("a.ajax").live("click", function (event) {
        event.preventDefault();

        $.get(this.href);

        // zobrazení spinneru a nastavení jeho pozice
        $("#ajax-spinner").show().css({
            position: "absolute",
            left: 307,
            top: 210
        });
    });

    // tisk stranky
    $('#print, #tisk, #comparison_print, #ltisk').click(function() {
        window.print();
        return false;
    });

    // oblibene
    $('#oblibene, #bookmark').click(function() {
        title = document.title;
        url = window.location.href;

        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url,"");
        } else if( window.external ) { // IE Favorite
            window.external.AddFavorite( url, title);
        }
        else if(window.opera && window.print) { // Opera Hotlist
        }    
        return false;
    })

    $('#frmfiltersForm-down').live('click', function() {
        $('#frmfiltersForm-order_by_clicked').val('down');
    });

    $('#frmfiltersForm-up').live('click', function() {
        $('#frmfiltersForm-order_by_clicked').val('up');
    });
  
  
    $('#hp_fast_choice_select a').live('click', function() {
        $('#hp_fast_choice_select a').removeClass('active');
        $(this).addClass('active');
     
        $('#fast_choice_1_content').css('display', 'none');
        $('#fast_choice_2_content').css('display', 'none');
        $('#fast_choice_3_content').css('display', 'none');
     
        $('#' + $(this).attr('id') + '_content').css('display', 'block');
     
        return false;
    });
    
    $('#hp_fast_choice_select_graphic a#fast_choice_1g').live('click', function() {
        $('#fast_choice_1_content').css('display', 'block');
        $('#fast_choice_2_content').css('display', 'none');
        $('#fast_choice_3_content').css('display', 'none');
        
        $('a#fast_choice_1g').removeAttr('class');
        $('a#fast_choice_2g').removeAttr('class');
        $('a#fast_choice_3g').removeAttr('class');
        
        $('a#fast_choice_1g').addClass('active-1');
        $('a#fast_choice_2g').addClass('active-2-a-n');
        $('a#fast_choice_3g').addClass('active-3-n-n');
        
        return false;
        
    });
    
    $('#hp_fast_choice_select_graphic a#fast_choice_2g').live('click', function() {
        $('#fast_choice_1_content').css('display', 'none');
        $('#fast_choice_2_content').css('display', 'block');
        $('#fast_choice_3_content').css('display', 'none');
        
        $('a#fast_choice_1g').removeAttr('class');
        $('a#fast_choice_2g').removeAttr('class');
        $('a#fast_choice_3g').removeAttr('class');
        
        $('a#fast_choice_1g').addClass('active-1-n-a');
        $('a#fast_choice_2g').addClass('active-2');
        $('a#fast_choice_3g').addClass('active-3-a-n');
        
        return false;
        
    });

    $('#hp_fast_choice_select_graphic a#fast_choice_3g').live('click', function() {
        $('#fast_choice_1_content').css('display', 'none');
        $('#fast_choice_2_content').css('display', 'none');
        $('#fast_choice_3_content').css('display', 'block');
        
        $('a#fast_choice_1g').removeAttr('class');
        $('a#fast_choice_2g').removeAttr('class');
        $('a#fast_choice_3g').removeAttr('class');
        
        $('a#fast_choice_1g').addClass('active-1-n-n');
        $('a#fast_choice_2g').addClass('active-2-n-a');
        $('a#fast_choice_3g').addClass('active-3');
        
        return false;
        
    });

  
    $('#close_window').live('click', function() {
        $('#comparison_window').css('display', 'none');
        return false; 
    });

    $('#left_search_block select').each(function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#left_search_block select').live('change', function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#left_search_block select').live('change', function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#left_search_block .value').live('click', function() {
        $(this).parent().find('select').click();
    });
  
  
    $('.fast_choice_img').live('mouseover', function() {
        $(this).children('.fast_choice_img_over').show();
    });
  
    $('.fast_choice_img').live('mouseout', function() {
        $(this).children('.fast_choice_img_over').hide();
    });
  
    $('a.porovnani').live('click', function() {
        $('html, body').animate({
            scrollTop: 0
        }, 1000); 
    });
  
    $('#snippet--requestForm .select select').each(function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#snippet--requestForm .select select').live('change', function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#snippet--requestForm .select select').live('change', function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#snippet--requestForm .select .value').live('click', function() {
        $(this).parent().find('select').click();
    });
    
    $('#snippet--offerForm .select select').each(function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#snippet--offerForm .select select').live('change', function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#snippet--offerForm .select select').live('change', function() {
        $(this).next('.value').html($(this).find("option:selected").text());
    });
  
    $('#snippet--offerForm .select .value').live('click', function() {
        $(this).parent().find('select').click();
    });
    
    $('#content_right .paginator a').live('click', function() {
        $('html, body').animate({
            scrollTop: 0
        }, 500);    
    });    

});
