/**
 * $Id: pf_load.js,v 1.4 2010/05/11 07:15:29 evgeniy.batanov Exp $
 * Этот скрипт загружает JQuery, CSS, HMTL-баннера и JS-баннера
 */

var oPfLoad = new function()
{
	// ищем тег SCRIPT чтобы узнать ID партнера
	var js_tag = null;
	var js_tags = document.body.getElementsByTagName('SCRIPT');
	
	this.initialize = function() {
		for (i = 0; i < js_tags.length; i++) {
			if (js_tags[i].src.indexOf('/pf_load.js?') != -1) {
				js_tag = js_tags[i];
			}
		}

		// если тег найден, то можем смело вставлять HTML код перед ним
		if (js_tag) {
			// JQuery
			this.nAddJS(this.sPlayFonUrl + 'js/jquery-ui-1.7.2.c.min.js');
			// Наша CSS'ка для баннера
			this.nAddCSS(this.sPlayFonUrl + 'css/pf_bnr.css');
			// Оформление диалоговых окон
			this.nAddCSS(this.sPlayFonUrl + 'css/ui-lightness/jquery-ui-1.7.2.custom.css');

			// HTML-код баннера

			// базовый элемент в который вставляется баннер
			var _div = this.oElement('DIV', {'id': this.sDemoDivId}, {'display': 'none'});
			js_tag.parentNode.insertBefore(_div, js_tag);
			// загружаем баннер
			var sGetParams = sGET(js_tag.src);
			
			if( sGetParams.length > 0 ){
				sGetParams += '&';
			}
			sGetParams += 'callback='+escape('oPfLoad.nWriteBlock');
			
			this.nAddJS(this.sPlayFonUrl + 'demo_html.php?' + sGetParams);
		}	
	}
		
	// базовый элемент в который вставляется баннер
	this.sDemoDivId = 'pf_demo_bnr';
	// наш хост, откуда всё грузится
	this.sPlayFonUrl = 'http://viewdemo.playfon.ru/';

	this.nWriteBlock = function (aData)
	{
		if( aData && typeof(aData.html) != 'undefined' ){
			this.nInsertHtml(document.getElementById(this.sDemoDivId), aData.html);
			// JS-код баннера
			this.nAddJS(this.sPlayFonUrl + 'js/pf_show_block.js');
		}
	};
	
	this.nInsertHtml = function (oDest, sHtml)
	{
		var regex_all = new RegExp('<script[^>]*>([\u0001-\uFFFF]*?)</script>', 'img');
		var regex_one = new RegExp('<script[^>]*>([\u0001-\uFFFF]*?)</script>', 'im');
		var matches = [];
		var match = '';
	
		if( oDest ){
			matches = sHtml.match(regex_all);
			oDest.innerHTML =  matches ? sHtml.replace(regex_all, '') : sHtml;

			if (matches) {
				for (var i=0 ; i< matches.length ; i++ ) {
					if (matches[i].match(regex_one)[1]) {
						if (window.execScript) {	// IE
							window.execScript(matches[i].match(regex_one)[1]);
						} else {
							window.eval(matches[i].match(regex_one)[1]);
						}
					}
				}
			}
		}
	};
	
	/**
	 * Создаем новый HTML элемент. Все аргументы за исключением name являются необязательными.
	 */
	this.oElement = function (name, attrs, style, text) {
		var e = document.createElement(name);
		if (attrs) {
			for (key in attrs) {
				if (key == 'class') {
					e.className = attrs[key];
				} else if (key == 'id') {
					e.id = attrs[key];
				} else {
					e.setAttribute(key, attrs[key]);
				}
			}
		}
		if (style) {
			for (key in style) {
				e.style[key] = style[key];
			}
		}
		if (text) {
			e.appendChild(document.createTextNode(text));
		}
		return e;
	};
	
	this.nAddJS = function (sUrl)
	{
		var _js = this.oElement(
				'SCRIPT', 
				{
					'charset': 'utf-8',
					'type': 'text/javascript',
					'src':  sUrl
				}
		);
		document.getElementsByTagName('head')[0].appendChild(_js);
	}

	this.nAddCSS = function (sUrl)
	{
		var _css = this.oElement(
				'LINK', 
				{
					'rel': 'stylesheet',
					'type': 'text/css',
					'href':  sUrl
				}
		);
		document.getElementsByTagName('head')[0].appendChild(_css);
	}
	
	
}

oPfLoad.initialize();

function sGET(sUrl)
{
	var sGET = new String(sUrl || window.location);
	var q = sGET.indexOf('?');
	sGET = ( q != -1 ) ? sGET.substr(q+1, sGET.length-q) : '';
	return sGET;
}

function aGET()
{
	var sGET = sGET(); 
	var _GET = {};

	if( sGET.length > 0 ){ 
		var aParams = sGET.split('&');
		for( k in aParams ){ 
			aPar = aParams[k].split('='); 
			_GET[aPar[0]] = aPar[1]; 
		}
	}
	return _GET;
}