$(document).ready(function() {


    if ($('#slider_container > div')) {
        if ($('#promoimgscontainer').children().length < 2) {

            //clearTimeout(tid);
            $('#numbers').remove();
        }
    }

    $("div.navigation_category_list div.category").hover(
        function() { $(this).addClass('hover'); },
        function() { $(this).removeClass('hover'); });
    $("div.navigation_category_all_items").hover(
        function() { $(this).addClass('hover'); },
        function() { $(this).removeClass('hover'); });
});

function global_initializer(data) {
    
    //Update the collapsers and expanders
    $(data).find('div.expander_header div.button').hover(
        function() { $(this).addClass('hover'); },
        function() { $(this).removeClass('hover'); }
    );
    $(data).find('div.expander_header div.button').each(function() {
        if ($(this).hasClass('expanded')) {
            $(this).html('collapse');
        }
        else {
            $(this).html('expand');
        }
    });
    $(data).find('div.expander_header div.button').click(function() {
        if ($(this).hasClass('expanded')) {
            $(this).removeClass('expanded');
            $(this).parent().next().slideUp();
            $(this).html('expand');
        }
        else {
            $(this).addClass('expanded');
            $(this).parent().next().slideDown();
            $(this).html('collapse');
        }
    });

    $(data).find('div.spotlight_bio_excerpt div.more a').click(function() {
        $('div.spotlight_bio_excerpt').hide();
        $('div.spotlight_bio_full').show();
        return false;
    });
    $(data).find('div.spotlight_bio_full div.less a').click(function() {
        $('div.spotlight_bio_excerpt').show();
        $('div.spotlight_bio_full').hide();
        return false;
    });

    $(data).find('div.navigation_category_all_items a').click(function() {
        if ($('div.navigation_category_extender').hasClass('active')) {
            $('div.navigation_category_extender').removeClass('active').slideUp();
        }
        else {
            $('div.navigation_category_extender').addClass('active').slideDown();
        }
        return false;
    });

    $(data).find('div.navigation_category_search input.submit').click(function() {
        window.location = "/Search.aspx?search=" + escape($('div.navigation_category_search input.textbox').attr('value'));
        return false;
    });

    $(data).find('a.tv_player_link').click(function() {
        $('a.tv_player_link').removeClass('active');
        $('div.tv_videos_slider div.item').removeClass('active');
        $(this).addClass('active');
        if ($(this).next()[0].tagName == 'DIV') {
            $(this).next().next().addClass('active');
        }
    });
    $(data).find('div.tv_videos_slider div.item').click(function() {
        $('a.tv_player_link').removeClass('active');
        $('div.tv_videos_slider div.item').removeClass('active');
        $(this).parent().find('a.tv_player_link').addClass('active');
        $(this).addClass('active');
    });

    $(data).find('a.spotlight_player_link').click(function() {
        $('a.spotlight_player_link').removeClass('active');
        $('div.spotlight_videos_slider div.item').removeClass('active');
        $(this).addClass('active');
        if ($(this).next()[0].tagName == 'DIV') {
            $(this).next().next().addClass('active');
        }
        if ($(this).attr('href').indexOf('#video') == 0) {
            $('div.spotlight_player').load(
                '/Template/Ajax/SpotlightVideoPlayer.aspx?ID=' + $(this).attr('href').replace('#video', '') + '&_=' + parseInt(Math.random() * 99999999),
                function() { global_initializer($('div.spotlight_player')); }
            );
            return false;
        }
    });

    $(data).find('div.spotlight_videos_slider a.button').hover(
        function() { if (!$(this).next().hasClass('active')) $(this).next().addClass('hover'); },
        function() { $(this).next().removeClass('hover'); }
    );

    $(data).find('div.tv_videos_slider a.button').hover(
        function() { if (!$(this).next().next().hasClass('active')) $(this).next().next().addClass('hover'); },
        function() { $(this).next().next().removeClass('hover'); }
    );

    $(data).find('div.tv_videos_slider div.item').hover(
        function() { if (!$(this).hasClass('active')) $(this).addClass('hover'); },
        function() { $(this).removeClass('hover'); }
    );

    $(data).find('div.spotlight_images_slider ul a').lightBox({ fixedNavigation: true });

    $(data).find('div.spotlight_scroller div').hover(
        function(){ if ($(this).hasClass('active')) $(this).addClass('hover'); },
        function(){ $(this).removeClass('hover'); }
    );
    $(data).find('div.spotlight_scroller div').mousedown(
        function() { if ($(this).hasClass('active')) $(this).addClass('click'); }
    );
    $(data).find('div.spotlight_scroller div').mouseup(
        function() { $(this).removeClass('click'); }
    );

    $(data).find('div.spotlight_images_slider ul').each(function() {
        items = $(this).find('li');
        columns = 3;
        $(this).css('height', (parseInt(items.length / columns) + (items.length % columns == 0 ? 0 : 1)) * items.first().height());

        if ($(this).height() > $(this).parent().height()) {
            $('div.spotlight_images_down').addClass('active');
        }
    });

    $(data).find('div.spotlight_videos_slider ul').each(function() {
        items = $(this).find('li');
        columns = 1;
        $(this).css('height', (parseInt(items.length / columns) + (items.length % columns == 0 ? 0 : 1)) * items.first().height());

        if ($(this).height() > $(this).parent().height()) {
            $('div.spotlight_videos_down').addClass('active');
        }
    });

    $(data).find('div.tv_videos_slider ul').each(function() {
        items = $(this).find('li');
        columns = 1;
        $(this).css('height', (parseInt(items.length / columns) + (items.length % columns == 0 ? 0 : 1)) * items.first().height());

        if ($(this).height() > $(this).parent().height()) {
            $('div.tv_videos_down').addClass('active');
        }
    });

    $(data).find('div.spotlight_images_up').click(function() {
        if ($(this).hasClass('active')) {
            ul = $('div.spotlight_images_slider ul');
            div = ul.parent();

            if (parseInt(ul.css('top')) + div.height() >= 0) {
                $(this).removeClass('active');
                $(this).removeClass('hover');
                $(this).removeClass('click');
            }

            if (ul.height() - parseInt(ul.css('top')) + div.height() > div.height()) {
                $('div.spotlight_images_down').addClass('active');
            }

            ul.animate({ top: '+=' + div.height() }, 400);
        }
    });

    $(data).find('div.spotlight_images_down').click(function() {
        if ($(this).hasClass('active')) {
            ul = $('div.spotlight_images_slider ul');
            div = ul.parent();

            if (ul.height() - parseInt(ul.css('top')) - div.height() <= div.height()) {
                $(this).removeClass('active');
                $(this).removeClass('hover');
                $(this).removeClass('click');
            }

            if (parseInt(ul.css('top')) - div.height() < 0) {
                $('div.spotlight_images_up').addClass('active');
            }

            ul.animate({ top: '-=' + div.height() }, 400);
        }
    });

    $(data).find('div.spotlight_videos_up').click(function() {
        if ($(this).hasClass('active')) {
            ul = $('div.spotlight_videos_slider ul');
            div = ul.parent();

            if (parseInt(ul.css('top')) + div.height() >= 0) {
                $(this).removeClass('active');
                $(this).removeClass('hover');
                $(this).removeClass('click');
            }

            if (ul.height() - parseInt(ul.css('top')) + div.height() > div.height()) {
                $('div.spotlight_videos_down').addClass('active');
            }

            ul.animate({ top: '+=' + div.height() }, 400);
        }
    });

    $(data).find('div.spotlight_videos_down').click(function() {
        if ($(this).hasClass('active')) {
            ul = $('div.spotlight_videos_slider ul');
            div = ul.parent();

            if (ul.height() - parseInt(ul.css('top')) - div.height() <= div.height()) {
                $(this).removeClass('active');
                $(this).removeClass('hover');
                $(this).removeClass('click');
            }

            if (parseInt(ul.css('top')) - div.height() < 0) {
                $('div.spotlight_videos_up').addClass('active');
            }

            ul.animate({ top: '-=' + div.height() }, 400);
        }
    });

    $(data).find('div.tv_videos_up').click(function() {
        if ($(this).hasClass('active')) {
            ul = $('div.tv_videos_slider ul');
            div = ul.parent();

            if (parseInt(ul.css('top')) + div.height() >= 0) {
                $(this).removeClass('active');
                $(this).removeClass('hover');
                $(this).removeClass('click');
            }

            if (ul.height() - parseInt(ul.css('top')) + div.height() > div.height()) {
                $('div.tv_videos_down').addClass('active');
            }

            ul.animate({ top: '+=' + (div.height() - 40) }, 400);
        }
    });

    $(data).find('div.tv_videos_down').click(function() {
        if ($(this).hasClass('active')) {
            ul = $('div.tv_videos_slider ul');
            div = ul.parent();

            if (ul.height() - parseInt(ul.css('top')) - div.height() <= div.height()) {
                $(this).removeClass('active');
                $(this).removeClass('hover');
                $(this).removeClass('click');
            }

            if (parseInt(ul.css('top')) - div.height() < 0) {
                $('div.tv_videos_up').addClass('active');
            }

            ul.animate({ top: '-=' + (div.height() - 40) }, 400);
        }
    });

    //$(data).find('div.spotlight_images_slider').easySlider({ orientation: 'vertical', colums: 3 });

    $(data).find('div.spotlight_profile_id').each(function() {
        $('div.spotlight_player').load(
            '/Template/Ajax/SpotlightSlider.aspx?ID=' + $(this).text() + '&_=' + parseInt(Math.random() * 99999999),
            function() {
                global_initializer($('div.spotlight_player'));
                activate_spotlight_slider(0);
                active_spotlight_slider_interval = setInterval(function() {
                    if (active_spotlight_slider_interval != 0) {
                        activate_spotlight_slider(++active_spotlight_slider_offset);
                    }
                }, 5000);
            }
        );
    });

    $(data).find('div.spotlight_slider li').each(function() {
        $('div.spotlight_slider div.dots').append('<div class="dot">dot' + active_spotlight_slider_count + '</div>');
        $('div.spotlight_slider div.dots div.dot').last().each(function() {
            $(this).attr('slider_offset', active_spotlight_slider_count);
            $(this).hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
            $(this).click(function() {
                active_spotlight_slider_interval = 0;
                activate_spotlight_slider($(this).attr('slider_offset'));
            });
        });
        ++active_spotlight_slider_count;
    });

    $(data).find('input.volusion_login').click(function() {
        username = $('input.volusion_username').val();
        password = $('input.volusion_password').val();
        $('form').attr('action', 'https://store.theorphan.com/login.asp');
        $('form').attr('method', 'post');
        $('form').find('input').remove();
        $('form').append('<input type="hidden" name="CustomerNewOld" value="old">');
        $('form').append('<input type="hidden" name="email" value="' + username.replace('"', '\\"') + '">');
        $('form').append('<input type="hidden" name="password" value="' + password.replace('"', '\\"') + '">');
        $('form').submit();
        return (false);
    });

    $(data).find('div.blog_archive_header').click(function() {
        $(this).parent().toggleClass('active');
    });

    $(data).find('a.ajax_category_link').click(function() {
        if ($(this).next().hasClass('ajax_id')) {
            $('input.tv_player_category_id').attr('value', $(this).next().text());
            $('input.tv_player_category_id_hidden').attr('value', '');
            __doPostBack('ctl00$Content$LoadCategoryButton', '');
            return false;
        }
    });

    $(data).find('a.ajax_video_link').click(function() {
        if ($(this).next().hasClass('ajax_id')) {
            $('input.tv_player_video_id').attr('value', $(this).next().text());
            $('input.tv_player_video_id_hidden').attr('value', '');
            __doPostBack('ctl00$Content$LoadVideoButton', '');
            return false;
        }
    });

    $(data).find('#nav_search_text').keypress(function(event) {
        if (event.which == 13) {
            submit_category_search($('#nav_search'));
            event.preventDefault();
        }
    });

    $(data).find('#nav_search').click(function() {
        submit_category_search($(this));
    });
}

