function csoPlayer(){player=this;this.stage=$('#csoPlayer-stage');this.contextArray=new Array;this.contextIndex=false;this.mediaType='image';player.setupHtml();player.attachEvents();}
csoPlayer.prototype.setupHtml=function(){if($('#csoPlayer').length==0){$('body').prepend('<div id="csoPlayer"><div id="csoPlayer-preload"></div><div id="csoPlayer-overlay"></div><div id="csoPlayer-stage"><div id="csoPlayer-close"></div><div id="csoPlayer-content"><div id="csoPlayer-media"></div><div id="csoPlayer-title"></div><div id="csoPlayer-controls"><span id="csoPlayer-next"></span><span id="csoPlayer-previous"></span></div></div></div></div>');}};csoPlayer.prototype.getCurrent=function(){return player.contextArray[player.contextIndex];};csoPlayer.prototype.getTotal=function(){return player.contextArray.length;};csoPlayer.prototype.getPosition=function(){return player.contextIndex;};csoPlayer.prototype.center=function(what,where){object=$(what);object.css("position","absolute");switch(where){default:object.css("top",(($(window).height()-object.height()-50)/2)+$(window).scrollTop()+"px");object.css("left",(($(window).width()-object.width())/2)+$(window).scrollLeft()+"px");break;}
return this;};csoPlayer.prototype.attachEvents=function(){$("body a").each(function(i,object){if(urlObj=player.parseUrl($(this).attr('href'))){$(this).bind('click',{player:player,urlObj:urlObj},function(event){event.data.player.showResource(event.data.urlObj,this);return false;});}});$('#csoPlayer-overlay').bind('click',{player:player},function(event){event.data.player.turnOff();return false;});$('#csoPlayer-next').bind('click',{player:player},function(event){event.data.player.next();return false;});$('#csoPlayer-previous').bind('click',{player:player},function(event){event.data.player.previous();return false;});$('#csoPlayer-close').bind('click',{player:player},function(event){event.data.player.turnOff();return false;});};csoPlayer.prototype.turnOn=function(){document.getElementById('csoPlayer').style.display='block';if(overlay=$('#csoPlayer-overlay')){overlay.css('display','block');overlay.width($(document.body).width());overlay.height(Math.max($(document.body).height(),$(window).height()));}};csoPlayer.prototype.turnOff=function(){$('#csoPlayer-stage').fadeOut(300);$('#csoPlayer-overlay').css('display','none');document.getElementById('csoPlayer').style.display='none';};csoPlayer.prototype.next=function(){if(player.getTotal()>1){var nextId=player.contextIndex+1>=player.contextArray.length?0:player.contextIndex+1;var next=player.contextArray[nextId];player.contextIndex=nextId;player.loadResource(next.url);}
else{player.turnOff();}};csoPlayer.prototype.previous=function(){if(player.getTotal()>1){var nextId=(player.contextIndex-1)<0?player.contextArray.length-1:player.contextIndex-1;var next=player.contextArray[nextId];player.contextIndex=nextId;player.loadResource(next.url);}
else{player.turnOff();}};csoPlayer.prototype.getUri=function(url){if(url.substring(0,4)=='http'){var parts=url.match(/^http:\/\/[a-z0-9.-]+\/([a-z0-9]{1,})/);}
else{var parts=url.match(/^([a-z0-9]{1,})/);}
return parts?parts[1]:'';};csoPlayer.prototype.parseUrl=function(url){if(url=='')
return false;if(url.substring(0,4)!='http'){url='http://'+document.location.host+(url.substring(0,1)=='/'?'':'/')+url;}
if(parts=url.match(/^http:\/\/([a-z0-9.-]+)\/([a-z0-9]{1,})/)){var domain=parts[1];var first=parts[2];switch(first){case'resource':var path=url.match(/^http:\/\/([a-z0-9.-]+)\/([a-z0-9\/]{1,})/);return{uri:path[2],url:url,service:'resource'};break;default:if(domain.match(/youtube.com$/)){if(id=url.match(/watch\?v=([a-z0-9_-]+)/i)){return{uri:id[1],url:url,service:'youtube'};}}
else if(domain.match(/vimeo.com$/)){if(id=first.match(/([0-9]{5,})/)){return{uri:'resource/vimeo/'+id[1],url:url,service:'resource'};}}
else if(matches=url.match(/flickr.com\/([0-9]+)\/([0-9]+)_/i)){return{uri:'resource/flickr/'+matches[2],url:url,service:'resource'}}
else if(matches=url.match(/issuu.com\/([^\/]+)\/docs\/([^\?]+)\?id=([a-zA-Z0-9-]+)/i)){return{uri:matches[3],url:url,service:'issuu',user:matches[1],doc:matches[2]}}
else if(matches=url.match(/brightcove.com\/services\/player\/(bcpid([0-9]+))\?bctid=([0-9]+)/i)){return{uri:matches[1],url:url,service:'brightcove',bcpid:matches[2],bctid:matches[3]}}
else if(matches=url.match(/\.(png|gif|jpg|jpeg)(\?|$)/i)){return{uri:url,url:url,service:'image'}}
else if(matches=url.match(/\.(swf)(\?|$)/i)){return{uri:url,url:url,service:'flash'}}
break;}}
return false;};csoPlayer.prototype.findContext=function(object,urlObj){player.contextArray=new Array;player.contextIndex=0;switch(object.nodeName){case'UL':case'DIV':var lastObj=false;$("a",object).each(function(i,object){var thisObj;if(thisObj=player.parseUrl($(this).attr('href'))){if(lastObj&&thisObj.url==lastObj.url){}
else{if(thisObj.url==urlObj.url){player.contextIndex=player.contextArray.length;}
player.contextArray[player.getTotal()]={url:thisObj,title:object.title,position:(player.getTotal()+1)};}
lastObj=thisObj;}});return true;break;default:if(parentNode=object.parentNode){return player.findContext(parentNode,urlObj);}
break;}
return false;};csoPlayer.prototype.updateContext=function(){if(current=player.getCurrent()){var titleHTML='';if(current.title.length){var title=current.title.length<45?current.title:current.title.substring(0,45)+'..';titleHTML+='<span title="'+content.title+'">'+title+'</span> ';}
if(current.publisher&&current.source){titleHTML+=' by <a'+(current.external_source?' target="_blank"':'')+' href="'+current.source+'">'+current.publisher+'</a>';}
if(player.getTotal()>1){titleHTML+=(titleHTML.length?'<br />':'')+current.position+' of '+player.getTotal();$('#csoPlayer-next').show();$('#csoPlayer-previous').show();}
else{$('#csoPlayer-next').hide();$('#csoPlayer-previous').hide();}
if(titleHTML.length){$('#csoPlayer-title').html('<p>'+titleHTML+'</p>').show();}
else{$('#csoPlayer-title').hide();}
$('#csoPlayer-title').css("left",(($('#csoPlayer-stage').width()-$('#csoPlayer-title').width())/2)+"px");$('#csoPlayer-title').css("bottom",(0-($('#csoPlayer-title').height()+20))+"px");switch(player.mediaType){case'image':if(!$('#csoPlayer-media').hasClass('clickable')){$('#csoPlayer-media').addClass('clickable').bind('click',{player:player},function(event){event.preventDefault();event.data.player.next();return false;});}
break;default:$('#csoPlayer-media').unbind('click').removeClass('clickable');break;}}};csoPlayer.prototype.showResource=function(urlObj,object){player.findContext(object,urlObj);player.turnOn();player.loadResource(urlObj);};csoPlayer.prototype.loadFlash=function(src,width,height,flashVars){player.mediaType='flash';if($.flash.hasVersion(8)){$('#csoPlayer-media').width(width).height(height).removeClass('loading').flash({swf:src,allowFullScreen:true,wmode:'transparent',width:width,height:height,bgcolor:'#000000',flashVars:flashVars});player.onMediaLoad();}
else{var flashvars='allowfullscreen=true&allowscriptaccess=always';$('#csoPlayer-media').width(width).height(height).html('<object width="'+width+'" height="'+height+'"><param name="movie" value="'+src+'"></param><param name="wmode" value="transparent"></param><param name="flashvars" value="'+flashvars+'"></param><embed src="'+src+'" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" flashvars="'+flashvars+'" wmode="transparent"></embed></object>');player.onMediaLoad();}};csoPlayer.prototype.loadIframe=function(src,width,height){player.mediaType='iframe';$('#csoPlayer-media').width(width).height(height).html('<iframe width="'+width+'" height="'+height+'" src="'+src+'" scrolling="no" frameborder="0" allowTransparency="true" style="background:transparent; border:none;"></iframe>');player.onMediaLoad();};csoPlayer.prototype.loadYouTube=function(videoId){var width=854;var height=510;player.loadFlash('http://www.youtube.com/v/'+videoId+'?fs=1&autoplay=1&amp;&amp;hl=en_GB&showinfo=0&hd=1',width,height);};csoPlayer.prototype.loadVimeo=function(videoId){var width=640;var height=360;player.loadFlash('http://www.vimeo.com/moogaloop.swf?clip_id='+videoId,width,height);};csoPlayer.prototype.loadIssuu=function(urlObj){var width=600;var height=482;var src='http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf';var flashVars={allowfullscreen:true,allowscriptaccess:'always',wmode:'transparent',mode:'embed',layout:'http://skin.issuu.com/v/dark/layout.xml',showFlipBtn:true,documentId:urlObj.uri,docName:urlObj.document,username:urlObj.user,loadingInfoText:'Loading...',et:1285757878808,er:87,bgcolor:'#000'}
if(size=player.guessSize(width,height)){width=size[0];height=size[1];}
player.loadFlash(src,width,height,flashVars);}
csoPlayer.prototype.guessSize=function(width,height){var maxHeight=$(window).height()-150;var diff=maxHeight-height;var maxWidth=(width/100)*(100+((100/height)*diff));return new Array(Math.round(maxWidth),Math.round(maxHeight));}
csoPlayer.prototype.loadResource=function(urlObj){$('#csoPlayer-media').empty().addClass('loading');switch(urlObj.service){case'youtube':player.loadYouTube(urlObj.uri);break;case'vimeo':player.loadVimeo(urlObj.uri);break;case'image':player.loadImage(urlObj.uri);break;case'issuu':player.loadIssuu(urlObj);break;case'brightcove':player.loadIframe('http://link.brightcove.com/services/player/bcpid'+urlObj.bcpid+'?bctid='+urlObj.bctid,480,270);break;case'flash':if(size=player.guessSize(640,480)){width=size[0];height=size[1];}
player.loadFlash(urlObj.uri,width,height);break;case'resource':$.getJSON(urlObj.uri+'.json',function(object){var current=player.getCurrent();if(object.content['created-by']){current.publisher=object.content['created-by'].title;current.source=object.content['created-by'].uri;current.external_source=false;}
else{if(object.content['publisher'])
current.publisher=object.content['publisher'];if(object.content['source'])
current.source=object.content['source'];current.external_source=true;}
switch(object.content['mime-type']){case'image/gif':case'image/png':case'image/jpeg':imageUrl='images/'+object.content.file.replace('://','/')+'/large.jpg';player.loadImage(imageUrl);break;case'video/x-flv':var fileParts=object.content.file.split('://',2);var videoId=fileParts[1];switch(fileParts[0]){case'youtube':player.loadYouTube(videoId);break;case'vimeo':player.loadVimeo(videoId);break;default:window.location.href=object.content.uri;break;}
break;default:window.location.href=object.content.uri;player.turnOff();break;}});break;}};csoPlayer.prototype.loadImage=function(url){var img=new Image();$(img).load(function(){player.mediaType='image';var width=this.width;var height=this.height;var maxHeight=$(window).height()-100;if(this.height>maxHeight){var diff=this.height-maxHeight;height=maxHeight;width=(this.width/100)*(100-((100/this.height)*diff));$(this).width(width).height(height);}
$('#csoPlayer-media').width(width).height(height).removeClass('loading').empty().append(this);$(this).fadeIn(300);player.onMediaLoad();}).error(function(){alert('Image Didnt load');}).attr('src',url);};csoPlayer.prototype.onMediaLoad=function(){$('#csoPlayer-media').removeClass().addClass('csoplayer-'+player.mediaType);$('#csoPlayer-stage').fadeIn(300);player.center('#csoPlayer-stage');player.updateContext();};$('#page').ready(function(){new csoPlayer();});
