// This Javascript is for Banner and other effects that found ONLY at index.html


// #slidingLinks - ("MOST POPULAR LINKS") slideToggle

//Default "1 of 9","2 of 9","3 of 9"
$(function(){
     $(window).load(function(){
          $("#slideLink").css("left","-680px");
    });
})

$(function(){
    $("#slideBtn").click(function(){
        $("#slidingLinks").slideToggle("slow", function(){
            $("#slideBtn").toggleClass("btnActive");
        });
    });
});

// Banner changing

function switchBanner(clicked) {
    var targetBanner = $('#' + $(clicked).parent('li').attr('rel'));
    if (targetBanner.size() == 0) {
        return;
    }
    $("#banner-content .banner-img-description").fadeOut("fast");
    targetBanner.fadeIn("slow");
    $("#first-btn").addClass('banner-active');
    $("#index-banner-selector .banner-selector").removeClass("banner-active");
    $(clicked).addClass("banner-active");
}

// banner slide show
$(function(){
    $('#first-btn').click();
});

$(window).load(function(){
    var slideshow = true;
    var next = 1;
    setInterval(function() {
        if (slideshow) {
            $("#index-banner-selector .banner-selector:eq(" + next + ")").click();
            next = (next == $('#index-banner-selector .banner-selector').size()-1) ? 0 : next + 1;
        }
    }, 8000);
    $("#banner-content").hover(function() {
        slideshow = false;
    }, function() {
        slideshow = true;
    });
});

// Banner "View All"
//$(function(){
//    $("#view-all").click(function(){
//        $("#view-all-show").slideToggle("slow");
//    });
//});


// Banner "View Thumb"
$(function() {
    $('#banner-container .banner-selector').parent().bt({
        animate: true,
        hoverIntentOpts: {interval: 50, timeout: 50},
        width: 126,
        height: 120,
        padding: 14,
        fill: '#323232',
        positions: 'top',
        contentSelector: "$(this).children('.banner-selector-preview').html()",
        cornerRadius: 8,
        spikeLength: 10,
        spikeGirth: 30
    });
});


// "Schedule On TV" - corousel

$(function(){
    $('#schedule-corousel').codaSlider();
});

// home page carousel carousel

function setupCarousel(items, carousel) {
    if (items.length == 0) {
        return false;
    }
    var par = null; //parent
    items.each(function(i) {
        if (i % 3 == 0) {
            carousel.append($('<div><ul class="channel"></ul></div>'));
            par = carousel.find('ul.channel:last');
        }
        var dis = $(this);
        var thumb = dis.find('.fpThumb').attr('src');
        var url = dis.find('.fpLink').attr('href');
        var title = dis.find('.fpTitle').text();
        var content = $('<li></li>');
        content.append('<a></a>');
        content.find('a').attr('href', url);
        content.find('a').attr('title', title);
        content.find('a').append('<span class="channel-img-container"><img alt=""/></span>');
        content.find('a .channel-img-container img').attr('src', thumb);
        content.find('a').append('<span class="description"><span class="title"></span></span>');
        content.find('a span.description span.title').text(title);
        par.append(content);
    });
    return true;
}

$(function() {
    var ok = setupCarousel(
        $('#featuredNews .featuredNews .item'),
        $('#featuredNews .index-carousel-channel .items_wSelector')
    );
    if (ok) {
        $("#featuredNews .index-carousel-channel").scrollable({circular: true, mousewheel: true}).navigator();
    }

    ok = setupCarousel(
        $('#featuredPages .featuredPages .item'),
        $('#featuredPages .index-carousel-channel .items_wSelector')
    );
    if (ok) {
        $("#featuredPages .index-carousel-channel").scrollable({circular: true, mousewheel: true}).navigator();
    }
});

function setupDontMissCarousel(items, carousel) {
    if (items.length == 0) {
        return false;
    }
    var par = null; //parent
    items.each(function(i) {
        if (i % 3 == 0) {
            carousel.append($('<div><ul class="tvShows"></ul></div>'));
            par = carousel.find('ul.tvShows:last');
        }
        var dis = $(this);
        var thumb = dis.find('.fpThumb').attr('src');
        var url = dis.find('.fpLink').attr('href');
        var title = dis.find('.fpTitle').text();
        var synopsis = dis.find('.fpSynopsis').text();
        var content = $('<li></li>');
        content.append('<a></a>');
        content.find('a').attr('href', url);
        content.find('a').attr('title', synopsis);
        content.find('a').append('<span class="video-img-container"><img alt=""/><span class="playBtn">Play</span></span>');
        content.find('a .video-img-container img').attr('src', thumb);
        content.find('a').append('<span class="description"><span class="title"></span><span class="definition"></span></span>');
        content.find('a span.description span.title').text(title);
        content.find('a span.description span.definition').text(synopsis);
        par.append(content);
    });
    return true;
}

$(function() {
    ok = setupDontMissCarousel(
        $('#featuredDontMiss .featuredDontMiss .item'),
        $('#featuredDontMiss .#index-carousel-tv-shows .items_wSelector')
    );
    if (ok) {
        $("#featuredDontMiss #index-carousel-tv-shows").scrollable({circular: true, mousewheel: true}).navigator()
    }
});

