function getTop(_this){
	var y = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);
	var top = ((y - _this.height)/2);
	return top;
}

function getLeft(_this){
	var x =  window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
	var left = ((x - _this.width)/2);
	return left;
}

function getWidth(){
	return 400;
}

function getHeight(){
	return 310;
}

if (!ArcticElvis) {
  var ArcticElvis = {};
}

ArcticElvis.Widget = {
  height: 310,
  width:  400,

  close: function(){
      widget = document.getElementById("arcticelvis_widget_box");
      widget.style.display = 'none';
      widget = document.getElementById("arcticelvis_widget_background");
      widget.style.display = 'none';
  },


	
  getCenterOfScreen: function(){
        //var de = document.documentElement;
        var x =  window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
				//var x = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth || document.documentElement.clientWidth || 1024;
       
				var y = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);
				
				//var y = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight || document.documentElement.clientHeight || 1024;
				
				//var _docHeight = document.documentElement.scrollHeight;
				//var tot_x,tot_y;
				var tot_x = window.innerWidth && window.scrollMaxX ? window.innerWidth + window.scrollMaxX :
				                        (document.body.scrollWidth > document.body.offsetWidth ?
				                            document.body.scrollWidth : document.body.offsetWidth);
				
				var tot_y = window.innerHeight && window.scrollMaxY ? window.innerHeight + window.scrollMaxY :
				                        (document.body.scrollHeight > document.body.offsetHeight ?
				                            document.body.scrollHeight : document.body.offsetHeight);
				 
			

        var width = 'auto';
        var height = 'auto';
        var left = ((x - this.width)/2);
        var top = ((y - this.height)/2);

        tot_y = tot_y;
        return{left: left, top: top, width: width, height: height, total_height: tot_y};
  },

  css: function(){ 
			return "#arcticelvis_widget_background {\
      position: absolute;\
      z-index:100009;\
      width: 100%;\
      left: 0;\
      top: 0;\
      background-color: #000;\
      opacity: .7;\
    	filter: alpha(opacity=70);\
    }\
    \
		#arcticelvis_iframe{\
				height: " + getHeight() + "px;\
				width: " + getWidth() + "px;\
				background: #333;\
				display:none;\
				border:none;\
		}\
		\
    #arcticelvis_widget_box {\
       position: fixed;\
       z-index:100011;\
			 padding:20px;\
			 top:"+ getTop(this)+ "px;\
			 left:" + getLeft(this) + "px;\
			 height: " + getHeight() + "px;\
			 width: " + getWidth() + "px;\
    }\
    \
		#arcticelvis_loader{\
			background: url("+subscribe_widget_options.host+"/images/loadingAnimation.gif) no-repeat;\
			width:300px;\
			height:20px;\
			top:"+ getTop(this)+ "px;\
			left:" + (getLeft(this) / 5) + "px;\
			position:absolute;\
		}\
		#arcticelvis_closer{\
			z-index: 100050;\
			position:absolute;\
			right:30px;\
			top:35px;\
			font-size:20px;\
			font-weight:bold;\
			color:#fff;\
			padding:10px;\
			cursor:pointer;\
			display:none;\
			background: url("+subscribe_widget_options.host+"/images/widget/close.png) no-repeat;\
		}"},
    
  
  button_css: "#arcticelvis_widget_button {\
      position: fixed;\
      top: 40%;\
      width: 100px;\
      "+subscribe_widget_options.side+": 0;\
      background-color: "+subscribe_widget_options.bgcolor+";\
      opacity: .7;\
    	filter: alpha(opacity=70);\
			padding:8px;\
    }\
    #arcticelvis_widget_button a{\
      color: "+subscribe_widget_options.fgcolor+";\
      font-size: 16px;\
      font-weight: bold;\
    }\
		#arcticelvis_widget_button span{\
      color: #FFF;\
      font-size: 8px;\
			font-weight:normal;\
    }",
 
  open: function(){
	
      var pos = ArcticElvis.Widget.getCenterOfScreen();
      //background blanket
      var old_widget_background = document.getElementById("arcticelvis_widget_background");
      if (!old_widget_background) {
        var widget_background = document.createElement('div');

        widget_background.innerHTML = "<div id='arcticelvis_widget_background'  style='height:"+pos.total_height+"px;' onclick='ArcticElvis.Widget.close(); return false;' ></div>";
				//widget.style.display = 'block';
        document.body.appendChild(widget_background.firstChild); 
      }
      else{
      old_widget_background.style.display="block";
      };
 
      
      
      //center screen box
      //see if allready loaded or not
      var old_widget_box = document.getElementById("arcticelvis_widget_box"); 
      if (!old_widget_box) {
				
        var box = document.createElement('div');
        		box.id="arcticelvis_widget_box"
         		document.body.appendChild(box); 

        var loading = document.createElement('div');        
        		loading.id  = "arcticelvis_loader";
						box.appendChild(loading); 
				
				//closing button
				var closer = document.createElement('div');
						closer.id = 'arcticelvis_closer';
						closer.onclick =function(){
						ArcticElvis.Widget.close();
				}
						box.appendChild(closer);
 				 var box_div = document.getElementById('arcticelvis_widget_box');
				 var host = subscribe_widget_options.host
				 var path = "/subscribers/new/";
				 var key  = subscribe_widget_options.API_group_key;
				 var id   = subscribe_widget_options.group_id;
				 var locale = subscribe_widget_options.lang;
	       var iframe = document.createElement("iframe");
	        	 iframe.id = "arcticelvis_iframe";
	        	 iframe.name = iframe.id; //seems firefox needs name to index
	           iframe.src = host+path+key+'?group_id='+id+'&locale='+locale;
	           iframe.frameborder="0"; 
	           iframe.scrolling="no"; 
	           iframe.allowtransparency="true";
						 if (iframe.attachEvent){
									iframe.attachEvent("onload", function(){
						 						document.getElementById('arcticelvis_loader').style.display = 'none';
						 						document.getElementById('arcticelvis_closer').style.display = 'block';
						 						iframe.style.display = 'block';
						 			});
						}else{
									iframe.onload =function(){
												document.getElementById('arcticelvis_loader').style.display = 'none';
					 							document.getElementById('arcticelvis_closer').style.display = 'block';
					 							iframe.style.display = 'block';
									}
						}
	       //get data from arcticelvis and put in on the site
	       box_div.appendChild(iframe);
      }
      else{
        //reload before show
        f = document.getElementById('arcticelvis_iframe');
        f.src = f.src;
        
        //show
        old_widget_box.style.display="block";
        
      };
      
			//if(box)
      //	document.body.appendChild(box);

       
  }//end open
  
}

/*writing the css to the page*/

	document.write("\n<style type=\"text/css\">" + ArcticElvis.Widget.css() + "</style>");
if(subscribe_widget_options.show_tab){
	document.write("\n<style type=\"text/css\">" + ArcticElvis.Widget.button_css + "</style>");
	document.write("<div id='arcticelvis_widget_button'><a href='#' onclick='ArcticElvis.Widget.open();return false;'><span>"+subscribe_widget_options.small_text+"</span><br/>"+subscribe_widget_options.large_text+"</a></div>");
}