
// preload image

function WEMB(c_)
{
	function __getIFrameDocument(aID){
	 var rv = null; 

	 // if contentDocument exists, W3C compliant (Mozilla) 
	 if (document.getElementById(aID).contentDocument){ 
	   rv = document.getElementById(aID).contentDocument; 
	 } else { 
	   // IE 
	   rv = document.frames[aID].document; 
	 } 

	 return rv; 
	  } 
	
	
	var __i1 = new Image(100,25);
	__i1.src = c_.server+'theme/ajax-loader.gif'	
	var config = {};
	var __i12 = new Image(100,25);
	__i12.src = c_.server+'theme/embedbk.png'	
	var config = {};
	var __i13 = new Image(100,25);
	__i13.src = c_.server+'theme/promo-background.png'	
	var config = {};
	
	config.background = {};
	config.background.image = false;
	config.background.opacity = 80;
	config.background.color = "#000000";

	config.widget = {};
	config.widget.image = c_.widget_background;
	config.widget.color = false;

	config.widget_url = c_.widget_file;
	config.hide_right_scrollbar = false;
	config.widget_w = 690;
	config.widget_h = 490;

	this.onhide = function() {return true;};


	var icompl_iframe_opened = false;

	// utility classes
	function utilities()
	{
		this.windowContentSize = function()
		{
			var n = document.body;
			var w;
			var h;
			w = n.scrollWidth;
			h = n.scrollHeight;
			var wnd = this.windowSize();
			return {w:Math.max(wnd.w, w), h:Math.max(wnd.h, h)};
		}
		
		this.windowSize = function()
		{
			var ww=0;
			var wh=0;
			if( typeof( window.innerWidth ) == 'number' )
			{
				ww = window.innerWidth;
				wh = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
			{
			    ww = document.documentElement.clientWidth;
			    wh = document.documentElement.clientHeight;
			}
			return {w:ww, h:wh};
		}
		
		this.windowScrollPos = function()
		{
			var scrollx = 0;
			var scrolly = 0;
			if( typeof( window.pageYOffset ) == 'number' )
			{
				//Netscape compliant
				scrollx = window.pageXOffset;
				scrolly = window.pageYOffset;
			}	
			else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
			{
				//DOM compliant
				scrolly = document.body.scrollTop;
				scrollx = document.body.scrollLeft;
			} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
			{
				//IE6 standards compliant mode
				scrolly = document.documentElement.scrollTop;
				scrollx = document.documentElement.scrollLeft;
			}

			return {sx:scrollx, sy:scrolly};
		}
		
		// used to center a box on the screen (for popups)
		this.centerScreen = function(w, h)
		{
			var w = Math.max(0, (screen.width - w) / 2);
			var h = Math.max(0, (screen.height - h) / 2);
			if (window.opera)
				h=10;
			return {x:w, y:h};
		}
		
		// cross browser compatible set opacity
		this.setOpacity = function(node, percentOpaque)
		{
			if (percentOpaque == 100)
			{
				node.style.opacity = '';
				node.style.filter = ''; 
				return;
			}
			
			var _vopac = percentOpaque/100;
			if (_vopac < 0.01)
			{
				_vopac = '0';
			}
			else
			{
				if (_vopac < 0.1)
					_vopac = '0.0'+percentOpaque;
			}
			
			node.style.opacity=_vopac;
			node.style.filter = "alpha(opacity=" + percentOpaque + ")"; 
		}	

		this.prepareBackground = function(node, config)
		{
			// background
			node.style.backgroundRepeat = "repeat";
			node.style.backgroundColor = config.color!==false ? config.color : '';
			node.style.backgroundImage = config.image!==false ? "url('"+config.image+"')" : '';
			if (config.opacity!==false)
			{
				utils.setOpacity(node, config.opacity);
			}	
		}
	}
	function anim(maxDuration, options)
	{
		this.getOption = function(name, def)
		{
			def = typeof(def) != 'undefined' ? def : false;
			if (typeof(options)=='undefined')
				return def;
			return typeof(options[name])!='undefined' ? options[name] : def;
		}

		this.options = typeof(options)!='undefined' ? options : {};
		this.options.maxDuration = maxDuration;
		this.options.animspeed = this.getOption('animspeed', 3);	
		
		this.step = 0;
		this.animating = false;
		
		this.animCallback = function(step) {};	
		this.finishedCallback = function(forward) {};
		
		this.forward = function()
		{
			if (this.animating == false)
			{
				this.step = Math.max(this.step, 0);
				this.animating = 1;	
				this.animate();
			}
			else
				this.animating = 1;
		}
		
		this.backward = function()
		{
			if (this.animating == false)
			{		
				this.step = Math.min(this.step, this.options.maxDuration);
				this.animating = -1;	
				this.animate();
			}
			else
				this.animating = -1;
		}
		
		this.lastStep = -1;
		this.animate = function()
		{	
			//if ( this.options.animreverse )
			
			if (this.animating == 1)
				var _val = Math.max(1,(this.options.maxDuration-this.step)/this.options.animspeed);
			else
				var _val = Math.max(1,(this.step/this.options.animspeed))
		
			if (this.animating == 1)
				this.step += _val;
			else
				this.step -= _val;	
		
			var cbv = Math.ceil(this.step);
			cbv = Math.max(0, cbv);
			cbv = Math.min(this.options.maxDuration, cbv);				
			
			if (this.lastStep != cbv)
			{
				this.lastStep = cbv;
				this.animCallback(cbv);
			}
		
			if (this.step > this.options.maxDuration || this.step<0)
			{
				this.animating = false;	
				this.finishedCallback(this.step > this.options.maxDuration);
			}
			else
			{
				var that = this;
				window.setTimeout(function() {that.animate();}, 50);
			}	
		}
	}
	var utils = new utilities();
	var animator = new anim(100, {animspeed:2, animreverse:true});

	this.embed = function()
	{
		// container DIV
		document.write("<div id='icomplhdframe8261'></div><div id='icomplhdframe8262'><div id='siprogress'>&nbsp;</div></div>");
		var cnt = document.getElementById('siprogress');
		cnt.style.background="url('signup/theme/ajax-loader.gif')";
		cnt.style.display = 'none';
		cnt.style.width = '150px';
		cnt.style.height = '170px';
		cnt.style.left = Math.ceil(config.widget_w/2 - 150/2) + 'px';
		cnt.style.top =  Math.ceil(config.widget_h/2 - 170/2) + 'px';
		cnt.style.position = 'absolute';
		cnt.style.zIndex = '59999';
		
		cnt = document.getElementById('icomplhdframe8261');
		cnt.className = 'icomplhdframe8261';
		cnt.style.display = 'none';
		cnt.style.zIndex = 49999;

		utils.prepareBackground(cnt, config.background);
		
		// position
		cnt.style.position = 'fixed';
		cnt.style.left = '0px';
		cnt.style.top = '0px';
		cnt.style.width = '100%';
		cnt.style.height = '100%';


		// widget container
		
		var cnt = document.getElementById('icomplhdframe8262');
		cnt.style.display = 'none';
		cnt.style.zIndex = 50000;
		cnt.style.position = 'fixed';
		cnt.style.overflow = 'hidden';
		
		var wnds = utils.windowSize();
		var lastw = false;
		var lasth = false;
		function cls_(cnt)
		{
			var wnds = utils.windowSize();

			if (lastw != wnds.w || lasth != wnds.h)
			{
				if (Math.abs(wnds.w - lastw)>2 && lastw !== false)
					wnds.w = ((wnds.w + lastw) / 2);
				if (Math.abs(wnds.h - lasth)>2 && lasth !== false)
					wnds.h = ((wnds.h + lasth) / 2);

				lastw = wnds.w;
				lasth = wnds.h;
				cnt.style.left = (wnds.w /2 - config.widget_w/2) + 'px';
				cnt.style.top =  (wnds.h /2 - config.widget_h/2) + 'px';
				cnt.style.width = config.widget_w+'px';
				cnt.style.height = config.widget_h+'px';
			}

			window.setTimeout(function() {cls_(cnt)}, 50);
		}
		cls_(cnt);
		utils.prepareBackground(cnt, config.widget);
	}

	this.show = function(package_num)
	{
		config.package_num = package_num;
		
		var cnt = document.getElementById('icomplhdframe8261');
		animator.forward();
		cnt.style.display = 'block';
		
		document.getElementById('siprogress').style.display='block';
	}

	this.hide = function()
	{
		if (!this.onhide())
			return false;
		
		try
		{
			var rem = document.getElementById('icomplhdframe8263');
			rem.parentNode.removeChild(rem);
		}
		catch (e)
		{}
		
		animator.backward();
		return true;
	}

	animator.animCallback = function(cbv)
	{
		var cnt = document.getElementById('icomplhdframe8262');
		cnt.style.display = 'block';
		utils.setOpacity(cnt, cbv);

	}

	animator.finishedCallback = function(showing)
	{
		if (showing)
		{
			var cnt = document.getElementById('icomplhdframe8262');
			var ifr = document.createElement('iframe');
			ifr.setAttribute('id', 'icomplhdframe8263');
			ifr.setAttribute('frameBorder', 0);
			ifr.frameBorder = 0;
			ifr.setAttribute('src', config.widget_url+(typeof(config.package_num)!=='undefined' ? "#p"+config.package_num : ""));
			ifr.style.border='none';
			ifr.style.width = '660px';
			ifr.style.height = '460px';
			ifr.style.marginTop = '25px';
			ifr.style.marginLeft = '20px';
			cnt.appendChild(ifr);
			// we can access parent iframe as ifr.contentDocument

			// several methods to hide the progress image follow
			function _check()
			{
				try
				{
					var _fr = __getIFrameDocument('icomplhdframe8263');
					var t = _fr.getElementById('loadfinished');
					if (t!=null)
					{
						document.getElementById('siprogress').style.display = 'none';
						return;
					}
				}
				catch (e)
				{
					return;
				}
				setTimeout(function () {_check();}, 500);
			}
			_check();
			
			// last resort - remove after 5secs
			setTimeout(function () {document.getElementById('siprogress').style.display = 'none';}, 5000);
			
		}
		else
		{
			document.getElementById("icomplhdframe8261").style.display = "none";
			document.getElementById("icomplhdframe8262").style.display = "none";
		}
	}



}



