$(document).bind("mobileinit", function(){
        //defaults
        $.extend(  $.mobile , {
            ajaxEnabled : false,
            hashListeningEnabled : false,
            loadingMessage : false,
            autoInitializePage : false
          });
});
$(document).ready(function() {
    var hash = location.hash.replace("#", "");
    if (hash == "voroncovskiy") {
        hashPos(1);
        correct();
    }
    if (hash == "risheleivskiy") {
        hashPos(2);
        correct();
    }
    if (hash == "potemkenskiy") {
        hashPos(3);
        correct();
    }
    if (hash == "aleksandrovskiy") {
        hashPos(4);
        correct();
    }
    if (hash == "razumovskiy") {

        hashPos(5);
           last();
    }
    
    // full text show
                    $(".page1").each(function(){
                        var $curObj = $(this);
                        $curObj.siblings(".information_more").bind("click", function(){
                                if($curObj.hasClass("opened")){
                                    $(".fulltext", $curObj).hide()
                                    $curObj.closest(".home").css({width:392,marginLeft:"-191px"})
                                    $curObj.siblings(".information_more").html("далее »"); 
                                    $curObj.removeClass("opened");
                                }
                                else {
                                    $(".fulltext", $curObj).show()
                                    $curObj.closest(".home").css({width:1100,marginLeft:"-550px"})
                                    $curObj.siblings(".information_more").html("свернуть"); 
                                    $curObj.addClass("opened"); 
                                }
                        })
                    })
    
    // as of 1.4.2 the mobile safari reports wrong values on offset()
    // http://dev.jquery.com/ticket/6446
    // remove once it's fixed
    if((navigator.userAgent.match(/iPad/i))) {
      (function($) {
          $.fn.offsetOld = $.fn.offset;
          $.fn.offset = function() {
            var result = this.offsetOld();
            result.top -= window.scrollY;
            result.left -= window.scrollX;
            return result;
          };
      })(jQuery);
    }
    
    // swipes
        if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
            {
                  $(".hmo, .gal, .dma > .slides > .slide > .top").bind("swipeleft", function(){
                   next();
                   first();
                   last() 
                  })

                  $(".hmo, .gal, .dma > .slides > .slide > .top").bind("swiperight", function(){
                       prev();
                       first();
                       last()             
                  })
            }

    // pop-ups

    /* Show Popup Window */
    $('.show').click(function() {
        $('.popup').hide().css('visibility', 'hidden');
        $('.fixed').show();
        $(".fixed #" + $(this).attr('class').replace('show p-', '') + "").show().css('visibility', 'visible');
        
                if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
                {
                var odf = $('body').offset();
                $('.popup').css({'top':''+-odf.top+'px'});

                }

        return false;
    });

    $('.fixed-in').click(function() {
        $('.fixed').hide();
    });
	
	$('.show-karta-proezda').click(function() {
        $('.popup').hide().css('visibility', 'hidden');
        $('.fixed').show();
        $(".fixed #" + $(this).attr('class').replace(' dmap', '') + "").show().css('visibility', 'visible');
        
                if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
                {
                var odf = $('body').offset();
                $('.popup').css({'top':''+-odf.top+'px', width: 727, height: 560});
                $('#show-karta-proezda > img').attr('width', '717');
                $('#show-karta-proezda > img').attr('height', '550');
                }

        return false;
    });
	
	$('#show-karta-proezda > .close').live("click", function() {
        $('.fixed').hide();
    });

    // #pop-ups


    function totalwidth() {
        var itemwidth = $('.wrapper').width();
        $('.slide').width(itemwidth);
        var itemnum = $('.slide').length;
        $('.slides').width(itemwidth * itemnum);

    }

    function correct() {
        var reLiTmp = $('.sc .active').attr('id');
        var rerLi = reLiTmp.replace('p-', '');
        var reLiWidth = $('.slides .slide.active').width();
        var wSlide = rerLi * reLiWidth;

        $('.slides').css({'left':-wSlide});
    }

    function prev() {
        var moveToTemp = $('.sc').find('.active').prev().attr('id');
         var hId = moveToTemp.replace('p-', '');
         hashTest(hId);
        $('.sc').find('.active').prev().addClass('active').siblings().removeClass('active');
        var moveTo2 = moveToTemp.replace('p-', '');
        var move = $('.slide').width() * moveTo2;

        if ($('.hmo').length) {
            $('#s-' + moveTo2).next().find('.top .home').fadeOut(function() {
                $('.slides').animate({
                    left:-move
                }, 1000, function() {
                    $('#s-' + moveTo2).find('.top .home').fadeIn();
                });
            });
//            if ($('#s-' + moveTo2).hasClass('light')) {
//
//                $('.log').fadeOut('slow', function() {
//                    $('.slides').animate({
//                        left:-move
//                    }, 1000, function() {
//                        $('.logo2').fadeIn();
//                    });
//
//                })
//            }
//            else {
//                  $('.logo2').fadeOut('slow', function() {
//                    $('.slides').animate({
//                        left:-move
//                    }, 1000, function() {
//                        $('.logo1').fadeIn();
//                    });
//
//                })
//            }
//
//        }


        }

        else {

            $('.slides').animate({
                left:-move
            }, 1000);
        }

    }

    function next() {
        var moveToTemp = $('.sc').find('.active').next().attr('id');
         var hId = moveToTemp.replace('p-', '');
         hashTest(hId);
        $('.sc').find('.active').next().addClass('active').siblings().removeClass('active');
        var moveTo2 = moveToTemp.replace('p-', '');
        var move = $('.slide').width() * moveTo2;


        if ($('.hmo').length) {
            $('#s-' + moveTo2).prev().find('.top .home').fadeOut(function() {
                $('.slides').animate({
                    left:-move
                }, 1000, function() {
                    $('#s-' + moveTo2).find('.top .home').fadeIn();
                });
            });
        }

        else {

            $('.slides').animate({
                left:-move
            }, 1000);
        }
    }

    function first() {
        if ($('.sc li:first').hasClass('active')) {

            $('.prev').css('visibility', 'hidden');
        }
        else {

            $('.prev').css('visibility', 'visible');
        }

        if ($('a.next').length) {
            $('a.next').remove();
        }
    }

    function last() {
        if ($('.sc li:last').hasClass('active')) {
            if ($('.sc li:last').hasClass('jump')) {
                $('span.next').css('visibility', 'hidden');
                $('.hmo').append('<a class="next" href="/about/"></a>');
                $('.hmo-en').append('<a class="next" href="/en/about/"></a>');
            }
            else {
                $('span.next').css('visibility', 'hidden');
            }

        }
        else {

            $('.next').css('visibility', 'visible');
        }
    }


    function windowheight() {

        if ($('.dma').length) {
            var wh = $(window).height();
            if (wh <= 685) {
                $('.slide .top').height(wh);
                $('.sc').css('top', '' + wh - 52 + 'px');
                $('.main-menu').css('top', '' + wh - 107 + 'px');
                $('.logw').css('top', '' + wh - 595 + 'px');
              //  $('.inna .map').css('top', '' + wh - 550 + 'px');
                $('.inna .next').css('top', '' + wh - 357 + 'px');
                $('.inna .prev').css('top', '' + wh - 357 + 'px');
            }
            else {
                wh = 685;
                $('.slide .top').height(wh);
                $('.sc').css('top', '' + wh - 52 + 'px');
                $('.main-menu').css('top', '' + wh - 107 + 'px');
                $('.logw').css('top', '' + wh - 595 + 'px');
              //  $('.inna .map').css('top', '' + wh - 550 + 'px');
                $('.inna .next').css('top', '' + wh - 357 + 'px');
                $('.inna .prev').css('top', '' + wh - 357 + 'px');
            }
        }
        else {
            var wh = $(window).height();
            $('.slide .top').height(wh);
            $('.sc').css('top', '' + wh - 52 + 'px');
            $('.main-menu').css('top', '' + wh - 107 + 'px');
            $('.logw').css('top', '' + wh - 595 + 'px');
          //  $('.inna .map').css('top', '' + wh - 550 + 'px');
            $('.inna .next').css('top', '' + wh - 357 + 'px');
            $('.inna .prev').css('top', '' + wh - 357 + 'px');
        }


    }

    $('.prev').live('click', function() {
        if ($('.hm').length) {
            prev();
            first();
            last()
        }
        else {
            if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
            {
               prev();
               first();
               last()
            }
            else {
                $(scrollElem).animate({scrollTop: 0}, 400, function() {
                    prev();
                    first();
                    last()
                });
            }
        }
    });

    $('.next').live('click', function() {
        if ($('.hm').length) {
            next();
            first();
            last()
        }
        else {
            if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
            {
               next();
               first();
               last()
            }
            else {
                $(scrollElem).animate({scrollTop: 0}, 400, function() {
                    next();
                    first();
                    last()

                });
            }
        }
    });


    $('.sc li').click(function() {

        var that = $(this);
        if ($('.hm').length) {

            var moveToTemp = that.attr('id');
            var moveTo = moveToTemp.replace('p', 's');
            var moveTo2 = moveToTemp.replace('p-', '');
            var move = $('.slide').width() * moveTo2;
            that.addClass('active').siblings().removeClass('active');
            $('.slide#' + moveTo + '').addClass('active').siblings().removeClass('active');
            $('.slides').animate({
                left:-move
            }, 1000, 'easeOutQuad');
            first();
            last()

        }
        
        if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
{
                var moveToTemp = that.attr('id');
                var hId = moveToTemp.replace('p-', '');
                hashTest(hId);

                var moveTo = moveToTemp.replace('p', 's');


                var moveTo2 = moveToTemp.replace('p-', '');
                var car = $('.sc .active').attr('id').replace('p-', '');


                var timer = Math.abs(moveTo2 - car);


                var move = $('.slide').width() * moveTo2;
                that.addClass('active').siblings().removeClass('active');
                $('.slide#' + moveTo + '').addClass('active').siblings().removeClass('active');
                $('.slides').animate({
                    left:-move
                }, 1200 * timer, 'easeOutQuad');
                first();
                last();

}

        else {


            $(scrollElem).animate({scrollTop: 0}, 400, function() {


                var moveToTemp = that.attr('id');
                var hId = moveToTemp.replace('p-', '');
                hashTest(hId);

                var moveTo = moveToTemp.replace('p', 's');


                var moveTo2 = moveToTemp.replace('p-', '');
                var car = $('.sc .active').attr('id').replace('p-', '');


                var timer = Math.abs(moveTo2 - car);


                var move = $('.slide').width() * moveTo2;
                that.addClass('active').siblings().removeClass('active');
                $('.slide#' + moveTo + '').addClass('active').siblings().removeClass('active');
                $('.slides').animate({
                    left:-move
                }, 1200 * timer, 'easeOutQuad');
                first();
                last()
            });


        }
    });

    $('.sc1 li').click(function() {
        var that = $(this);
        if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
        {
            var moveToTemp = that.attr('id');
            var hId = moveToTemp.replace('d-', '');
            hashTest(hId);

            var moveTo = moveToTemp.replace('d', 's');
            var liact = moveToTemp.replace('d', 'p');
            var moveTo2 = moveToTemp.replace('d-', '');
            var car = $('.sc .active').attr('id').replace('p-', '');


            var timer = Math.abs(moveTo2 - car);

            var move = $('.slide').width() * moveTo2;
            $('.sc li#' + liact + '').addClass('active').siblings().removeClass('active');
            $('.slide#' + moveTo + '').addClass('active').siblings().removeClass('active');
            $('.slides').animate({
                left:-move
            }, 1200 * timer, 'easeOutQuad');
            first();
            last()
        } 
        else {
        $(scrollElem).animate({scrollTop: 0}, 400, function() {

            var moveToTemp = that.attr('id');
            var hId = moveToTemp.replace('d-', '');
            hashTest(hId);

            var moveTo = moveToTemp.replace('d', 's');
            var liact = moveToTemp.replace('d', 'p');
            var moveTo2 = moveToTemp.replace('d-', '');
            var car = $('.sc .active').attr('id').replace('p-', '');


            var timer = Math.abs(moveTo2 - car);

            var move = $('.slide').width() * moveTo2;
            $('.sc li#' + liact + '').addClass('active').siblings().removeClass('active');
            $('.slide#' + moveTo + '').addClass('active').siblings().removeClass('active');
            $('.slides').animate({
                left:-move
            }, 1200 * timer, 'easeOutQuad');
            first();
            last()
        });
       }

    });


    totalwidth();
    correct();
    first();
    windowheight();

    $(window).resize(function() {
        totalwidth();
        correct();
        windowheight();

    });

    $('.sc1 li:not(".active")').hover(function() {
        $(this).find('img').stop().animate({
            opacity:1
        }, 1000);


    }, function() {
        $(this).find('img').stop().animate({
            opacity:0.55
        }, 1000);
    });


    // Tabs
    $('.tabl li').live('click', function() {
        var that = $(this);
        var cls = $(this).attr('class');
        var clsa = cls.replace('active ', '');
        that.parent().parent().next().find('.c' + clsa + '').show().addClass('active').siblings().hide().removeClass('active');
        that.addClass('active').siblings().removeClass('active');
    });
    // END Tabs


    // Contact tabs
    $('.conth .pla span').live('click', function() {
        var id = $(this).attr('id');
        $(this).addClass('active').siblings().removeClass('active');
        $('.ctabs .' + id).show().siblings().hide();
    });
    // End Contact tabs


    $('.err').live('click', function() {
        $(this).next().focus();
        $(this).parent().removeClass('error');
    });

    $('.sc1 img').css('opacity', '0.55');
    $('.sc1 .active img').css('opacity', '1');


    $('.pp a').click(function() {
        $('.bub').hide();
        $('.pp').removeClass('shown');
        $(this).parent().addClass('shown');
        $(this).parent().parent().find('.tclose').show();
        $(this).parent().find('.bub').show();
        return false;
    });

    $('.tclose').live('click', function() {
        $('.pp').removeClass('shown');
        $('.tclose').hide();
        $('.bub').hide();
    });


    $('.comp_char table tr:even').addClass('c_ch_even');
    $('.comp_char table td:last-child').css('border', '0');
    $('.comp_char table tr:last-child').addClass('c_ch_last');

    $('.char').live('click', function() {
        var td = parseInt($('.sc').find('.active').attr('id').replace('p-', ''));
        td = parseInt(td + 2);
        $('.comp_char table td').removeClass('active');
        $('.comp_char table td:nth-child(' + td + ')').addClass('active');
        $('.comp_char').css('visibility', 'visible').show().siblings().hide().parent().show();
        var odf = $('body').offset();
        $('.popup').css('top',''+-odf.top+'px');
        $('.fixed-in').show();
        return false;
    });

    $('.close').click(function() {
        $('.comp_char').hide().parent().hide();

    });
    $('.popup').each(function() {
        $(this).find('.lay_h_wrap_pp').hide();
        $(this).find('.lay_h_wrap_pp:first').show();
        $(this).find('.lay_h_w_inn').hide();
        $(this).find('.lay_h_w_inn:first-child').show();
    });


    $('.l_h_tabs li').click(function() {
        $(this).addClass('active').siblings().removeClass('active').parents('.lay_house').find('.lay_h_wrap_pp').hide().eq($(this).index()).show();
    });

    $('.l-h-t dl').click(function() {
        $(this).parents('li').addClass('active').siblings().removeClass('active').parents('.lay_h_wrap_pp').find('.lay_h_w_inn').hide().eq($(this).parents('li').index()).show();
    });


    // Smooth anchors
    if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
                {
                  // do nothing  
                }
                else {
                    function filterPath(string) {
                        return string
                                .replace(/^\//, '')
                                .replace(/(index|default).[a-zA-Z]{3,4}$/, '')
                                .replace(/\/$/, '');
                    }

                    var locationPath = filterPath(location.pathname);

                    var scrollElem = scrollableElement('html', 'body');

                    $('a[href*=#]').each(function() {
                        var thisPath = filterPath(this.pathname) || locationPath;
                        if (locationPath == thisPath
                                && (location.hostname == this.hostname || !this.hostname)
                                && this.hash.replace(/#/, '')) {
                            var $target = $(this.hash), target = this.hash;
                            if (target) {

                                $(this).click(function(event) {
                                    var targetOffset = $target.offset().top;
                                    event.preventDefault();
                                    $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
                                        location.hash = target;
                                    });
                                });
                            }
                        }
                    });
                }

    // use the first element that is "scrollable"
    function scrollableElement(els) {
        for (var i = 0, argLength = arguments.length; i < argLength; i++) {
            var el = arguments[i],
                    $scrollElement = $(el);
            if ($scrollElement.scrollTop() > 0) {
                return el;
            } else {
                $scrollElement.scrollTop(1);
                var isScrollable = $scrollElement.scrollTop() > 0;
                $scrollElement.scrollTop(0);
                if (isScrollable) {
                    return el;
                }
            }
        }
        return [];
    }

    // End Smooth anchors

    $(document).keypress(function(k) {
        if (k.ctrlKey && k.keyCode == 37 ) {
                 prev();
                first();
            last()
        }
           if (k.ctrlKey && k.keyCode == 39 || k.ctrlKey && k.charCode == 39) {
                 next();
                   first();
            last()
        }
    });



   
});

function hashPos(id) {
    if ($('.dma').length) {
    $('#s-' + id).addClass('active').siblings().removeClass('active');
    $('#p-' + id).addClass('active').siblings().removeClass('active');
        }

}

function hashTest(id) {
      if ($('.dma').length) {
    if (id == '0') {
        window.location.hash = 'pushkinskiy'
    }

    if (id == '1') {
        window.location.hash = 'voroncovskiy'
    }
    if (id == '2') {
        window.location.hash = 'risheleivskiy'
    }
    if (id == '3') {
        window.location.hash = 'potemkenskiy'
    }
    if (id == '4') {
        window.location.hash = 'aleksandrovskiy'
    }
    if (id == '5') {
        window.location.hash = 'razumovskiy'
    }
}
}
