/*!
  jQuery outside events - v1.1 - 3/16/2010
  http://benalman.com/projects/jquery-outside-events-plugin/ 
  Copyright (c) 2010 "Cowboy" Ben Alman
  Dual licensed under the MIT and GPL licenses.
  http://benalman.com/about/license/
 */(function($,doc,outside){'$:nomunge';$.map('click dblclick mousemove mousedown mouseup mouseover mouseout change select submit keydown keypress keyup'.split(' '),function(event_name){jq_addOutsideEvent(event_name);});jq_addOutsideEvent('focusin','focus'+outside);jq_addOutsideEvent('focusout','blur'+outside);$.addOutsideEvent=jq_addOutsideEvent;function jq_addOutsideEvent(event_name,outside_event_name){outside_event_name=outside_event_name||event_name+outside;var elems=$(),event_namespaced=event_name+'.'+outside_event_name+'-special-event';$.event.special[outside_event_name]={setup:function(){elems=elems.add(this);if(elems.length===1){$(doc).bind(event_namespaced,handle_event);}},teardown:function(){elems=elems.not(this);if(elems.length===0){$(doc).unbind(event_namespaced);}},add:function(handleObj){var old_handler=handleObj.handler;handleObj.handler=function(event,elem){event.target=elem;old_handler.apply(this,arguments);};}};function handle_event(event){$(elems).each(function(){var elem=$(this);if(this!==event.target&&!elem.has(event.target).length){elem.triggerHandler(outside_event_name,[event.target]);}});};};})(jQuery,document,"outside");
var time='';
var check=0;
var current_here=6;
function topstorycounter(vecto)
{
    var size = $('#list_items>div').size();
    var cur=$('#dw_show'+current_here+'');    
    $('#dw_show'+current_here+'').hide();
    $('#here'+current_here+'').removeClass();
    
    if(vecto==1)
    {
        current_here=parseInt(current_here)+1;    
    }
    else
    {
        current_here=parseInt(current_here)-1;    
    }
    if(current_here > size) current_here = 1;
    else if(current_here == 0)
        current_here = size;
    var next_cur = $('#dw_show'+current_here+'');
    cur.hide();
    next_cur.show();    
    $('#here'+current_here+'').removeClass().addClass('active');
    clearTimeout(time);
    start();

}
function start(){
    time = setInterval(function(){topstorycounter(1);},5000);
    //check=0;
}
function change_slide(num)
{
    clearInterval(time);
    if(current_here!=num)
    {
        $('#dw_show'+current_here+'').hide();
        $('#here'+current_here+'').removeClass();
        current_here=num;
        $('#here'+num+'').removeClass().addClass('active');
        $('#dw_show'+num+'').show();
    }
}
$(function(){
    $('#list_items>div:not(:first-child)').hide();
    //time = setInterval(function(){topstorycounter(1);},5000);    
    topstorycounter(1);
    $("a#next").click(function(){
        topstorycounter(1);
        $(this).parent('li').addClass('active');
        $('#stop_slide').parent('li').removeClass();
        $('#prev').parent('li').removeClass();
    });
    
    $("a#prev").click(function(){
        topstorycounter(2);
        $(this).parent('li').addClass('active');
        $('#stop_slide').parent('li').removeClass();
        $('#next').parent('li').removeClass();
    });
    
    $('#stop_slide').click(function(){
        if($(this).parent('li').attr('class')=='')
        {
            $(this).parent('li').addClass('active');
            $('#prev').parent('li').removeClass();
            $('#next').parent('li').removeClass();
            clearInterval(time);
        }
        else
        {
            $(this).parent('li').removeClass();
            setTimeout(function(){start()},5000);
            check=1;
        }    
    }); 
});
