(function ($) {
    /* do not use document ready here! */
    $(window).bind('load', function () {
        $('.toggle-pic').each(function (container) {
            var tabs = $('.violettabs li', container),
                images = $('img', container),
                printlink = $('#print_link'),
                active = 'active',
                currenttab = tabs.filter('.' + active);

            var toggleTab = function (e) {
                if (this !== currenttab.get(0)) {
                    var tab = $(this),
                        tabname = $.trim(tab.text());

                    currenttab.removeClass(active);
                    tab.addClass(active);
                    currenttab = tab;

                    printlink.attr('href', '?print=y#' + tabname);

                    images.removeClass(active)
                        .filter('[alt="' + tabname + '"]')
                        //.eq(tab.index())
                        .addClass(active);

                    window.location.hash = tabname;
                }

                e && e.preventDefault();
            };

            /* bind tabs */
            tabs.click(toggleTab);

            /* set hash tab */
            setTimeout(function () {
                var hash = decodeURIComponent(window.location.hash.substring(1));
                if (hash.length) {
                    /* trigger dynamic and inline HTML handlers */
                    tabs.filter(':contains(' + hash + ')').click();
                }
            }, 1000);
        });

        $('#ukraine_map').load(function () {
            $('#toggle-content .ukraine').removeClass('active');

            if(window.location.hash == '#ukraine') {
                $('.ukraine').click();
            }
        });

        $('.toggle-text ul.violettabs span').click(function () {
            $(this).css('outline','none');
            $('.toggle-text ul.violettabs .active').removeClass('active');
            $(this).parent().addClass('active');

            var filterVal = $(this).attr('class');

            if(filterVal == 'tgl-all') {
                $('.toggle-text .bigicons ins.hidden').fadeTo('slow',1).removeClass('hidden');
            } else {
                $('.toggle-text .bigicons ins').each(function () {
                    if(!$(this).hasClass(filterVal)) {
                        $(this).fadeTo('normal',1).addClass('hidden');
                    } else {
                        $(this).fadeTo('slow',1).removeClass('hidden');
                    }
                });
            }

            return false;
        });

        $('#toggle-content .violettabs li').click(function () {
            $('#toggle-content ul.violettabs .active').removeClass('active');
            $('#toggle-content blockquote').removeClass('active');
            var classn = $(this).attr('class');
            $(this).addClass('active');
            $.each($('#toggle-content blockquote'), function () {
                if (classn == $(this).attr('class'))
                {
                    $(this).addClass('active');
                    if (classn == 'ukraine') {
	    	            var iframe = document.getElementById('kiev-iframe');
    			    iframe.src = iframe.src;
    		    }
                }
            });
        });

        $('#moscow').click(function () {
            window.location.hash = 'moscow';
            $('#print_link').attr('href', '?print=y#moscow');

            $('#spb-address').hide();
            $('#moscow-address').show();
            $('.moscow div').css('visibility', 'visible');
            $('.spb div').css('visibility', 'hidden');
            $('.r_content').css('visibility', 'visible');

            var iframe = document.getElementById('moscow-iframe');
            iframe.src = iframe.src;
        });

        $('#spb').click(function () {
            window.location.hash = 'spb';
            $('#print_link').attr('href', '?print=y#spb');

            $('#moscow-address').hide();
            $('#spb-address').show();
            $('.spb div').css('visibility', 'visible');
            $('.moscow div').css('visibility', 'hidden');
            $('.r_content').css('visibility', 'visible');

            var iframe = document.getElementById('spb-iframe');
            iframe.src = iframe.src;
        });

        $('#kiev').click(function () {
            $('#kiev-address').toggle();
        });

        $('#spb_address').load(function () {
            $('#spb-address').hide();

            if(window.location.hash == '#russia') {
                $('.russia').click();
            }else if(window.location.hash == '#moscow' || window.location.hash == '#spb') {
                $(window.location.hash).click();
            }
        });
    });
})(jQuery);
