
(function($){$.fn.hstxmediacenter=function(options){var media_center=this,items=[],defaults={videos_feed:'videos.xml',tier_one_tag:'',tier_two_tag:'',tier_three_tag:'',cached_video_xml:null,current_video_xml:null,player_ready:false,player_id:'#player1'},options=$.extend(defaults,options);function load(xml_url)
{$.ajax({type:"GET",url:xml_url+'?'+Math.random(),dataType:"xml",error:onLoadFail,success:parseResults});}
function parseResults(xml){options.cached_video_xml=xml;var current_title=null;if(document.location.href.indexOf("#")!=-1)
current_title=decodeURI(document.location.href.split("#")[1]);$(xml).find('video').each(function(){var item=build_grid_item($(this));if(current_title!=null&&$(this).find('title').text()==current_title)
{options.current_video_xml=$(this);$(item).find('img').addClass('current_video');}});if(options.current_video_xml!=null)
playVideo(options.current_video_xml);}
function onLoadFail(XMLHttpRequest,textStatus,errorThrown){$('<div>Failed to load Content</div>').appendTo($('#video_grid'));}
function playVideo(video_xml){if(options.player_id)
{var player=$(options.player_id)[0];if(!player||typeof player.sendEvent!='function')
return;player.sendEvent('STOP');player.sendEvent('LOAD',{file:video_xml.find('videoUrl').text()});$('#video_title').html(video_xml.find('title').text());$('#video_description').html(video_xml.find('description').text());$('#video_length').html(video_xml.find('video_length').text());$('#video_uploaded').html(video_xml.find('date_uploaded').text());$('#low_res_link').click(function(){playLowRes(video_xml)});var podCastLink=video_xml.find('podcastUrl').text();if(podCastLink!="")
{$('#podcast_link').html("Podcast Download");$('#podcast_link').attr("href",podCastLink);}
else
{$('#podcast_link').html("");$('#podcast_link').attr("href","");}
$('#video_details').show();}}
function playLowRes(video_xml){if(options.player_id)
{var player=$(options.player_id)[0];if(!player||typeof player.sendEvent!='function')
return;player.sendEvent('STOP');player.sendEvent('LOAD',{file:video_xml.find('low_res_videoUrl').text(),provider:'youtube',autostart:true});}}
function build_grid_item(video_xml)
{var title=video_xml.find('title').text();var short_title=video_xml.find('shortTitle').text();var description=video_xml.find('description').text();var video_url=video_xml.find('videoUrl').text();var img_url=video_xml.find('imgUrl').text();var length=video_xml.find('video_length').text();var uploaded_date=video_xml.find('date_uploaded').text();var item=$('<div class="video_grid_item"><a href="#'+encodeURI(title)+'"><img src="'+img_url+'" />'+short_title+'</a></div>').appendTo($('#video_grid'));if(!show_item(video_xml))
if(jQuery.support.opacity)
$(items[i]).fadeTo("slow",0.3);else
$(items[i]).hide();items.push(item);item.click(function(){playVideo(video_xml);$(items).each(function(){$(this).find('img').removeClass('current_video');});$(this).find('img').addClass('current_video');});return item;}
function update_display_list(){if(options.cached_video_xml!=null)
{$(options.cached_video_xml).find('video').each(function(i){if(show_item($(this)))
if(jQuery.support.opacity)
$(items[i]).fadeTo("slow",1);else
$(items[i]).show();else
if(jQuery.support.opacity)
$(items[i]).fadeTo("slow",0.3);else
$(items[i]).hide();if($(this)==options.current_video_xml)
$(items[i]).find('img').addClass('current_video');else
$(items[i]).find('img').removeClass('current_video');});}}
function set_filter(target,tier,tag)
{tag=tag.toLowerCase().replace(" ","_");switch(tier)
{case"0":options.tier_one_tag='';options.tier_two_tag='';options.tier_three_tag='';break;case"1":options.tier_one_tag=tag;break;case"2":options.tier_two_tag=tag;break;case"3":options.tier_three_tag=tag;break;}
$('a[rel^="filter:"]').each(function(){if($(this).attr('rel').toLowerCase().replace(" ","_")=="filter:"+tier+","+tag)
$(this).parent().parent().addClass("selected");else
$(this).parent().parent().removeClass("selected");})
update_display_list();}
function show_item(item_xml){var show=true;if(options.tier_one_tag!=''){if($(item_xml).find('tier_one_tags').text().toLowerCase().replace(" ","_").search(options.tier_one_tag)==-1){show=false;}}
if(options.tier_two_tag!=''){if($(item_xml).find('tier_two_tags').text().toLowerCase().replace(" ","_").search(options.tier_two_tag)==-1){show=false;}}
if(options.tier_three_tag!=''){if($(item_xml).find('tier_three_tags').text().toLowerCase().replace(" ","_").search(options.tier_three_tag)==-1){show=false;}}
return show;}
function parse_filter_links(){$('a[rel^="filter:"]').each(function(){var filter=$(this).attr('rel').split(':')[1];var tier=filter.split(',')[0];var tag=filter.split(',')[1];$(this).click(function(evt){set_filter(this,tier,tag)});})};load(options.videos_feed);parse_filter_links();return media_center;};})(jQuery);
