/*Function.prototype.bind = function(scope, args){
 var _this = this;
 return function() {
  return _this.apply(scope, args ? args : []);
 }
};
*/
function _setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}
function _getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

var __extend = function(obj, extObj) {
    if (arguments.length > 2) {
        for (var a = 1; a < arguments.length; a++) {
            extend(obj, arguments[a]);
        }
    } else {
        for (var i in extObj) {
            if(obj[i] && typeof extObj[i] == 'object'){
			  obj[i] = __extend(obj[i], extObj[i]);
			}else{
			  obj[i] = extObj[i];
			}
        }
    }
    return obj;
};

var AdMover = function(c){
	var o = this;
	//if(o && c && typeof c == 'object'){
    //    for(var p in c){
    //        o[p] = c[p];
    //    }
    //}
	o = __extend(o, c);
    this.init();
};

AdMover.prototype = {
	_static: {
		ads: [],
		started: null,
		showing: false,
		closelink_will_work_after_seconds: 3000,
		count_tries_to_finish: 100,
		count_days_to_reset: 10,
		generate_limit: 50,
		min_limit: 10,
		from_top: "200px",
		avto_close_window_seconds: 35000,
		start: function(){
			
			var timetoshow = _getCookie("timetoshow_ad");
			if(!timetoshow){
				timetoshow = 0;
			}
			timetoshow++;
			
			var limittoshow = _getCookie("limittoshow");
			if(!limittoshow){
				limittoshow = Math.floor(Math.random()*this.generate_limit);
				limittoshow = (limittoshow > this.min_limit ? limittoshow : this.min_limit);
				_setCookie("limittoshow", limittoshow, 1);
			}

			if(AdMover.prototype._static.showing == false){
				_setCookie("timetoshow_ad", timetoshow, this.count_days_to_reset);
				var ads = AdMover.prototype._static.ads;
				var should_show = null;
				for(var k=1; k <= ads.length; k++){
					if(limittoshow*k == timetoshow){
						should_show = true;
						break;
					}
				}
				if(should_show){
					for(var i=0; i < ads.length; i++){
						if(!ads[i]['showed'] && (i+1 >= k)){
							ads[i]['showed'] = true;
							ads[i]['obj'].show();
							setTimeout(function(_el){
								_el.hide();
							}.bind(this, [ads[i]['obj']]), this.avto_close_window_seconds);	
							break;
						}
					}
				}
			}
			setTimeout(AdMover.prototype._static.start.bind(this), 2000);
		}
	},
	id: null,
	adcont: null,
	effect: "roundedborder",
	init: function(){
		AdMover.prototype._static.ads.push({'id': this.id, 'obj': this});
		if(!AdMover.prototype._static.started){
			AdMover.prototype._static.start();
		}
	},
	show: function(){
		AdMover.prototype._static.showing = true;
		this.el = document.getElementById(this.id);
		if(!this.el) return false;

		this.createMask();
		this.createAdCont();
		this.adcont.innerHTML = '<p style="width: '+(this.el.offsetWidth-10)+'px; font-size: 11px; background-color: #fff; padding: 2px; line-height: 12px; margin: 2px;">Вы окажете огромную поддержку этому социальному проекту, посетив сайт одного из наших спонсоров. Спонсорная реклама представлена ниже.'+
		' <a style="text-decoration: underline; cursor: pointer; color: blue; font-size: 9px;" id="_close-ads">закрыть</a></p>'+this.el.innerHTML;
		this.effects[this.effect](this.adcont);
		
		setTimeout(function(){
			var closebtn = document.getElementById("_close-ads");
			if(closebtn){
				closebtn.onclick = function(){
					_setCookie("timetoshow_ad", this._static.count_tries_to_finish, this._static.count_days_to_reset);
					this.hide();
				}.bind(this);
			}
		}.bind(this), this._static.closelink_will_work_after_seconds);
		window.scrollTo(100, 100);
	},
	hide: function(){
		AdMover.prototype._static.showing = false;
		this.adcont.style.display 	= "none";
		this.mask.style.display 	= "none";
	},
	createAdCont: function(){
		if(this.adcont) return;
		this.adcont = document.createElement("div");
		this.adcont.style.position = "absolute";
		this.adcont.style.left 	   = ((this.getWidth()/2)-150)+"px";
		this.adcont.style.top 	   = this._static.from_top;
		this.adcont.style.zIndex   = 100001;
		document.body.appendChild(this.adcont);
		//this.adcont.style.width    = "absolute";
	},
	createMask: function(){
		if(this.mask) return false;
		this.mask = document.createElement("div");
		this.mask.style.position = "absolute";
		this.mask.style.left = "0px";
		this.mask.style.top = "0px";
		this.mask.style.zIndex = 100000;
		this.mask.style.width = "100%";
		this.mask.style.height = "1600px";
		this.mask.style.opacity = "0.5";
		this.mask.style["filter"] = "alpha(opacity=50)";
		this.mask.style.backgroundColor = "#c3c3c3";
		document.body.appendChild(this.mask);
	},
	effects: {
		roundedborder: function(el){

			this.el = el;
			this.border = 5;
			this.el.style.padding = "10px";
			this.el.style.border = this.border+"px solid red";
			this.el.style['-webkit-border-radius'] = '5px';
			this.el.style['-moz-border-radius'] = '5px';
			this.el.style['border-radius'] = '5px';
			
		
			this.padding = 20;
			this.left	 		= parseInt(this.el.style.left.replace("px", ""));
			this.el.style.left 	= (this.left - this.padding) + "px";
			this.top	 		= parseInt(this.el.style.top.replace("px", ""));
			this.el.style.top 	= (this.top - this.padding) + "px";
			var moveradius = function(){
				this.padding -= 1;
				if(this.padding == 0){
					this.padding = 20;
				}
				//this.border -= 0.5;
				//if(this.border <= 1){
				//	this.border = 10;
				//}
				//this.el.style.border = parseInt(this.border)+"px solid red";
				this.el.style.left = (this.left - this.padding) + "px";
				this.el.style.top = (this.top - this.padding) + "px";
				this.el.style.padding = this.padding+"px";
				
				setTimeout(moveradius.bind(this), 200);
			}.bind(this);
			moveradius();
			
		}
	},
	getWidth: function(){
		var width;
		if(document.documentElement
		&& document.documentElement.offsetWidth
		&& document.documentElement.offsetWidth > 0) {
			width = document.documentElement.offsetWidth;
		} else {
			width = document.body.offsetWidth;
		}
		return width;
	}
}
