/** ReportObject for Equitystory QuickHTML 3.0
* @Autor: Christoph Krichenbauer
* @Copyright: Equitystory AG
* @Version 1.7.c
*/

function Report(reportPath, xSize, ySize, pageName, pageDescObj, jpgFallback, currentFrame, HTMLPages, SWFVersion) {
	this.reportPath=reportPath;
	this.flashPath=reportPath+"/swf/";
	this.jpgPath=reportPath+"/jpg/"
	this.xSize=xSize;
	this.ySize=ySize;
	this.jpgFallback=jpgFallback; 				//boolean
	this.pageDescObj=pageDescObj;				//Array of Page descriptions....
	this.pageName=pageName;						//Array of Page Names [Deckblatt, Index, Index, 1, 2, 3, ... ]
	this.pages=getPages(pageName); 				//Array of all pages [0,1,2,3,...]
	this.pageFiles=getPageFiles(this.pages); 	//Array of all page "filenames" [01,02,03,....,99,100,101,...]
	this.currentFrame=currentFrame-1;
	this.contentIsFlash=false;					//indicates if the current content is flash or real html.
	this.HTMLPages = HTMLPages;
	this.SWFVersion = SWFVersion;				//Version of SWF-Preloader
	this.isSpecialPage=false;					//for special HTML-pages that are not part of the report! (i.e. Download)

/*Private*/
	function getPages(pageName) {
		var result = [];
		for (var i=0;  i<pageName.length; i++) {
			result[i]=i;
		}
		return result;
	}
	
	function getPageFiles(pages) {
		var result = [];
		for (var i=0;  i<pages.length; i++) {
			if ((i+1)<10) {
				result[i]="0"+(i+1);
			} else {
				result[i]=""+(i+1);
			}
		}
		return result;
	}

/*Public*/
	/*checks if traceFrame exists*/
	this.checkTraceFrameActive = function() {
		if (window.traceFrame) {
			return true;	
		} else {
			return false;	
		}
	}

	/*returns the PageNo of the currentFrame*/
	this.getCurrentFrame = function () {
		return this.currentFrame+1;
	}

	/*return page Description of given pageNumber (replaces redundant array of strings) */
	this.getPageDesc = function (pageNumber) {
		for (var desc in this.pageDescObj) {
			for(var i = 0; i < this.pageDescObj[desc].length; i++) {
				if (this.pageDescObj[desc][i] == pageNumber) {
					return desc;
					break;
				}
			}
		}
		return "undefined";
	}

	/*JPG preloader*/
	this.preload= function() {
		if (this.jpgFallback) {
			for(var img = new Array, i = 0; i <= this.pages[this.pages.length-1]; i++) {
				img[i] = new Image;
			    img[i].src = this.jpgPath+'slide_'+this.pageFiles[i]+'.jpg';
			}
		}
	}
	
	/* Embeds the Flash or JPG */
	this.getEmbed = function() {
		this.getEmbed("#FFFFFF");
		}
	
	this.getEmbed = function(BGColor) {
		if (!this.jpgFallback) {
			var flashUrl='/common/swf/'+this.SWFVersion+'/preloader.swf?path='+this.flashPath+'report.swf';//&stageWidth='+this.xSize+'&stageHeight='+this.ySize;
			if (this.currentFrame!=this.pages[0]) {
				flashUrl+='&pageNr='+(this.getCurrentFrame());
			} else {
				flashUrl+='&pageNr=0';
			}
			this.SWFObject= new SWFObject(flashUrl, "content_flash", this.xSize, this.ySize, "7", BGColor);
			this.SWFObject.addParam("quality", "high");
			this.SWFObject.addParam("wmode", "opaque");
			this.SWFObject.addParam("scale", "showAll");
			this.SWFObject.addParam("allowScriptAccess", "always");
//			this.SWFObject.addParam("scale", "exactFit");
			this.SWFObject.write("flashContent");
			this.contentIsFlash=true;
		} else {
			document.getElementById("imgContent").innerHTML='<img id="content_jpg" class="jpg_content" src="'+this.jpgPath+'slide_'+this.pageFiles[this.currentFrame]+'.jpg"/>';
		}
		if (this.HTMLPages[this.currentFrame] != '') {
			$('HTMLContent').className = "";
			$('HTMLContent').innerHTML = "<iframe name='content_html' id='content_html' src='"+this.reportPath+"/html/"+this.HTMLPages[this.currentFrame]+"' width='"+this.xSize+"' height='"+this.ySize+"' frameborder='0' />";
		} else {
			if (!this.jpgFallback) {
				$('flashContent').className = "";
			} else {
				$('imgContent').className = "";
			}
		}
	}
	
	/* Jumps to Page */
	this.goToPage=function (pageNo) {
		pageNo=pageNo-1; //page Numberation starts with 1 and not with 0 any longer...
		if (pageNo>=this.pages[0] && pageNo<=this.pages[this.pages.length-1]&&(this.currentFrame!=pageNo || this.isSpecialPage)) {
			this.currentFrame=pageNo;
			this.isSpecialPage=false;
			window.scrollTo(0,0); //TODO: geht nur im FF /OP. IE nichtmal 7!
			if (this.HTMLPages[pageNo] != '') {
				$('imgContent').className = "invisible";
				$('flashContent').className = "invisible";
				$('HTMLContent').className = "";
				$('HTMLContent').innerHTML = "<iframe name='content_html' id='content_html' src='"+this.reportPath+"/html/"+this.HTMLPages[this.currentFrame]+"' width='"+this.xSize+"' height='"+this.ySize+"' frameborder='0' />";
			} else {
				$('HTMLContent').className = "invisible";
				if (!this.jpgFallback) { //flash version
					$('imgContent').className = "invisible";
					$('flashContent').className = "";
					$('content_flash').GotoFrame(this.currentFrame+1); //da PreLoader
				} else { //jpg-version
					$('flashContent').className = "invisible";
					$('imgContent').className = "";
					$('content_jpg').src=this.jpgPath+'slide_'+this.pageFiles[pageNo]+'.jpg';
				}
			}
			$('navigation_handle').innerHTML=this.pageName[this.currentFrame];
			pageSlider.setValue(this.currentFrame);
			this.zoom(100);
			if (this.checkTraceFrameActive()) {
				if(window.traceFrame.location.href!=window.traceFrame.location.href.replace(/\?.*$/g, "\?pageNo="+escape(pageNo+1)+"&title="+escape(document.title))) {
					window.traceFrame.location.href=window.traceFrame.location.href.replace(/\?.*$/g, "\?pageNo="+(pageNo+1)+"&title="+document.title);
				}
			}
		}
	}
	
	/* Opens a special page in content area */
	this.goToSpecialPage=function (pageName) {
		if(!this.isSpecialPage || (window.content_html && window.content_html.location.href.indexOf(pageName) != -1) ) {
			window.scrollTo(0,0); //TODO: geht nur im FF /OP. IE nichtmal 7!
			$('imgContent').className = "invisible";
			$('flashContent').className = "invisible";
			$('HTMLContent').className = "";
			$('HTMLContent').innerHTML = "<iframe name='content_html' id='content_html' src='"+this.reportPath+"/html/"+pageName+"' width='"+this.xSize+"' height='"+this.ySize+"' frameborder='0' />";
			$('navigation_handle').innerHTML=this.pageName[this.currentFrame];
			this.zoom(100);
			if (this.checkTraceFrameActive() && !this.isSpecialPage) {
				window.traceFrame.location.href=window.traceFrame.location.href.replace(/\?.*$/g, "\?specialPage="+pageName+"&title="+document.title);
			}
			this.isSpecialPage=true;
			
		}
	}
	
		/* Opens a special page in content area */
	this.goToURLPage=function (url) {
		if(!this.isSpecialPage || (window.content_html && window.content_html.location.href.indexOf(url) != -1) ) {
			window.scrollTo(0,0); //TODO: geht nur im FF /OP. IE nichtmal 7!
			$('imgContent').className = "invisible";
			$('flashContent').className = "invisible";
			$('HTMLContent').className = "";
			$('HTMLContent').innerHTML = "<iframe name='content_html' id='content_html' src='"+url+"' width='"+this.xSize+"' height='"+this.ySize+"' frameborder='0' />";
			$('navigation_handle').innerHTML=this.pageName[this.currentFrame];
			this.zoom(100);
			this.isSpecialPage=true;
		}
	}

	
	
	
	
	this.leaveSpecialPage=function () {
		this.goToPage(this.getCurrentFrame());
		this.isSpecialPage=false;
	}
	
	/*Zoom (Flash Only)*/
	this.zoomStep=10;
	this.zoomMaximum=300;
	this.zoomGrade=100;
	this.zoom = function(zoom_value) {
		if (this.zoomGrade != zoom_value){
			if (!this.jpgFallback) {
				zoom_value=Math.round(zoom_value);
				$('content_flash').Zoom( 0 );
				$('content_flash').Zoom( 100 / zoom_value * 100 );
				//alert(100/zoom_value * 100);
				this.zoomGrade=zoom_value;
				zoomSlider.setValue(zoom_value);
			} else {
				alert("JPG-Fallback does not support Zoom");
			}
		}
	}
	this.zoomBigger=function() {
		var newZoom=this.zoomGrade+this.zoomStep;
		if (newZoom > this.zoomMaximum) {
			newZoom=this.zoomMaximum;
		}
		this.zoom(newZoom);
	}
	this.zoomSmaller = function () {
		var newZoom=this.zoomGrade-this.zoomStep;
		if (newZoom < 100) {
			newZoom=100; //man kann nicht kleiner Zoomen
		}
		this.zoom(newZoom);
	}
	
	this.OpenCurrentPDF = function (basePath) {
		open(basePath+"slide_"+this.pageFiles[this.currentFrame]+".pdf");
	}

	this.OpenCompletePDF = function (basePath) {
		open(basePath+"report.pdf");
	}

	this.changeLanguage = function(toLang) {
	    this.reportPath.match(/\/([^\/\?]*)$/);
	    var oldLang = RegExp.$1;

	    if (toLang == null || toLang == "") {
		toLang = (oldLang == "German"?"English":"German");
	    }

	    var languageReplace = new RegExp(oldLang, "");
	    var url = document.location.href.replace(languageReplace,toLang);
	    
	    var pageParam = new RegExp("page=[0-9]*");
	    var pageString = "page="+this.getCurrentFrame();
	    if (url.indexOf("page=") != -1) {
		url = url.replace(pageParam, pageString);
	    } else {
		if (url.indexOf("?") != -1) {
		    url += "&"+pageString;
		} else {
		    url += "?"+pageString;
		}
	    }
	    document.location.href=url;
	}


}
