﻿var element=document.createElement('script');
document.getElementById('123direct').appendChild(element);
element.charset = 'utf-8';
element.src='http://www.123service.ru/123directservice/service.ashx?client_id=' + direct123_client + "&size=" + direct123_size + "&format=" + direct123_format + "&c=callback_func";

var styleBanner;
var contentHeightBanner, contentWidthBanner;
var Direct123={};

Direct123.box = function() {
	var divBox, box, mask, close, urlBox, heightclose = 16,  flashArray = [];
	return {
		show: function(url) {
			this.changeFlash();
			urlBox = url;
			divBox = document.createElement('div');
			divBox.id = 'direct123box';
			box = document.createElement('iframe');
			box.id = 'direct123boxiframe';
			box.setAttribute("frameBorder", "0");
			box.setAttribute("allowtransparency", "true");
			box.setAttribute("scrolling", "no");
			box.src = url;
			mask = document.createElement('div');
			mask.id = 'direct123mask';
			close = document.createElement('div');
			close.id = 'direct123close';
			close.innerHTML = '<img src="http://www.123service.ru/123directservice/img/b-close.gif" alt="Закрыть" onclick="Direct123.box.hide()">';
			this.setStyle();
			document.body.appendChild(mask);
			document.body.appendChild(close);
			document.body.appendChild(divBox);
			this.position();
			this.mask();
			divBox.appendChild(box);
			if(box.addEventListener)
				box.addEventListener("load", Direct123.box.hidePreload, false);
			else if(box.attachEvent)
				box.attachEvent("onload", Direct123.box.hidePreload);
			else
				box.onload = Direct123.box.hidePreload;

			if(mask.addEventListener)
				mask.addEventListener("click", Direct123.box.hide, false);
			else if(mask.attachEvent)
				mask.attachEvent("onclick", Direct123.box.hide);
			else
				mask.onclick = Direct123.box.hide;
			window.onresize = Direct123.box.resize;
		},
		hide: function() {
			divBox.parentNode.removeChild(divBox);
			close.parentNode.removeChild(close);
			Direct123.box.restoreFlash();
			mask.parentNode.removeChild(mask);
		},
		hidePreload: function() {
			with(box.style) {
				opacity = '1';
				mozOpacity = '1';
				khtmlOpacity = '1';
				filter = 'alpha(opacity=100)';
			}
		},
		resize: function() {
			Direct123.box.position();
			Direct123.box.mask();
		},
		mask: function() {
			var heightMin = parseInt(divBox.style.height) + heightclose + parseInt(close.style.top);
			var heightMax = Direct123.page.theight();
			var widthMin = parseInt(divBox.style.width);
			var widthMax = Direct123.page.twidth();
			var height = Direct123.page.height();
			height = heightMax > height ? heightMax : (height > heightMin ? height : heightMin);
			var width = Direct123.page.width();
			width = widthMax > width ? widthMax : (width > widthMin ? width : widthMin);
			mask.style.height = height + 'px';
			mask.style.width = width + 'px';
		},
		position: function() {
			scrollTop = document.body.scrollTop;
			if (scrollTop == 0)
			{
				if (window.pageYOffset)
					scrollTop = window.pageYOffset;
				else
					scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			}
			scrollLeft = document.body.scrollLeft
			if (scrollLeft == 0)
			{
				if (window.pageXOffset)
					scrollLeft = window.pageXOffset;
				else
					scrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
			}
			var top = (Direct123.page.height() / 2) - (divBox.offsetHeight / 2) + scrollTop - heightclose;
			top = top < 10 ? 10 : top;
			var left = (Direct123.page.width() / 2) - (divBox.offsetWidth / 2) + scrollLeft;
			left = left > 0 ? left : 0;
			close.style.top = top + 'px';
			close.style.left = left + 'px';
			divBox.style.top = (top + heightclose) + 'px';
			divBox.style.left = left + 'px';
		},
		setStyle: function() {
			with (divBox.style) {
				position = 'absolute';
				display = 'block';
				width = '720px';
				height = '510px';
				border = '0 none';
				zIndex = '20000';
				opacity = '1';
				padding = '0';
				margin = '0';
				overflow = 'hidden';
				background = 'white url(http://www.123service.ru/123directservice/img/preload.gif) no-repeat 50% 50%';
			}
			with(box.style) {
				width = '100%';
				height = '100%';
				display = 'block';
				border = '0 none';
				zIndex = '20000';
				padding = '0';
				margin = '0';
				overflow = 'hidden';
				opacity = '0.1';
				mozOpacity = '0.1';
				khtmlOpacity = '0.1';
				filter = 'alpha(opacity=10)';
			}
			with (mask.style) {
				position = 'absolute';
				display = 'block';
				width = '100%';
				height = '100%';
				top = '0';
				left = '0';
				background = '#000';
				zIndex = '15000';
				opacity = '0.7';
				mozOpacity = '0.7';
				khtmlOpacity = '0.7';
				filter = 'alpha(opacity=70)';
				position = 'absolute';
			}
			with (close.style) {
				position = 'absolute';
				display = 'block';
				width = '720px';
				height = heightclose + 'px';
				background = 'gray';
				border = '0 none';
				zIndex = '20000';
				opacity = '1';
				padding = '0';
				margin = '0';
				overflow = 'hidden';
				fontSize = '13px';
				fontFamily = 'Arial, Sans-Serif';
				textAlign = 'right';
			}
		},
		changeFlash: function() {
			flashArray = [];
			objsFlash = document.getElementsByTagName('object');
			objectFlashMark: for (var i = 0; i < objsFlash.length; i++) {
				var objFlash = new Object();
				objFlash.element = objsFlash[i];
				objFlash.valueVisibility = objFlash.element.style.visibility;
				objFlash.element.style.visibility = 'hidden';
				flashArray.push(objFlash);
			}
			objsFlash = document.getElementsByTagName('embed');
			objectFlashMark: for (var i = 0; i < objsFlash.length; i++) {
				var objFlash = new Object();
				objFlash.element = objsFlash[i];
				objFlash.valueVisibility = objFlash.element.style.visibility;
				objFlash.element.style.visibility = 'hidden';
				flashArray.push(objFlash);
			}
		},
		restoreFlash: function() {
			for(var i = 0; i < flashArray.length; i++) {
				insertFlash = flashArray[i].element;
				insertFlash.style.visibility = flashArray[i].valueVisibility;
			}
		}
	}
	} ();

