//Scripting Classic Mode by Kunal Bhardwaj
var busy_trans = false;
var feat_count;
var feat_interval;
var feat_old = 0;
var feat_current_mapped = 0;
var feat_up;
var first_id;
var thumb_up;
var ordered_feats = new Array();

function featRotate() {
    var next_ordered = (feat_current_mapped + 1) % feat_count;
    sgSwitch(ordered_feats[next_ordered]);
}

function sgSwitch(next_story_id) {
    if (busy_trans) return false;
    busy_trans=true;
    var thumb_next = '#' + next_story_id;
    var id_next   = thumb_next.substring(10);
    var feat_next  = '#sg_feat_' + id_next;
    if (thumb_up != thumb_next) {
        $(thumb_up + ' span').css("display","none");
        $(thumb_next + ' span').css("display","inline");
        if($.browser.msie) { // IE7 flickers with the slide effect
            $(feat_up).hide();
            $(feat_next).show();
            busy_trans=false;
        } else {
            //$(feat_up).slideToggle(1000, function(){$(feat_next).slideToggle(1000,function(){busy_trans=false})});
            $(feat_up).fadeOut(500, function(){$(feat_next).fadeIn(500,function(){busy_trans=false})});
        }
        thumb_up=thumb_next;
        feat_up=feat_next;
    }
    for(var i=0;i<ordered_feats.length;i++){
        if (ordered_feats[i] == 'sg_thumb_' + id_next) {
            feat_current_mapped = i;
        }
    }
}

$(document).ready(function() {
    feat_up = '#' + $("#sg_feats > div:not(.none)").attr('id');// Determine what story is initially up (00-15)
    first_id = feat_up.substring(9);
    thumb_up = '#sg_thumb_'+first_id;
    $(thumb_up + ' span').css("display", "inline"); // set the initial thumbnail

    feat_count = $("li[id^=sg_thumb_].story").size(); //number of stories
    setTimeout(function() { feat_interval = setInterval(featRotate,9000); }, 2000);

    // create a mapping for 0-n for thumb LIs
    $("li[id^=sg_thumb_].story").each(function(){
        ordered_feats.push($(this).attr('id'));
    });
    
    // figure out the index of ordered_feats that is up initially
    for(var i=0;i<ordered_feats.length;i++){
        if (ordered_feats[i] == 'sg_thumb_' + first_id) {
            feat_current_mapped = i;
        }
    }
    
    //highlight thumbs on hover
    $("#sg_thumbs ul li img").hoverIntent({
        sensitivity: 7, 
        interval: 200, 
        over: function() {$(this).fadeTo(500,0.5)}, 
        timeout: 250, 
        out: function() {$(this).fadeTo(250,1)}
    });

    // handle switches and stop auto rotations
    $("li[id^=sg_thumb_].story").click(function(){clearInterval(feat_interval);sgSwitch($(this).attr('id'))});
    $("a.sg-prev").click(function(){clearInterval(feat_interval);sgSwitch(ordered_feats[(((feat_current_mapped - 1) % feat_count) + feat_count) % feat_count])}); //beware the negative mod bug
    $("a.sg-next").click(function(){clearInterval(feat_interval);sgSwitch(ordered_feats[(feat_current_mapped + 1) % feat_count])});

});
