
/* - ++resource++collective.contentcarousel.js/contentcarousel.js - */
var carousel_interval;
var videosID = {}; // The videos to be loaded
var slide = 0; //Current slide

// If width is not defined, provide a default
if ('undefined' == typeof width) {
    var width = 700; // in px
}
// If height is not defined, provide a default
if ('undefined' == typeof height) {
    var height = 270; // in px
}
// If duration is not defined, provide a default
if ('undefined' == typeof duration) {
    var duration = 300; // The duration of the animations in miliseconds
}
// If transition is not defined, provide a default
if ('undefined' == typeof transition) {
    var transition = 8000; // The time before passing to the next slide in miliseconds
}

/* TODO: Create an actual plugin with this functions defined */ 
function onYouTubePlayerReady(playerId) {
	videosID[playerId][0] = document.getElementById("o-" + playerId);
    jq(videosID[playerId][0]).parent().addClass("loaded");
    jq(videosID[playerId][0]).closest('li.not-loaded').removeClass("not-loaded");
};

function startVideo(vid){
	$vid = videosID[vid][0];
	$vid.playVideo();
};

function isPlaying(vid){
    var $vid = videosID[vid][0];
    return (($vid.getPlayerState() == 1 || $vid.getPlayerState() > 2)) ? true : false;
};

function pauseVideo(vid){
	var $vid = videosID[vid][0];
	$vid.pauseVideo();
	videosID[vid][1] = $vid.getCurrentTime();
};

function resumeVideo(vid){
	var $vid = videosID[vid][0];
	if (videosID[vid][1] > 0){
		$vid.seekTo(videosID[vid][1]);
		$vid.playVideo();
	};
};

function pauseIfPlaying(obj){
	if (obj.length){
		var playerid = obj.attr('id').slice(2);
		if(isPlaying(playerid)){
			pauseVideo(playerid);
		};
	};
};

function resumeIfPlaying(){
	if (jq('#portal-contentcarouselNumbers .current .loaded object').length){
		var playerid = jq('#portal-contentcarouselNumbers .current object').attr('id').slice(2);
		resumeVideo(playerid);
	};
};

jq(document).ready(function(){

/*********** LOAD YOUTUBE VIDEOS USING SWFOBJECT *******************/
// Lets Flash from another domain call JavaScript
// wmode is very important because it allows the hover property to work!!
var params = { allowScriptAccess: "always", wmode: "transparent" };

// The element id of the Flash embed videos
$("#portal-contentcarouselNumbers .youtube-video").each(function(){ 
	videosID[this.id] = [null,0];
	atts = { id: 'o-' + this.id };
	// this will load all the videos
	swfobject.embedSWF("http://www.youtube.com/v/" + this.id + "?version=3&enablejsapi=1&playerapiid=" + this.id,
                    this.id, width, height, "9", null, null, params, atts);
});

$('#portal-contentcarouselNumbers .content-button').each(function(index){
	id = this.id;
	jq(this).mouseenter(function(){
		pauseIfPlaying(jq('.content .current .loaded object'));
		jq('.content .current').fadeTo(duration,0);
		jq('#portal-contentcarouselNumbers .carousel').css('left', index*(-width) + 'px');
		jq('#content-item-' + index).fadeTo(duration,1);
		jq('.content .current').removeClass('current');
		jq('#content-item-' + index).addClass('current');
		jq('.content-button.selected').removeClass('selected');
		jq(this).addClass('selected');
		slide = index;
		resumeIfPlaying();
	});
});

if (jq('#portal-contentcarouselNumbers')) {
    var carousel_rotate = function() {
        features = jq('.content-item');
        if (features.length < 2)
            return;

		pauseIfPlaying(jq('.content-item .current .loaded object'));

        next = jq('.content .current').next('.content-item');
		current = jq('.content .current');
        jq('#content-button-' + current.attr('id').substr(13)).removeClass('selected');
        current.fadeTo(duration,0);
		current.removeClass('current');
		
        if (next.length) {
			slide ++;
			jq('#portal-contentcarouselNumbers .carousel').css("left", slide*(-width) + "px");
            next.fadeTo(duration, 1);
            jq('#content-button-' + next.attr('id').substr(13)).addClass('selected');
			next.addClass('current');

        } else {
			slide = 0;
			jq('#portal-contentcarouselNumbers .carousel').css('left', 0);
            jq('#content-item-0').fadeTo(duration,1);
            jq('#content-item-0').addClass('current');

            jq('.content-button:first').addClass('selected');
        };
    };
    jq(function() {
		jq('#portal-contentcarouselNumbers .carousel').css("left", 0);
		jq('#portal-contentcarouselNumbers .carousel').css("width", $('#portal-contentcarouselNumbers .content-item').length*width);
        carousel_interval = setInterval(carousel_rotate, transition);
        setTimeout(function() {
            jq('#portal-contentcarouselNumbers .link-https, #portal-contentcarouselNumbers .link-external').each( function() {
                jq(this).replaceWith(jq(this).html());
            })
        }, 1000); 
        jq('#portal-contentcarouselNumbers').hover(
            function() { clearInterval(carousel_interval);},
            function() { 
				if (jq('#portal-contentcarouselNumbers .current .loaded object').length){ // If it's a video	 
					var playerid = jq('#portal-contentcarouselNumbers .current object').attr('id').slice(13);
					if (isPlaying(playerid)){
						clearInterval(carousel_interval);	
					}else{
						carousel_interval = setInterval(carousel_rotate, transition); 
					}
					
				} else{
					carousel_interval = setInterval(carousel_rotate, transition); 
				}
        });
    });
}
});


