function mobi_el(id){return document.getElementById(id);}
function mobi_addEvent(obj, type, fn){if( obj.attachEvent ){obj['e'+type+fn] = fn; obj[type+fn] = function(){obj['e'+type+fn]( window.event );}; obj.attachEvent( 'on'+type, obj[type+fn]);} else{obj.addEventListener(type, fn, false)}}

var mobi_browser = new (function () {
        this.Opera = window.opera ? true : false;
        this.IE = document.all && !this.Opera ? true : false;
        this.IE6 = this.IE && typeof(window.XMLHttpRequest) == "undefined" ? true : false;
        this.IE8 = this.IE && typeof(document.querySelectorAll) != "undefined" ? true : false;
        this.IE7 = this.IE && ! this.IE6 && !this.IE8 ? true : false;
        this.WebKit = /WebKit/i.test(navigator.userAgent) ? true : false, 
        this.iPhone = /iPhone|iPod/i.test(navigator.userAgent)? true : false;
        this.Chrome = /Chrome/i.test(navigator.userAgent) ? true : false;
        this.Safari = /Safari/i.test(navigator.userAgent) && !this.Chrome ? true : false;
        this.Konqueror = navigator.vendor == "KDE" ? true : false;
        this.Konqueror4 = this.Konqueror && /native code/.test(document.getElementsByClassName) ? true : false;
        this.Gecko = !this.WebKit && navigator.product == "Gecko" ? true : false;
        this.Gecko19 = this.Gecko && Array.reduce ? true : false;
    })();


mobi_Base64 = {
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = mobi_Base64._utf8_encode(input);
		while (i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
		}
		return output;
	},
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < input.length) {
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = mobi_Base64._utf8_decode(output);

		return output;
	},
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}
		return string;
	}

}


