/* JScript source code for generating a page banner based on one or more Flickr photostreams plus individual photos
   This code can be used on an as-is basis without modification and removal of the copyright notice.

   Copyright    David Power,2010   FlickrBanner@dwpower.freeserve.co.uk

   Calling logic
		FlickrBanner(division_element,    - ID of division to hold the banner
				 max_images,	    - Number of images to be shown
				 x,			    - Max width of image
				 y, 			    - Max height of image
				 gap,			    - Gap in pixels between images
				 Images,		    - Optional - list of URLs for images to be shown outside of library of images downloaded from Flickr
				 flickr_id,		    - Flickr account id
				 Tags, 		    - List of Flickr photostreams to be used as source of images
				 QuietTim_MS,	    - Gap between switching an image (in milliseconds)
			 	 ChangeTim_MS)	    - Time to switch an image (expressed in milliseconds)

    Logic
        1.  List of Flickr Images used is based on either variable FlickrBanner stored within a cookie, or
            if the cookie is undefined or does not hold the variable, then it is rebuilt using the User supplied list of images
            plus additional ones downloaded from the Flickr photostream.
            Note that Flickr only provides the first 20 images from a photostream.  The list of photostreams argument enables more
            the banner to the based on more images.
        2.  The timing arguments of the FlickrBanner routine define the gap between changing a photo when the banner area will remain
            static and the time to transition a particular slot.
            
    Note
        1.  As the code uses site redirection to read the flickr photostream it is necessary to set an Apache rewrite rule to direct all requests
            to www.yoursite.com/flickr to api.flickr.com.        
          
*/
var g,h,n,o,p,q,r,s,t,u;function g1(a,b,c){var d=new Date();d.setDate(d.getDate()+c);document.cookie=a+"="+escape(b)+((c==null)?"":";expires="+d.toUTCString())};function h1(a){if(document.cookie.length>0){var b=document.cookie.indexOf(a+"=");if(b!=-1){b=b+a.length+1;var c=document.cookie.indexOf(";",b);if(c==-1)c=document.cookie.length;return unescape(document.cookie.substring(b,c))}};return""}function i1(){var c=h1("FlickrBanner");if(c=="")return false;g=c.split(",");return true};function j1(){var c="";for(var i=0;i<g.length;i++){c+=(i==0?'':',')+(g[i]instanceof Array?g[i][0]+"|"+escape(g[i][1]):(typeof(g[i])=='string'?g[i]:g[i].src+(g[i].alt==''?'':'|'+escape(g[i].alt))))};g1("FlickrBanner",c,7)};function k1(d,e){function l1(a,b){var c;if(window.ActiveXObject!=undefined){c=new ActiveXObject('Microsoft.XMLHTTP');c.open(a,b,true)}else{c=new XMLHttpRequest();if("withCredentials"in c){c.open(a,b,true);c.setRequestHeader('X-PINGOTHER','pingpong');c.setRequestHeader('Content-Type','application/xml');c.withCredentials="true"}else if(typeof XDomainRequest!="undefined"){c=new XDomainRequest();c.open(a,b)}else{c=null}};return c};var f=l1('GET',d);if(f){f.onreadystatechange=function(){if(f.readyState==4){f.onreadystatechange=o1;if(f.status==200){e(f.responseText,f.status)}else{alert("Error: returned status code "+f.status+" "+f.getAllResponseHeaders())}}};f.send(null)}};function o1(){};function p1(a,b){var i=a.indexOf(b+'="')+b.length+2,j=a.indexOf('"',i);return(a.substr(i,j-i))};function q1(a,b){var i;var p;do{p=Math.floor(Math.random()*b);for(i=0;i<a.length;i++){if(a[i]==p)break}}while(i<a.length);return p};function r1(a,b){for(var i=0;i<a.length;i++){if(a[i][0]==b)return true};return false};function s1(a){var b;var c;with(g[h[a]]){var d=(clientHeight==0?style.height:clientHeight);var e=(clientWidth==0?style.width:clientWidth);if(d>=e){b=d/e;style.top=(p+s)+"px";c=(o+a*(q+s)+(q-(e*r/d))/2)+"px";style.left=c;style.height=r+"px";style.width=r/b+"px"}else{b=d/clientWidth;style.top=(p+s+(r-(clientHeight*q/clientWidth))/2)+"px";c=(o+a*(q+s))+"px";style.left=c;style.width=q+"px";style.height=q*b+"px"};style.visibility="visible";style.display=""};if((a==c1)&&(b1==1)){b1=2}};function t1(a,b){var c,src;switch(g[a]instanceof Array?"array":typeof(g[a])){case"object":s1(b);break;case"string":if(g[a].indexOf("|")==0){src=g[a];c=document.createElement("IMG");c.style.visibility="hidden";c.setAttribute("id","Banner_Index_"+a);c.style.position="absolute";c.style.z_index=1;if((b==c1)&&(b1==1))c2(c,0);c.setAttribute("onload","s1("+b+")");document.body.appendChild(c);g[a]=document.getElementById("Banner_Index_"+a);c.src=src}else{g[a]=g[a].split("|");g[a][1]=unescape(g[a][1])};case"array":src=g[a][0];c=document.createElement("IMG");c.setAttribute("onload","s1("+b+")");c.setAttribute("alt",g[a][1]);c.setAttribute("id","Banner_Index_"+a);c.style.visibility="hidden";c.style.position="absolute";c.style.z_index=1;if((b==c1)&&(b1==1))c2(c,0);document.body.appendChild(c);g[a]=document.getElementById("Banner_Index_"+a);c.src=src;break}};function u1(){for(var i=0;i<h.length;i++){if(h[i]==undefined){if(i<g.length){h[i]=q1(h,g.length);t1(h[i],i)}}};if(h[h.length-1]!=undefined)setTimeout("e2()",t)};function m1(l,m){function b2(a){if(a[0].text==undefined){return a[0].textContent}else{return a[0].text}};k1(document.URL.substr(0,document.URL.indexOf("/",7)+1)+'flickr/services/feeds/photos_public.gne?id='+l+'&lang=en-us&tags='+m+'&tagmode=ANY',function(a,b){var c;try{c=new ActiveXObject("Microsoft.XMLDOM");c.async="false";c.loadXML(a)}catch(e){c=(new DOMParser()).parseFromString(a,"text/xml")};var d=c.getElementsByTagName("entry");if(d.length>0){for(var i=0;i<d.length;i++){var f=b2(d[i].getElementsByTagName('content'));var j=f.indexOf('<img'),i2=f.indexOf('/>',j);f=f.substr(j,i2-j+2);var k=p1(f,'src');if(!r1(g,k)){g[g.length]=new Array(k,p1(f,'alt'))}};j1()};if(h[h.length-1]==undefined)u1()})};function n1(e){function b2(a){if(a[0].text==undefined){return a[0].textContent}else{return a[0].text}};var f=document.URL.substr(0,document.URL.indexOf('/',10));k1(f+e,function(a,b){var i=a.indexOf('<div class="photo-gallery">');i=a.indexOf("<img",i);while(i>0){var c=a.substr(i,a.indexOf("/>",i));var d=p1(c,'src');d=f+d.substr(d.indexOf('/',10)+1);if(!r1(g,d)){g[g.length]=new Array(d,p1(c,'alt'))};i=a.indexOf("<img",i+20)};j1();if(h[h.length-1]==undefined)u1()})};function c2(a,b){if(document.all){a.style.filter='alpha(opacity='+b+')'}else{var O=b/100;if(O<0.01)O=0.01;if(O>0.99)O=0.99;a.style.opacity=O}};var c1;var d1;var e1;var f1=5;var b1;function e2(){c1=Math.floor(Math.random()*h.length);d1=g[h[c1]];d1.style.z_index=10;h[c1]=q1(h,g.length);b1=1;t1(h[c1],c1);e1=1;setTimeout('d2()',u/f1)};function d2(){if(b1==2){e1+=f1;if(e1>=100){d1.style.visibilty="hidden";d1.style.display="none";c2(g[h[c1]],100);setTimeout('e2()',t)}else{c2(g[h[c1]],e1);c2(d1,100-e1);setTimeout('d2()',u/f1)}}else{setTimeout('d2()',u/f1)}};function FlickrBanner(a,b,x,y,c,d,e,f,j,k,l){n=document.getElementById(a);var m=n;o=m.offsetLeft;p=m.offsetTop;while(m=m.offsetParent){o+=m.offsetLeft;p+=m.offsetTop};q=x;r=y;s=c;t=k;u=l;if(n==null){alert("FlickBanner: Division "+a+" is not defined");return(-1)};if((parseInt(n.style.width)<(b*(x+c)-c))||(parseInt(n.style.height)<(y+2*c))){alert("FlickrBanner: Division is too small to hold the requested images");return(-2)};if(!i1()){if(d instanceof Array){g=(d.length>0?d:new Array())}else if(typeof(d)=="number"){g=new Array(d)};if(j!='')n1(j);if(f instanceof Array){for(var i=0;i<f.length;i++){m1(e,f[i])}}else m1(e,f)};h=new Array();h.length=b;u1()};
