﻿(function() { 
    var $ = jQuery;
    
    // OnDocumentReady
    $(function() {
        var styles = 
        {
            width: '6px',
            height: '6px',
            position: 'absolute',
            top: '0',       
            left: '0',
            background: 'url("/images/round-tl.gif") top left no-repeat',     
            overflow: 'hidden',
            zIndex: 100
        }
        
        
        // Round corners - menu
        $('#menu-main li')
            .prepend('<div class="round-tl"></div><div class="round-tr"></div>')
            .css('position', 'relative')
            .children('.round-tl').css(styles)
            .end()
            .children('.round-tr').each(function() {
                var tr = $(this);
                tr.css($.extend(styles, {
                    left: (tr.parent().width()-6)+'px',
                    background: 'url("/images/round-tr.gif") top right no-repeat'
                }));
            });

        // Round corner - heading
        $('#heading')
            .prepend('<div class="round-tl"></div>')
            .css('position', 'relative')
            .children('.round-tl').css($.extend(styles, {
                    top: '-1px',
                    left: '-1px',
                    background: 'url("/images/round-tl.gif") top left no-repeat'
                }));
         
        
        // Rounded corners - courses & lanaguages menu
        $('#courses-languages, #courses-tags, #exams-date-order-details').corner('15px');
        $('#courses-languages-inner, #courses-tags-inner, #exams-date-order-details-inner').corner('12px');
        
        
        $('#exams-date-order input, #exams-date-order textarea')
            .bind('focus', function() {
                var id = $(this).addClass('active').attr('id');
                $('#exams-date-order label[@for='+id+']').addClass('active');
            })
            .bind('blur', function() {
                var id = $(this).removeClass('active').attr('id');
                $('#exams-date-order label[@for='+id+']').removeClass('active');
            });
            
        
            
        
        // Exam date order validation 
        $('#exams-date-order form').bind('submit', function() {
            
            var ids = ['#name', '#surname', '#birth_date', '#birth_place', '#region', '#nationality', '#finished_schools', '#domicile', '#postal_code', '#email', '#phone']
            
            for (var i in ids) {
                if ($(ids[i]).val() == "") {
                    $(ids[i]).get(0).focus();
                    alert('Pro odeslání přihlášky vyplňte prosím pole.');
                    return false;
                }
            }
            
            if ($('#exams-date-order #passed_exams').val() == "") {
                var message =

                "Pokud chcete skládat Státní jazykovou zkoušku tlumočnickou nebo překladatelskou, \
musí být položka 'Dosud složené jazykové zkoušky' vyplněna\n\n Opravdu odeslat?";
                
                var ret = confirm(message);
                if (!ret) {
                    $('#exams-date-order #passed_exams').get(0).focus();
                    return false;
                }
            }
        });
        
        // Focus form elements
        $('#validation-errors a').click(function() {
            var id = $(this).attr('href').substr(1);
            $('#'+id).get(0).focus();
            return false;
        });
        
        // Table rows mouseover and click handling
        $('#courses-list table tbody tr')
            .mouseover(function() {
                $(this).addClass('over');
            })
            .mouseout(function() {
                $(this).removeClass('over');
            })
            .click(function() {
                window.location = $(this).find('a:nth(0)').attr('href');
            });
        
        $('#courses-list table tbody td .tooltip').hide()
            .wrap('<div class="tooltip-wrapper"></div>')
            .parent().parent().addClass('has-tooltip')
            .mouseover(function() {
                $(this).find('.tooltip').show();
            })
            .mouseout(function() {
                $(this).find('.tooltip').hide();
            });
            
        
        // Eshop form
        $('#eshop input[@type=checkbox]')
            .change(function() {
                if($(this).is('[@checked]')) {
                    $(this).siblings('input[@type=text]').attr('disabled', '');
                } else {
                    $(this).siblings('input[@type=text]').attr('disabled', 'disabled');
                }
            })
            .change();
        /*
        
        
        
        
        
        
        
        $('#courses-list thead a, .pager a').click(function() {
            $('#courses-list tbody').load($(this).attr('href'));
            $('#courses-list h2').ajaxStart(function() {
                $(this).css('background', 'url("/images/loading.gif") no-repeat 100% 50% #F1F6FF');
            });
            $('#courses-list h2').ajaxStop(function() {
                $(this).css('background', '#fff');
            });
            return false;
        });
        */

    });

})();