/// <reference path="jquery-1.4.1-vsdoc.js" />

function bookmarksite(title, url) {
    if (document.all)
        window.external.AddFavorite(url, title);
    else if (window.sidebar)
        window.sidebar.addPanel(title, url, "")
}

(function () {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            //try {
            return new Array(n + 1).join(str);
            //} catch (err) {
            //return new Array(1).join(str);
            //}
        }        

        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first'),

                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = $items.length;
                //pages = Math.ceil($items.length / visible);

                var timer;

            
            /// This is old code fore View description
            //$wrapper.after('<span class="FotoHint"></span>');
            //var $informer = $('> span', this)

            //$($informer).html($single.find('> a').attr('title'));

            /* TASKS */

            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }

            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');

            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);            

            // 4. paging function
            /*//This move to all line
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;

                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft: '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }

                    //$informer.html($items.filter(":not(.cloned):eq(" + (page - 1) + ")").children().attr('title'));

                    currentPage = page;
                });
            }*/

            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * 1 * n;

                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft: '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == -4) {
                        page = pages - 4;
                        $wrapper.scrollLeft(singleWidth * pages);
                    }

                    //$informer.html($items.filter(":not(.cloned):eq(" + (page - 1) + ")").children().attr('title'));

                    currentPage = page;
                });
            }

            function SetTimer () {
                timer = setTimeout(function () {
                                    gotoPage(currentPage + 1);
                                    SetTimer ();
                                    return false;
                                    },3000);
                return false;               
            }

            function StopTimer() {
                clearTimeout(timer);
                return false;
            }

            SetTimer();

            // 5. insert the back and forward link
            $wrapper.after('<a href="#" class="arrow back"></a><a href="#" class="arrow forward"></a>');

            // 6. bind the back and forward links
            $('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });

            $('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });

            $('a.back', this).mouseover(function () {
                StopTimer();
                return false;
            });

            $('a.forward', this).mouseover(function () {
                StopTimer();
                return false;
            });

            $('a.back', this).mouseleave(function () {
                SetTimer();
                return false;
            });

            $('a.forward', this).mouseleave(function () {
                SetTimer();
                return false;
            });

            $wrapper.mouseover(function () {
                StopTimer();
                return false;
            });

            $wrapper.mouseleave(function () {
                SetTimer();
                return false;
            });

            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });

            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);


function MakeLeftBlock() {
    var nodeTemp = $('.SyteGuest');
    if (nodeTemp.length > 0) {
        $(nodeTemp).each(function (Index, Element) {
            var FirstH2 = $(Element).find("h2:first");
            FirstH2.wrap('<div class="HeaderH2">');

            var FirstIMG = $(Element).find("img:first");
            var FirstImgContaner = FirstIMG.parent();

            $(Element).find('.HeaderH2').after('<div class="HeaderIMG">');
            var IMGContaner = $(Element).find('.HeaderIMG');

            FirstIMG.appendTo(IMGContaner);
            FirstImgContaner.detach();

            IMGContaner.append('<div class="LeftSideIMGBorder">');
            IMGContaner.prepend('<div class="LeftSideIMGBorder">');
        });
    }
}

function AddCurentPageToFavorite() {
    bookmarksite(document.title, document.URL);
}

function MakeTopBlocks () {
    $('.TopTextBlocksNodes>.TopTextBlocks').each(function(Index, Element) {
        var ParentNode = $(Element);
        ParentNode.find('img:first').appendTo(ParentNode).removeAttr('width').removeAttr('height');

        ParentNode.append('<div class="ShortTextElementHeader">');
        ParentNode.children('.ShortTextElementHeader').html(ParentNode.find('h2:first').html());

        ParentNode.append('<div class="ShortTextElementSlider">');
        var Slider = ParentNode.children('.ShortTextElementSlider');
        Slider.append('<div>');
        Slider.append(ParentNode.find('a:first'));
        Slider.append('<a href="'+ParentNode.find('a:first').attr('href')+'" class="Array">');
        ParentNode.children('h2').detach();
        ParentNode.children('.NewsTextBlock').detach();

        ParentNode.mouseover(function() {
            $(Slider).animate({ bottom : '22px' }, 500);
        });

        ParentNode.mouseleave(function() {
            $(Slider).animate({ bottom : '-54px' }, 500);
        });

    });
}


$(document).ready(function () {
    MakeLeftBlock();
    MakeTopBlocks ();
});
