function getScrollTop() {
	if (self.pageYOffset)  return self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
	else if (document.body)  return document.body.scrollTop;
}
function getScrollLeft() {
	if (self.pageXOffset) return self.pageXOffset;
	else if (document.documentElement && document.documentElement.scrollLeft) return document.documentElement.scrollLeft;
	else if (document.body) return document.body.scrollLeft;
}
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

function loadOverlay() {
	var myMugzy_objBody = document.getElementsByTagName('body').item(0);
	var myMugzy_objOverlay = document.getElementById('mymugzy_overlay');
	
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	var popHeight;
	var popWidth;
	
	if (fullHeight > myMugzy_objBody.scrollHeight)
		popHeight = fullHeight;
	else
		popHeight = myMugzy_objBody.scrollHeight;
	
	if (fullWidth > myMugzy_objBody.scrollWidth)
		popWidth = fullWidth;
	else
		popWidth = myMugzy_objBody.scrollWidth;

	popWidth -= (navigator.appName != 'Microsoft Internet Explorer') ? 20 : 0;
	myMugzy_objOverlay.style.height = popHeight + 'px';
	myMugzy_objOverlay.style.width = popWidth + 'px';
	myMugzy_objOverlay.style.display = 'block';
}
function login(width,height,url){
	var myMugzy_objBody = document.getElementsByTagName('body').item(0);	
	var myMugzy_objContainer = document.getElementById('vwindow');
	
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	var scTop;
	var scLeft;
	var topPos;
	var leftPos;
	var main_mymugzy_swf;
	
	loadOverlay();

	// Main SWF Container
	if (width == null || isNaN(width))
		width = myMugzy_objContainer.offsetWidth;
	if (height == null) 
		height = myMugzy_objContainer.offsetHeight;
	
	scTop = parseInt(getScrollTop(),10);
	scLeft = parseInt(myMugzy_objBody.scrollLeft,10);
	
	topPos = (scTop + ((fullHeight - height) / 2));
	topPos = (topPos < 0) ? 0 : topPos;
	leftPos = (scLeft + ((fullWidth - width) / 2));
	leftPos = (navigator.appName != 'Microsoft Internet Explorer')? (leftPos - 12) : (leftPos - 4);
	
	// Centering the container in a page
	myMugzy_objContainer.style.top = topPos + 'px';
	myMugzy_objContainer.style.left =  leftPos + 'px';
	myMugzy_objContainer.style.width = width + 'px';
	myMugzy_objContainer.style.height = height + 'px';
	
	//SWF: URL, width and height properties
	var ms = new Date().getTime().toString();
	url += '/swf/main.swf?divid=mymugzy_divMg&'+ms; 
	main_mymugzy_swf = new SWFObject(url, 'mymugzy_swf_container_updates', width, height, '10');
	main_mymugzy_swf.addParam('allowScriptAccess','always');
	main_mymugzy_swf.write(myMugzy_objContainer);
	myMugzy_objContainer.style.display = '';
}

function mymugzy_utf8_encode(argString){var string=(argString+'').replace(/\\r\\n/g,'\\n').replace(/\\r/g,'\\n');var utftext='';var start,end;var stringl=0;start=end=0;stringl=string.length;for(var n=0;n<stringl;n++){var c1=string.charCodeAt(n);var enc=null;if(c1<128){end++}else if((c1>127)&&(c1<2048)){enc=String.fromCharCode((c1>>6)|192)+String.fromCharCode((c1&63)|128)}else{enc=String.fromCharCode((c1>>12)|224)+String.fromCharCode(((c1>>6)&63)|128)+String.fromCharCode((c1&63)|128)}if(enc!==null){if(end>start){utftext+=string.substring(start,end)}utftext+=enc;start=end=n+1}}if(end>start){utftext+=string.substring(start,string.length)}return utftext};
function mymugzy_base64_encode(data){var b64='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';var o1,o2,o3,h1,h2,h3,h4,bits,i=0,ac=0,enc="",tmp_arr=[];if(!data){return data}data=this.mymugzy_utf8_encode(data+'');do{o1=data.charCodeAt(i++);o2=data.charCodeAt(i++);o3=data.charCodeAt(i++);bits=o1<<16|o2<<8|o3;h1=bits>>18&0x3f;h2=bits>>12&0x3f;h3=bits>>6&0x3f;h4=bits&0x3f;tmp_arr[ac++]=b64.charAt(h1)+b64.charAt(h2)+b64.charAt(h3)+b64.charAt(h4)}while(i<data.length);enc=tmp_arr.join('');switch(data.length%3){case 1:enc=enc.slice(0,-2)+'';break;case 2:enc=enc.slice(0,-1)+'';break}return enc};

