// TABS + Jquery History
$.fn.pdTabs = function (option) {
    option = jQuery.extend({
        tabSelector: '.tab-fragment',
        tabNameSelector: '.tab-name',
        defTab: 0,
        tabPosition: 'top',
        hideTabClass: 'hide-tab',
        activeTabClass: 'active',
        callFunction: function () {},
        prefix: 'hi_'
    }, option);
    var helpHash = '';
    var sdiak = "áäčďéěíĺľňóôöŕšťúůüýřžÁÄČĎÉĚÍĹĽŇÓÔÖŔŠŤÚŮÜÝŘŽ/";
    var bdiak = "aacdeeillnooorstuuuyrzAACDEEILLNOOORSTUUUYRZ.";

    function bezdiak(str) {
        var tx = "";
        var txt = str;
        for (var p = 0; p < txt.length; p++) {
            if (sdiak.indexOf(txt.charAt(p)) != -1) {
                tx += bdiak.charAt(sdiak.indexOf(txt.charAt(p)))
            } else tx += txt.charAt(p)
        }
        return tx
    };
    $(this).each(function () {
        var element = $(this);
        $(element).addClass('js');
        var tabConstruct = {
            name: function () {
                var i = 0;
                var arr = [];
                $('' + option.tabSelector, element).each(function () {
                    var size = $('' + option.tabNameSelector, this).size();
                    if (size > 0) {
                        if ($('' + option.tabNameSelector + ':eq(0)', this).attr('title') != '') {
                            arr[i] = $('' + option.tabNameSelector + ':eq(0)', this).attr('title')
                        } else {
                            arr[i] = $('' + option.tabNameSelector + ':eq(0)', this).html()
                        }
                    } else {
                        var j = i + 1;
                        arr[i] = 'Tab ' + j
                    }
                    i++
                });
                return arr
            },
            id: function () {
                var arr = this.name();
                for (var i = 0; i < arr.length; i++) {
                    arr[i] = bezdiak(arr[i].toLowerCase());
                    arr[i] = arr[i].replace(/\s/g, "-").replace(/[-]{2,}/g, "-").replace(/\.|\{|\}|\(|\)|\[|\]|\\|@|:|\d/g, "").replace(/^-/, "").replace(/-$/, "").replace(/<span>/, "").replace(/<span>/, "")
                }
                return arr
            },
            size: $('' + option.tabSelector, element).size(),
            tabList: function () {
                var arrName = tabConstruct.name();
                var arrId = tabConstruct.id();
                var list = '<ul class="tab-list reset clearfix tab-list-small">';
                for (var i = 0; i < this.size; i++) {
                    if ($('' + option.tabSelector, element).eq(i).hasClass('special')) {
                        list += '<li class="tab-' + (i + 1) + ' tab-' + arrId[i] + ' tab-special"><a href="#' + arrId[i] + '">' + arrName[i] + '</a></li>'
                    } else {
                        list += '<li class="tab-' + (i + 1) + ' tab-' + arrId[i] + '"><a href="#' + arrId[i] + '">' + arrName[i] + '</a></li>'
                    }
                }
                list += '</ul>';
                return list
            }
        };
        if (option.tabPosition == 'bottom') {
            $(element).append(tabConstruct.tabList());
            $('.tab-list a').each(function () {
                $(this).attr('href', '#' + $(this).attr('href').split('#')[1])
            })
        } else {
            $(element).prepend(tabConstruct.tabList());
            $('.tab-list a').each(function () {
                $(this).attr('href', '#' + $(this).attr('href').split('#')[1])
            })
        }
        var i = 0;
        var arrId = tabConstruct.id();
        $('' + option.tabSelector, element).each(function () {
            $(this).attr('id', arrId[i]);
            i++
        });

        function makeTabByHash(hash) {
            if (typeof hash != 'undefined') {
                var hash = hash
            } else {
                var hash = ''
            }
            if (hash != '') {
                var onlyHash = hash;
                hash = option.prefix + hash
            } else {
                hash = window.location.hash.slice(1)
            }
            if (hash == '' && helpHash == '') {
                var index = typeof option.defTab == 'number' ? option.defTab : 0;
                $('' + option.tabSelector, element).eq(index).removeClass(option.hideTabClass);
                $('' + option.tabSelector, element).not(':eq(' + index + ')').addClass(option.hideTabClass);
                $('.tab-list li', element).not(':eq(' + index + ')').removeClass(option.activeTabClass);
                $('.tab-list li', element).eq(index).addClass(option.activeTabClass)
            } else {
                if ($('#' + hash, element).is(option.tabSelector) == true) {
                    $('#' + hash, element).filter('.hide-tab').removeClass(option.hideTabClass);
                    $('' + option.tabSelector, element).not('#' + hash + '').addClass(option.hideTabClass);
                    var index = $('' + option.tabSelector, element).index($('#' + hash));
                    $('.tab-list li', element).not(':eq(' + index + ')').removeClass(option.activeTabClass);
                    $('.tab-list li', element).eq(index).addClass(option.activeTabClass);
                    helpHash = hash
                } else if ($('' + option.tabSelector + ' #' + hash, element).size() > 0 || $('' + option.tabSelector + ' #' + onlyHash, element).size() > 0) {
                    if ($('' + option.tabSelector + ' #' + hash, element).size() > 0) {
                        var index = $('' + option.tabSelector, element).index($('#' + hash).parents('' + option.tabSelector))
                    } else {
                        var index = $('' + option.tabSelector, element).index($('#' + onlyHash).parents('' + option.tabSelector))
                    }
                    $('' + option.tabSelector, element).eq(index).removeClass(option.hideTabClass);
                    $('' + option.tabSelector, element).not(':eq(' + index + ')').addClass(option.hideTabClass);
                    $('.tab-list li', element).not(':eq(' + index + ')').removeClass(option.activeTabClass);
                    $('.tab-list li', element).eq(index).addClass(option.activeTabClass);
                    helpHash = $('' + option.tabSelector + ' #' + hash, element).parents('' + option.tabSelector).attr('id')
                } else {
                    if (helpHash == '') {
                        var index = typeof option.defTab == 'number' ? option.defTab : 0
                    } else {
                        var index = $('' + option.tabSelector, element).index($('#' + helpHash))
                    }
                    $('' + option.tabSelector, element).not(':eq(' + index + ')').addClass(option.hideTabClass);
                    $('.tab-list li', element).not(':eq(' + index + ')').removeClass(option.activeTabClass);
                    $('.tab-list li', element).eq(index).addClass(option.activeTabClass)
                }
                option.callFunction();
                if ($('' + option.tabSelector + ' #' + hash, element).size() > 0) {
                    var top = $('#' + hash).offset().top;
                    $('html, body').scrollTop(top)
                }
            }
        };
        $('a[href^="#"]').each(function () {
            var hash = $(this).attr('href').split('#');
            hash = hash[hash.length - 1];
            if (hash != '' && $('#' + hash, element).size()) {
                $('#' + hash).each(function () {
                    this.id = option.prefix + this.id
                });
                $('label[for="' + hash + '"]').each(function () {
                    this.htmlFor = option.prefix + this.htmlFor
                });
                $(this).click(function () {
                    $.history.load(hash);
                    return false
                })
            }
        });
        $.history.init(makeTabByHash);
        var hash = window.location.hash;
        hash = hash.replace(/^.*#/, '');
        if (hash != '') {
            $.history.load(hash)
        } else {
            makeTabByHash()
        }
    })
};
(function ($) {
    function History() {
        this._curHash = '';
        this._callback = function (hash) {}
    };
    $.extend(History.prototype, {
        init: function (callback) {
            this._callback = callback;
            this._curHash = location.hash;
            if ($.browser.msie) {
                if (this._curHash == '') {
                    this._curHash = '#'
                }
                $("body").prepend('<iframe id="jQuery_history" style="display: none;"></iframe>');
                var iframe = $("#jQuery_history")[0].contentWindow.document;
                iframe.open();
                iframe.close();
                iframe.location.hash = this._curHash
            } else if ($.browser.safari) {
                this._historyBackStack = [];
                this._historyBackStack.length = history.length;
                this._historyForwardStack = [];
                this._isFirst = true;
                this._dontCheck = false
            }
            this._callback(this._curHash.replace(/^#/, ''));
            setInterval(this._check, 100)
        },
        add: function (hash) {
            this._historyBackStack.push(hash);
            this._historyForwardStack.length = 0;
            this._isFirst = true
        },
        _check: function () {
            if ($.browser.msie) {
                var ihistory = $("#jQuery_history")[0];
                var iframe = ihistory.contentDocument || ihistory.contentWindow.document;
                var current_hash = iframe.location.hash;
                if (current_hash != $.history._curHash) {
                    location.hash = current_hash;
                    $.history._curHash = current_hash;
                    $.history._callback(current_hash.replace(/^#/, ''))
                }
            } else if ($.browser.safari) {
                if (!$.history._dontCheck) {
                    var historyDelta = history.length - $.history._historyBackStack.length;
                    if (historyDelta) {
                        $.history._isFirst = false;
                        if (historyDelta < 0) {
                            for (var i = 0; i < Math.abs(historyDelta); i++) $.history._historyForwardStack.unshift($.history._historyBackStack.pop())
                        } else {
                            for (var i = 0; i < historyDelta; i++) $.history._historyBackStack.push($.history._historyForwardStack.shift())
                        }
                        var cachedHash = $.history._historyBackStack[$.history._historyBackStack.length - 1];
                        if (cachedHash != undefined) {
                            $.history._curHash = location.hash;
                            $.history._callback(cachedHash)
                        }
                    } else if ($.history._historyBackStack[$.history._historyBackStack.length - 1] == undefined && !$.history._isFirst) {
                        if (location.hash.indexOf('#') >= 0) {
                            $.history._callback(location.hash.split('#')[1])
                        } else {
                            $.history._callback('')
                        }
                        $.history._isFirst = true
                    }
                }
            } else {
                var current_hash = location.hash;
                if (current_hash != $.history._curHash) {
                    $.history._curHash = current_hash;
                    $.history._callback(current_hash.replace(/^#/, ''))
                }
            }
        },
        isKonqueror: function () {
            return /KHTML|Konqueror/.test(navigator.userAgent)
        },
        load: function (hash) {
            var newhash;
            if ($.browser.safari) {
                newhash = hash
            } else {
                newhash = (this.isKonqueror() ? '' : '#') + hash;
                location.hash = newhash
            }
            this._curHash = newhash;
            if ($.browser.msie) {
                var ihistory = $("#jQuery_history")[0];
                var iframe = ihistory.contentWindow.document;
                iframe.open();
                iframe.close();
                iframe.location.hash = newhash;
                this._callback(hash)
            } else if ($.browser.safari) {
                this._dontCheck = true;
                this.add(hash);
                var fn = function () {
                    $.history._dontCheck = false
                };
                window.setTimeout(fn, 200);
                this._callback(hash);
                location.hash = newhash
            } else {
                this._callback(hash)
            }
        }
    });
    $(document).ready(function () {
        $.history = new History()
    })
})(jQuery);
