// JavaScript Document
var freewheelviewer= Class.create();

freewheelviewer.prototype = {		

	initialize: function(file) {
	
	this.visible = false;
	this.file = 'http://www.gestellbau.com/temp/produkte/data/'+file;
	this.container = 0;
	this.frm = 0;
	this.loaded = false;
	
	var fileBottomNavCloseImage = "images/resources/closelabel.gif";
	
	var theBody = document.getElementsByTagName("body").item(0);


	var overlay = document.createElement("div");
	overlay.setAttribute('id','cad-overlay');
	overlay.style.display = 'none';
	overlay.style.height = getPageSize()[1]+"px";
	theBody.appendChild(overlay);
	
	var ueberischt = document.createElement("div");
	ueberischt.setAttribute('id','cad-body');
	ueberischt.style.display = 'none';
	theBody.appendChild(ueberischt);
	
	this.container = document.createElement("div");
	this.container.setAttribute('id','cad-container');
	ueberischt.appendChild(this.container);
	
	var objBottomNav = document.createElement("div");
	objBottomNav.setAttribute('id','cad-nav');
	this.container.appendChild(objBottomNav);
	
		var objBottomNavCloseLink = document.createElement("a");
		objBottomNavCloseLink.setAttribute('id','bottomNavClose');
		//objBottomNavCloseLink.setAttribute('href','#');
		objBottomNavCloseLink.style.border = ('0');
		objBottomNavCloseLink.onclick = function() { cadviewer.toggleDiv();}
		objBottomNav.appendChild(objBottomNavCloseLink);
	
		var objBottomNavCloseImage = document.createElement("img");
		objBottomNavCloseImage.setAttribute('src', fileBottomNavCloseImage);
		objBottomNavCloseLink.appendChild(objBottomNavCloseImage);
	
	
	this.frm = document.createElement("iframe");
	this.frm.setAttribute('id','cad-iframe');
	this.container.appendChild(this.frm);
	},

	toggleDiv: function(){
		if(!this.visible){
			new Effect.Appear('cad-overlay', { duration: 1.0, from: 0.0, to: 0.8, afterFinish: this.show.bind(this) });
			this.visible = true;
		}
		else{
			Element.hide('cad-body');
			new Effect.Fade('cad-overlay', { duration: 0.5, from: 0.8, to: 0.0, afterFinish: this.hide.bind(this)  });
			this.visible = false;
		}
	},
	
	show: function(){
		if(!this.loaded){
		var arrayPageSize = getPageSize();
		var divMargin = 50;
		var width = eval(arrayPageSize[2]- divMargin);
		var height = eval(arrayPageSize[3]- divMargin);
		var arrayPageScroll = getPageScroll();
		var lightboxTop =  eval(arrayPageScroll[1] + (arrayPageSize[3] / 10))+"px";
		Element.setStyle($('cad-body'), {
			'top': lightboxTop
		});
		this.frm.width = "900px";
		this.frm.height= "550px";
		//new Effect.ScrollTo('cad-body', { duration:'0.2', offset:-20 });
		new Effect.Appear('cad-body', {duration:0.35});
		this.frm.src='http://freewheel.autodesk.com/dwf.aspx?path=' + this.file + '&mode=ViewOnly  ';
		this.loaded = true;
		}else{
			new Effect.Appear('ueberischt', {duration:0.35});
		}
	},
	
	hide: function(){
		Element.hide('cad-overlay');
	}
	
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