function mymugzy_javascript_functions(cdn,url_link) {
		var myMugzy_objBody = document.getElementsByTagName('body').item(0);	
		var myMugzy_objContainer = document.getElementById('vwindow');
		var myMugzy_objOverlay = document.getElementById('mymugzy_overlay');
		var main_mymugzy_swf;
		var mymugzy_divElement = "mymugzy_divMg";
		
		this.closeLoadSwfFile = function() {
			myMugzy_objContainer.style.display = 'none';
			myMugzy_objContainer.innerHTML = '';
			myMugzy_objOverlay.style.display = 'none';
		}
		
		// Container for the SWF file(s)
		this.loadSwfFile = function(width,height,url){
			var fullHeight = getViewportHeight();
			var fullWidth = getViewportWidth();
			var scTop;
			var scLeft;
			var topPos;
			var leftPos;
			var main_mymugzy_swf;
			
			loadOverlay();
		
			// Main SWF Container
			if (width == null || isNaN(width))
				width = myMugzy_objContainer.offsetWidth;
			if (height == null) 
				height = myMugzy_objContainer.offsetHeight;
			
			scTop = parseInt(getScrollTop(),10);
			scLeft = parseInt(myMugzy_objBody.scrollLeft,10);
			
			topPos = (scTop + ((fullHeight - height) / 2));
			topPos = (topPos < 0) ? 0 : topPos;
			leftPos = (scLeft + ((fullWidth - width) / 2));
			leftPos = (navigator.appName != 'Microsoft Internet Explorer')? (leftPos - 12) : (leftPos - 4);
			
			// Centering the container in a page
			myMugzy_objContainer.style.top = topPos + 'px';
			myMugzy_objContainer.style.left =  leftPos + 'px';
			myMugzy_objContainer.style.width = width + 'px';
			myMugzy_objContainer.style.height = height + 'px';
			
			//SWF: URL, width and height properties
			var ms = new Date().getTime().toString();
			url += '&'+ms; 
			main_mymugzy_swf = new SWFObject(url, 'mymugzy_swf_container_updates', width, height, '10');
			main_mymugzy_swf.addParam('allowScriptAccess','always');
			main_mymugzy_swf.write(myMugzy_objContainer);
			myMugzy_objContainer.style.display = '';
		}
		
		this.openShoppingPage = function(url,width,height,param){
			var fullHeight = getViewportHeight();
			var fullWidth = getViewportWidth();
			var scTop;
			var scLeft;
			var topPos;
			var leftPos;
			var main_mymugzy_swf;
			
			// Overlay
			loadOverlay();
			
			// Main SWF Container
			if (width == null || isNaN(width))
				width = myMugzy_objContainer.offsetWidth;
			if (height == null) 
				height = myMugzy_objContainer.offsetHeight;
			
			scTop = parseInt(getScrollTop(),10);
			scLeft = parseInt(myMugzy_objBody.scrollLeft,10);
			topPos = (scTop + ((fullHeight - height) / 2));
			topPos = (topPos < 0) ? 0 : topPos;
			leftPos = (scLeft + ((fullWidth - width) / 2));
			leftPos = (navigator.appName != 'Microsoft Internet Explorer')? (leftPos - 12) : (leftPos - 4);

			// Centering the container in a page
			myMugzy_objContainer.innerHTML = '';
			myMugzy_objContainer.style.top = topPos + 'px';
			myMugzy_objContainer.style.left =  leftPos + 'px';
			myMugzy_objContainer.style.width = width + 'px';
			myMugzy_objContainer.style.height = height + 'px';
			myMugzy_objContainer.style.border = '3px solid #CCCCCC';
			
			var myMugzy_objShoppingContainer = document.createElement('iframe');	
			myMugzy_objShoppingContainer.setAttribute('id','myMugzy_objShoppingContainer');
			myMugzy_objShoppingContainer.setAttribute('name','myMugzy_objShoppingContainer');
			myMugzy_objShoppingContainer.setAttribute('width',width);
			myMugzy_objShoppingContainer.setAttribute('height',height);
			myMugzy_objShoppingContainer.setAttribute('frameborder',0);
			myMugzy_objShoppingContainer.setAttribute('scrolling','no');
			myMugzy_objContainer.appendChild(myMugzy_objShoppingContainer);
			myMugzy_objShoppingContainer.style.zIndex = '999999999';
			myMugzy_objContainer.style.display = '';
			document.getElementById('myMugzy_objShoppingContainer').src = url + '#'+mymugzy_base64_encode(url_link)+'|'+param;
			//document.getElementById('myMugzy_objShoppingContainer').src = '".$vwala."/webservices/jscall/shop/verify#".base64_encode($url_link)."';
			
			window.location = url_link+'#vwindow';
		}
		
		this.checkMainUrl = function(){
			hashValue = window.location.hash.substr(1);
			if(hashValue != '') {
				switch(hashValue)
				{
					case 'cshopping':
									closeLoadSwfFile();
									window.location = '#';
									//myMugzy_objContainer.style.top = '10px';
									break;
					case 'mymugzy_login':			
									loadSwfFile(680,520,cdn+'/swf/main.swf?divid='+mymugzy_divElement);
									window.location = url_link+'#';
									break;
					case 'mymugzy_myaccount':			
									loadSwfFile(680,520,cdn+'/swf/main.swf?divid='+mymugzy_divElement+'&redirect=editaccount');
									window.location = url_link+'#';
									break;
				}
				window.status = '';
			}
		}
		
		var checkMainUrl = this.checkMainUrl;
		var closeLoadSwfFile = this.closeLoadSwfFile;
		var loadSwfFile = this.loadSwfFile;
		var openShoppingPage = this.openShoppingPage;
		
		setInterval(checkMainUrl, 200);
}