Direct123.page=function(){
	return{
		width:function(){
			width = 0;
			if(document.body.clientWidth)
				width = document.body.clientWidth;
			if(document.documentElement.clientWidth)
				width = document.documentElement.clientWidth;
			if (self.innerWidth)
				width = self.innerWidth;
			return width;
		},
		height:function(){
			height = 0;
			if(document.body.clientHeight)
				height = document.body.clientHeight;
			if(document.documentElement.clientHeight)
				height = document.documentElement.clientHeight;
			if (self.innerHeight)
				height = self.innerHeight;
			return height;
		},
		theight:function(){
			var d=document, b=d.body, e=d.documentElement;
			return Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight))
		},
		twidth:function(){
			var d=document, b=d.body, e=d.documentElement;
			return Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();

function callback_func(text) {
	var heightBanner, widthBanner;
	if(direct123_size == '240x400') {
		widthBanner = 240;
		heightBanner = 400;
		styleBanner = 'vertical';
	} else if(direct123_size == '240x300'){
		widthBanner = 240;
		heightBanner = 300;
		styleBanner = 'vertical';
	} else {
		widthBanner = 720;
		heightBanner = 90;
		styleBanner = 'horizontal';
	}
	var xml;
	var rowtag = "text_ad";
	if(window.DOMParser) {
		parser = new DOMParser();
		xml = parser.parseFromString(text, "text/xml");
	} else {
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.loadXML(text);
	}
	var direct123 = document.getElementById('123direct');
	var divBanner = document.createElement('div');
	if(styleBanner == 'horizontal') {
	setImportantAttributes(divBanner, "width: " + widthBanner + "px !important; height: " + heightBanner + "px !important;");
	} else {
		contentWidthBanner = widthBanner - 8;
		contentHeightBanner = heightBanner - 8;
		setImportantAttributes(divBanner, "width: " + contentWidthBanner + "px !important; height: " + contentHeightBanner + "px !important; border: 1px double #c9d8e6 !important; padding: 4px !important;");
	}
	var divNotices = document.createElement('div');
	if(styleBanner == 'horizontal') {
		setImportantAttributes(divNotices, "");
	} else {
		setImportantAttributes(divNotices, "height: " + (contentHeightBanner - 34) + "px !important;");
	}
	var xmlrows = xml.getElementsByTagName(rowtag);
	for (var i=0; i<xmlrows.length; i++) {
		var divNotice = addDivTextAd(xmlrows[i], xmlrows.length);
		divNotices.appendChild(divNotice);
	}
	divBanner.appendChild(divNotices);
	if(styleBanner == 'horizontal') {
	divBanner.innerHTML=divBanner.innerHTML + '<div style="text-align:right !important;"><a href="#" style="font:10px Arial, sans-serif !important;text-decoration: underline !important;color: blue !important;" onclick="window.open(\'http://www.123service.ru\')">123Директ</a></div>';
	} else {
		divBanner.innerHTML = '<div style="text-align:left !important; height: 34px !important;"><a href="#" onclick="window.open(\'http://www.123service.ru\')"><img src="http://www.123service.ru/123directservice/img/title.png" alt="123Директ" style="border: 0 none !important;"/></a></div>' + divBanner.innerHTML;
	}
	direct123.appendChild(divBanner);
}

function addDivTextAd(xmlrow, length) {
	var divNotice = document.createElement('div');
	if(styleBanner == 'horizontal') {
		setImportantAttributes(divNotice, "border: 0 none !important; float: left !important; width: " + 100/length + "% !important;");
		addTextAd(xmlrow, divNotice);
	} else {
		setImportantAttributes(divNotice, "border: 0 none !important; float: none !important; height: " + 100/length + "% !important;");
		var divNoticeMark = document.createElement('div');
		divNoticeMark.innerHTML = '<img src="http://www.123service.ru/123directservice/img/mark.png" alt="123Директ">';
		setImportantAttributes(divNoticeMark, "width: 28px !important; border: 0 none !important; float: left !important;");
		var divNoticeContent = document.createElement('div');
		setImportantAttributes(divNoticeContent, "border: 0 none !important; float: left !important; width: " + (contentWidthBanner - 28) + "px !important;");
		addTextAd(xmlrow, divNoticeContent);
		divNotice.appendChild(divNoticeMark);
		divNotice.appendChild(divNoticeContent);
	}
	return divNotice;
}

function addTextAd(xmlrow, divNotice) {
	var columns;
	if(styleBanner == 'horizontal') {
		columns = ["title", "line1", "line2", "url"];
	} else {
		columns = ["title", "line1", "line2", "url", "display_url"];
	}
	var link = document.createElement('a');
	link.href = "#";
	setImportantAttributes(link, "font-weight: bold !important; text-decoration: underline !important; color: blue !important;");
	var divString = document.createElement('div');
	setImportantAttributes(divString, "");
	var span1 = document.createElement('span');
	setImportantAttributes(span1, "");
	var span2 = document.createElement('span');
	setImportantAttributes(span2, "");
	var displayLink;
	if(styleBanner == 'vertical') {
		displayLink = document.createElement('a');
		displayLink.href = "#";
		setImportantAttributes(displayLink, "color: #006633 !important;");
	}
	for (var j=0; j<columns.length; j++) {
		var sc = columns[j];
		var celltext = xmlrow.getElementsByTagName(sc)[0].firstChild.data;
		switch(sc) {
			case "title":
				link.appendChild(document.createTextNode(celltext));
				break;
			case "url":
				link.setAttribute("onclick","javascript:Direct123.box.show('" + celltext + "');return false;");
				if(styleBanner == 'vertical') {
					displayLink.setAttribute("onclick","javascript:Direct123.box.show('" + celltext + "');return false;");
				}
				break;
			case "line1":
				span1.appendChild(document.createTextNode(celltext));
				break;
			case "line2":
				span2.appendChild(document.createTextNode(celltext));
				break;
			case "display_url":
				displayLink.appendChild(document.createTextNode(celltext));
				break;
		}
	}
	divNotice.appendChild(link);
	divString.appendChild(span1);
	divString.appendChild(span2);
	divNotice.appendChild(divString);
	if(styleBanner == 'vertical') {
		divNotice.appendChild(displayLink);
	}
}

function setImportantAttributes(elem, attr) {
	elem.style.cssText = "background-color: white !important; color: black !important; background-image: none !important; font-family: Arial, sans-serif !important; font-size: 13px !important; font-style: normal !important; font-weight: normal !important; font-variant: normal !important; font-stretch: normal !important; text-indent: 0px !important; text-align: left !important; text-decoration: none !important; text-shadow: none !important; letter-spacing: normal !important; word-spacing: normal !important; text-transform: none !important; line-height: normal !important; white-space: normal !important; margin: 0 !important; padding: 0 !important; border: 0 none !important; position: static !important; visibility: visible !important; opacity: 1 !important; -moz-opacity: 1 !important; -khtml-opacity: 1 !important; filter: alpha(opacity=100) !important;" + attr;
}
