﻿

$(document).ready(function () {

    //center splash page
    $('#Wrapper_Splash').css('margin-top', $(document).height() / 2 - $('#Wrapper_Splash').height() / 2);
    $(window).resize(function () {
        $('#Wrapper_Splash').css('margin-top', $(document).height() / 2 - $('#Wrapper_Splash').height() / 2);
    });


    if (location.href.indexOf('public-architecture-and-interiors') > 0) {
        $(".ModDnnForgeChildLinksC").children('a:first').addClass('current');
    }

    $("#MainMenu li:last-child a").css('border-right', 'none');
    if ($("#HouseMenuNavCurrentItem").parent().parent().attr('id') != null) {
        if ($("#HouseMenuNavCurrentItem").parent().parent().attr('id').match("HouseMenuNavItem")) {
            $($("#HouseMenuNavCurrentItem").parent().parent()).attr('id', 'HouseMenuNavCurrentItem')
                                       .find("a").css('border-right', 'none');
        }
    }


    $("#HouseMenuNavCurrentItem > a").css('border-right', 'none');
    var names = location.pathname.toUpperCase().split('/');
    var currentTopLevelName = names[0] == "" ? names[1] : names[0];

    //change & for |
    $("#HouseMenuNav li").each(function () {
        if ($(this).text() == currentTopLevelName) {
            $(this).attr('id', 'HouseMenuNavCurrentItem').find("a").css('border-right', 'none');
        }
        $(this).find('a:first').html($(this).find('a:first').html().replace('&amp;', '<span class="gray">|</span>'));
    });



    //short codes
    $("a[title]:contains('[highlight]'), h3:contains('[highlight]')").each(function (index) {
        var newText = $(this).text().replace('[highlight]', '<span class="highlight">').replace('[/highlight]', '</span>');
        $(this).html(newText);
    });


    if ($.browser.msie && parseInt($.browser.version) <= 7) {
        $('.ModDnnForgeChildLinksC').css('cssText', 'left:42px; top:-16px; ').find('a:first').css('cssText', 'position:absolute; left:-46px; top:16px;');
        $('#lastSubMenu').find('a:first').css('cssText', 'position:absolute; left:-73px; top:16px;');
    }

    //select links in menu
    if (location.href.indexOf('public-architecture-and-interiors') > 0) {
        $('#ChildLinksContainer1').find('a:first').addClass('current');
    }
    else if ((location.href.indexOf('people') > 0) && (location.href.indexOf('/corporate-events') < 0) && (location.href.indexOf('/festivals-events') < 0)) {
        $('#ChildLinksContainer2').find('a:first').addClass('current');
    }

    //when click on thumbnail - update large image
    $(".dispLargeImage").livequery('click', function (event) {
        var largeImagePath = $(this).attr('rel');
        $("#MainBigImg").attr('src', largeImagePath);
        return false;
    });


    //thumbnail carouselle
    if ($('div.thumb-images').length > 0) {
        var table = $('div.thumb-images table');
        $('div.thumb-images').append('<ul>');
        var newList = $('div.thumb-images ul');
        var thcount = 0;
        var totalWidth = 0;
        var fLiWidth = 0;
        table.find('td').each(function () {
            if ($(this).html() != '') {
                thcount += 1;
                var imgwidth = $(this).find('img').width();
                totalWidth += imgwidth + 10;
                var link = $(this).find('a')
                var img = $(this).find('img:first');
                img.attr('width', imgwidth);

                var liText = '<li width="' + imgwidth + '"><a rel="' + link.attr('rel') + '" href="' + link.attr("href") + '" class="dispLargeImage"><img src="' + img.attr('src') + '" width="' + img.attr('width') + '" height="' + img.attr('height') + '" /></a></li>';

                newList.append(liText);
            }
        })
        //hide table
        table.remove();

        //move the last list item before the first item. The purpose of this is if the user clicks previous he will be able to see the last item.  

        if (totalWidth >= 820) {

            if ($.browser.msie && parseInt($.browser.version) <= 7) {

                $('div.thumb-images').css('cssText', 'position:relative; ')
                $('div.thumb-images ul').css('cssText', 'overflow:hidden!important;')

                $('div.thumb-images ul li').each(function () {
                    $(this).css('cssText', 'width:' + $(this).attr('width') + 'px!important; display:inline-block!important; overflow:hidden;');
                })

                $('div.thumb-images ul li:first').before($('div.thumb-images ul li:last'));
                $('div.thumb-images ul').css('left', ($('div.thumb-images ul li:first').attr('width')) * (-1) - 15);


                //when user clicks the image for sliding right  
                $('div.ftitemSliderRight').click(function () {

                    //get the width of the items ( i like making the jquery part dynamic, so if you change the width in the css you won't have o change it here too ) '  

                    var item_width = parseInt($('div.thumb-images ul li:eq(1)').attr('width')) + 10;

                    //calculate the new left indent of the unordered list  
                    var left_indent = parseInt($('div.thumb-images ul').css('left')) - item_width; //item_width * (-1); //parseInt($('div.thumb-images ul').css('left')) - item_width;

                    $('div.thumb-images ul').animate({
                        left: left_indent
                    }, 300, function () {
                        $('div.thumb-images ul li:last').after($('div.thumb-images ul li:first'));
                        var newindent = parseInt($('div.thumb-images ul').css('left')) + $('div.thumb-images ul li:last').outerWidth() + 10;
                        $('div.thumb-images ul').css('left', newindent);
                    });
                });

            }
            else {

                $('div.thumb-images ul li:first').before($('div.thumb-images ul li:last'));
                $('div.thumb-images ul').css('left', ($('div.thumb-images ul li:first').outerWidth()) * (-1) - 15);


                //when user clicks the image for sliding right  
                $('div.ftitemSliderRight').click(function () {

                    //get the width of the items ( i like making the jquery part dynamic, so if you change the width in the css you won't have o change it here too ) '  

                    var item_width = $('div.thumb-images ul li:eq(1)').outerWidth() + 10;

                    //calculate the new left indent of the unordered list  
                    var left_indent = parseInt($('div.thumb-images ul').css('left')) - item_width; //item_width * (-1); //parseInt($('div.thumb-images ul').css('left')) - item_width;

                    $('div.thumb-images ul').animate({
                        left: left_indent
                    }, 300, function () {
                        $('div.thumb-images ul li:last').after($('div.thumb-images ul li:first'));
                        var newindent = parseInt($('div.thumb-images ul').css('left')) + $('div.thumb-images ul li:last').outerWidth() + 10;
                        $('div.thumb-images ul').css('left', newindent);
                    });
                });
            }
        }
        else {

            if (totalWidth < 810) {
                $('.ftitemSliderRight').css('display', 'none');
            }
        }
    }


    //changes for Blocked Sliders (Property Agent Module and Simple Gallery)
    if ($('#BlockedSlider').length > 0) {
        var table = $('#BlockedSlider').find('table');

        if (table.length == 0)
            table = $('table.View');

        var list = $('#BlockedSlider').find('ul');
        var NumInRow = 5;
        var NumOfRows = 2;

        var count = 0;
        $('.PropertyBlock').each(function () {
            count += 1;
            if (count % (NumInRow * NumOfRows) == 1) {
                list.append('<li class="BlockedSliderItem"></li>');
            }
            list.children('li:last-child').append($(this).parent().html());
        })
        table.remove();

        var autoVal = 0;
        var ShowArrows = false;
        if (count > (NumInRow * NumOfRows)) {
            //autoVal = 7000;
            ShowArrows = true;
        }
        //slider
        $(document).ready(function ($) {
            $("#property-images").jCarouselLite(
            {
                btnNext: ".ftnext",
                btnPrev: ".ftprev",
                speed: 350,
                auto: autoVal,
                visible: 1
            });
        });

        //hide arrows if number of images less than NumInRow * NumOfRows
        if (ShowArrows == false) {
            $('#BlockedSliderLeft').css('display', 'none');
            $('#BlockedSliderRight').css('display', 'none');
        }
    }

    //Property Agent second Popup Image
    $('a.PopImageLink').each(function () {
        var LinkContainer = $(this).siblings('div.LinkContainer');
        var link = LinkContainer.find('a.dispLargeImage:eq(1)').attr('rel');
        $(this).attr('href', link);
    })

    //images lightbox
    if ($('a[rel*="lightbox"]').length > 0) {
        $('a[rel*="lightbox"]').fancybox({
            'transitionIn': 'none',
            'transitionOut': 'none',
            'titlePosition': 'over',
            'titleShow': false
        });
    }


    //fix IE7-6 property Slider
    if ($.browser.msie && parseInt($.browser.version) <= 7) {

        $('div.PropertyBlock a').css('cursor', 'pointer');
        $('div.PropertyBlock a[rel!="lightbox"]').click(function () {
            location.href = $(this).attr('href');
        })

        //        $('div.PropertyBlock').each(function () {
        //            $(this).children('a').attr('href', $(this).children('a:first').attr('href')).css('cssText', 'cursor:pointer;');
        //        })



    }

    $("#HouseMenuNavCurrentItem").prev().children('a').css('border-right', 'none').addClass('NoRightBorder');


    //hide property view's header
    $('div.ModPropertyAgentC').children('table:first').find('td:first').html('');
    $('div.ModPropertyAgentC').children('table:first').find('td:eq(1)').find('a').css('cssText','color:#fff!important;')


    $('#HouseMenuNav li a').each(function () {
        $(this).attr('rel', $(this).attr('href')).attr('href', '#');

    })



    //ajax save img num to session
    $('#HouseMenuNav li a').click(function () {

        var link = $(this).attr('rel');

        if ((location.href.indexOf('home') > 0) || (location.href.indexOf('architecture') > 0) || (location.href.indexOf('portrait') > 0)) {
            if ($('#mycarousel').length > 0) {

                var img = $('#mycarousel').children('.active:first').index();
                $.ajax({
                    type: "POST",
                    url: "/DesktopModules/BrentWinstoneSlider/JSonAjax.aspx/SaveImgToSession",
                    contentType: "application/json; charset=utf-8",
                    data: "{ num: " + img + "}",
                    dataType: "json",
                    success: function (data) {
                        //alert(data.d);
                        location.href = link;
                    },
                    error: function (data) {
                        location.href = link;
                    }
                });
            }
            else {
                location.href = link;
            }
        }
        else {
            location.href = link;
        }


    })

});


