$(document).ready(function() {
    
    /* tabs system */
    $('.tabs-container .tabs-captions li a').bind('click', function() {
        if ($(this).is('.allow-click')) {
            return true;
        } else {
            var prnt = $(this).parents('.tabs-container');

            // KOREL - odstraneni Bertova triku
            var href = $(this).attr('href').replace(/#a-/, "#");

            // odstranim focus z IE a FF
            this.hideFocus = true; 
            $(this).blur();
            
            // vynulujem/schovam taby a fieldsety
            $('.tabs-captions li a', prnt).removeClass('open');     
            $('fieldset', prnt).removeClass('open').hide(); 

            // otvorim tab a fieldset
            $(href).add($(this)).addClass('open').show(); 
                 
            return false;
        }
    });
    
     /* zebra tables, tabs navigation table */
    $('table.with-ruling-row tbody tr').each(function() {
        var link = $('a:first', $(this)).attr('href');

        if (link !== '' && link !== undefined) {
            $(this).css('cursor', 'pointer').bind('click', function () {                    
                if(event.which != 2) { /* v Chrome udalost click generovalo i prostredni tlacitko - pro nas nevhodne */
                    document.location.href = link;
                }
            });
        }

        $(this)
        .mouseover(function() {$(this).addClass('over');})
        .mouseout(function() {$(this).removeClass('over');});
    });

    /* table selectable headers */
    $('table.common').each(function() {
        var table       = $(this),
            select_box  = null;

        // vytvorim select boxy
        table.find('th.sw').each(function() {
            select_box = doComboBox($(this), '.sw', select_box, table);
        });

        // vyberam prednastaveny stlpec z roletiek
        $("option[value='" + $('td.sw.open', table).attr('rel') + "']", select_box).attr('selected', 'selected');
        select_box.triggerHandler('change');
    });
    
    
    /* popup div with overlay */
    $('.trigger-popup-overlay').bind('click', function() {         
        var rel = $(this).attr('rel');  // zjisteni id zdroje
        var $el_src = $('#'+rel);
        var $el_prnt = $el_src.parent();
        
        // Hide some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
        $('embed, object').css({'visibility' : 'hidden'});       
        
        // zobazeni overlay
        $("<div id='jquery-overlay'></div>").css('opacity', '0.8').appendTo('body');             
        
        // vytvoreni popup boxiku        
        var $el_popup = $("<div id='popup-overlay'></div>").append( $el_src );               
        
        // nastaveni pozice popup boxiku
        var arrPageScroll = getPageScroll();
        var arrayWindowSize = getWindowSize();

        $($el_popup).css({
				    top:	arrPageScroll[1] + arrayWindowSize[1] / 10
			  });
			  
        // vlozeni popup boxiku do body
			  $('body').append($el_popup);
        
        // zavreni okna
        $('#popup-overlay').find('.close-popup-overlay').bind('click', function() {
            
            $el_src.appendTo($el_prnt);
            $('#jquery-overlay, #popup-overlay').remove();
            $('embed, object').css({'visibility' : 'visible'});   
            
            // pokud obsahoval nejake warningy odstranit
            if(('p.warning', $el_src).size() > 0) {
                $('.label-error', $el_src).removeClass('label-error');
                $('.element-error', $el_src).removeClass('element-error');
                $('p.warning', $el_src).remove();
            }
            return false; 
        });     
        
        return false;
    });
    
    /* druh kurzu - kratke info */
    $('#kurz').bind('change', function() {
        var tmp = $('option:selected', $(this));

        $('#druh-perex').hide('fast');
        
        if (tmp.val() != '') {
            $('#druh-perex').html($('#kurz-' + tmp.val()).html()).show('fast');
        } 
    });    

    /* sprava chovani formu */
    $('#poptavka-zahranicni-skola, #poptavka-agentura').each(function() {
        
        $el_chkbx = $('input#prace', this);
        if(!$el_chkbx.is(':checked')) {
            $('.typ-prace, .zivotopis').hide();
        }    
        
        // skryti/zobrazeni dat podle stavu checkboxu
        $el_chkbx.bind('change', function() {
            if( $(this).attr('checked') )
                 $('.typ-prace, .zivotopis').show();
            else
                $('.typ-prace, .zivotopis').hide();     
        });
        
         // date picker
        $('#datumprijezdu, #datumodjezdu')
        .datePicker({clickInput : true, showYearNavigation : true})
        .dpSetStartDate('01.01.2000')
        .dpSetOffset(23, 0)
        .dpSetPosition($.dpConst.POS_TOP, $.dpConst.POS_RIGHT);
        
        // vyvolani vyobrazeni formulare pri chybnem odeslani
        if($('p.warning', this).size() > 0) {                  
            $('.trigger-popup-overlay').eq(0).trigger('click');
        }
        
        // vyvolani potvrzeni odeslani formulare (pokud existuje)
        $('#potvrzeni-trigger').trigger('click');

    });
    

});

/* GDI texts */
                   
$.gdiText =  {
	TOGGLE_DETAIL : 'Kliknutím zobrazíte/skryjete podrobnosti',
	HIDE_DETAIL   : 'Kliknutím skryjete podrobnosti',
	UNSAVED_DATA  : 'Na stránce jsou zadaná nová neuložená data. Uložte je tlačítkem "Uložit zadané údaje" nebo o ně přijdete.',
	ENTER_TEXT    : 'Zadejte text',
	HIDDEN_TEXT   : 'Editovaná položka nebude v prezentaci firmy zobrazena. Zobrazíte ji zaškrtnutím k ní příslušejícího čtverečku.',
	ERROR_SEND    : 'Při odeslání žádosti došlo k chybě.'
}; 

$.dpText = {
	TEXT_PREV_YEAR	:	'Předešlý rok',
	TEXT_PREV_MONTH	:	'Předešlý měsíc',
	TEXT_NEXT_YEAR	:	'Další rok',
	TEXT_NEXT_MONTH	:	'Další měsíc',
	TEXT_CLOSE		:	'Zavřít',
	TEXT_CHOOSE_DATE:	'Vyberte datum'
};

Date.dayNames       =   ['Neděle','Pondělí','Úterý','Středa','Čtvrtek','Pátek','Sobota'];
Date.abbrDayNames   =   ['Ne','Po','Út','St','Čt','Pá','So'];
Date.monthNames     =   ['Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec'];
Date.abbrMonthNames =   ['Led','Úno','Bře','Dub','Kvě','Črv','Čvc','Srp','Zář','Říj','Lis','Pro'];
Date.firstDayOfWeek =   1;
Date.format         =   'dd.mm.yyyy';


/* ---------- funkcie ----------- */

// Return an array with x,y page scroll values
function getPageScroll() {
    var xScroll, yScroll;
		
    if (self.pageYOffset) {
			  yScroll = self.pageYOffset;
			  xScroll = self.pageXOffset;
			  
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			  yScroll = document.documentElement.scrollTop;
			  xScroll = document.documentElement.scrollLeft;
			  
		} else if (document.body) {// all other Explorers
			  yScroll = document.body.scrollTop;
			  xScroll = document.body.scrollLeft;	
		}
		
    arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
}

// Return an array with window width, height
function getWindowSize() {
    var windowWidth, windowHeight;
		
    if (self.innerHeight) {	// all except Explorer
  			if(document.documentElement.clientWidth){
  				  windowWidth = document.documentElement.clientWidth; 
  			} else {
  				  windowWidth = self.innerWidth;
  			}
  			windowHeight = self.innerHeight;
  			
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
        
		} else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
		}
    
    var arrayWindowSize = new Array(windowWidth, windowHeight);
    return arrayWindowSize;	
}

// do combo box from table headers
function doComboBox(el, cls, sb, table) {
    if (el.is(cls)) {
        // text hlavicky
        var tmp = el.text();

        // vytvorim roletku, resp. doplnim udajmi + on-change, focus a blur
        if (sb == null) {
            sb = el.html('<select></select>').find('select', el)
                .bind('change', function() {
                    $('td' + cls, table).removeClass('open').hide();
                    $(cls + '[rel=' + $(this).val() + ']', table).addClass('open');
                })

                // vysvietenie roletky po jej oznaceni
                .blur(function() { $(this).removeClass('highlighted-options'); });

            if ($.browser.msie) {
                sb.mouseover(function() { $(this).addClass('highlighted-options'); });
            } else {
                sb.focus(function() { $(this).addClass('highlighted-options'); })
            }
        }

        // pridanie hlaviciek do roletky
        sb.append('<option value="' + el.attr('rel') + '">' + tmp + '</option>');

        // odstranim bunku s neotvorenou hlavickou
        if (!el.is('.open')) {
            el.empty().remove();
        }
    }

    return sb;
}
