// JavaScript Document
/* Menu Tab and Sub Nav */
$(function() {
    /* Load menu data for SEO max link reasons (current menu sent down by server) */
    $('ul.level1 li.tab:not(.active) ul.level2 li span').each(function() {
        var linkName = $(this).attr("linkName");
        var linkUrl = $(this).attr("linkUrl");
        if (linkName == "Bingo") {
            $(this).replaceWith('<a target="_parent" rel="nofollow" href="' + linkUrl + '"><span>' + linkName + '</span></a>');
        } else {
            $(this).replaceWith('<a target="_parent" href="' + linkUrl + '"><span>' + linkName + '</span></a>');
        }
    });

    /* UI swapping of visible menus on hover */
    $('#navigation ul.level1 li.tab').bind('mouseenter', function() {
        $(this).addClass("hover");
        $('ul.level1 li.tab ul.active').removeClass("active");
        $(this).children($('ul.level2')).addClass("active");
    });
    $('#navigation ul.level1 li.tab').bind('mouseleave', function() {
        $(this).removeClass("hover");
        $(this).children($('ul.level2')).removeClass("active");
        $('ul.level1 li.active ul.level2').addClass("active");
    });
});

/* Timed Functions */
$loads = [];
function $timed(func, priority) {

    if (!priority) priority = 0;
    var ob = { func: func, priority: priority };
  
    for (var i = 0; i < $loads.length; i++) {
        if (priority > $loads[i].priority) {
            $loads.splice(i, 0, ob);
            return true;
        }
    }
    $loads.push(ob);
    return true;
};
function $oneByOneInit() {
    $oneByOne();
}
var $runTimeTreshold = 150;
var $runTime = 0;
function $oneByOne() {
    if ($loads.length == 0) {
        return false;
    }
    var t0 = new Date().getTime();
    $loads[0].func();
    var t1 = new Date().getTime();

    $runTime += t1 - t0;

    $loads.splice(0, 1);
    if ($runTime < $runTimeTreshold) {
        try {
            $oneByOne();
        } catch (ex) {
        }
    } else {
        $runTime = 0;
        setTimeout($oneByOne, 1);
    }
}
$($oneByOneInit);

/* Concierge Scripts */
function resetFeaturedContentVerticalScrollerFeature() {
    var f = $('.concierge > div > div.content .feature');
    if (f.length == 0) {
        f = $('div.content .feature');
    }
    // reset image
    f.find('div.image a').attr('href', '#').removeClass('popout');
    // reset caption
    f.find('.caption a').attr('href', '#').removeClass('popout').text('');
}

