/*
Name:       ImageFlow
Version:    1.1 (March 13 2008)
Author:     Finn Rudolph
Support:    http://finnrudolph.de/ImageFlow

Licence:    ImageFlow is licensed under a Creative Commons 
            Attribution-Noncommercial 3.0 Unported License 
            (http://creativecommons.org/licenses/by-nc/3.0/).

            You are free:
                + to Share - to copy, distribute and transmit the work
                + to Remix - to adapt the work

            Under the following conditions:
                + Attribution. You must attribute the work in the manner specified by the author or licensor 
                  (but not in any way that suggests that they endorse you or your use of the work). 
                + Noncommercial. You may not use this work for commercial purposes. 

            + For any reuse or distribution, you must make clear to others the license terms of this work.
            + Any of the above conditions can be waived if you get permission from the copyright holder.
            + Nothing in this license impairs or restricts the author's moral rights.

Credits:    This script is based on Michael L. Perrys Cover flow in Javascript [1].
            The reflections are generated server-sided by a slightly hacked version 
            of Richard Daveys easyreflections [2] written in PHP. The mouse wheel 
            support is an implementation of Adomas Paltanavicius JavaScript mouse 
            wheel code [3]. It also uses the domReadyEvent from Tanny O'Haley [4].

            [1] http://www.adventuresinsoftware.com/blog/?p=104#comment-1981
            [2] http://reflection.corephp.co.uk/v2.php
            [3] http://adomas.org/javascript-mouse-wheel/
            [4] http://tanny.ica.com/ICA/TKO/tkoblog.nsf/dx/domcontentloaded-for-browsers-part-v
*/
function ImageFlow(){this.defaults={aspectRatio:1.964,captions:true,imageCursor:"default",ImageFlowID:"imageflow",imageFocusM:1,imageFocusMax:4,imagesHeight:0.67,imagesM:1,onClick:function(){document.location=this.url},opacity:false,opacityArray:[10,8,6,4,2],percentLandscape:118,percentOther:100,preloadImages:true,reflections:true,reflectionGET:"",reflectionP:0.5,reflectionPNG:false,scrollbarP:0.6,slider:true,sliderCursor:"e-resize",sliderWidth:14,startID:1,startAnimation:false,xStep:150};var a=this;this.init=function(f){var e=new Array("aspectRatio","captions","imageCursor","imagesM","ImageFlowID","imageFocusM","imageFocusMax","imagesHeight","onClick","opacity","opacityArray","percentLandscape","percentOther","preloadImages","reflections","reflectionGET","reflectionP","reflectionPNG","scrollbarP","slider","sliderCursor","sliderWidth","startID","startAnimation","xStep");var c=e.length;for(var g=0;g<c;g++){var d=e[g];this[d]=(f!==undefined&&f[d]!==undefined)?f[d]:a.defaults[d]}var j=document.getElementById(a.ImageFlowID);if(j){j.style.visibility="visible";this.ImageFlowDiv=j;if(this.createStructure()){this.imagesDiv=document.getElementById(a.ImageFlowID+"_images");this.captionDiv=document.getElementById(a.ImageFlowID+"_caption");this.scrollbarDiv=document.getElementById(a.ImageFlowID+"_scrollbar");this.sliderDiv=document.getElementById(a.ImageFlowID+"_slider");this.indexArray=[];this.current=0;this.imageID=0;this.target=0;this.memTarget=0;this.firstRefresh=true;this.firstCheck=true;this.busy=false;if(this.slider===false){this.scrollbarDiv.style.display="none"}var h=this.ImageFlowDiv.offsetWidth;var b=Math.round(h/a.aspectRatio);document.getElementById(a.ImageFlowID+"_loading_txt").style.paddingTop=((b*0.5)-22)+"px";j.style.height=b+"px";this.loadingProgress()}}};this.createStructure=function(){var g=document.createElement("div");g.setAttribute("id",a.ImageFlowID+"_images");g.setAttribute("class","images");g.setAttribute("className","images");var e=null;var l=this.ImageFlowDiv.childNodes.length;for(var j=0;j<l;j++){e=this.ImageFlowDiv.childNodes[j];if(e&&e.nodeType==1&&e.nodeName=="IMG"){if(a.reflections===true){var k="2";if(a.reflectionPNG===true){k="3"}var b=e.getAttribute("src",2);b=b+a.reflectionGET;e.setAttribute("src",b)}var n=e.cloneNode(true);g.appendChild(n)}}var p=document.createElement("p");var d=document.createTextNode(" ");p.setAttribute("id",a.ImageFlowID+"_loading_txt");p.appendChild(d);var c=document.createElement("div");c.setAttribute("id",a.ImageFlowID+"_loading");c.setAttribute("class","loading");c.setAttribute("className","loading");var f=document.createElement("div");f.setAttribute("id",a.ImageFlowID+"_loading_bar");f.setAttribute("class","loading_bar");f.setAttribute("className","loading_bar");c.appendChild(f);var h=document.createElement("div");h.setAttribute("id",a.ImageFlowID+"_caption");h.setAttribute("class","caption");h.setAttribute("className","caption");var m=document.createElement("div");m.setAttribute("id",a.ImageFlowID+"_scrollbar");m.setAttribute("class","scrollbar");m.setAttribute("className","scrollbar");var i=document.createElement("div");i.setAttribute("id",a.ImageFlowID+"_slider");i.setAttribute("class","slider");i.setAttribute("className","slider");m.appendChild(i);var o=false;if(a.ImageFlowDiv.appendChild(g)&&a.ImageFlowDiv.appendChild(p)&&a.ImageFlowDiv.appendChild(c)&&a.ImageFlowDiv.appendChild(h)&&a.ImageFlowDiv.appendChild(m)){for(j=0;j<l;j++){e=this.ImageFlowDiv.childNodes[j];if(e&&e.nodeType==1&&e.nodeName=="IMG"){this.ImageFlowDiv.removeChild(e)}}o=true}return o};this.loadingProgress=function(){var b=a.loadingStatus();if(b<100||a.firstCheck===true&&a.preloadImages===true){if(a.firstCheck===true&&b==100){a.firstCheck=false;window.setTimeout(a.loadingProgress,100)}else{window.setTimeout(a.loadingProgress,40)}}else{document.getElementById(a.ImageFlowID+"_loading_txt").style.display="none";document.getElementById(a.ImageFlowID+"_loading").style.display="none";window.setTimeout(a.addResizeEvent,1000);a.initMouseWheel();a.MouseDrag.init();a.Key.init();a.refresh(true);document.getElementById(a.ImageFlowID+"_scrollbar").style.visibility="visible";var c=a.startID-1;if(c<0){c=0}if(c>a.max){c=a.max-1}a.glideTo(c);if(a.startAnimation===true){a.moveTo(5000)}}};this.loadingStatus=function(){var j=a.imagesDiv.childNodes.length;var e=0,d=0;var b=null;for(var g=0;g<j;g++){b=a.imagesDiv.childNodes[g];if(b&&b.nodeType==1&&b.nodeName=="IMG"){if(b.complete===true){d++}e++}}var c=Math.round((d/e)*100);var h=document.getElementById(a.ImageFlowID+"_loading_bar");h.style.width=c+"%";var k=document.getElementById(a.ImageFlowID+"_loading_txt");var f=document.createTextNode("loading images "+d+"/"+e);k.replaceChild(f,k.firstChild);return c};this.refresh=function(){this.iWidth=a.imagesDiv.offsetWidth;this.maxHeight=Math.round(a.iWidth/a.aspectRatio);this.maxFocus=a.imageFocusMax*a.xStep;this.size=a.iWidth*0.5;this.sliderWidth=a.sliderWidth*0.5;this.scrollbarWidth=(a.iWidth-(Math.round(a.sliderWidth)*2))*a.scrollbarP;this.imagesDivHeight=Math.round(a.maxHeight*a.imagesHeight);a.ImageFlowDiv.style.height=a.maxHeight+"px";a.imagesDiv.style.height=a.imagesDivHeight+"px";a.captionDiv.style.width=a.iWidth+"px";a.captionDiv.style.marginTop=Math.round(a.iWidth*0.02)+"px";a.scrollbarDiv.style.width=a.scrollbarWidth+"px";a.scrollbarDiv.style.marginTop=Math.round(a.iWidth*0.02)+"px";a.scrollbarDiv.style.marginLeft=Math.round(a.sliderWidth+((a.iWidth-a.scrollbarWidth)/2))+"px";a.sliderDiv.style.cursor=a.sliderCursor;a.sliderDiv.onmousedown=function(){a.MouseDrag.start(this);return false};var f=(a.reflections===true)?a.reflectionP+1:1;var b=a.imagesDiv.childNodes.length;var d=0;var e=null;for(var c=0;c<b;c++){e=a.imagesDiv.childNodes[c];if(e!==null&&e.nodeType==1&&e.nodeName=="IMG"){this.indexArray[d]=c;e.url=e.getAttribute("longdesc");e.xPosition=(-d*a.xStep);e.i=d;if(a.firstRefresh){if(e.getAttribute("width")!==null&&e.getAttribute("height")!==null){e.w=e.getAttribute("width");e.h=e.getAttribute("height")*f}else{e.w=e.width;e.h=e.height}}if((e.w)>(e.h/(a.reflectionP+1))){e.pc=a.percentLandscape;e.pcMem=a.percentLandscape}else{e.pc=a.percentOther;e.pcMem=a.percentOther}e.style.cursor=a.imageCursor;d++}}this.max=a.indexArray.length;if(a.firstRefresh){a.firstRefresh=false}a.glideTo(a.imageID);a.moveTo(a.current)};this.moveTo=function(j){this.current=j;this.zIndex=a.max;for(var g=0;g<a.max;g++){var d=a.imagesDiv.childNodes[a.indexArray[g]];var e=g*-a.xStep;if((e+a.maxFocus)<a.memTarget||(e-a.maxFocus)>a.memTarget){d.style.visibility="hidden";d.style.display="none"}else{var h=(Math.sqrt(10000+j*j)+100)*a.imagesM;var c=j/h*a.size+a.size;d.style.display="block";var i=(d.h/d.w*d.pc)/h*a.size;var b=0;switch(i>a.maxHeight){case false:b=d.pc/h*a.size;break;default:i=a.maxHeight;b=d.w*i/d.h;break}var f=(a.imagesDivHeight-i)+((i/(a.reflectionP+1))*a.reflectionP);d.style.left=c-(d.pc/2)/h*a.size+"px";if(b&&i){d.style.height=i+"px";d.style.width=b+"px";d.style.top=f+"px"}d.style.visibility="visible";switch(j<0){case true:this.zIndex++;break;default:this.zIndex=a.zIndex-1;break}switch(d.i==a.imageID){case false:d.onclick=function(){a.glideTo(this.i)};break;default:this.zIndex=a.zIndex+1;if(d.url!==""){d.onclick=a.onClick}break}d.style.zIndex=a.zIndex}j+=a.xStep}};this.glideTo=function(e){var b=-e*a.xStep;this.target=b;this.memTarget=b;this.imageID=e;var d=a.imagesDiv.childNodes[e].getAttribute("alt");if(d===""||a.captions===false){d="&nbsp;"}a.captionDiv.innerHTML=d;if(a.MouseDrag.busy===false){this.newSliderX=(e*a.scrollbarWidth)/(a.max-1)-a.MouseDrag.newX;a.sliderDiv.style.marginLeft=(a.newSliderX-a.sliderWidth)+"px"}if(a.opacity===true||a.imageFocusM!==a.defaults.imageFocusM){a.setOpacity(a.imagesDiv.childNodes[e],a.opacityArray[0]);a.imagesDiv.childNodes[e].pc=a.imagesDiv.childNodes[e].pc*a.imageFocusM;var c=0;var j=0;var g=0;var h=a.opacityArray.length;for(var f=1;f<(a.imageFocusMax+1);f++){if((f+1)>h){c=a.opacityArray[h-1]}else{c=a.opacityArray[f]}j=e+f;g=e-f;if(j<a.max){a.setOpacity(a.imagesDiv.childNodes[j],c);a.imagesDiv.childNodes[j].pc=a.imagesDiv.childNodes[j].pcMem}if(g>=0){a.setOpacity(a.imagesDiv.childNodes[g],c);a.imagesDiv.childNodes[g].pc=a.imagesDiv.childNodes[g].pcMem}}}if(a.busy===false){window.setTimeout(a.animate,50);a.busy=true}};this.animate=function(){switch(a.target<a.current-1||a.target>a.current+1){case true:a.moveTo(a.current+(a.target-a.current)/3);window.setTimeout(a.animate,50);a.busy=true;break;default:a.busy=false;break}};this.setOpacity=function(b,c){if(a.opacity===true){b.style.opacity=c/10;b.style.filter="alpha(opacity="+c*10+")"}};this.initMouseWheel=function(){if(window.addEventListener){a.ImageFlowDiv.addEventListener("DOMMouseScroll",a.eventMouseWheel,false)}a.ImageFlowDiv.onmousewheel=a.eventMouseWheel};this.eventMouseWheel=function(b){var c=0;if(!b){b=window.event}if(b.wheelDelta){c=b.wheelDelta/120}else{if(b.detail){c=-b.detail/3}}if(c){a.handleMouseWheel(c)}if(b.preventDefault){b.preventDefault()}b.returnValue=false};this.handleMouseWheel=function(d){var c=false;var b=0;if(d>0){if(a.imageID>=1){b=a.imageID-1;c=true}}else{if(a.imageID<(a.max-1)){b=a.imageID+1;c=true}}if(c===true){a.glideTo(b)}};this.MouseDrag={object:null,objectX:0,mouseX:0,newX:0,busy:false,init:function(){a.addEvent(a.ImageFlowDiv,"mousemove",a.MouseDrag.drag);a.addEvent(a.ImageFlowDiv,"mouseup",a.MouseDrag.stop);a.addEvent(document,"mouseup",a.MouseDrag.stop);a.ImageFlowDiv.onselectstart=function(){var b=true;if(a.MouseDrag.busy===true){b=false}return b}},start:function(b){a.MouseDrag.object=b;a.MouseDrag.objectX=a.MouseDrag.mouseX-b.offsetLeft+a.newSliderX},stop:function(){a.MouseDrag.object=null;a.MouseDrag.busy=false},drag:function(f){var b=0;if(!f){f=window.event}if(f.pageX){b=f.pageX}else{if(f.clientX){b=f.clientX+document.body.scrollLeft+document.documentElement.scrollLeft}}a.MouseDrag.mouseX=b;if(a.MouseDrag.object!==null){var g=(a.MouseDrag.mouseX-a.MouseDrag.objectX)+a.sliderWidth;if(g<(-a.newSliderX)){g=-a.newSliderX}if(g>(a.scrollbarWidth-a.newSliderX)){g=a.scrollbarWidth-a.newSliderX}var d=(g+a.newSliderX)/(a.scrollbarWidth/(a.max-1));var c=Math.round(d);a.MouseDrag.newX=g;a.MouseDrag.object.style.left=g+"px";if(a.imageID!==c){a.glideTo(c)}a.MouseDrag.busy=true}}};this.Key={init:function(){document.onkeydown=function(b){a.Key.handle(b)}},handle:function(c){var b=a.Key.get(c);switch(b){case 39:a.handleMouseWheel(-1);break;case 37:a.handleMouseWheel(1);break}},get:function(b){b=b||window.event;return b.keyCode}};this.addEvent=function(d,c,b){if(d.addEventListener){d.addEventListener(c,b,false)}else{if(d.attachEvent){d["e"+c+b]=b;d[c+b]=function(){d["e"+c+b](window.event)};d.attachEvent("on"+c,d[c+b])}}};this.addResizeEvent=function(){var b=window.onresize;if(typeof window.onresize!="function"){window.onresize=function(){a.refresh()}}else{window.onresize=function(){if(b){b()}a.refresh()}}}}var domReadyEvent={name:"domReadyEvent",events:{},domReadyID:1,bDone:false,DOMContentLoadedCustom:null,add:function(a){if(!a.$$domReadyID){a.$$domReadyID=this.domReadyID++;if(this.bDone){a()}this.events[a.$$domReadyID]=a}},remove:function(a){if(a.$$domReadyID){delete this.events[a.$$domReadyID]}},run:function(){if(this.bDone){return}this.bDone=true;for(var a in this.events){this.events[a]()}},schedule:function(){if(this.bDone){return}if(/KHTML|WebKit/i.test(navigator.userAgent)){if(/loaded|complete/.test(document.readyState)){this.run()}else{setTimeout(this.name+".schedule()",100)}}else{if(document.getElementById("__ie_onload")){return true}}if(typeof this.DOMContentLoadedCustom==="function"){if(typeof document.getElementsByTagName!=="undefined"&&(document.getElementsByTagName("body")[0]!==null||document.body!==null)){if(this.DOMContentLoadedCustom()){this.run()}else{setTimeout(this.name+".schedule()",250)}}}return true},init:function(){if(document.addEventListener){document.addEventListener("DOMContentLoaded",function(){domReadyEvent.run()},false)}setTimeout("domReadyEvent.schedule()",100);function run(){domReadyEvent.run()}if(typeof addEvent!=="undefined"){addEvent(window,"load",run)}else{if(document.addEventListener){document.addEventListener("load",run,false)}else{if(typeof window.onload==="function"){var oldonload=window.onload;window.onload=function(){domReadyEvent.run();oldonload()}}else{window.onload=run}}}
/*@cc_on
			@if (@_win32 || @_win64)
			document.write("<script id=__ie_onload defer src=\"//:\"><\/script>");
			var script = document.getElementById("__ie_onload");
			script.onreadystatechange = function()
			{
				if (this.readyState == "complete")
				{
					domReadyEvent.run(); // call the onload handler
				}
			};
			@end
		@*/
}};var domReady=function(a){domReadyEvent.add(a)};domReadyEvent.init();domReady(function(){var a=new ImageFlow();a.init({ImageFlowID:"myImageFlow"})});
