$(document).ready( function() {
    /* menu fixor */
    
    // generate footer boxes
    for( i = 0; i < 6; i++ ) {
        $('#rich-foot').append('<div class="box"></div>');
        $('#rich-foot > div:eq('+i+')').append('<h2></h2>');

        var head = $('#main-menu > ul > li:eq('+i+') > a').clone();
        head.appendTo('#rich-foot > div:eq('+i+') > h2');

        if( $('#main-menu > ul > li:eq('+i+') > ul').size() > 0 ) {
            var content = $('#main-menu > ul > li:eq('+i+') > ul').clone();
            content.appendTo('#rich-foot > div:eq('+i+')');
        }
    }
    
    // fillout konfigurator models
    $('#main-menu > ul > li:eq(1)').append('<ul></ul>');
    $('#rich-foot > div.box:eq(1)').append('<ul></ul>');
    $('#main-menu > ul > li:eq(0) > ul > li').each( function() {
        var name = $(this).children('a').html();
        var url = $(this).children('a').attr('href');
        url = url.split('/');
        url = url[2];
        
        // skipped urls
        if( url == 'archiv' ) {
            return true;
        }
        
        var content = '<li><a href="/konfigurator/model/?selectedModel='+url+'">'+name+'</a></li>';
        $('#rich-foot > div.box:eq(1) > ul').append( content );
        $('#main-menu > ul > li:eq(1) > ul').append( content );
        
        // novinka in konf
        //if( url == 'i10' || url == 'i40' || url == 'genesis-coupe' || url == 'elantra' || url == 'veloster' ) {
        if( url == 'i40' || url == 'elantra' || url == 'veloster' || url == 'i40-kombi' || url =='i40-sedan'|| url =='nova-i30' ) {
            $('#main-menu > ul > li:eq(1) > ul > li:last > a').append(' <span>NOVINKA</span>');
        }
    });

    // mark menu items with children
    $('#main-menu > ul > li').each( function() {
        if( $(this).children('ul').size() > 0 ) {
            $(this).addClass('group');
        }
    });
    
    // grey out last 2
    var count = $('#main-menu > ul > li').size() - 3;
    $('#main-menu > ul > li:gt('+count+')').addClass('minor');


    // extended model menu
    if( $('#model-menu').size() > 0 ) {
        $('#main-menu > ul > li:eq(0) > ul').remove();
//        $('#main-menu > ul > li:eq(0)').css({
//            'position': 'static'
//        });
        $('#model-menu')
            .css({
                'top': '35px',
                'left': '-173px'
            })
            .prependTo('#main-menu > ul > li:eq(0)');
    }


    // add hover defect
    $('#main-menu > ul > li[class*="group"] > ul, #main-menu > ul > li[class*="group"] > div#model-menu, #main-menu > ul > li[class*="group"] > a').hover( function() {
        $(this).parent('li').addClass('hover');
    }, function() {
        $(this).parent('li').removeClass('hover');
    });
    
    
    // move <a> in front of <ul>, no more z-index issue
    $('#main-menu > ul > li[class*="group"]').each( function() {
        $(this).children('a').appendTo( $(this) );
    });
    
    
    // "novinka" tag in menu
    $('#main-menu > ul > li:eq(0) > ul > li').each( function( i, v ) {
        var url = $(this).children('a').attr('href');
        url = url.split('/');
        url = url[2];
        
        if( url == 'i10' || url == 'i40' || url == 'genesis-coupe' || url == 'elantra' || url == 'veloster' || url == 'i40-kombi' || url == 'i40-sedan' ) {
            $(this).children('a').append(' <span>NOVINKA</span>');
        }
    });


    // date picker CZ
    if( $.datepicker ) {
    	$.datepicker.regional['cs'] = {
    		closeText: 'Zavřít',
    		prevText: '&#x3c;Dříve',
    		nextText: 'Později&#x3e;',
    		currentText: 'Nyní',
    		monthNames: ['leden','únor','březen','duben','květen','červen','červenec','srpen','září','říjen','listopad','prosinec'],
    		monthNamesShort: ['led','úno','bře','dub','kvě','čer','čvc','srp','zář','říj','lis','pro'],
    		dayNames: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek', 'sobota'],
    		dayNamesShort: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
    		dayNamesMin: ['ne','po','út','st','čt','pá','so'],
    		weekHeader: 'Týd',
    		dateFormat: 'd. m. yy',
    		firstDay: 1,
    		isRTL: false,
    		showMonthAfterYear: false,
    		yearSuffix: '',
            constrainInput: false 
        };
        
    	$.datepicker.setDefaults($.datepicker.regional['cs']);

        // init
        $('input.datepicker, .datepicker input').datepicker({ 
            minDate: '+1D',
            maxDate: '+2M',
    		numberOfMonths: 3,
    		showButtonPanel: true
        });        
    }


    // auto default values
    $(".auto-defaults textarea[title], .auto-defaults input[type=text][title], input.auto-defaults[title]").each( function(index) {
        if( '' == $(this).val() || $(this).attr('title') == $(this).val() ) {
            $(this).val( $(this).attr('title') );
            $(this).addClass('default');
        }
        $(this).focus( function() {
            if( $(this).attr('title') == $(this).val() ) {
                $(this).val('');
                $(this).removeClass('default');
            } 
        });
        $(this).blur( function() {
            if( '' == $(this).val() ) {
                $(this).val( $(this).attr('title') );
                $(this).addClass('default');
            } 
        });
    });
    
    
    // stupid roboforms fillout also the spam field
    $('form.mk1 button[type="submit"]').mouseover( function() {
        $('form.mk1 input[name="email"]').val('');
    });
    
    
    // form model list
    $('form.mk1 dl.models dd:nth-child(3n+2)').css('clear','both');
 
 
    // shortening legal note
    $('p.legal-note[title]').each( function() {
        var shortNote = $(this).attr('title') + ' <a href="#">Zobrazit celý text</a>';
        $(this).html('<span style="display: none;">' + $(this).html() + '</span>'); 
        $(this).append( shortNote );
        $(this).children('a:last-child').click( function() {
            $(this).parent().html( $(this).parent().children('span').html() );
            return false;
        });
    });
    
    
    // form auto hide
    if( $('#formular-trigger').size() == 1 && $('#formular').size() == 1 ) {
        var urlHash = window.location.hash;
        if( urlHash != '#formular' ) {
            $('#formular-trigger').show();
            $('#formular-trigger').click( function() {
                $('#formular').show();
                $(this).hide();
                window.location.hash = 'formular';
            });
            $('#formular').hide();            
        }
    }
    
    
    // table rowizer
    $('table.rowizer tr:nth-child(2n+1)').addClass('bgcolor');


    // iWhatever detect ??
    var isiPad = navigator.userAgent.match(/iPad/i) != null; 	
    var isiPhone = navigator.userAgent.match(/iPhone/i) != null; 	
    var isiPod = navigator.userAgent.match(/iPod/i) != null;    


    // collapser ??
    $('body').collapser({
        autoClose: true,
        duration: 300
    });
    
    
    // fancybox
    $('a.gallery, a[rel=thumbnail]').fancybox({
    
    });


    // konf. last step to send mails
    $('a[rel=konf-ilayer]').fancybox({
        scrolling: 'no',
        width: 628,
        height: 300,
        padding: 30,
        autoScale: false,
        autoDimensions: false,
        showNavArrows: false,
        type: 'inline'
    });
    

    // selected plugin ??
    $('.model-color').selected();
    
    $('#models, #models-left, #models-right').selected({
        target: 'a',
        addedClass: 'selected',
        number: 1
    });
    
    $('#variants, #motors, #interier').selected({
        target: 'li.mark',
        addedClass: 'selected'
    });
    
    $('#wheels, #colors').selected({
        target: 'tr',
        addedClass: 'selected'
    });
    
    $('.accessories').selected({
        target: 'tr',
        addedClass: 'selected',
        number: 0
    });
    
    $('#pomocnik').selected({
        target: 'li',
        addedClass: 'selected',
        number: 2
    });


    // konfigurator ??
    var cont = $('#konf-content').height() + parseInt($('#konf-content').css('padding-top')) + parseInt($('#konf-content').css('padding-bottom'));
    var nextTop = $('.konf-next-top').height() + parseInt($('.konf-next-top').css('padding-top')) + parseInt($('.konf-next-top').css('padding-bottom'));
    var nextBottom = $('.konf-next-bottom').height() + parseInt($('.konf-next-bottom').css('padding-top')) + parseInt($('.konf-next-bottom').css('padding-bottom'));
    
    $('.konf-panel-text').height(cont - (nextTop + nextBottom));


    // safety ??
    $('#safety').safety();
    
    
    // tabs ??
    $('.tabs').tabs();


    // click toggle box ??
    $('.click-box').hide();
    $('.click').click( function() {
        $(this).next('.click-box').toggle(250);
        return false;
    });


    // price format ??
    $.fn.priceFormat = function() {
        return $(this).text().replace('.', ',');
    };
});


