var soundIsPlaying = true;

function ContShow () {
    $("#content").show();
    $("#footer").show();
    return false;
}

function ContHide () {
    $("#content").hide();
    $("#footer").hide();
    return false;
}

function CallCufon () {
    if (typeof userInfo != 'undefined' || /msie [6|7]/i.test(navigator.userAgent)) return;
    Cufon.set('fontFamily', 'Futura');
    Cufon.replace('h1, h2, h3, p.perex, span.number, #header #msg_box p');
    Cufon.replace('ul#main, ul#sub, .but_orange', {
        hover:true,
        hoverables: {
            a: true
        }
    });
}
function CallSwitchBubbleCufon() {
    if (typeof userInfo != 'undefined' || /msie [6|7]/i.test(navigator.userAgent)) return;
    Cufon.replace('#header #msg_box p');
}

function updateSwitchBubbleText(lang) {
    var texts = {
      "cz": {"int": "PŘEPNOUT NA INTERAKTIVNÍ ČÁST",    "inf": "PŘEPNOUT NA INFORMAČNÍ ČÁST"},
      "en": {"int": "SWITCH TO INTERACTIVE PART",       "inf": "SWITCH TO INFORMATION PART"},
      "pl": {"int": "PRZEJŚCIE DO SEKCJI INTERAKTYWNEJ",       "inf": "PRZEŁĄCZANIE DO SEKSCJI INFORMACYJNEJ STRONY INTERNETOWEJ"},
      "sk": {"int": "PREPNÚŤ NA INTERAKTIVNÚ ČASŤ",     "inf": "PREPNÚŤ NA INFORMAČNÚ ČASŤ"}
    };
    var text = '';
    if (GUI.active == 1) {
        text = texts[lang]['inf'];
    } else {
        text = texts[lang]['int'];
    }
    $('#header #msg_box').find('p').text(text);
    CallSwitchBubbleCufon();
}
function updateFlashLoadedStatus(lang) {
    var position = '0 0';
    if (lang == 'en') {
        position = '0 -80px';
    } else if (lang == 'pl') {
        position = '0 -160px';
    } else if (lang == 'sk') {
        position = '0 -240px';
    }
    $("#flash_loaded #msg").css('background-position', position);
}
function checkSoundVisibility() {
    if (soundIsPlaying) {
        $("#sound").removeClass('off').addClass('on');
    } else {
        $("#sound").removeClass('on').addClass('off');
    }
    if (GUI.active == 1) {
        $("#sound").show();
    } else {
        $("#sound").hide();
    }
}

function initLangDropMenu() {
    $('#header .lang').css({'right': '70px'});
    
    $('#langDropMenu').dropMenu();
    $('#langDropMenu').unbind('change').bind('change', onLangMenuChange);
    $('#langDropMenu').unbind('click').bind('click', onLangMenuClick);
    $('#langDropMenu').find('ul > li').each(function(){
        $(this).find('a').removeAttr('href');
    });
        
    updateLangDropMenuDirection();
}
function onLangMenuClick(e) {
    updateLangDropMenuDirection();
}
function onLangMenuChange(e, option) {
    // console.log($('#langDropMenu').dropMenu('selectedOption'));
    changeFlashLang(option);
    updateFlashLoadedStatus(option);
    updateSwitchBubbleText(option);
    GUI.load('/'+option);
}
function changeFlashLang(lang) {
    if (typeof thisMovie('flash') != 'undefined') thisMovie('flash').change_lang(lang);
}
function updateLangDropMenuDirection() {
    // if Flash is active, use drop-up instead of drop-down
    if (GUI.active == 1) {
        $('#langDropMenu').dropMenu('directionUp');
    } else {
        $('#langDropMenu').dropMenu('directionDown');
    }
}

$("#go2flash").click (function (){
    $("#switch").click();
    $("#flash_loaded").fadeOut('slow');
    return false;
});

