/// <reference path="libraries/jquery-vsdoc2.js" />

var runOnceScript = false;

function pageLoad() {

    $(document).ready(function () {

        if (!runOnceScript) {

            // Hide closing prizes panel and then integrate with latest prizes
            $('div#left-column div.closing-prizes').hide(function () {
                var ClosingPrizeList = $('div.panel-container', this).html();
                var ClosingPrizeLink = $('div.panel-option-container a', this).attr('href');
                var LatestPrizeList = $('div#left-column div.latest-prizes div.panel-container').html();
                var LatestPrizeLink = $('div#left-column div.latest-prizes div.panel-option-container a').attr('href');


                $('div#left-column div.latest-prizes div.panel-header h3').addClass('latest-tab').after('<h4 class="closing-tab"><span class="l"><span class="r"><a href="/">Closing Prizes</a></span></span></h4>');
                $('div#left-column div.latest-prizes div.panel-header h4 a').live('click', function (event) {
                    event.preventDefault();
                    if ($(this).parents('h4').attr('class') == 'closing-tab') {
                        $(this).parents('div.latest-prizes').children('div.panel-content').find('ul').fadeOut(function () {
                            $(this).parent('div.panel-container').html(ClosingPrizeList).fadeIn(function () {
                                $(this).parents('div.latest-prizes').children('div.panel-header').find('h4.closing-tab').replaceWith('<h3 class="closing-tab"><span class="l"><span class="r">Closing Prizes</span></span></h3>').end()
                                .parents('div.latest-prizes').children('div.panel-header').find('h3.latest-tab').replaceWith('<h4 class="latest-tab"><span class="l"><span class="r"><a href="/">Latest Prizes</a></span></span></h4>').end()
                                .parents('div.latest-prizes').children('div.panel-option-container').find('a').attr('href', ClosingPrizeLink).text('All closing prizes').end()
                                .parents('div.latest-prizes').children('div.panel-content').find('a').each(function () {
                                    $(this).jTruncate({
                                        stringlength: 120,
                                        displayType: 'text'
                                    });

                                });

                            });
                        });
                    } else {
                        $(this).parents('div.latest-prizes').children('div.panel-content').find('ul').fadeOut(function () {
                            $(this).parent('div.panel-container').html(LatestPrizeList).fadeIn(function () {
                                $(this).parents('div.latest-prizes').children('div.panel-header').find('h3.closing-tab').replaceWith('<h4 class="closing-tab"><span class="l"><span class="r"><a href="/">Closing Prizes</a></span></span></h4>').end()
                                .parents('div.latest-prizes').children('div.panel-header').find('h4.latest-tab').replaceWith('<h3 class="latest-tab"><span class="l"><span class="r">Latest Prizes</span></span></h3>').end()
                                .parents('div.latest-prizes').children('div.panel-option-container').find('a').attr('href', LatestPrizeLink).text('All latest prizes');
                            });
                        });
                    };
                });
            });

            var autoscrolling = true;

            $('div#carousel').infiniteCarousel();
            $('div#carousel span.prize-heading').addClass('enlarge');

            $('div#carousel').infiniteCarousel().mouseover(function () {
                autoscrolling = false;
            }).mouseout(function () {
                autoscrolling = true;
            });

            setInterval(function () {
                if (autoscrolling) {
                    $('div#carousel').trigger('next');
                }
            }, 4000);

            
            $('div#utility-panel div.sign-in ul.secondary-link').prepend('<li><a href="/" id="forgotten-password-link">Password forgotten?</a></li>');
            $('div#utility-panel div.forgotten').hide();
            $('div#utility-panel div.sign-in a#forgotten-password-link').click(function (event) {
                event.preventDefault();
                if ($('div#utility-panel div.forgotten').is(':visible')) {
                    $('div#utility-panel div.forgotten').slideUp('slow');
                } else {
                    $('div#utility-panel div.forgotten').slideDown('slow');
                }
            });

            AJAXRequest();

            runOnceScript = true;

        } else {
            AJAXRequest();
        };

        function AJAXRequest() {

            $('div#utility-panel input:submit').click(function () {
                ValidationItems();
                if ($('div#utility-panel div.sign-in fieldset div.validation-summary:visible').length > 1) {
                    $('div#utility-panel div.sign-in fieldset div.validation-summary:first').hide();
                }
                if (Page_IsValid) {
                    $('body').css('cursor', 'progress');
                };
            });

            if ($('div#utility-panel span.validation-item:visible')) {
                $('body').css('cursor', 'default');
            };

            function ValidationItems() {
                $('div#utility-panel fieldset ul li').each(function () {
                    if ($('span.validation-item', this).is(':visible')) {
                        $('span.validation-item', this).text("");
                    } else {
                        $('span.validation-item', this).text("");
                    };
                });
            }

            // Use lightbox technique for popup elements
            $("div#home-page-layout a.popup").mousedown(function (event) {
                event.preventDefault();
                var maintitle = this.title || $(this).text();
                $(this).lightBox({ iframeUrl: this.href, boxTitle: maintitle, iframeWidth: 700, iframeHight: 600 });
            });
        };

	    // Create spy effect for main winners panel
        $('div#home-page-layout div.shrink-wrapper ul').simpleSpy('1', '20000');
        $('div#home-page-layout div.shrink-wrapper div.spyWrapper').css('height', '190px').css('overflow', 'hidden');
    });

}
