$(document).ready(function() {

	$("#view_homepage_slider .view-content").after('<div id="view_homepage_slider_pager">').cycle({ 
		fx:      	'cover',
		speed:    	500, 
		timeout:  	10000,
		cleartype:  1,
		pager: 		"#view_homepage_slider_pager",
			pagerAnchorBuilder: function(index, el) {
				return '<a href="#">&nbsp;</a>';
			}
	});
	
	$('div[id^="MAX"]').css("z-index","300");
	$(".alt-pop").altpop();
	
	var width = $(".field-field-photo").find("img").width(); 
	$(".field-field-photo").css("width",parseInt(width + 6));
	
	$('input, textarea').placeholder();
	
	$(".page-view-all .parent .child").click(function(e){
		e.preventDefault();
		e.stopPropagation();
		$(this).toggleClass("open");
		$(this).children("ul").slideToggle("fast");
		$(this).children("ul li").click(function(){
			return false;
		});
	});
	
	$("#domain_conf-domain-secondary-links .expanded").hover(function(){
		$(this).children(".menu").show();
	},function(){
		$(this).children(".menu").hide();
	});
	
	$(".pane-menu-menu-rsna-menu .expanded").hover(function(){
		$(this).children(".menu").show();
		$(this).find("a").addClass("rsna-active");
	},function(){
		$(this).children(".menu").hide();
		$(this).find("a").removeClass("rsna-active");
	});
	
	/*COMPARIONS CHARTS*/
	$(".comparion-selection input[name='t[]']").click(function(){
		if($(".comparion-selection input[name='t[]']:checked").length == 5){
			$(".comparion-selection input[name='t[]']:not(:checked)").attr('disabled', true);
		}else{
			$(".comparion-selection input[name='t[]']:not(:checked)").removeAttr('disabled');
		}
	});
	
	$("#bc_channel_links .cycle").cycle({
		fx:      'scrollHorz',
		timeout:  0,
		prev:    '#prev',
		next:    '#next',
		nowrap:  0	
	});
	
    $('.node-slider').cycle({
        fx:     'scrollHorz',
        speed:  'fast',
        timeout: 0,
        prev: '#prev',
        next: '#next', 
        pager: '#nav',
        pagerAnchorBuilder: function(idx, slide) {
            return '#nav li:eq(' + (idx) + ') a';
        }
    });

	$("#rsna-navigation").hover(function(){
		$(this).children("li").removeClass("right").addClass("down");
		$(this).find("ul").stop().slideDown(100);
	},function(){
		$(this).children("li").removeClass("down").addClass("right");
		$(this).find("ul").stop().slideUp(100);
	}); 

});


function addScriptTag(id, url, callback) {
    var scriptTag = document.createElement("script");
    var noCacheIE = '&noCacheIE=' + (new Date()).getTime();
   
   // Add script object attributes
   scriptTag.setAttribute("type", "text/javascript");
   scriptTag.setAttribute("charset", "utf-8");
   scriptTag.setAttribute("src", url + "&callback=" + callback + noCacheIE);
   scriptTag.setAttribute("id", id);
    
    var head = document.getElementsByTagName("head").item(0);
    head.appendChild(scriptTag);    
}


function getTopVideos(e) {
    addScriptTag("topVideos", "http://api.brightcove.com/services/library?command=find_videos_by_tags&and_tags="+e+"&token=rnmMLR30wvJ0XWJsVkrcEpgka0qWn0grqYRxfxfeODrkjOYGCDknCg..&sort_by=MODIFIED_DATE&sort_order=DESC", "loadVideosById");
}


var player;
var video, content, exp, menu, ads, social;
var videoList;

function onTemplateLoaded(pPlayer) {

    player = bcPlayer.getPlayer(pPlayer);

    video     = player.getModule(APIModules.VIDEO_PLAYER);
    content = player.getModule(APIModules.CONTENT);
    exp     = player.getModule(APIModules.EXPERIENCE);

    exp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    content.addEventListener(BCContentEvent.MEDIA_COLLECTION_LOAD, onMediaCollectionLoad);
        video.addEventListener(BCMediaEvent.BEGIN, onMediaBegin);
}

//function onTemplateReady(e) {
    //videoList = exp.getElementByID("videoList");
//}

function onMediaCollectionLoad(e) {

    var mediaDTOs = new Array();
    for(var i = 0; i < e.mediaCollection.mediaCount; i++) {
            mediaDTOs[i] = content.getMedia(e.mediaCollection.mediaIds[i]);
    }
    videoList.setData(mediaDTOs);
}

function onMediaBegin(e){
	$("#videoWrapper").slideDown("fast")
    $("#videoTitle").html(video.getCurrentVideo().displayName);
	
	$("#videoDesc").html("");
    $("#videoLongDesc").html("");
    
    $("#videoDesc").html(video.getCurrentVideo().shortDescription);
    $("#videoLongDesc").html(video.getCurrentVideo().longDescription);
}

function loadVideosById(jsonData) {

    var vids = [];
    for (var i=0; i<jsonData["items"].length; i++) {
        var title = jsonData["items"][i];
        
        if (title.id == parseFloat(title.id)){
        	if(i == 0){
	        	//video.loadVideo(title.id);
        	}
            vids.push(title.id);
        }
    }
    
    content.getMediaInGroupAsynch(vids);
}
