var site_url = "http://jawaban.com/";
var headline_timer;
var video_timer;

function doAnimateHeadline() {
    //Animate headline news
    current_active = $('#headline_news .news li.active');
    next_active = $('#headline_news .news li.active').next();
    if(!next_active.get(0)) {
        next_active = $('#headline_news .news ul').children(":first");
    }
    current_active.removeClass('active');
    next_active.addClass('active');
    changeImageTitle(next_active.attr("news"));

    doTimerHeadline();
}

function doAnimateVideo() {
    //Animate watch kisah nyata
    current_active_vid = $('#watch_program_tv .links li.active');
    next_active_vid = current_active_vid.next();    
    if(!next_active_vid.get(0)) {
        next_active_vid = $('#watch_program_tv .links ul').children(":first");
    }

    current_pointer_vid = current_active_vid.find('.pointer');
    next_pointer_vid = next_active_vid.find('.pointer');
    
    current_active_vid.removeClass('active');
    current_pointer_vid.html("");
    next_active_vid.addClass('active');
    next_pointer_vid.html('<img class="pointer" src="'+ site_url +'assets/images/system/watch_kisah_nyata_pointer.jpg" />');
    changeImageVideo(next_active_vid.attr("links"));

    doTimerVideo();
}

function doTimerHeadline() {
    headline_timer = setTimeout("doAnimateHeadline()",10000);
}

function doTimerVideo() {
    video_timer = setTimeout("doAnimateVideo()",10000);
}

//Show & hide image & title headline news
function changeImageTitle(Id) {
    $('#headline_news_title1').hide();
    $('#headline_news_title2').hide();
    $('#headline_news_title3').hide();
    $('#headline_news_title4').hide();
    $('#headline_news_img1').hide();
    $('#headline_news_img2').hide();
    $('#headline_news_img3').hide();
    $('#headline_news_img4').hide();
    $('#headline_news_simg1').show();
    $('#headline_news_simg2').show();
    $('#headline_news_simg3').show();
    $('#headline_news_simg4').show();

    $('#headline_news_img' + Id).fadeIn(500);
    $('#headline_news_title' + Id).show();
    $('#headline_news_simg' + Id).hide();
}

//Show & hide image & title watch kisah nyata
function changeImageVideo(Id) {
    $('#watch_program_tv .news a').hide();
    $('#PT_img' + Id).show();
}

function changeVideo(ismobile, divcontent, videofile, videotitle, videopath) {
    clearTimeout(video_timer);
    $('#watch_program_tv_image').hide();
    $('#watch_program_tv_video').show();
    if(ismobile == '') {
        thediv = document.getElementById(divcontent);
        thediv.innerHTML = '<embed src="'+videopath+'mediaplayer.swf" allowfullscreen="true" bgcolor="#FFFFFF" type="application/x-shockwave-flash" wmode="transparent" width="270" height="170" flashvars="file='+videofile+'.flv&caption=false&autostart=true&allowfullscreen=true&backcolor=0xFFFFFF&frontcolor=0x000000"></embed><br />'+videotitle+'';
    }
    else {
        $video = $.ajax({
            url: "http://www.jawaban.com/index.php/home/get_video/vidname/" + videofile,
            type: "GET",
            async: false
        }).responseText;

        $('#watch_program_tv_video').html($video);
    }
}

function onecube_close(){
    $('#onecube_mpr').hide();
}
function doTimerOnecube() {
    setTimeout("onecube_close()",11000);
}

//Function to add friend
function AddFriendAlert(uid, site_url){
    var conBox = confirm("Are you sure want to add?");
    if(conBox) {
        location.href = "";
        location.href = site_url +"/home/add_friend/uid/" + uid;
    }
    else {
        return;
    }
}

$(document).ready(function() {
    //Show & Hide headline images & title
    $('#headline_news_title1').show();
    $('#headline_news_title2').hide();
    $('#headline_news_title3').hide();
    $('#headline_news_title4').hide();
    $('#headline_news_img1').show();
    $('#headline_news_img2').hide();
    $('#headline_news_img3').hide();
    $('#headline_news_img4').hide();
    $('#headline_news_simg1').hide();
    $('#headline_news_simg2').show();
    $('#headline_news_simg3').show();
    $('#headline_news_simg4').show();
    
    $('#headline_news .news li').click(function() {
        clearTimeout(headline_timer);
        current_click = $(this);
        current_active = $(this).siblings('.active');
        current_active.removeClass('active');
        current_click.addClass('active');
        changeImageTitle(current_click.attr("news"));
        doTimerHeadline();
    });

    //Show & hide most_recommended & most_popular
    $('#most_news_rec').show();
    $('#most_news_pop').hide();
    $('#most_news .title li').click(function() {
        if($(this).attr("id") == "most_recommended" && $(this).attr("class") == "most_recommended_inactive") {
            $(this).removeClass('most_recommended_inactive');
            $(this).addClass('most_recommended_active');
            $('#most_popular').removeClass('most_popular_active');
            $('#most_popular').addClass('most_popular_inactive');
            $('#most_news_rec').show();
            $('#most_news_pop').hide();
        }
        else if ($(this).attr("id") == "most_popular" && $(this).attr("class") == "most_popular_inactive") {
            $(this).removeClass('most_popular_inactive');
            $(this).addClass('most_popular_active');
            $('#most_recommended').removeClass('most_recommended_active');
            $('#most_recommended').addClass('most_recommended_inactive');
            $('#most_news_rec').hide();
            $('#most_news_pop').show();
        }
    });

    //Show & Hide video watch kisah nyata
    $('#watch_program_tv .news a').hide();
    $('#PT_img1').show();
    $('#watch_program_tv_video').hide();
    $('#watch_program_tv .links li').click(function() {
        $('#watch_program_tv_image').show();
        $('#watch_program_tv_video').hide();
        clearTimeout(video_timer);
        current_click_vid = $(this);
        current_active_vid = $(this).siblings('.active');

        current_pointer_vid = current_active_vid.find('.pointer');
        next_pointer_vid = current_click_vid.find('.pointer');

        current_active_vid.removeClass('active');
        current_pointer_vid.html("");
        current_click_vid.addClass('active');
        next_pointer_vid.html('<img class="pointer" src="http://192.168.4.31/jawaban/assets/images/system/watch_kisah_nyata_pointer.jpg" />');
        changeImageVideo(current_click_vid.attr("links"));

        doTimerVideo();
    });

    //Get monitor resolution for onecube advertise
    var div = $('#onecube_mpr'); //var name = "#floatMenu";
    div.css("left", ($(window).width() - div.width())/2  + 'px');
    $(window).scroll(function () {
        var offset = 150 + $(document).scrollTop()+"px";
        div.animate({top:offset},{duration:500,queue:false});
    });

    doTimerOnecube();


});

doTimerHeadline();
doTimerVideo();
