$(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' || url == 'h-1-van') {
            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 );
    });

    // 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').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) );
    });
    
    
    // date picker CZ
    if( $.datepicker ) {
    	$.datepicker.regional['sk'] = {
    		closeText: 'Zavrieť',
    		prevText: '&#x3c;Predchádzajúci',
    		nextText: 'Nasledujúci&#x3e;',
    		currentText: 'Dnes',
    		monthNames: ['Január','Február','Marec','Apríl','Máj','Jún','Júl','August','September','Október','November','December'],
    		monthNamesShort: ['Jan','Feb','Mar','Apr','Máj','Jún','Júl','Aug','Sep','Okt','Nov','Dec'],
    		dayNames: ['Nedel\'a','Pondelok','Utorok','Streda','Štvrtok','Piatok','Sobota'],
    		dayNamesShort: ['Ned','Pon','Uto','Str','Štv','Pia','Sob'],
    		dayNamesMin: ['Ne','Po','Ut','St','Št','Pia','So'],
    		weekHeader: 'Ty',
    		dateFormat: 'd. m. yy',
    		firstDay: 1,
    		isRTL: false,
    		showMonthAfterYear: false,
    		yearSuffix: '',
            constrainInput: false
        };
        
    	$.datepicker.setDefaults($.datepicker.regional['sk']);

        // init
        $('input.datepicker, .datepicker input').datepicker({ 
            minDate: '+1D',
            maxDate: '+2M',
    		numberOfMonths: 3,
    		showButtonPanel: true
        });        

        // april-service
        $('#april-service input[name=date]').datepicker({ 
            minDate: '+1D',
            maxDate: new Date( 2011, 4, 31 ),
    		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="#">Zobraziť 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], a[rel=gallery]').fancybox({
        titlePosition: 'over'
    });


    // 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('.', ',');
    };
});

/**
 * Replace the banner for pictures
 */
function replaceBanner( ident ) {
    position = $(''+ident);        
    altImage = $(''+ident+' .altImage').html();
    position.html('');
    if(altImage) {
        $(''+ident+' .altImage img').css('border', '0px');
        $(''+ident+' .altImage').css('display','block');
        position.html( altImage );
    }
}


