
/*

	Types of mySurveyLab boxes:


		1. DEFAULT -	you dont need to add div (it will be created),
				you dont need to add css
			
			config:
				src (required)


			EXAMPLE:

			<link href="http://mysurveylab.com/inc.css/mysurveylab-box_base.css" rel="stylesheet" type="text/css"/>
			<link href="http://mysurveylab.com/inc.css/mysurveylab-box_en.css" rel="stylesheet" type="text/css"/>
			<script type="text/javascript" src="http://mysurveylab.com/inc.js/mysurveylab-box.js"></script>
			<script type="text/javascript">

				var mysurveyLabBoxConfig = new Array();
	
				mysurveyLabBoxConfig.src = 'http://mysurveylab.com/index.php?cId=44dae8285854e956dac5dea6712a9e30bff3f1d7&pid=133&lng=pl';

				var mysurveyLabBox = new MysurveyLabBox( mysurveyLabBoxConfig );

			</script>
	
	
		2. CUSTOM -	you have full control of element placement and look
			
			config:	
		 		id (required),
				src (required)<head>
				(...)
					
				(...)
				</head>
				


			EXAMPLE:
				
			<link href="http://mysurveylab.com/inc.css/mysurveylab-box_base.css" rel="stylesheet" type="text/css"/>
			<script type="text/javascript" src="http://mysurveylab.com/inc.js/mysurveylab-box.js"></script>
			<script type="text/javascript">

				var mysurveyLabBoxConfig = new Array();
	
				mysurveyLabBoxConfig.src = 'http://mysurveylab.com/index.php?cId=44dae8285854e956dac5dea6712a9e30bff3f1d7&pid=133&lng=pl';
				mysurveyLabBoxConfig.id = 'your-button-id'; // set to id of your html button, link or image
				
				var mysurveyLabBox = new MysurveyLabBox( mysurveyLabBoxConfig );

			</script>
*/


function MysurveyLabBox( config ) {
	
	this.id = 'mysurveyLabBox';
	this.src = 'http://mysurveylab.com';
	this.className = 'mysurveylab_box';
	
	this.dom = '';
	
	
	// methods
	
	this.parseConfig = ParseConfig;
	this.createElement = CreateElement;
	this.setOnClick = SetOnClick;
	
	function ParseConfig( config ) {
	
		for ( i in config ) {
			
			this[ i ] = config[ i ];
		}
	}
	
	function SetOnClick() {
		
		var src = this.src ;
		this.dom.onclick = function(){ MysurveyLabBox.showSurvey( src ); }
	}
	
	function CreateElement() {
		
		this.dom = document.createElement( 'div' );
		
		this.dom.id = this.id;
		this.dom.className = this.className;

		this.setOnClick();
		
		document.body.appendChild( this.dom );
	}
	
	// set attributes from config
	this.parseConfig( config );
	
	if( document.getElementById( this.id )  ) {
			
		this.dom = document.getElementById( this.id );
		this.setOnClick();

		return;
	}
	
	this.createElement();
	
	return;
}

	MysurveyLabBox.showSurvey = function( src ) {
		
		var el;

		if( el = document.getElementById( 'mysurveylab_popup' ) ) {
			
			document.body.removeChild( el );
		}
		
		var popup = document.createElement( 'div' );
		
		popup.id = 'mysurveylab_popup';
		popup.style.display = "none";
		
		var inside ='<div id="transparent_bg"></div>';
		inside +=	'<div id="mysurveylab_popup_content">';
		inside +=		'<div id="mysurveylab_popup_exit" onclick="return MysurveyLabBox.closeSurvey();"></div>';
		inside +=		'<div class="content">';
		inside +=			'<iframe id="mysurveylab_iframe" src="' + src + '"></iframe>';
		inside +=  	'</div></div>';
		
		popup.innerHTML = inside;
		
		document.body.appendChild( popup );
		MysurveyLabBox.showPopup();
	}

	MysurveyLabBox.closeSurvey = function() {

		if( el = document.getElementById( 'mysurveylab_popup' ) ) {
			
			document.body.removeChild( el );
		}
		
		return true;
	}

	MysurveyLabBox.showPopup = function() {
	
		this.show( 'mysurveylab_popup' );
		
		var arrayPageSize = MysurveyLabBox.getPageSize();

		document.getElementById( 'transparent_bg' ).style.height = ( arrayPageSize[1] + 'px');
		document.getElementById( 'mysurveylab_popup_content' ).style.top = MysurveyLabBox.getPageScroll() + 100 + 'px';
		document.getElementById( 'mysurveylab_popup_content' ).style.left =  ( arrayPageSize[0] - MysurveyLabBox.getElementWidth( 'mysurveylab_popup_content' ) )/2 + 'px';		
	}

	MysurveyLabBox.show = function( id ) {
		
		if ( !document.getElementById( id ) ) {
	
			return false;
		}				
		
		document.getElementById( id ).style.display = "";
				
		return;
	}
	
	MysurveyLabBox.hide = function( id ) {
	
		if ( !document.getElementById( id ) ) {
		
			return false;
		}
			
		document.getElementById( id ).style.display = 'none';
		return ;
	}

	MysurveyLabBox.getElementWidth = function( id ) {
		
		var el = document.getElementById( id );
	
		if ( typeof el.clip !== "undefined" ) {
			
			return el.clip.width;
		} else {
			
			if (el.style.pixelWidth) {
				
				return el.style.pixelWidth;
			} else {
				
				return el.offsetWidth;
			}
		}
	}
	
	MysurveyLabBox.getElementHeight = function( id ) {
		
		var el = document.getElementById( id );
		
		if ( typeof el.clip !== "undefined" ) {
			
			return el.clip.height;
		} else {
			
			if (el.style.pixelHeight) {
				
				return el.style.pixelHeight;
			} else {
				
				return el.offsetHeight;
			}
		}
	}
	
	//getPageSize(), getPageScroll()
	//Core code from - quirksmode.org
	//Edit for Firefox by pHaez
	
	MysurveyLabBox.getPageScroll = function(){
		
		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;
		}
	
		return yScroll;
	}
	
	MysurveyLabBox.getPageSize = function(){
		
		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){ //4 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;
	}