function populateFeaturedContentVerticalScrollerFeature() {
    var fc = $('.concierge > div > div.content');
    if (fc.length == 0) {
        fc = $('div.content');
    }
    var c = fc.find('div.carousel .active');
    var f = fc.find('div.feature');



	var itemImgSrc = c.find('input.img').val();
	/*
    var itemTitle = c.find('input.title').val();
    var itemSubtitle = c.find('input.teaser').val();
    */    
    //var itemImgSrc = c.find('img').attr('src');
    var itemTitle = c.find('h3').text();
    var itemSubtitle = c.find('p.teaser').text();
	
    var itemLink = c.find('a:first').attr('href');
    var itemLocation = c.find('a:first').hasClass('popout');
    
    // update feature image
    if (itemImgSrc !== undefined) {
        f.find('div.image img').attr('src', itemImgSrc);
    }
    if (itemTitle !== undefined && itemSubtitle !== undefined) {
        f.find('div.image img').attr('alt', itemTitle + '. ' + itemSubtitle);
    }
    // update feature h3
    if (itemTitle !== undefined) {
        f.find('div.caption h3 a').text(itemTitle);
    }
    // update feature p
    if (itemSubtitle !== undefined) {
        f.find('div.caption p a').text(itemSubtitle);
    }
	f.find('div.caption p a a').text("ds");
    // update links
    if (itemLink !== undefined) {
        f.find('div.image a, div.caption a').attr('href', itemLink);
        if (itemLocation) {
            f.find('div.image a, div.caption a').addClass('popout');
        }
    }
}
function updateFeaturedContentVerticalScrollerCount() {
    var active = $('#conciergecarousel li.active').attr('jcarouselindex');
    //$('.itemCount p span').text(active);
//	console.log(active);
}
// This is the callback function which receives notification when an item becomes the first one in the visible range.
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
    var fc = $('.concierge > div > div.content');
    if (fc.length == 0) {
        fc = $('div.content');
    }
    fc.find('li.active').removeClass('active');
    fc.find('li:eq(' + (idx - 1) + ')').addClass('active');

    resetFeaturedContentVerticalScrollerFeature();
    populateFeaturedContentVerticalScrollerFeature();

    // change displayed count
    //fc.find('div.itemCount p span').text(idx);
    // put a class of visible on matched elements so that we can hook the step function from something
    fc.find('li.visible').removeClass('visible');
    fc.find('li:eq(' + (idx - 1) + '),li:eq(' + (idx) + '),li:eq(' + (idx + 1) + ')').addClass('visible');
}
function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
    var fc = $('.concierge > div > div.content');
    if (fc.length == 0) {
        fc = $('div.content');
    }
    var c = fc.find('div.carousel');
    var f = fc.find('div.feature');
    // reset visible anchors
    c.find('li.visible').removeClass('visible');
    c.find('li:eq(' + (idx - 1) + ')').addClass('visible');
}
function startTimer() {
	
    var fc = $('.concierge > div > div.content');
    if (fc.length == 0) {
        fc = $('div.content');
    }
    var vs = fc.find('div.carousel ul');
    var cmsVal = fc.find('input.rotationTime').val();
    var rotateTime = parseInt(cmsVal, 10);
    // retrieve rotation time from hidden input

    vs.everyTime(rotateTime, 'controlled', function() {

        var next = fc.find('div.carousel ul li.active').next().hasClass('visible');
        //alert(next);
        if (next) {
            vs.find('li.visible.active').removeClass('active').next().addClass('active');
            resetFeaturedContentVerticalScrollerFeature();
            populateFeaturedContentVerticalScrollerFeature();
            updateFeaturedContentVerticalScrollerCount();
        } else {
            // trigger click
            fc.find('div.jcarousel-next-vertical').click();
            // update count
            updateFeaturedContentVerticalScrollerCount();
        }
    });
}
function stopTimer() {
    var vs = $('.concierge > div > div.content .carousel ul');
    if (vs.length == 0) {
        vs = $('div.content .carousel ul');
    }
    vs.stopTime('controlled');
}
function hideLoader() {
    var content = $('.concierge > div > div.content');
    if (content.length == 0) {
        content = $('div.content');
    }
    content.css('visibility', 'visible');
}
$timed(function() {
    //console2.time('4');
    // Initiate the jcarousel object
   	
    var fc = $('.concierge > div > div.content');
    if (fc.length == 0) {
        fc = $('div.content');
    }
    //skip function if image concierge isn't found
    if (fc.length == 0) {
        //console2.timeEnd('4');
        return false;
    }
    fc.find('#conciergecarousel').jcarousel({
        scroll: 3,
        wrap: 'last',
        vertical: true,
        //initCallback: hideLoader,
        itemFirstInCallback: mycarousel_itemFirstInCallback
    });
	
    resetFeaturedContentVerticalScrollerFeature();
    populateFeaturedContentVerticalScrollerFeature();
    
    // space saving variables
    var c = fc.find('div.carousel');
    var f = fc.find('div.feature');
    // populate the feature with content from the first list item
    var active = c.find('li.active').length;
    if (active === 0) {
        // reset active state
        c.find('li:eq(0)').addClass('active');

        resetFeaturedContentVerticalScrollerFeature();
        populateFeaturedContentVerticalScrollerFeature();
        updateFeaturedContentVerticalScrollerCount();
    }
    // item hover state
	
    fc.find('div.carousel li').hover(function() {
        // remove classes class then add active class to current item
        c.find('li.active').removeClass('active');
        $(this).addClass('active');

        resetFeaturedContentVerticalScrollerFeature();
        populateFeaturedContentVerticalScrollerFeature();
        updateFeaturedContentVerticalScrollerCount();
    }, function() {
        return true;
    });

    // item step functions
    startTimer();

    fc.hover(function() {
        stopTimer();
    }, function() {
        startTimer();
    });

    // trigger external window
    fc.find('div.carousel ul, div.feature').click(function(e) {

        var loc = fc.find('li.active h3 a').hasClass('popout');
        var link = fc.find('li.active h3 a').attr('href');
       //if href is not set or is empty link might get undefined

        //if clicked on a link open it instead of the active tab link
        var clickedlink = $(e.target).parents('a').andSelf().filter('a');
        if (clickedlink.length != 0) {
            link = clickedlink.attr('href');
        }
        //if clicked on tne channel link, don't open it in new window
        if (clickedlink.parents('p.channel').length != 0) {
            loc = false;
        }
        //fix empty href attributes
        if (link === undefined) {
			//console.log(link);
			e.stopPropagation();
	        return false;
		}
        if (loc) {
            window.open(link, '_blank');
            return false;
        } else {
            window.open(link, '_self');
        }
        e.stopPropagation();
        return false;
    });

    //fc.find('div.jcarousel-skin-featured-content div.jcarousel-clip-vertical').css('backgroundColor', '#fff');
    //console2.timeEnd('4');
}, -1000);



