/* shobject 1.0, 2010 by inventive designs <www.inventivedesigns.cz> (taking time to reinvent the wheel)
   ! - requires AddEvent() and removeEvent() functions defined below.
*/
var d = document;
var w = window;

$(d).ready(function() {
    TargetAnimator.init();
    Tabs.init();
    RevealingMenu.init();
});
$.fn.hasClassStart = function(classNameStart) {
    var classes = this.attr("class").split(" ");
    for (var i in classes) {
        if (classes[i].substr(0, classNameStart.length) == classNameStart) {
            return classes[i];
        }
    }
    return false;
}
var TargetAnimator = {
    delayDur: 600,
    highlightDur: 300,
    stayDur: 400,
    fadeDur: 600,
    scrollSpeed: 1000, // [px/s]
    scrollToOffset: 70, // [px]
    minScrollDur: 800,
    preventScroll: false,
    init: function() {
        var t = this;
        // Animate the target (if any) when the page is visitted
        var $target = $(location.hash);
        t.animate($target); 
        // Animate when an element is targatted by a link within the actual page
        $('a[href*="#"]').click(function() {
            var href = $(this).attr("href");
            var $target = $(href.substr(href.lastIndexOf("#")));
            t.animate($target); 
        });
    },
    animate: function($target) {
        if ($target.length) {
            var t = this;
            if (!t.preventScroll) {
                var $body = $("html,body"); // html,body for compatibility
                var scrollTo = $target.offset().top - t.scrollToOffset;
                var maxScrollTop = $("body").height() - $(window).height();
                if (scrollTo > maxScrollTop) {
                    scrollTo = maxScrollTop;
                }
                var bodyScrollTop = $("html").scrollTop() || $("body").scrollTop();
                var scrollDur = Math.round(Math.abs(bodyScrollTop - scrollTo) / t.scrollSpeed) * 1000; // [ms]
                if (scrollDur < t.minScrollDur) {
                    scrollDur = t.minScrollDur;
                }
                $body.animate({scrollTop: scrollTo}, scrollDur);
            }
            t.preventScroll = false;
            $target.find("*").andSelf().each(function(index) {
                var $descendant = $(this); // descendants and Self
                if (!$descendant.hasClass("target")) {
                    var origStyle = $descendant.attr("style");
                    var origColor = $descendant.css("color");
                    var origBgColor = $descendant.css("backgroundColor");
                    var origBdtColor = $descendant.css("borderTopColor");
                    var origBdrColor = $descendant.css("borderRightColor");
                    var origBdbColor = $descendant.css("borderBottomColor");
                    var origBdlColor = $descendant.css("borderLeftColor");
                    $descendant.addClass("target");
                    var targetColor = $descendant.css("color");
                    var targetBgColor = $descendant.css("backgroundColor");
                    var targetBdtColor = $descendant.css("borderTopColor");
                    var targetBdrColor = $descendant.css("borderRightColor");
                    var targetBdbColor = $descendant.css("borderBottomColor");
                    var targetBdlColor = $descendant.css("borderLeftColor");
                    $descendant.removeClass("target");
                    $targetAnimProps = { // higlight animation
                        color: targetColor,
                        backgroundColor: targetBgColor,
                        borderTopColor: targetBdtColor,
                        borderRightColor: targetBdrColor,
                        borderBottomColor: targetBdbColor,
                        borderLeftColor: targetBdlColor
                    };
                    $origAnimProps = { // fade animation
                        color: origColor,
                        backgroundColor: origBgColor,
                        borderTopColor: origBdtColor,
                        borderRightColor: origBdrColor,
                        borderBottomColor: origBdbColor,
                        borderLeftColor: origBdlColor
                    };
                    $descendant.delay(scrollDur + t.delayDur).animate($targetAnimProps, t.highlightDur).delay(t.stayDur).animate($origAnimProps, t.fadeDur).delay(t.stayDur).animate($targetAnimProps, t.highlightDur).delay(t.stayDur).animate($origAnimProps, t.fadeDur, function() {
                        if (origStyle) {
                            $descendant.attr("style", origStyle);
                        } else {
                            $descendant.attr("style", ""); // removeAttr is bugged (delayed)
                        }
                    });
                }
            });
        }
    }
}

