﻿var timeout;
var IE6;

$('.video').live('click', function() {
    var whichvideo = $(this).attr('id');
    $.ajax({
        type: "POST",
        url: "MinistryWatch.asmx/Video",
        data: "{'selectedvideo':'" + whichvideo + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $("#" + whichvideo).removeClass("video").html(msg.d);
        }
    });
});

$(document).ready(function() {
    preloadimages();
    RotateTopics();
    IE6 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent); //and IE 5.5
    $('#nexttopic').click(function() {
        clearTimeout(timeout);
        gotonextarticle(1); //go forward 1
        RotateTopics();
    });
    $('#lasttopic').click(function() {
        clearTimeout(timeout);
        gotonextarticle(-1); //go back 1
        RotateTopics();
    });
    $('#stoptopic').click(function() {
        clearTimeout(timeout);
    });

    $('#clickextra').html("Click to show/hide more");
    if ($("input#expandart").val() == "closed") {
        $("#extrafeed").hide();
    }
    $('#clickextra').live('click', function() {
        $("#extrafeed").slideToggle(600);
        //extra open/close values will allow the browser to remember the position for the "back button"
        if ($("input#expandart").val() == "closed") {
            $("input#expandart").val("open");
        }
        else {
            $("input#expandart").val("closed")
        }
    });
});

function RotateTopics() {
    timeout = setTimeout("gotonextarticle(1); RotateTopics()", 7000);
}

function gotonextarticle(sequence) {
    var x = $("#toparticles div:first").attr("id") * 1 + sequence;

    if (x >= LeadingDocuments.bindings.length) {
        x = 0;
    } 
    else if (x <= -1) {
        x = LeadingDocuments.bindings.length - 1;
    }
    $("#toparticles div:first").attr("id", x);

    if (IE6) { //corner fix
        $("#rotate a").css({ 'position': 'static' });
        $("#rotate").css({ 'position': 'relative' });    
    }
    $("#rotate").fadeOut("normal", function() {
        $("#rotate").html(LeadingDocuments.bindings[x].Image);
        if (IE6) {
            var filteredimage = LeadingDocuments.bindings[x].Image.replace(/.*?src=['"]([^'"]*)['"].*/, "$1");
            $("#rotate").css({ 'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../' + filteredimage + '\')' });
        }        
    });
    $("#rotate").fadeIn("normal", function() {
        if (IE6) {//corner fix, allow clicking <a>
            $("#rotate").css({ 'position': 'static' });
            $("#rotate a").css({ 'position': 'relative' });
        }
    });
}

function preloadimages() {
    for (var i = 0; i < LeadingDocuments.bindings.length - 1; i++) {
        pic1 = new Image(288, 233);
        pic1.src = "/" + LeadingDocuments.bindings[i].Image.replace(/.*?src=['"]([^'"]*)['"].*/, "$1");
    }
}