(function($){$.fn.hstxwall=function(options){var wall=this,items=[],cached_xml='',defaults={videos_feed:'videos.xml',tier_one_tag:'',tier_two_tag:'',tier_three_tag:'',media_center_url:'player.html'},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){cached_xml=xml;var coords,packer=new NETXUS.RectanglePacker(wall.width(),700);$(xml).find('video').each(function(){var grid_size=$(this).find('size').text();var size=null;var padding=1;switch(grid_size){case"1":size={w:120,h:90};break;case"2":size={w:240,h:180};break;default:case"3":size={w:480,h:360};break;}
coords=packer.findCoords(size.w,size.h);if(coords)
build_wall_item($(this),coords.x,coords.y);});var list=$('#tag_filters_1');$(xml).find('tier_one_tag').each(function(){add_filter_link(1,$(this),list);});var list=$('#tag_filters_2');$(xml).find('tier_two_tag').each(function(){add_filter_link(2,$(this),list);});var list=$('#tag_filters_3');$(xml).find('tier_three_tag').each(function(){add_filter_link(3,$(this),list);});$('<li><a class="clear current" href="#filter:0,clear">Clear filters</a></li>').appendTo(list);wall.jsquares();parse_filter_links();}
function onLoadFail(XMLHttpRequest,textStatus,errorThrown){$('<div>Failed to load Content</div>').appendTo(wall);}
function build_wall_item(video_xml,x,y)
{var top=y;var left=x;var size=video_xml.find('size').text();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="js-image size-'+size+'" style="top:'+top+'px; left:'+left+'px; display: block;"><a href="'+options.media_center_url+'#'+encodeURI(title)+'"><img class="js-small-image" src="'+img_url+'"/></a><div class="js-small-caption"><span>'+short_title+'</span></div><div class="js-overlay-caption-content"><h4 class="green">'+title+'</h4><p>'+description+'</p><div class="length_container"><span class="green">length:</span>'+length+' <br /><span class="green">uploaded:</span>'+uploaded_date+'</div><p class="watch"><a href="'+options.media_center_url+'#'+encodeURI(title)+'" class="video_link">WATCH VIDEO <img src="images/watch_arrow.jpg" /></a></p></div></div>');if(show_item(video_xml))
item.appendTo($(wall));items.push(item);return item;}
function add_filter_link(tier,tag_xml,target){$(tag_xml).find('tag').each(function(){var item=$('<li><a href="#filter:'+tier+','+$(this).text().toLowerCase().replace(" ","_")+'">'+$(this).text()+'</a></li>');item.appendTo(target);});}
function update_display_list(){var coords,packer=new NETXUS.RectanglePacker(wall.width(),700);if(cached_xml!=null)
{$(wall).empty();$(cached_xml).find('video').each(function(i){var show=show_item($(this));if(show)
{var grid_size=$(this).find('size').text();var size=null;var padding=1;switch(grid_size){case"1":size={w:120,h:90};break;case"2":size={w:240,h:180};break;default:case"3":size={w:480,h:360};break;}
coords=packer.findCoords(size.w,size.h);if(coords&&items[i]&&wall)
{var cssObj={'top':coords.y+'px','left':coords.x+'px'};$(items[i]).css(cssObj);$(items[i]).appendTo($(wall));}}});$(wall).jsquares();}}
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":if(options.tier_one_tag==tag)
options.tier_one_tag=""
else
options.tier_one_tag=tag;break;case"2":if(options.tier_two_tag==tag)
options.tier_two_tag="";else
options.tier_two_tag=tag;break;case"3":if(options.tier_three_tag==tag)
options.tier_three_tag="";else
options.tier_three_tag=tag;break;}
$('a[href*="#filter:"]').each(function(){if($(this).attr('href').toLowerCase().replace(" ","_").split('#filter:')[1]==+tier+","+tag&&!$(this).hasClass("current"))
$(this).addClass("current");else
{var filter_link=$(this).attr('href').toLowerCase().replace(" ","_").split('#filter:')[1].substr(0,1)
if(filter_link==tier||tier==0||(tier!=0&&filter_link=="0"))
$(this).removeClass("current");}})
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[href*="#filter:"]').each(function(){var filter=$(this).attr('href').split('#filter:')[1];var ar=filter.split(',');var tier=ar[0];var tag=ar[1];$(this).wall=wall;$(this).click(function(evt){set_filter(this,tier,tag)});})};load(options.videos_feed);return wall;};})(jQuery);if(typeof NETXUS==='undefined')
var NETXUS=function(){};NETXUS.RectanglePacker=function(width,height){this.root={};this.reset(width,height);}
NETXUS.RectanglePacker.prototype.reset=function(width,height){this.root.x=0;this.root.y=0;this.root.w=width;this.root.h=height;delete this.root.lft;delete this.root.rgt;this.usedWidth=0;this.usedHeight=0;}
NETXUS.RectanglePacker.prototype.getDimensions=function(){return{w:this.usedWidth,h:this.usedHeight};}
NETXUS.RectanglePacker.prototype.findCoords=function(w,h){function recursiveFindCoords(node,w,h){function cloneNode(node){return{x:node.x,y:node.y,w:node.w,h:node.h};}
if(node.lft){var coords=recursiveFindCoords(node.lft,w,h);return coords?coords:recursiveFindCoords(node.rgt,w,h);}
else
{if(node.used||w>node.w||h>node.h)
return null;if(w==node.w&&h==node.h){node.used=true;return{x:node.x,y:node.y};}
node.lft=cloneNode(node);node.rgt=cloneNode(node);if(node.w-w>node.h-h){node.lft.w=w;node.rgt.x=node.x+w;node.rgt.w=node.w-w;}else{node.lft.h=h;node.rgt.y=node.y+h;node.rgt.h=node.h-h;}
return recursiveFindCoords(node.lft,w,h);}}
var coords=recursiveFindCoords(this.root,w,h);if(coords){if(this.usedWidth<coords.x+w)
this.usedWidth=coords.x+w;if(this.usedHeight<coords.y+h)
this.usedHeight=coords.y+h;}
return coords;}