//
// headline content gallery
//
function setupHeadlineContentGalleryFeature(hmodule) {
    // space savers
    var f = hmodule.find('div > div > div.feature:eq(0)');
    var l = hmodule.find('div > div > div.list:eq(0)');
    // drop extra items
    l.find('li:gt(9)').remove();
    l.find('li.active').removeClass('active');
    l.find('ul li:eq(0)').addClass('active');
    populateHeadlineContentGalleryFeature(hmodule);
}
function populateHeadlineContentGalleryFeature(hmodule) {
    var f = hmodule.find('div > div > div.feature:eq(0)');
    var l = hmodule.find('div > div > div.list:eq(0) li.active');

    // set vars
    var title = l.find('div.title a').text();
    //alert(title);
    var description = l.find('p').text();
    //alert(description);
    var imgPath = l.find('div.image img').attr('src');
    //alert(imgPath);
    var link = l.find('div.title a').attr('href');
    //alert(link);

    if (title !== undefined) { f.find('div.title a').text(title); }
    if (description !== undefined) { f.find('p a').text(description); }
    if (imgPath !== undefined) { f.find('div.image a').html('<img src="' + imgPath + '" />'); }
    if (link !== undefined) { f.find('a').attr('href', link); }

    f.find('div.title a').text(title);
    f.find('p a').text(description);
    f.find('img').attr('src', imgPath);
    f.find('a').attr('href', link);
}
$timed(function() {
    var hmodule = $('#container > div > div > div.headlinelist');
    if (hmodule.length == 0) { hmodule = $('div.headlinelist'); }
    hmodule.each(function() { setupHeadlineContentGalleryFeature($(this)); });

    // on hover
    hmodule.find('div.list li').hover(function() {
        var parent_headline = $(this).parents('div.headlinelist');
        // remove any other active classes
        parent_headline.find('div.list:eq(0)').find('li.active').removeClass('active');
        // set active class
        $(this).addClass('active');

        // populate feature
        populateHeadlineContentGalleryFeature(parent_headline);
    },
	function() { });
    // on click
    hmodule.find('div.list:eq(0) li').find('li').click(function() {
        // open link
        var link = $(this).find('div.title a').attr('href');
        window.location.href = link;
    });


}, 999);    	//second highest priority

