var bs = new function() {
	this.host = 'http://bs.spb-komandor.ru';
	this.path = '/show/';
	this.key = null;
	this.dataReq = '&url=' + escape( document.URL );
	this.tagName = 'div';
	this.className = 'banner';
	this.idPrefix = 'banner_';
	
	this.initOnLoad = function() {
		// check for BS key
		if (typeof _komandorBsKey === 'undefined') {
			return false;
		}
		
		// set BS key
		bs.key = _komandorBsKey;
		
		// jQuery way
		if ( window.jQuery ) {
			return jQ();
		}
	
		// plain JS way
		if ( window.addEventListener ) {
			window.addEventListener( 'DOMContentLoaded', pJ, false );
			window.addEventListener( 'load', pJ, false );
		} else if ( window.attachEvent )
			window.attachEvent( 'onload', pJ );
		else {
			window.onload = pJ;
		}
	}
	
	function pJ() {
		if (pJ.called === true) {
			return;
		}
		
		pJ.called = true;

		var banners = document.getElementsByTagName( 'div' );
		for ( i=0; i<banners.length; i++ ) {
			if ( banners[i].className != bs.className ) {
				continue;
			}
			
			var position = banners[i].id.replace( bs.idPrefix, '' );
			if ( position ) {
				url = bs.host + bs.path + position;
				params = 'bskey=' + bs.key + bs.dataReq;

				// функция асинхронна, соответственно position в ней не зависит от цикла
				sendScriptRequest( url, params, function( arg ) {
					actualPos = arg.id.replace( bs.idPrefix, '' );

					response = 'html' + actualPos;
					arg.innerHTML = window[ response ];
				}, [ banners[i] ] );
			}
		};
	}
	
	function sendScriptRequest( url, httpParams, callback, callbackArgsArray ) {
		var currentScript = document.createElement('SCRIPT');
		httpParams = '?' + httpParams;
		
		currentScript.ajax_readyState = false;
		currentScript.onload = scriptCallback(currentScript, callback, callbackArgsArray);
		currentScript.onreadystatechange = scriptCallback(currentScript, callback, callbackArgsArray);
		currentScript.async = 'async';
		currentScript.src = url + httpParams;
		document.getElementsByTagName( 'script' )[0].parentNode.appendChild(currentScript);
	}
	
	function scriptCallback( currentScript, callback, callbackArgsArray ) {
		return function() {
			if ( currentScript.ajax_readyState )
				return;

			if ( !currentScript.readyState || currentScript.readyState == 'loaded' || currentScript.readyState == 'complete' ) {
				currentScript.ajax_readyState = true;
				callback.apply( currentScript, callbackArgsArray )
				currentScript.parentNode.removeChild( currentScript );
			}
		}
	}
	
	function jQ() {
		$(document).ready( function() {
			$(bs.tagName + '.' + bs.className).each(function(i) {
				var position = this.id.replace( bs.idPrefix, '' );
				var element = this;

				$.ajax({
					url: bs.host + bs.path + position,
					data: 'bskey=' + bs.key + bs.dataReq,
					cache: true,
					success: function() {
						actualPos = $(element).attr('id').replace( bs.idPrefix, '' );
						response = 'html' + actualPos;
						$(element).html( window[ response ] );
					}, dataType: 'script'
				});
			});
		});
	}
}();

bs.initOnLoad();