var Tabs = {
    init: function() {
        var t = this;
        var fragment = location.hash.substr(1);
        $('[id^="tabswitch-"]').each(function() {
            var $switch = $(this);
            var switchName = this.id.substr(10);
            var $ownTab = $("#tab-" + switchName);
            var $siblingTabs = $ownTab.siblings('[id^="tab-"]');
            if (!$switch.hasClass("actual")) {
                $ownTab.hide();
            }
            $switch.click(function(event) {
                var $siblingSwitches = $();
                $siblingTabs.each(function() {
                    var siblingSwitchName = this.id.substr(4); 
                    $siblingSwitches = $siblingSwitches.add("#tabswitch-" + siblingSwitchName);
                });
                if ($ownTab.is(":visible")) {
                    $ownTab.hide(); // enable tab closing
                    event.preventDefault();
                    location.hash = "#";
                } else {
                    $ownTab.show();
                }
                $siblingTabs.hide();
                $switch.addClass("actual");
                $siblingSwitches.removeClass("actual");
            });
            if (fragment == switchName) {
                $switch.click();
            }
        });
    }
}
var RevealingMenu = {
    fadeOutDur: 200,
    fadeInDur: 200,
    fadeOutAlpha: 0,
    fadeInAlpha: 1,
    groups: {},
    init: function() {
        var t = this;
        var fragment = location.hash.substr(1);
        var $potentialContainers = $('[class*="rmc-"]');
        var $potentialOpeners = $('[class*="rmo-"]');
        $potentialOpeners.each(function() {
            var $opener = $(this);
            var hmClass = $opener.hasClassStart("rmo-");
            if (!hmClass) {
                return;
            }
            $opener.css("cursor", "pointer");
            var menuName = hmClass.substr(4);
            var $opener = $(".rmo-" + menuName);
            var $container = $(".rmc-" + menuName);
            //var $items = $container.children();
            $opener.show();
            $container.hide();
            $opener.mouseover(function() {
                $opener.stop(true).fadeTo(t.fadeOutDur, t.fadeOutAlpha);
                $container.stop(true).fadeTo(t.fadeInDur, t.fadeInAlpha);
            });
            $container.mouseleave(function() {
                $container.stop(true).fadeTo(t.fadeOutDur, t.fadeOutAlpha, function() {
                    $container.hide();
                });
                $opener.stop(true).fadeTo(t.fadeInDur, t.fadeInAlpha);
            });
        });
    }
}
// v-- it would be nice if all the junk below is rewritten in jQuery.. sometime by someone --v 
function shobject(settings,id) {
    for (var i in settings) this[i] = settings[i];
    if (id) this.init(id);
}
shobject.prototype = {
    a_shown:100,
    a_hidden:0,
    dur_setNextAlpha:60,
    step:15,
    onAnimFinished:function(){},
    init:function(id) {
        this.id = id;
        this.a = this.a_hidden;
        this.setAlpha(this.a);
    },
    setAlpha:function(a,id) {
        if (!id) {
            var id = this.id;
            this.a = a;
            w.clearInterval(this.int_setAlpha);
        }
        var s = d.getElementById(id).style;
        s.opacity = (a/100);
        s.MozOpacity = (a/100);
        s.KhtmlOpacity = (a/100);
        s.filter = 'Alpha(opacity='+a+')';
        s.visibility = 'visible';
    },
    setNextAlpha:function(sh,a_end) {
        if (sh.sign*sh.a<a_end) {
            sh.a += sh.step*sh.sign;
        } else {
            sh.a = a_end;
            w.clearInterval(sh.int_setAlpha);
            sh.onAnimFinished();
        }
        sh.setAlpha(sh.a,sh.id)
    },
    show:function() {
        w.clearInterval(this.int_setAlpha);
        var t = this;
        this.sign = 1;
        this.int_setAlpha = w.setInterval(function(){shobject.prototype.setNextAlpha(t,t.a_shown)},t.dur_setNextAlpha);
    },
    hide:function() {
        w.clearInterval(this.int_setAlpha);
        var t = this;
        this.sign = -1;
        this.int_setAlpha = w.setInterval(function(){shobject.prototype.setNextAlpha(t,t.a_hidden)},t.dur_setNextAlpha);
    }
}
function displayobj(id_hidden) {
    this.el = d.getElementById(id_hidden);
    this.el.style.display = 'none';
}
displayobj.prototype = {
    show:function() {
        this.el.style.display = 'block';
        this.displayed = true;
    },
    hide:function() {
        this.el.style.display = 'none';
        this.displayed = false;
    },
    revert:function() {
        if (this.displayed) this.hide();
        else this.show();
    }
}
// Animace novinek
function news(items,id_crop,id_content,id_lbrack,id_rbrack,id_table) {
    var t = this;
    this.els = {};
    this.els.crop = d.getElementById(id_crop);
    this.els.content = d.getElementById(id_content);
    this.els.lbrack = d.getElementById(id_lbrack);
    this.els.rbrack = d.getElementById(id_rbrack);
    this.els.table = d.getElementById(id_table);
    this.eu_settings.onAnimFinished = function(){news.prototype.onAnimFinished(t)};
    this.eu = new easeutil(id_crop,this.eu_settings);
    this.eu.updateProp = this.updateProp;
    this.eu.s2 = this.els.content.style;
    this.eu.t = this;
    addEvent(this.els.table,'mouseover',function(){t.mouseOver(t)});
    addEvent(this.els.table,'mouseout',function(){t.mouseOut(t)});
    this.items = [];
    for (var i=0;i<items.length;i++) {
        try {
            var item = {};
            item.el = d.getElementById(items[i][0]);
            item.cl = (items[i][1]?items[i][1]:''); // voson/spso class (bracket color)
            item.el.style.display = 'none';
            this.items.push(item);
        } catch (e) {} 
    }
    this.w_content = this.els.content.offsetWidth;
    this.updateActive(this);
    this.eu.setTo(this.w_active,true);
    this.els.content.style.left = -Math.floor((this.w_content-this.w_active)/2)+'px';
    this.els.lbrack.style.visibility = 'visible';
    this.els.rbrack.style.visibility = 'visible';
    this.setDurChange(this);
    this.startTimer(this);
}
news.prototype = {
    dur_min_change:3000,
    dur_max_change:6000,
    durReduceCoef:1/4,
    w_min_durChange:300,
    w_max_durChange:820,
    w_closed:1,
    activeNum:0,
    eu_settings:{propName:'width',dur_update:45},
    mouseOver:function(t) {
        w.clearTimeout(t.int_change);
        if (t.state=='opening') t.state = 'over';
    },
    mouseOut:function(t) {
        if (t.state=='opened') {
            t.dur_change *= t.durReduceCoef;
            t.state = 'out';
            t.startTimer(t);
        }
        else if (t.state=='out') t.startTimer(t);
        else if (t.state=='over') t.state='opening';
    },
    startTimer:function(t) {
        t.int_change = w.setTimeout(function(){t.change(t)},this.dur_change);
    },
    change:function(t) {
        t.state = 'closing';
        t.eu.easeTo(t.w_closed); 
    },
    setDurChange:function(t) {
        var c
        if (t.w_active>t.w_min_durChange) {
            if (t.w_active<t.w_max_durChange) {
                c = (t.w_active-t.w_min_durChange)/(t.w_max_durChange-t.w_min_durChange);
            } else c = 1;
        } else c = 0;
        t.dur_change = t.dur_min_change+Math.floor((t.dur_max_change-t.dur_min_change)*c);
        t.state = 'opened';
    },
    updateProp:function(eu) {
        eu.s[eu.propName] = eu.value+eu.value%2+'px';
        eu.s2.left = -Math.floor((eu.t.w_active-eu.value)/2+(eu.t.w_content-eu.t.w_active)/2)+'px';
    },
    updateActive:function(t){
        t.active = t.items[t.activeNum];
        t.active.el.style.display = 'block';
        t.w_active = t.active.el.offsetWidth;
        t.els.lbrack.className = t.active.cl;
        t.els.rbrack.className = t.active.cl;
        if (!t.active.cl) {
            t.els.lbrack.className = '';
            t.els.rbrack.className = '';
        }
    },
    onAnimFinished:function(t) {
        if (t.state=='closing') {
            t.state = 'opening';
            t.active.el.style.display = 'none';
            if (t.activeNum<t.items.length-1) t.activeNum++;
            else t.activeNum = 0;
            t.updateActive(t);
            t.eu.easeTo(t.w_active);
        } else if (t.state=='opening') {
            t.setDurChange(t);
            t.startTimer(t);
        } else if (t.state=='over') {
            t.setDurChange(t);
        }
    }
}
// Animace popisku galerie
function descriptions(ids,max) {
    var n;
    this.max = max;
    this.descs = [];
    for (var i in ids) {
        n = Number(ids[i].substr(4));
        this.descs[n] = {};
        this.descs[n].sh = new shobject({},ids[i]);
    }
    if (this.descs[1]) this.actualdesc = this.descs[1];
    else this.actualdesc = this.descs[0];
    this.actualdesc.sh.show();
    if (location.hash) {
        this.imgNum = Number(location.hash.substr(1));
        this.sh.show();
        var value = -(this.imgNum-1)*gallery.shift;
        if (value<gallery.value_max && value>=gallery.value_min) gallery.setTo(value); // shift gallery to desired item
    }
}
descriptions.prototype = {
    actualNum:1,
    dur_show:1000,
    change:function() {
        var change = false;
        var toshowNum = 0;
        if (this.descs[this.actualNum]) {
            change = true;
            toshowNum = this.actualNum;
        } else if (this.actualdesc!=this.descs[0]) change = true;
        if (change) {
            this.actualdesc.sh.hide();
            this.actualdesc = this.descs[toshowNum];
            var t = this;
            this.int_show = w.setTimeout(function(){t.show(t)},this.dur_show);
        } 
    },
    show:function(t) {
        t.actualdesc.sh.show();
    },
    next:function() {
        if (this.actualNum<this.max) this.actualNum++;
        this.change();
    },
    prev:function() {
        if (this.actualNum>1) this.actualNum--;
        this.change();
    }
}
// Animace rozbalovani tabu
function tabs(ids_left,ids_right) {
    this.ltab = this.initTab('left',ids_left);
    this.rtab = this.initTab('right',ids_right);
    this.els = {};
    this.els.a = d.getElementById('tabtitle-a');
    this.els.logo = d.getElementById('logo');
    this.selected = 'none';
    if (location.hash=='#voson') this.setOpened('left');
    else if (location.hash=='#spso') this.setOpened('right');
}
tabs.prototype = {
    h_opened_mask:124/*162*/,
    h_closed_mask:35/*42*/,
    a_paddingBot:114/*149*/,
    pos_opened_sub:0,
    pos_closed_sub:-464,
    settings_mask:{propName:'height',step_max:12,a:2},
    settings_sub:{step_max:26,a:1.8},
    initTab:function(side,ids) {
        var t = this;
        var tab = {};
        tab.els = {};
        var settings_mask = this.settings_mask;
        var settings_sub = {};
        for (var i in this.settings_sub) {settings_sub[i] = this.settings_sub[i];}
        settings_sub.propName = side;
        settings_sub.onAnimFinished = function(){tabs.prototype.close(t)}
        settings_mask.onAnimFinished = function(){tabs.prototype.openSub(t)}
        for (var i in ids) tab.els[i] = d.getElementById(ids[i]);
        tab.els.content.style.visibility = 'hidden';
        tab.eu_mask = new easeutil(ids.mask,settings_mask);
        tab.eu_sub = new easeutil(ids.sub,settings_sub);
        tab.eu_mask.setTo(this.h_closed_mask);
        tab.link = tab.els.title.href;
        tab.sh_btn = new shobject();
        tab.sh_btn.init(tab.els.btn.id);
        return tab;
    },
    open:function(side) {
        if (this.selected!=side) {
            this.changeTitleImgs(side);
            if (this.selected=='none') {
                if (side=='left') {
                    this.activetab = this.ltab;
                    this.passivetab = this.rtab;
                }
                else if (side=='right') {
                    this.activetab = this.rtab;
                    this.passivetab = this.ltab;
                }
                this.activetab.els.content.style.visibility = 'visible';
                this.passivetab.els.content.style.visibility = 'hidden';
                this.selected = side;
                this.setTabsZindex();
                this.activetab.eu_mask.easeTo(this.h_opened_mask);
                this.els.a.className = 'grey';
            } else {
                this.selected = 'second';
                this.sideToOpen = side;
                this.closeSub();
            }
        } else {
                this.activetab.els.title.href = this.activetab.link;
        }
    },
    openSub:function(t){
        if (t.selected=='second') {
            t.selected = 'none';
            t.open(t.sideToOpen);
        } else {
            if (t.selected!='none') {
                t.activetab.els.mask.style.visibility = 'hidden';
                t.activetab.eu_sub.easeTo(t.pos_opened_sub);
                t.els.a.style.paddingBottom = t.a_paddingBot+'px';
            }
        }
    },
    close:function(t) {
        if (t.selected=='second') {
            t.activetab.els.mask.style.visibility = 'visible';
            t.activetab.eu_mask.easeBack();
        } else {
            t.activetab.sh_btn.show();
            t.activetab.els.title.href = t.activetab.link;
        }
    },
    closeSub:function() {
        this.activetab.eu_sub.easeBack();
        this.activetab.sh_btn.hide();
    },
    setOpened:function(side) {
        this.selected = side;
        if (side=='left') {
            this.activetab = this.ltab;
            this.passivetab = this.rtab;
        } else {
            this.activetab = this.rtab;
            this.passivetab = this.ltab;
        }
        this.changeTitleImgs(side);
        this.setTabsZindex();
        this.activetab.sh_btn.setAlpha(100);
        this.activetab.eu_mask.setTo(this.h_opened_mask,false);
        this.activetab.eu_sub.setTo(this.pos_opened_sub,false);
        this.activetab.els.content.style.visibility = 'visible';
        this.activetab.els.mask.style.visibility = 'hidden';
        this.activetab.els.title.href = this.activetab.link;
        this.els.a.className = 'grey';
        this.els.a.style.paddingBottom = this.a_paddingBot+'px';
    },
    setTabsZindex:function() {
        var zI_active = Number(w.getComputedStyle(this.activetab.els.box,null).getPropertyValue('z-index'));
        var zI_passive = Number(w.getComputedStyle(this.passivetab.els.box,null).getPropertyValue('z-index'));
        var cache;
        if (zI_active<zI_passive) {
            for (i in this.activetab.els) {
                cache = w.getComputedStyle(this.activetab.els[i],null).getPropertyValue('z-index');
                this.activetab.els[i].style.zIndex = w.getComputedStyle(this.passivetab.els[i],null).getPropertyValue('z-index');
                this.passivetab.els[i].style.zIndex = cache;
            }
        }
    },
    changeTitleImgs:function(side) {
        var over,out;
        if (side=='left') {
            over = this.ltab;
            out = this.rtab;
            this.els.logo.className = 'imgtext voson';
        } else {
            over = this.rtab;
            out = this.ltab;
            this.els.logo.className = 'imgtext spso';
        }
        over.els.title.href = "javascript:";
        over.els.title.className = 'title over';
        out.els.title.className = 'title grey';
    }
}
// Mouseover animace slozek v galerii
function initGalleryDirs(dirCount) {
    var bg,a;
    box_bgs = [];
    box_as = [];
    box_shs = [];
    for (var i=1; i<=dirCount; i++) {
        bg = 'box-bg'+i;
        a = 'box-a'+i;
        box_shs[i] = new shobject({},bg);
        setMouseOverOut(a,'box_shs['+i+'].setAlpha(100)','box_shs['+i+'].hide()');
    }
}
// global functions
function setMouseOverOut(id,fncOver,fncOut) {
    var el = d.getElementById(id);
    if (fncOver) addEvent(el,'mouseover',function(){eval(fncOver)});
    if (fncOut) addEvent(el,'mouseout',function(){eval(fncOut)});
}
function setClass(id,c) {
    d.getElementById(id).className = c;
}
function findNodeType(el,type) {
    var child = el.firstChild
    while (child != null) {
        if (child.nodeType==type) return child;
        child = child.nextSibling;
    }
    return false;
}
// unification of browsers's functions
if (!d.getElementsByClassName) {
    d.getElementsByClassName = function(cl) {
        var a = [];
        var reg = new RegExp('\\b'+cl+'\\b');
        var els = this.getElementsByTagName('*');
        for (var i=0; i<els.length; i++) {
            var c = els[i].className;
            if (reg.test(c)) a.push(els[i]);
        }
        return a;
    }
}
if (!w.getComputedStyle) {
    w.getComputedStyle = function(el,pseudo) {
        this.el = el;
        this.getPropertyValue = function(prop) {
            var reg = /(\-([a-z]){1})/g;
            if (prop=='float') prop = 'styleFloat';
            if (reg.test(prop)) {
                prop = prop.replace(reg, function () {
                    return arguments[2].toUpperCase();
                });
            }
            return el.currentStyle[prop] ? el.currentStyle[prop] : null;
        }
        return this;
    }
}
function colorToHex(str) {
    var hex = '';
    if (str.substr(0,1)=='#') {
        hex = str.substr(1);
        if (hex.length==3) hex += hex;
    } else {
        var re = /[^0-9,]/g;
        var rgb = str.replace(re, '').split(',');
        for (var i=0; i<rgb.length; i++) {
            hex += Number(rgb[i]).toString(16);
        }
    }
    return hex;
}
function addEvent(o,eventName,fnc) {
    if(o.addEventListener) {
        o.addEventListener(eventName,fnc,true);
        return true;
    } else if (o.attachEvent) {
        var r = o.attachEvent('on'+eventName,fnc);
        return r;
    } else {
        return false;
    }
}
function removeEvent(o,eventName,fnc) {
    if(o.removeEventListener) {
        o.removeEventListener(eventName,fnc,true);
        return true;
    } else if (o.detachEvent) {
        var r = o.detachEvent('on'+eventName,fnc);
        return r;
    } else {
        return false;
    }
}