function submit_category_search(item) {
    if (item.hasClass('nav_search_video')) {
        window.location = "/Search.aspx?contentType=video&search=" + escape($('#nav_search_text').attr('value'));
    }
    else if (item.hasClass('nav_search_blog')) {
        window.location = "/Search.aspx?contentType=blog&search=" + escape($('#nav_search_text').attr('value'));
    }
    else if (item.hasClass('nav_search_spotlight')) {
        window.location = "/Search.aspx?contentType=spotlight&search=" + escape($('#nav_search_text').attr('value'));
    }
    else {
        window.location = "/Search.aspx?search=" + escape($('#nav_search_text').attr('value'));
    }
}

$(document).ready(function() {
    global_initializer(document);
    var manager = Sys.WebForms.PageRequestManager.getInstance();
    manager.add_endRequest(EndRequestHandler);
    manager.add_pageLoaded(global_pageloaded_initializer);
});

function EndRequestHandler(sender, args) {
    if (args.get_error() != undefined) {
        var errorMessage = args.get_error().message;
        alert(errorMessage);
    }
}

function global_pageloaded_initializer(sender, args) {
    var panels = args.get_panelsUpdated();
    if (panels.length > 0) {
        var reload_addthis = false;
        for (i = 0; i < panels.length; i++) {
            global_initializer(panels[i]);
            if ($(panels[i]).find('.addthis_toolbox').length > 0) {
                reload_addthis = true;
            }
        }
        if (reload_addthis) {
            window.addthis.ost = 0;
            window.addthis.ready();
        }
    }
}

var active_spotlight_slider_count = 0;
var active_spotlight_slider_offset = 0;
var active_spotlight_slider_interval = 0;

function activate_spotlight_slider(offset) {
    var sliders = $('div.spotlight_slider li');

    if (offset < 0) {
        activate_spotlight_slider(sliders.length - 1);
        return;
    }
    else if (offset > (sliders.length - 1)) {
        activate_spotlight_slider(0);
        return;
    }

    active_spotlight_slider_offset = offset;
    if (sliders.length != 1) {
        sliders.fadeOut();
    }
    sliders = sliders.slice(offset).first();
    sliders.fadeIn();

    link = sliders.find('a');

    $('div.spotlight_slider div.title').html('<a href="' + link.attr('href') + '" target="' + link.attr('target') + '">' + link.attr('title') + '</a>');
    $('div.spotlight_slider div.dots div.dot').removeClass('active').slice(offset).first().addClass('active');

    if (sliders.length == 1) {
        active_spotlight_slider_interval = 0;
    }
}
