/* ======================================================================
 * on tvlistning - i populate the top preview window
 * Code is element specific. Do not copy/paste without checking IDs
 * ======================================================================i
 */

//on dom ready - select all links below #content-gallery
$(function() {
        // our target
        
        var ontv = $("#tab-tvlist-nyhederne .wrapper");
        var ontv2 = $("#tab-tvlist-news .wrapper");
        //our data
        var list = $("#tab-tvlist-nyhederne ol.ontv li");
        var list2 = $("#tab-tvlist-news ol.ontv li");
       
        //our image target 
        var image = ontv.children(".image");
        var image2 = ontv2.children(".image");
        // Evilhack to make image work in IE7 (THE DEVIL)
        image2.css('display', 'block');
        //our title target
        var title = ontv.find(".title p");
        var title2 = ontv2.find(".title p");
        //.text("dette er en overskrift - maaske i flere linier");
        
        var run_timers = function(i) {
            timer_event(i);
            timer_event_two(i);
        }
        
        var timer_event = function(i) {
            
            var c_elm = $(list[i % list.length]);
            var c_text  = c_elm.children("a").text();
            var c_image = c_elm.find("img").attr('src');
            //reset the class
            //list.removeClass('active');
            list.each(function(i,e) {
                $(e).css('background-position', '0px 0px');
            });
            //c_elm.addClass('active');
            c_elm.css('background-position', '0px 30px');
            title.text(c_text); 
            image.css('background-image', 'url('+ c_image +')');
            
        };
        
        var timer_event_two = function(i) {
            
            var c_elm = $(list2[i % list2.length]);
            var c_text  = c_elm.children("a").text();
            var c_image = c_elm.find("img").attr('src');
            //reset the class
            //list.removeClass('active');
            list2.each(function(i,e) {
                $(e).css('background-position', '0px 0px');
            });
            //c_elm.addClass('active');
            c_elm.css('background-position', '0px 30px');
            title2.text(c_text); 
            image2.css('background-image', 'url('+ c_image +')');
            
        };
        
        //run once before the timer starts, discuss here: http://plugins.jquery.com/node/6658
        timer_event_two(0);
        timer_event(0); 
        
        ontv.everyTime(20000, run_timers);
});

