/*
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:20,startID:1,startAnimation:false,xStep:150};var thisObject=this;this.init=function(options){var optionsArray=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 max=optionsArray.length;for(var i=0;i<max;i++){var name=optionsArray[i];this[name]=(options!==undefined&&options[name]!==undefined)?options[name]:thisObject.defaults[name]}var ImageFlowDiv=document.getElementById(thisObject.ImageFlowID);if(ImageFlowDiv){ImageFlowDiv.style.visibility="visible";this.ImageFlowDiv=ImageFlowDiv;if(this.createStructure()){this.imagesDiv=document.getElementById(thisObject.ImageFlowID+"_images");this.captionDiv=document.getElementById(thisObject.ImageFlowID+"_caption");this.newCaption=document.getElementById("rotator_caption");this.cardLink=document.getElementById("rotator_link");this.scrollbarDiv=document.getElementById(thisObject.ImageFlowID+"_scrollbar");this.sliderDiv=document.getElementById(thisObject.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 width=this.ImageFlowDiv.offsetWidth;var height=Math.round(width/thisObject.aspectRatio);document.getElementById(thisObject.ImageFlowID+"_loading_txt").style.paddingTop=((height*0.5)-22)+"px";ImageFlowDiv.style.height=height+"px";this.loadingProgress()}}};this.createStructure=function(){var imagesDiv=document.createElement("div");imagesDiv.setAttribute("id",thisObject.ImageFlowID+"_images");imagesDiv.setAttribute("class","images");imagesDiv.setAttribute("className","images");var node=null;var max=this.ImageFlowDiv.childNodes.length;for(var index=0;index<max;index++){node=this.ImageFlowDiv.childNodes[index];if(node&&node.nodeType==1&&node.nodeName=="IMG"){if(thisObject.reflections===true){var version="2";if(thisObject.reflectionPNG===true){version="3"}var src=node.getAttribute("src",2);src="reflect"+version+".php?img="+src+thisObject.reflectionGET;node.setAttribute("src",src)}var imageNode=node.cloneNode(true);imagesDiv.appendChild(imageNode)}}var loadingP=document.createElement("p");var loadingText=document.createTextNode(" ");loadingP.setAttribute("id",thisObject.ImageFlowID+"_loading_txt");loadingP.appendChild(loadingText);var loadingDiv=document.createElement("div");loadingDiv.setAttribute("id",thisObject.ImageFlowID+"_loading");loadingDiv.setAttribute("class","loading");loadingDiv.setAttribute("className","loading");var loadingBarDiv=document.createElement("div");loadingBarDiv.setAttribute("id",thisObject.ImageFlowID+"_loading_bar");loadingBarDiv.setAttribute("class","loading_bar");loadingBarDiv.setAttribute("className","loading_bar");loadingDiv.appendChild(loadingBarDiv);var captionDiv=document.createElement("div");captionDiv.setAttribute("id",thisObject.ImageFlowID+"_caption");captionDiv.setAttribute("class","caption");captionDiv.setAttribute("className","caption");var scrollbarDiv=document.createElement("div");scrollbarDiv.setAttribute("id",thisObject.ImageFlowID+"_scrollbar");scrollbarDiv.setAttribute("class","scrollbar");scrollbarDiv.setAttribute("className","scrollbar");var sliderDiv=document.createElement("div");sliderDiv.setAttribute("id",thisObject.ImageFlowID+"_slider");sliderDiv.setAttribute("class","slider");sliderDiv.setAttribute("className","slider");scrollbarDiv.appendChild(sliderDiv);var success=false;if(thisObject.ImageFlowDiv.appendChild(imagesDiv)&&thisObject.ImageFlowDiv.appendChild(loadingP)&&thisObject.ImageFlowDiv.appendChild(loadingDiv)&&thisObject.ImageFlowDiv.appendChild(captionDiv)&&thisObject.ImageFlowDiv.appendChild(scrollbarDiv)){for(index=0;index<max;index++){node=this.ImageFlowDiv.childNodes[index];if(node&&node.nodeType==1&&node.nodeName=="IMG"){this.ImageFlowDiv.removeChild(node)}}success=true}return success};this.loadingProgress=function(){var p=thisObject.loadingStatus();if(p<100||thisObject.firstCheck===true&&thisObject.preloadImages===true){if(thisObject.firstCheck===true&&p==100){thisObject.firstCheck=false;window.setTimeout(thisObject.loadingProgress,100)}else{window.setTimeout(thisObject.loadingProgress,40)}}else{document.getElementById(thisObject.ImageFlowID+"_loading_txt").style.display="none";document.getElementById(thisObject.ImageFlowID+"_loading").style.display="none";window.setTimeout(thisObject.addResizeEvent,1000);thisObject.initMouseWheel();thisObject.MouseDrag.init();thisObject.Key.init();thisObject.refresh(true);document.getElementById(thisObject.ImageFlowID+"_scrollbar").style.visibility="visible";var startID=thisObject.startID-1;if(startID<0){startID=0}if(startID>thisObject.max){startID=thisObject.max-1}thisObject.glideTo(startID);if(thisObject.startAnimation===true){thisObject.moveTo(5000)}}};this.loadingStatus=function(){var max=thisObject.imagesDiv.childNodes.length;var i=0,completed=0;var image=null;for(var index=0;index<max;index++){image=thisObject.imagesDiv.childNodes[index];if(image&&image.nodeType==1&&image.nodeName=="IMG"){if(image.complete===true){completed++}i++}}var finished=Math.round((completed/i)*100);var loadingBar=document.getElementById(thisObject.ImageFlowID+"_loading_bar");loadingBar.style.width=finished+"%";var loadingP=document.getElementById(thisObject.ImageFlowID+"_loading_txt");var loadingTxt=document.createTextNode("loading images "+completed+"/"+i);loadingP.replaceChild(loadingTxt,loadingP.firstChild);return finished};this.refresh=function(){this.iWidth=thisObject.imagesDiv.offsetWidth;this.maxHeight=Math.round(thisObject.iWidth/thisObject.aspectRatio);this.maxFocus=thisObject.imageFocusMax*thisObject.xStep;this.size=thisObject.iWidth*0.5;this.sliderWidth=thisObject.sliderWidth*0.5;this.scrollbarWidth=251;/*(thisObject.iWidth-(Math.round(thisObject.sliderWidth)*2))*thisObject.scrollbarP*/;this.imagesDivHeight=Math.round(thisObject.maxHeight*thisObject.imagesHeight);thisObject.ImageFlowDiv.style.height=thisObject.maxHeight+"px";thisObject.imagesDiv.style.height=thisObject.imagesDivHeight+"px";thisObject.captionDiv.style.width=thisObject.iWidth+"px";thisObject.captionDiv.style.marginTop=Math.round(thisObject.iWidth*0.02)+"px";thisObject.scrollbarDiv.style.width=thisObject.scrollbarWidth+"px";thisObject.scrollbarDiv.style.marginTop=Math.round(thisObject.iWidth*0.02)+"px";thisObject.scrollbarDiv.style.marginLeft=Math.round(thisObject.sliderWidth+((thisObject.iWidth-thisObject.scrollbarWidth)/2))+"px";thisObject.sliderDiv.style.cursor=thisObject.sliderCursor;thisObject.sliderDiv.onmousedown=function(){thisObject.MouseDrag.start(this);return false};var multi=(thisObject.reflections===true)?thisObject.reflectionP+1:1;var max=thisObject.imagesDiv.childNodes.length;var i=0;var image=null;for(var index=0;index<max;index++){image=thisObject.imagesDiv.childNodes[index];if(image!==null&&image.nodeType==1&&image.nodeName=="IMG"){this.indexArray[i]=index;image.url=image.getAttribute("longdesc");image.xPosition=(-i*thisObject.xStep);image.i=i;if(thisObject.firstRefresh){if(image.getAttribute("width")!==null&&image.getAttribute("height")!==null){image.w=image.getAttribute("width");image.h=image.getAttribute("height")*multi}else{image.w=image.width;image.h=image.height}}if((image.w)>(image.h/(thisObject.reflectionP+1))){image.pc=thisObject.percentLandscape;image.pcMem=thisObject.percentLandscape}else{image.pc=thisObject.percentOther;image.pcMem=thisObject.percentOther}image.style.cursor=thisObject.imageCursor;i++}}this.max=thisObject.indexArray.length;if(thisObject.firstRefresh){thisObject.firstRefresh=false}thisObject.glideTo(thisObject.imageID);thisObject.moveTo(thisObject.current)};this.moveTo=function(x){this.current=x;this.zIndex=thisObject.max;for(var index=0;index<thisObject.max;index++){var image=thisObject.imagesDiv.childNodes[thisObject.indexArray[index]];var currentImage=index*-thisObject.xStep;if((currentImage+thisObject.maxFocus)<thisObject.memTarget||(currentImage-thisObject.maxFocus)>thisObject.memTarget){image.style.visibility="hidden";image.style.display="none"}else{var z=(Math.sqrt(10000+x*x)+100)*thisObject.imagesM;var xs=x/z*thisObject.size+thisObject.size;image.style.display="block";var newImageH=(image.h/image.w*image.pc)/z*thisObject.size;var newImageW=0;switch(newImageH>thisObject.maxHeight){case false:newImageW=image.pc/z*thisObject.size;break;default:newImageH=thisObject.maxHeight;newImageW=image.w*newImageH/image.h;break}var newImageTop=(thisObject.imagesDivHeight-newImageH)+((newImageH/(thisObject.reflectionP+1))*thisObject.reflectionP);image.style.left=xs-(image.pc/2)/z*thisObject.size+"px";if(newImageW&&newImageH){image.style.height=newImageH+"px";image.style.width=newImageW+"px";image.style.top=newImageTop+"px"}image.style.visibility="visible";switch(x<0){case true:this.zIndex++;break;default:this.zIndex=thisObject.zIndex-1;break}switch(image.i==thisObject.imageID){case false:image.onclick=function(){thisObject.glideTo(this.i)};break;default:this.zIndex=thisObject.zIndex+1;if(image.url!==""){image.onclick=thisObject.onClick}break}image.style.zIndex=thisObject.zIndex}x+=thisObject.xStep}};this.glideTo=function(imageID){var x=-imageID*thisObject.xStep;this.target=x;this.memTarget=x;this.imageID=imageID;var caption=thisObject.imagesDiv.childNodes[imageID].getAttribute("alt");if(caption===""||thisObject.captions===false){caption="&nbsp;"}if(thisObject.newCaption!=null){thisObject.newCaption.innerHTML=caption}if($(".overviewContainer").length>0&&(typeof application.layout.rotateContainer=="function")){application.layout.rotateContainer(imageID)}var link=thisObject.imagesDiv.childNodes[imageID].getAttribute("longdesc");if(link===""){link="#"}if(thisObject.cardLink!=null){thisObject.cardLink.setAttribute("href",""+link+"",0)}if(thisObject.MouseDrag.busy===false){this.newSliderX=(imageID*thisObject.scrollbarWidth)/(thisObject.max-1)-thisObject.MouseDrag.newX;thisObject.sliderDiv.style.marginLeft=(thisObject.newSliderX-thisObject.sliderWidth)+"px"}if(thisObject.opacity===true||thisObject.imageFocusM!==thisObject.defaults.imageFocusM){thisObject.setOpacity(thisObject.imagesDiv.childNodes[imageID],thisObject.opacityArray[0]);thisObject.imagesDiv.childNodes[imageID].pc=thisObject.imagesDiv.childNodes[imageID].pc*thisObject.imageFocusM;var opacityValue=0;var rightID=0;var leftID=0;var last=thisObject.opacityArray.length;for(var i=1;i<(thisObject.imageFocusMax+1);i++){if((i+1)>last){opacityValue=thisObject.opacityArray[last-1]}else{opacityValue=thisObject.opacityArray[i]}rightID=imageID+i;leftID=imageID-i;if(rightID<thisObject.max){thisObject.setOpacity(thisObject.imagesDiv.childNodes[rightID],opacityValue);thisObject.imagesDiv.childNodes[rightID].pc=thisObject.imagesDiv.childNodes[rightID].pcMem}if(leftID>=0){thisObject.setOpacity(thisObject.imagesDiv.childNodes[leftID],opacityValue);thisObject.imagesDiv.childNodes[leftID].pc=thisObject.imagesDiv.childNodes[leftID].pcMem}}}if(thisObject.busy===false){window.setTimeout(thisObject.animate,50);thisObject.busy=true}};this.animate=function(){switch(thisObject.target<thisObject.current-1||thisObject.target>thisObject.current+1){case true:thisObject.moveTo(thisObject.current+(thisObject.target-thisObject.current)/3);window.setTimeout(thisObject.animate,50);thisObject.busy=true;break;default:thisObject.busy=false;break}};this.setOpacity=function(object,value){if(thisObject.opacity===true){object.style.opacity=value/10;object.style.filter="alpha(opacity="+value*10+")"}};this.initMouseWheel=function(){if(window.addEventListener){thisObject.ImageFlowDiv.addEventListener("DOMMouseScroll",thisObject.eventMouseWheel,false)}thisObject.ImageFlowDiv.onmousewheel=thisObject.eventMouseWheel};this.eventMouseWheel=function(event){var delta=0;if(!event){event=window.event}if(event.wheelDelta){delta=event.wheelDelta/120}else{if(event.detail){delta=-event.detail/3}}if(delta){thisObject.handleMouseWheel(delta)}if(event.preventDefault){event.preventDefault()}event.returnValue=false};this.handleMouseWheel=function(delta){var change=false;var newImageID=0;if(delta>0){if(thisObject.imageID>=1){newImageID=thisObject.imageID-1;change=true}}else{if(thisObject.imageID<(thisObject.max-1)){newImageID=thisObject.imageID+1;change=true}}if(change===true){thisObject.glideTo(newImageID)}};this.MouseDrag={object:null,objectX:0,mouseX:0,newX:0,busy:false,init:function(){thisObject.addEvent(thisObject.ImageFlowDiv,"mousemove",thisObject.MouseDrag.drag);thisObject.addEvent(thisObject.ImageFlowDiv,"mouseup",thisObject.MouseDrag.stop);thisObject.addEvent(document,"mouseup",thisObject.MouseDrag.stop);thisObject.ImageFlowDiv.onselectstart=function(){var selection=true;if(thisObject.MouseDrag.busy===true){selection=false}return selection}},start:function(o){thisObject.MouseDrag.object=o;thisObject.MouseDrag.objectX=thisObject.MouseDrag.mouseX-o.offsetLeft+thisObject.newSliderX},stop:function(){thisObject.MouseDrag.object=null;thisObject.MouseDrag.busy=false},drag:function(e){var posx=0;if(!e){e=window.event}if(e.pageX){posx=e.pageX}else{if(e.clientX){posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft}}thisObject.MouseDrag.mouseX=posx;if(thisObject.MouseDrag.object!==null){var newX=(thisObject.MouseDrag.mouseX-thisObject.MouseDrag.objectX)+thisObject.sliderWidth;if(newX<(-thisObject.newSliderX)){newX=-thisObject.newSliderX}if(newX>(thisObject.scrollbarWidth-thisObject.newSliderX)){newX=thisObject.scrollbarWidth-thisObject.newSliderX}var step=(newX+thisObject.newSliderX)/(thisObject.scrollbarWidth/(thisObject.max-1));var imageID=Math.round(step);thisObject.MouseDrag.newX=newX;thisObject.MouseDrag.object.style.left=newX+"px";if(thisObject.imageID!==imageID){thisObject.glideTo(imageID)}thisObject.MouseDrag.busy=true}}};function handle(delta){var change=false;switch(delta>0){case true:if(caption_id>=1){target=target+xstep;new_caption_id=caption_id-1;change=true}break;default:if(caption_id<(max-1)){target=target-xstep;new_caption_id=caption_id+1;change=true}break}if(change==true){glideTo(target,new_caption_id)}}this.Key={init:function(){document.onkeydown=function(event){thisObject.Key.handle(event)}},handle:function(event){var charCode=thisObject.Key.get(event);switch(charCode){case 39:thisObject.handleMouseWheel(-1);break;case 37:thisObject.handleMouseWheel(1);break}},get:function(event){event=event||window.event;return event.keyCode}};this.addEvent=function(obj,type,fn){if(obj.addEventListener){obj.addEventListener(type,fn,false)}else{if(obj.attachEvent){obj["e"+type+fn]=fn;obj[type+fn]=function(){obj["e"+type+fn](window.event)};obj.attachEvent("on"+type,obj[type+fn])}}};this.addResizeEvent=function(){var otherFunctions=window.onresize;if(typeof window.onresize!="function"){window.onresize=function(){thisObject.refresh()}}else{window.onresize=function(){if(otherFunctions){otherFunctions()}thisObject.refresh()}}}}var domReadyEvent={name:"domReadyEvent",events:{},domReadyID:1,bDone:false,DOMContentLoadedCustom:null,add:function(handler){if(!handler.$$domReadyID){handler.$$domReadyID=this.domReadyID++;if(this.bDone){handler()}this.events[handler.$$domReadyID]=handler}},remove:function(handler){if(handler.$$domReadyID){delete this.events[handler.$$domReadyID]}},run:function(){if(this.bDone){return }this.bDone=true;for(var i in this.events){this.events[i]()}},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
		@*/
}};