$("#go2flash_close").click (function () {
    $("#flash_loaded").animate({
        'height': '1px'
    }, 500, null, function () {
        $(this).css({
            'display': 'none'
        });
    });
    $("#page_all").animate({
        'top': '0px'
    }, 500);
});

function flash_callback() {
    if (typeof thisMovie('flash') != 'undefined') {
        thisMovie('flash').flash_stop(true, 'pause_flash', {
            fromMenu:false,
            rollUp:true
        });
    }
}

function pause_flash(object) {

    if(typeof object.fromMenu != 'undefined' && object.fromMenu == true) {
        GUI.load(object.url, null, null, 'flash_callback();', 'no', 'yes');
        return;
    }

    if( typeof object.rollUp != 'undefined' && object.rollUp == true && !GUI.animating) {
        GUI.animating = true;

        ContShow();

        $('#page_all').animate({
            'top': 0
        }, {
            duration:500,
            complete: function(){
                $(this).css({
                    'bottom': null
                }).css({
                    'top':0
                });
//                $('#page_all').css('height', ($("#content").height() > 760)? $("#content").height() : 760);
                $('#page_all').css('height', ($("#page_all").height() > $(document).height())? $("#page_all").height() : $(document).height());
                GUI.animating = false;
            }
        });
        GUI.active = 2;
        updateLangDropMenuDirection();
        TIP.check();

        return;
    };
}


cGUI.prototype.parseLinks = function() {

    if (typeof userInfo != 'undefined') return;

    $('#content a[target!="_blank"], #footer a[target!="_blank"]').not('[href^="http"]').unbind('click');
    $('#content a[target!="_blank"], #footer a[target!="_blank"]').not('[href^="http"]').bind('click', function(){
        url = $(this).attr('href');
        if (($(this).attr('target') == '_blank' && $(this).attr('class') != 'lightbox') || url.substr(0, 7) == 'mailto:') {
            return true;
        }
        GUI.load_func_starter = 'yes';
        GUI.load(url, null, null, null, null, 'yes');
        return false;
    });
    
    $('#main a[target!="_blank"]').not('[href^="http"]').unbind('click');
    $('#main a[target!="_blank"]').not('[href^="http"]').bind('click', function(){
        if ($(this).parent().hasClass('active') && GUI.active == 1) {
            $('a#switch').trigger('click');
            return false;
        }
        if(GUI.active == 1) {
            url = $(this).attr('href');
            if($.browser.flash && typeof userInfo == 'undefined') {
                if (typeof thisMovie('flash') != 'undefined') {
                    thisMovie('flash').flash_stop(false, 'pause_flash', {
                        fromMenu:true,
                        url:url,
                        rollUp:false,
                        loading:true
                    });
                }
                //                return false;
                $("#switch").removeClass('down');
                $("#switch").addClass('up');
                TIP.check();
            } else {
                GUI.load(url, null, null, null, null, 'yes');
            }
        } else {
            url = $(this).attr('href');
            GUI.load(url, null, null, null, null, 'yes');
            $("#switch").removeClass('down');
            $("#switch").addClass('up');
            TIP.check();
        }
        return false;
    });
    $('a#switch').unbind('click');
    $('a#switch').bind('click', function(){
        //return false;
        if ((typeof thisMovie('flash') != 'undefined') && (typeof thisMovie('flash').flash_stop != 'function')) {
            setTimeout(function(){
                $('a#switch').trigger('click');
            }, 500);
            return false;
        }
        if (GUI.active == 1) {
            if($.browser.flash) {
                if (typeof thisMovie('flash') != 'undefined') {
                    thisMovie('flash').flash_stop(true, 'pause_flash', {
                        fromMenu:false,
                        rollUp:true
                    });
                }
                $("#header #sound").hide();
                $("#switch").removeClass('down');
                $("#switch").addClass('up');
                ContShow();
            } else {
                $("#switch").removeClass('down');
                $("#switch").addClass('up');
                GUI.active = 2;
                updateLangDropMenuDirection();
                TIP.check();
                $('#page_all').animate({
                    'top': 0
                }, {
                    duration:500,
                    complete: function(){
                        $(this).css({
                            'top':0
                        });
                    }
                });
            }
        } else {
            if (!GUI.animating) {
                $("#flash_loaded").fadeOut();
                if ($(window).width() < 940) {
                    $("#home_interactive").css('width', 940);
                    $("#home_interactive").css('height', $(window).height() - 90);
                } else {
                    $("#home_interactive").css('width', $(window).width());
                    $("#home_interactive").css('height', $(window).height() - 90);
                }
                $("#switch").addClass('down');
                $("#switch").removeClass('up');
                GUI.active = 1;
                updateLangDropMenuDirection();
                TIP.check();
                GUI.animating = true;
                $('#page_all').animate({
                    'top': $(window).height() - 90
                }, {
                    duration:500,
                    complete: function() {
                        $(this).css('height', '90px');
                        ContHide();
                        GUI.animating = false;
                        if (GUI.flashLoaded == true) thisMovie('flash').flash_stop(false);
                        $("#header #sound").show();
                    }
                });
            }
        }
        updateLangDropMenuDirection();
        return false;
    });

    $("a#sound").unbind('click').bind('click', function (){
        if ($.browser.flash) {
            var is_on = $(this).hasClass ('on');
            if (is_on) {
                $(this).removeClass ('on');
                $(this).addClass ('off');
                if (typeof thisMovie('flash') != 'undefined') thisMovie('flash').allSoundsEnabled(false);
                soundIsPlaying = false;
            } else {
                $(this).removeClass ('off');
                $(this).addClass ('on');
                if (typeof thisMovie('flash') != 'undefined') thisMovie('flash').allSoundsEnabled(true);
                soundIsPlaying = true;
            }
        }
        return false;
    });
    

    $('form').unbind('submit').bind('submit', function(){
        url = $(this).attr('action');
        pars = $(this).serialize();

        target = '#content';
        source = '#content';

        //        alert ("ok 1");
        //        $('#header').append('<img class="throbber" src="'+ROOT+'themes/images/ajax-loader.gif" style="z-index: 1001; position: absolute; top: 25px; left: 95px;"/>');

        $.post(url, pars, function(data){
            $(target).html($(source, data).html());
            GUI.postLoad();
        });

        return false;
    });

    $('a.lightbox').lightBox();
    
    $('a.lightbox_single').each(function(){
        $(this).lightBox();
    });
    
    

};