mobi_fx = {	
	show: function(o){o.style.display = 'block'},	
	hide: function(o){o.style.display = 'none'},	
	fadeIn: function(l){if(!l) l=1;mobi_fx.setOpacity(0); mobi_fx.show(mobi_modal.overlay); for(i=0;i<=1;i+=(1/50)){setTimeout("mobi_fx.setOpacity("+i*l+")",i*700);} setTimeout("mobi_fx.setOpacity("+l+")", 700); setTimeout("mobi_fx.show(mobi_modal.overlay)", 700);},
	fadeOut: function(l){if(!l) l=1;for(i=0;i<=l;i+=(1/50)){setTimeout("mobi_fx.setOpacity("+(l-i)+")",i*700);}setTimeout("mobi_fx.hide(mobi_modal.overlay)", 700);},
	ease: function(t,b,c,d){var s=1.30158; if((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) +b; return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;},	
	animateTop: function(s,e){for(i=0;i<=1;i+=(1/60)){setTimeout("mobi_fx.setTop("+mobi_fx.ease(i,s,e,1)+")",i*1300);}},
	animateHeight: function(s,e){for(i=0;i<=1;i+=(1/60)){setTimeout("mobi_fx.setHeight("+mobi_fx.ease(i, s, e-s,1)+")", i*500);}setTimeout("mobi_fx.setHeight("+e+")", i*500);},	
	setTop: function(x){mobi_modal.elLayer.style.top = Math.round(x) + 'px'},
	setHeight: function(x){x = Math.round(x); mobi_modal.elBox1.style.height=(x+20)+'px'; mobi_modal.elBox2.style.height=(x)+'px';mobi_modal.elIframe.style.height=(x)+'px';},		
	setOpacity: function(x){var o = mobi_modal.overlay; o.style.opacity=x;o.style.MozOpacity=x;o.style.KhtmlOpacity=x;o.style.filter="alpha(opacity=" + (x * 100) + ");"}
}

mobi_modal = window.mobi_modal ? window.mobi_modal : false;
if(!mobi_modal){
mobi_modal = {
	status: false, optSytle: 'default', overlay: false, periodical: false, elLayer: false, elTable: false, elWindow: false, goHeight: 0, elContent: false, elIframe: false,
	__start: function(){	
		mobi_modal.loadCSS();

		mobi_modal._createOverlay();
		
		mobi_modal._createModal();		
		
		mobi_modal.elContent = mobi_el('mobi_form_content');
		
		mobi_modal.elBox1 = mobi_el('mobi_form_box1');
		
		mobi_modal.elBox2 = mobi_el('mobi_form_box2');
		
		mobi_modal._createIframe();
		var c = mobi_el('mobi_form_close');
		mobi_addEvent(c,'click',function(){mobi_modal.modalOut();});
		this.status=1;
	},

	loadCSS: function()
	{
		var h = document.getElementsByTagName("head")[0];         
		var c = document.createElement('link');			
		c.href = 'http://tools.mobiadvanced.com/_sections/UI_admin/default/__css/ptool_mp_v2/ext_' + mobi_modal.optSytle + '.css';				
		c.type = 'text/css';
		c.rel = 'stylesheet';
		h.appendChild(c);
	},

	_setIframe: function(a){
		var url = 'http://tools.mobiadvanced.com/ptool_mp/v2/?iframe_load=true';
		url = url + '&id_psms_cmp='+ a.id_psms_cmp;
		if(a.id_country) url = url + '&id_country='+ a.id_country;
		url = url + '&mp_form_hash=' + a.mp_form_hash;
		url = url + '&mp_form_extra_args=' + mobi_modal.encode(a.mp_form_extra_args);		
		url = url + '&mp_form_offer_defined=' + mobi_modal.encode(a.mp_form_offer_defined);
		url = url + '&mp_form_url_css=' + mobi_modal.encode(a.mp_form_url_css);
		url = url + '&iframe_back=' + mobi_modal.encode(top.location);
		
		var useFrameSet = false;
						
		if (top.location != location) {
		   useFrameSet = true;
		}		

		if(useFrameSet && mobi_browser.WebKit){
			url = url + '&__autonoresize=no';
			mobi_modal.setHeight(450);
		}
		
		//url = url + '&iframe_back=' + mobi_modal.encode(window.location);
		mobi_modal.elIframe.src = url;	
		mobi_fx.show(mobi_modal.elIframe);		
	},

	_createIframe: function(a){
		mobi_modal.elIframe = document.createElement('iframe');
		mobi_modal.elIframe.scrolling='no';
		mobi_fx.hide(mobi_modal.elIframe);
		mobi_modal.elContent.appendChild(mobi_modal.elIframe);			
	},
	
	_createOverlay: function(){
		var b = document.getElementsByTagName("body")[0];         	
		mobi_modal.overlay = document.createElement('div');
		mobi_modal.overlay.className = 'ma_overlay';
		mobi_fx.setOpacity(mobi_modal.overlay,0);
		mobi_fx.hide(mobi_modal.overlay);
		b.appendChild(mobi_modal.overlay);
	},

	_createModal: function(){
		
		var b = document.getElementsByTagName("body")[0];
				
		var modelPattern = '<div id="mobi_form_box1" class="ma_bx1"><div class="ma_a1">&nbsp;</div><div class="ma_a3">&nbsp;</div><div class="ma_a7">&nbsp;</div><div class="ma_a9">&nbsp;</div><div class="ma_top"><div id="mobi_form_close" class="ma_close">&nbsp;</div><div class="ma_a2">&nbsp;</div></div><div class="ma_bot"><div class="ma_a8">&nbsp;</div></div><div class="ma_clear"></div><div id="mobi_form_box2" class="ma_bx2"><div class="ma_a4"></div><div class="ma_a6"></div><div id="mobi_form_content" class="ma_a5"></div></div></div>';		
		mobi_modal.elLayer = document.createElement('div');
		mobi_modal.elLayer.id = 'mobi_general_layer';
		mobi_modal.elLayer.className = 'ma_modal';
		mobi_modal.elLayer.innerHTML = modelPattern;		
		b.appendChild(mobi_modal.elLayer);				
	},

	setTrigger: function(obj){var t = mobi_el(obj.id);mobi_addEvent(t,'click',function(){mobi_modal.modalIn();mobi_modal._setIframe(obj);});},
	_perOff: function(){window.clearInterval(mobi_modal.periodical);},
	_perOn: function(){mobi_modal.periodical = window.setInterval(mobi_modal._hashToHeight, 1000);},
	_hashToHeight: function()
	{
		var s = top.location.hash.split('MAFY:'); 
		if(s < 1) return;
		mobi_modal.setHeight(s[1]);
		if(s[2])
		{
			window.top.location = s[0] + 'MAFY:' + s[1];
			eval(mobi_Base64.decode(s[2]));
		}
	},

	encode: function(s){s=escape(s); s=s.replace(new RegExp('\\+','g'),'%2B'); return s.replace(new RegExp('%20','g'),'+');},	
	setHeight: function(y){if(mobi_modal.goHeight == y || y == undefined) return;mobi_fx.animateHeight(mobi_modal.elBox2.clientHeight, y); mobi_modal.goHeight = y;},
	modalIn: function(){scroll(0,0); mobi_fx.animateTop(-1500, 1600); mobi_modal.overlayIn(); mobi_modal._perOn();},
	modalOut: function(){mobi_fx.animateTop(100, -2000); mobi_modal.overlayOut(); mobi_modal._perOff();},	
	overlayIn: function(){mobi_fx.fadeIn(.4)},
	overlayOut: function(){mobi_fx.fadeOut(.4)}	
}



mobi_modal.__start();}

