/**
 * deployment for wizard2 - document management Perrit.nl
 * 2009 - 2010 - JV - Pagelink B.V.
 * CVS: $Revision: 1.2 $
 */
(function () {
	//var dm_jsurl = 'https://www.dm_online.nl';
	
	//var dm_jsurl = 'http://test.dm_online.pageprocessor.nl';
	

	// var dm_jsurl = 'http://ankum';
	
	var dm_jsurl = 'http://shop.perrit.nl';

	//var dm_jsurl = 'http://localhost';

	
	var dm_path = '/documentmanagementwizard/?a=0';
	
	var dm_jsargs = '';

	var dm_sessionurl = 'usesessionurls=true';
	
	/* deze argumenten zijn nodig; begin en eindkomma zijn ook nodig */
	var dm_jsNeededArgs=",dm_id,";

	//try to get this script. Therefore get all SCRIPT tags. The last one is this script itself, because the rest isn't loaded yet. Quite simple huh? ;-)
	var scripttags = document.getElementsByTagName('script');
	var dm_js = scripttags[scripttags.length - 1];
	
	//no script found, try it the old way via the ID dm_js.
	if (!dm_js)
		var dm_js = document.getElementById ('dm_js');
	
	if (!dm_js)
		return;
	
	if (dm_js !== false) {
		// get the possible attributes
		
		
		// width of iframe: height is set automaticly
		var dm_js_iframewidth = dm_js.getAttribute ('iframewidth');
		
		// am I a popup frame. If so, we are atached to the body and centered
		var dm_js_popup = dm_js.getAttribute ('popup');
		
		// the color of the popup border
		var dm_js_popupborder = dm_js.getAttribute ('popupborder');
		
		// the text (if any) to click on to start the popup
		var dm_js_popupclick = dm_js.getAttribute ('popupclick');
		
		// the callback function name (if any) to execute after the click-event
		var dm_js_callback = dm_js.getAttribute ('callback');
		
		var container = false;
		
		
		if (dm_sessionurl ) {
			dm_jsargs += '&' + dm_sessionurl;
		}

	
		try {
			var dm_jslocation = window.top.document.location.toString ();
		} catch (e) {
			var dm_jslocation = document.location.toString ();
		}
	
		dm_jslocation = unescape (dm_jslocation);
		
		
		var _dm_jsargs = '';

		dm_jslocation = dm_jslocation.split(/\&|\?/);
	
		var dm_jssign = "?";
	
		if (typeof dm_jslocation=='object') {
			for (var dm_jsel = 1; dm_jsel < dm_jslocation.length; dm_jsel++) {
				var dm_jsloc2 = dm_jslocation[dm_jsel].split(/=/);
	
				var re = new RegExp (',' + dm_jsloc2[0] + ',');
	
				if (re.test(dm_jsNeededArgs)) {
					_dm_jsargs += dm_jssign + dm_jsloc2[0] + '=' + dm_jsloc2[1]
					dm_jssign="&";
				}
			}
		}
	
		if (_dm_jsargs!='')
			dm_jsargs = _dm_jsargs;
		else
			dm_jsargs = '';
	

		if (dm_js_iframewidth == null)
			dm_js_iframewidth = '100%';
	

	
		var dm_jsiframe = document.createElement ('IFRAME');
		dm_jsiframe.frameBorder = 0;
			
		try {
			var toplocation = top.window.location.toString();
		} catch (e) {
			var toplocation = false;
		}
		dm_jsargs =  dm_jsargs.replace(/^&/,'?');
		

		if (toplocation) 
			dm_jsiframe.setAttribute ('src', dm_jsurl + dm_path + dm_jsargs +  '&siteurl=' +  escape(toplocation.replace (/\#.*$/,'')));
		else
			dm_jsiframe.setAttribute ('src', dm_jsurl + dm_path +  dm_jsargs);

		dm_jsiframe.setAttribute ('width', dm_js_iframewidth);
		dm_jsiframe.setAttribute ('height', '100%');
		dm_jsiframe.style.overflow = 'hidden';

		switch (dm_js_popup) {
		case "popup" :
		
			create_popup (dm_jsiframe, dm_js_iframewidth);
			break;
			
		case "click":
			var dm_textobj = create_text (dm_js_popupclick, dm_js_callback);
			dm_js.parentNode.insertBefore (dm_textobj, dm_js);
			break;
		
		default:	
			dm_js.parentNode.insertBefore (dm_jsiframe, dm_js);
		}
		
		var autoscroll =  dm_js.getAttribute ('autoscroll');
		autoscroll = autoscroll=='no'?autoscroll:'yes';
		
		if (autoscroll=='no')
			return;
			
		var block = false;
		var oldh = dm_jsiframe.style.offsetHeight;
		
		var intervalId = window.setInterval ( function () {

			try {
				if (!block) {
					var h = window.location.hash.replace(/^\#/,'');

					if (oldh!=h && h!='') {
					
						(function () {
							var orgHeight = dm_jsiframe.style.offsetHeight;
							block = true;
							
							var interval2 = window.setInterval ( function () {
								
								if ( dm_jsiframe.offsetHeight == h) {
									block = false;
									window.clearInterval (interval2);
									
								} else {
									
									if (dm_jsiframe.offsetHeight < h) {
										
										dm_jsiframe.style.height =  (dm_jsiframe.offsetHeight + 30) + "px"
										if (dm_jsiframe.offsetHeight > h)
											dm_jsiframe.style.height = h +'px'
									 } else {
									 	
										dm_jsiframe.style.height =  (dm_jsiframe.offsetHeight - 30) + "px"
										if (dm_jsiframe.offsetHeight < h)
											dm_jsiframe.style.height = h +'px'
									}
								}
								
							}, 2)
	
						})();
					}
					oldh = h;
				}
			} catch (err) {
				window.clearInterval (intervalId);
			}
		
		},200)
	}
	
	// create a popup for the dm_ app
	function create_popup (dm_jsiframe, dm_js_iframewidth, closebutton) {
		
		if (container) {
			container.style.display = 'block'
			return;
		}
		
		container = document.createElement('DIV');
		container.style.position = 'absolute';
		//container.style.width = (parseInt(dm_js_iframewidth))+ 'px';
		container.style.width = ((parseInt(dm_js_iframewidth) + 20))+ 'px';
			
		container.style.overflow = 'auto';
		container.style.border = '10px '+ dm_js_popupborder + ' solid';
		container.style.backgroundColor = 'white';
		
		if (closebutton) {
			var closeb = document.createElement('DIV');
			container.appendChild (closeb);
			closeb.innerHTML = "close";
			closeb.style.cursor = 'pointer';
			closeb.style.fontFamily = 'arial , helvetica';
			closeb.style.textAlign = 'right';
			closeb.style.backgroundColor = dm_js_popupborder;
			closeb.style.marginTop = "-5px"
			closeb.onclick = function () {
				container.style.display='none';
			}
		}
		document.body.appendChild (container);
		container.style.top= "100px";
		var xy = getBrowserSize ();
		var cleft = (xy.x/2) - (parseInt(dm_js_iframewidth) /2);
				
		container.style.left= (cleft < 0? 0: cleft ) + "px";
		dm_jsiframe.style.position = 'relative';
		dm_jsiframe.style.top = "0px";
		dm_jsiframe.style.left = "0px";
		container.appendChild (dm_jsiframe);
	}
	
	// create a text link to popup the dm_ app
	function create_text (dm_js_popupclick, dm_js_callback) {
	    var span = document.createElement('SPAN');
        span.innerHTML =  dm_js_popupclick;
		span.style.cursor = 'pointer';
        span.onclick = function () {
			//if we have a (valid) callback, execute it
			if(typeof top.window[dm_js_callback] == 'function') { 
				top.window[dm_js_callback]();
			} 
			
			//open the dm_ app
			create_popup (dm_jsiframe, dm_js_iframewidth, true);
		};
        return span ;

	}
	
	// common browser size function
	function getBrowserSize () {
		var x,y;
		if (self.innerHeight) {// all except Explorer
			x = self.innerWidth;
			y = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			// Explorer 6 Strict Mode
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		} else if (document.body) {// other Explorers
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		return {x:x,y:y};
	}
})();