cGUI.prototype.postLoad = function() {
    setTimeout(function(){
        $.scrollTo(0, 250);
    }, 100);
    TIP.check();
    $("#page_all a").unbind('click');
    setTimeout (function () {
        GUI.parseLinks();
    }, 100);
    setTimeout(function(){
        $('.throbber').remove();
    }, 100);
    $('body').css({
        cursor: 'default'
    });
    CallCufon();
    //$('#msg_box').hide();
    initLangDropMenu();
    checkSoundVisibility();
};

cGUI.prototype.load = function(url, target, source, _callback, reurl, history, from_flash) {
    if (!target) target = 'page';
    if (!source) source = 'page';
    if (!_callback) _callback = 'void(0);';
    if (GUI.callback != false) _callback = GUI.callback;
    if (!reurl) reurl = 'no';
    if (!history) history = 'yes';
    if (!from_flash) from_flash = 'no';

    reurl = 'no';

    if (url.indexOf(window.location.hostname) != -1) {
        url = (url.substr(url.indexOf(window.location.hostname) + window.location.hostname.length));
    }
    
    //log('load: history = '+history + ' | url: '+url);

    if (target == 'page') {
        $("#page_all a").unbind('click');
        $('body').css({
            'cursor': 'wait'
        });
        w = $(document).width();
        h = $(document).height();
        $('.throbber').remove();
        $('body').append('<div class="throbber" style="z-index: 110; cursor: wait; opacity: 0.2; position: absolute; width: ' + w + 'px; height: ' + h + 'px; top: 0px; left: 0px; background: transparent;">&nbsp;</div>');
    }
    
    if (history == 'yes' && !$.browser.opera) {
        url = url.replace(/^.*#/, '');
        gatrack(url); 
        GUI.callback = _callback;
        $('.throbber').remove();
        $('body').css({
            'cursor': 'default'
        });
        GUI.postLoad();
        $.history.load(url, null, null, null, null, 'no'); 
        return false;
    }
    
    if (reurl == 'yes') url = '/ajax' + url + '/' + source;

    $.get(url, function(data){
        $('body').css({
            'cursor': 'default'
        });
        if (window.location.hash != '#' + window.location.pathname || GUI.start_hash_test == url) {
            if (reurl == 'yes') {
                $('#' + target).html(data);
            } else {
                if (target == 'page') $('#menu').html($(data).find('#menu').html());
                $('#' + target).html($(data).find('#' + source).html());
                if (target == 'page') $('#' + target).css('position', 'static');
            }
            p = data.indexOf(find = '<title>');
            title = data.substr(p + find.length);
            p = title.indexOf(find = '</title>');
            title = title.substr(0, p);
            try {
                window.title = title;
                document.title = title;
            } catch(err) {}
            eval(_callback);
            GUI.callback = false;
        }
        GUI.postLoad();
    });
    
    
    void(0);
};

var startUpTriggerFix = function(){
    if (!$.browser.flash) return;

    if (GUI.flashLoaded == true || typeof thisMovie('flash') != 'undefined') {
        setTimeout(function(){
            //changeFlashLang($("#header").attr('data-lang'));
            $('a#switch').trigger('click');
        }, 1000);
    } else {
        setTimeout(function(){
            startUpTriggerFix();
        }, 100);
    }
};

var cTIP = function() {

    this.id = '#msg_box';
    this.closed_1 = false;
    this.closed_2 = false;

    this.init = function(_callback){
        //  log('TIP.init - GUI.active: ' + GUI.active);
        el = $(this.id);
        if ((GUI.active == 1 && this.closed_1 == false) || (GUI.active == 2 && this.closed_2 == false)) {
            el.append('<span href="#" style="background: transparent; opacity: 0.4; cursor: pointer; width: 10px; height: 10px; position: absolute; top: 8px; right: 20px;"/>');
            el.find('span').bind('click', function(){
                TIP._onclick(this);
            });
            if (!_callback) {} else {
                eval(_callback);
            }
        }
    };
    /*
	 * GUI.active = 1 - flash
	 * GUI.active = 2 - html
	 */
	
    this.check = function(){
        //		log('TIP.check - GUI.active: ' + GUI.active);
        el = $(this.id);
        if (el.find('span').length != 1) {
            TIP.init('TIP.check();');
            el.hide();
            return;
        }
        
        updateSwitchBubbleText($("#header").attr('data-lang'));
        
        if (GUI.active == 1) {
            if (this.closed_1 == false) {
                ///setTimeout(function(){
                    el.css({
                        'top': '-27px',
                        'background-position': '0 -49px'
                    });
					el.show();
                //}, 5000);
            } else {
                //el.css({'background-image': 'none'});
                el.find('span').remove();
				el.hide();
            }
        } else if (GUI.active == 2) {
            if (this.closed_2 == false) {
                //setTimeout(function(){
                    el.css({
                        'top': '67px',
                        'background-position': '0 0'
                    });
					el.show();
                //}, 5000);
            } else {
                //el.css({'background-image': 'none'});
                el.find('span').remove();
				el.hide();
            }
        }
    };
	
    this._onclick = function(el){
        if (GUI.active == 1) {
            this.closed_1 = true;
        } else if (GUI.active == 2) {
            this.closed_2 = true;
        }
        //		log('TIP.click - GUI.active: ' + GUI.active);
        //		log('TIP.click - this.closed_1: ' + this.closed_1);
        //		log('TIP.click - this.closed_2: ' + this.closed_2);
        $(el).remove();
        $(this.id).css({
            'background-image': 'none'
        });
		$(this.id).hide();
    };
	
};

var TIP = new cTIP();

var cPage = function() {

    this.init = function() {
        
        GUI.animating = false;
//        $('ul#main li').removeClass('active');        
        
        $(window).bind('resize', function(){

            if ($.browser.msie) if (parseInt($.browser.version) <= 6) return;
            if (GUI.active == 1) {
				
                if ($(window).width() < 940) {
                    $("#home_interactive").css('width', 940);
                    $("#page_all").css('width', 940);
                    //$("#flash_loaded").css('width', 940);
                    $("#home_interactive").css('height', $(window).height() - 90);
                } else {
                    $("#home_interactive").css('width', $(window).width());
                    $("#page_all").css('width', $(window).width());
                    //$("#flash_loaded").css('width', $(window).height() - 90);
                    $("#home_interactive").css('height', $(window).height() - 90);
                }
                $('#page_all').css({
                    'top': $(window).height() - 90
                });
            } else {

                if ($(window).width() < 940) {
                    $("#page_all").css('width', 940);
                } else {
                    $("#page_all").css('width', $(window).width());
                }

            }
        });
        
        if (typeof userInfo == 'undefined') {

            if (window.location.hash != '') {
                GUI.active = 2;
                startUpTriggerFix();
            }
            
            TIP.init();
            
            tmp = window.location.hash;
            tmp = tmp.replace(/^.*#/, '');
            GUI.start_hash_test = GUI.hash_test = tmp;
            if (GUI.start_hash_test != window.location.pathname) {
                GUI.start_hash_test = window.location.pathname;
            }
            GUI.clean_enter = false;
            if (!$.browser.opera) $.history.init(function(hash, a, b, c, d, e, f){
                //console.log('hash: '+hash);
                //console.log('GUI.hash_test: '+GUI.hash_test);
                //console.log('GUI.start_hash_test: '+GUI.start_hash_test);
                //console.log('GUI.clean_enter: '+GUI.clean_enter);
                if(hash == "") {
                    window.location.hash = '#' + window.location.pathname;
                    tmp = window.location.hash;
                    tmp = tmp.replace(/^.*#/, '');
                    GUI.clean_enter = true;
                } else if ( hash != GUI.hash_test || window.location.pathname != GUI.hash_test || GUI.clean_enter == false) {
                    GUI.hash_test = hash;
                    if (GUI.clean_enter == false) {
                        GUI.load(hash, a, b, c, d, 'no', f);
                    }
                    GUI.clean_enter = false;
                }
            },
            {
                unescape: ",/"
            }); 

            GUI.callback = false;
            GUI.flashLoaded = false;
            if(typeof userInfo == 'undefined' && $.browser.flash) {// && window.location.hash == ''
                GUI.active = 1;
                $('#flash_box').css('display', 'block');
                $("#home_interactive").css('height', $(window).height() - 90);
                $("#page_all").css('top', $(window).height() - 90);
//                $("#page_all").animate({'top': $(window).height() - 90}, 500, function(){});
                $("#page_all").css('height', '90px');
                $("#page_all").css('width', $(window).width());

                $("#switch").removeClass ('up');
                $("#switch").addClass ('down');
                ContHide();
                TIP.check();
                GUI.parseLinks();
                return;
            }
        /*
            if (!window.location.hash) {
                GUI.hash_test = ROOT + HTMLinfo.page_name + '.html';
                window.location.hash = '#' + GUI.hash_test;
            } else {

        	}*/
        }


    };
    this.faq = function(){
        $('.module_faq .item div.c_item').hide();
        $('.module_faq h3 a').click(function(){
            if($(this).parents('.item').children('div.c_item').css('display') == 'none'){
                $(this).parents('.item').children('div.c_item').slideDown();
                $(this).parents('.item').addClass('active');
            } else {
                $(this).parents('.item').children('div.c_item').slideUp();
                $(this).parents('.item').removeClass('active');
            }
            return false;
        });
    };

    this.pagePath = window.location.pathname;

    this.block = function() {
        $('#systemWorking').fadeIn();
    };
    this.unblock = function() {
        $('#systemWorking').fadeOut();
    };
    this.throbber = function(id) {
        $(id).html('<center style="padding-top: 50px;"><img src="' + ROOT + 'themes/images/cms/throbber.gif"/></center>');
    }
    this.tableRows = function() {
        $('#text_box table tr').each( function() {
            if($(this).parent().parent().hasClass("normal")||$(this).parent().parent().hasClass("images"))
            {
                return;
            }
            if( !$(this).hasClass("dark") && !$(this).prev("tr").hasClass("dark")) {
                $(this).addClass("dark");
            }
            else {
        }
        }
        )
    }
};

var PAGE = new cPage();

$(function(){

    if ($.browser.msie) if (parseInt($.browser.version) <= 6) {
        return;
    }
    
    $("#page_all_inside").css({
    	'display': 'block',
    	'height': '1px'
    });
    // $("#page_all_inside").css({'overflow': 'hidden'});
    
    /*
    setTimeout(function(){
    	$("#header #sound").trigger('click');
    	$("a#switch").trigger('click');
    }, 1500);
  */ 

    $('body.no-js').removeClass('no-js');
    if ( ! swfobject.hasFlashPlayerVersion("10.1")) {
        $('body').addClass('no-flash');
        $.browser.flash = false;
    }
    
    PAGE.init();
    
    CallCufon();
    
    initLangDropMenu();
    
    $('a.print').bind('click', function(){
        window.print();
        return false;
    });

    $('#submenu li ul').each(function(){
        $(this).find('li a img[src$="default.gif"]').parent().parent().parent().css({
            'margin-left': '15px'
        });
    });

    $(".carousel[rel^=run]").each(function(){
        _rel = $(this).attr('rel');
        tmp = _rel.split('_');
        $(this).jCarouselLite({
            btnNext: ".next",
            btnPrev: ".prev",
            visible: parseInt(tmp[1])
        })
    });

    if (typeof $.ui != 'undefined' && typeof $.ui.accordion != 'undefined') $("#accordion").accordion({
        autoHeight: true,
        navigation: true
    });

    $('a[rel=facebox]').bind('click', function(){
        url = $(this).attr('href');
        $.post(url, null, function(data, textStatus) {
            _body = $(data).find('#text_box').html();
            $.facebox(_body);
        });
        return false;
    });
    $('a[rel=facebox-image]').bind('click', function(){
        var url = $(this).attr('href');
        $.facebox(function() {
            data = '<img src="' + url + '"/>';
            $.facebox('<div style="width:600px;"><center>' + data +'</center></div>');
        });
        return false;

    });
    $('a[rel^=facebox-int]').bind('click', function(){
        url = $(this).attr('href');
        _rel = $(this).attr('rel');
        $.post(url, null, function(data, textStatus) {
            _body = $(data).find('#content').html();
            $.facebox('<div style="height: auto; overflow-y:auto;">' + _body +'</div>');
        });
        return false;
    });

    $("#page_all_inside").css({
    	'height': 'auto'
    });
    // $("#page_all_inside").css({'overflow': 'auto'});
    
});

var gatrack = function(url) {
    if (typeof _gaq == 'undefined') {} else {
        _gaq.push(['_trackPageview', url]);
    }
    void(0);
}; 

flashLoaded = function() {
    GUI.flashLoaded = true;
    if (GUI.active == 2) {
        $("#flash_loaded").css({
            'display': 'block'
        }).animate({
            'height': '80px'
        }, 500);
        $("#page_all").animate({
            'top': '80px'
        }, 500);
    }
    void(0);
};
flashPreloadStart = function() {
    var lang = $("#header").attr('data-lang');
    changeFlashLang(lang);
    updateFlashLoadedStatus(lang);
    updateSwitchBubbleText(lang);
}
/*
setInterval(function(){
	console.log(GUI.active);
}, 1000);
*/
