var pv = {
	cur : "",
	num : 1,
	pvhash : new Hash(),
		
	getObj : function(obj) { 
		var name = obj.getAttribute('href');
		if(typeof this.pvhash.get(name) != "object") { 
			this.pvhash.set(name, new this.pvObj(obj, this.num++));
		}
		this.cur = obj;
		return this.pvhash.get(name);
	},
	
	showMenu : function() { 
		this.getObj(this.cur).menuOpened = true;
		this.getObj(this.cur).unbindEvents();
	},
	pvMute : function() {  
		this.getObj(this.cur).allowLeftClick = false;
	},
	pvStop : function() {  
		this.getObj(this.cur).remove();
	},
	pvReset : function() {
		this.getObj(this.cur).reset();
	},
	getURL : function(url, t) { 
		if(typeof url != 'undefined') {
			this.getObj(this.cur).allowLeftClick = true;
		
			if(t != 1) {
				this.getObj(this.cur).pvParent.setStyle({outlineStyle : 'dotted', outlineWidth : '1px'});
				window.location.href = url;
			}
			else 
				window.open(url, "", "");
						
			this.getObj(this.cur).remove();
		}
		return true;
	}
	
};

pv.pvObj = function(obj, n){
	this.Name = n;
	this.inited = false;
	this.nowPlaying = false;
	this.allowLeftClick = true;
	this.notPlay = false;
	this.pvHolderId = "";
	this.pvId = "" ;
	this.pvParent = obj; 
	this.favPath = "";
	this.favAlbum = "";
	this.favId = "";
	this.flvUrl = "";
	this.flashvars = "";
	this.menuOpened = false;
	this.DefswfURL = "http://img.mail.ru/r/video2/videoPreview.swf?3";
	this.DefswfWidth = "120";
	this.DefswfHeight = "90";
	this.CurVer = "9.0.124";
	this.DefswfVer = "9.0.0";
	this.DefInstall = "http://img.mail.ru/r/video2/expressInstall.swf";
	this.DefswfObjParams = { allowScriptAccess: "always", wmode : "transparent"};
	this.timerId = null;
	this.dynamic = false;
	this.hosts = "mail.ru" ;
}

pv.pvObj.prototype = {
	makeFlv : function(url) { 
		var reh = /([0-9]+)\.html.*/, arr, flv;
		
		arr = reh.exec(url);
		if(!arr) return false;
		url = url.replace(/r\.mail\.ru\/cln\d*\//,'');
		
		if(!this.checkFlv(url)) return false;
		
		url = url.replace(reh, "pv-"+arr[1]+".flv"); 
		if(!/^http\:\/\//.test(url))
			url = "http://" + VideoContentHost() + url;
			
		if(this.favId) 
			url = "http://" + VideoContentHost() + "/" + this.favPath + "/" + "pv-" + this.favId + ".flv";
		
		flv = url;
		
		return flv;

	},
	
	checkFlv : function(url) { 
		var re = new RegExp('^http\:\/\/[a-zA-Z0-9-.]{1,}\.('+this.hosts+')\/'); 
		if(re.test(url))  
			return url;
		else
			return false;
	},
	
	create : function() { 
		
		var el, id;
		var href = this.pvParent.getAttribute("href"); 
		this.flashvars = new Hash();
		
		if(this.flvUrl == '') 
			this.flvUrl = this.makeFlv(href);
		else 
			this.flvUrl = this.checkFlv(this.flvUrl);
			
		if(this.flvUrl) { 
			with(this.flashvars) {
				set("flvUrl", this.flvUrl); 
				set("hrefUrl", href); 
				set("lang", navigator.browserLanguage || navigator.language); 
			}
			this.flashvars = this.flashvars.toObject(); 
			
			if(!$("pvc" + this.Name)) { 
				id = "pvc" + this.Name;
				el = new Element("SPAN", {"id" : id});
				el.setStyle({zIndex:"2"});
				this.pvHolderId = id;
				new Insertion.Top(this.pvParent, el);
			}
			
			this.inited = true; 
		}
		
		/*
		var el, id;
		this.flashvars = new Hash();
		
		if(typeof arr == "object") {
			with(this.flashvars) {
				set("host", VideoContentHost());
				set("curbox", arr[0]);
				set("album", arr[1]);
				set("id", arr[2]);
				set("hrefUrl", this.pvParent.getAttribute("href")); 
				set("lang", navigator.language); 
			}
			this.flashvars = this.flashvars.toObject(); 
		}
		
		el = new Element("SPAN");
		id = "pvc" + this.Name;
		el.writeAttribute("id", id);
		el.setStyle({zIndex:"5000"});
		this.pvHolderId = id;
		new Insertion.Top(this.pvParent, el);
			
		this.inited = true;
		*/
	},
	
	display : function() { 
		
		var settings = arguments[0];
		var flv = '', favPath = '', favId = '', width, height, dynamic = false;
		if(typeof settings == "object") {
			settings = new Hash(settings);
			flv = settings.get("flv");
			favPath = settings.get("favPath");
			favId = settings.get("favId");
			width = settings.get("width");
			height = settings.get("height");
			dynamic = settings.get("dynamic");
			
			this.flvUrl = flv || '';
			this.favPath = favPath || '';
			this.favId = favId || '';
			this.DefswfWidth = width || this.DefswfWidth;
			this.DefswfHeight = height || this.DefswfHeight;
			this.dynamic = dynamic || false;
		}
		
		if(this.dynamic) this.inited = false;
				
		this.timerId = setTimeout(function() { 
			if(this.timerId) {
				if(!this.inited) this.create();
				
				if(this.inited && !this.nowPlaying && !this.notPlay) {  
					var id;			
					id = "pv" + this.Name;
					el = new Element("span", {"id":id});
					this.pvId = id;  
					
					if($(this.pvHolderId)) $(this.pvHolderId).insert(el); else return;
				
					if(typeof this.flashvars == "object" && $(this.pvId)) { 
						swfobject.embedSWF(
							this.DefswfURL, 
							this.pvId,
							this.DefswfWidth,
							this.DefswfHeight,
							this.DefswfVer,
							this.DefInstall,
							this.flashvars,
							this.DefswfObjParams
						);
						swfobject.createCSS("#"+this.pvId,"z-index:100;cursor:pointer"); 
					} 
					this.nowPlaying = true;
				}
			}
		}.bind(this), 300);
		
		this.bindEvents();	
	},
	
	restore : function(e) {   
		e = e || window.event;
		if(typeof e == 'object') {
			var out = e.toElement ? e.toElement : e.relatedTarget;
			if (out == null || out.getAttribute("id") != this.pvHolderId && out.getAttribute("id") != this.pvId) 	{
				this.reset();
		
				if(this.dynamic) {	
					pv.pvhash.unset(this.pvParent);
					if($(this.pvHolderId)) $(this.pvHolderId).remove();
				}
			}
		}else {
			this.reset();
			if(this.dynamic) {	
				pv.pvhash.unset(this.pvParent);
				if($(this.pvHolderId)) $(this.pvHolderId).remove();
			}
		}		
		this.notPlay = false;
		this.clearTimerid();
	},
	
	remove : function() { //remove pv without playing
		setTimeout(function(){
			if($(this.pvId)) $(this.pvId).remove();
			this.nowPlaying = false;
			this.notPlay = true;
			this.unbindEvents();
			this.clearTimerid();
		}.bind(this),100);
	},
	
	reset : function() { //delete pv
		setTimeout(function(){
			if($(this.pvId)) $(this.pvId).remove();
			this.nowPlaying = false;
			this.notPlay = false;
			this.clearTimerid();
		}.bind(this),100);
	},
	
	clearTimerid : function() {
		this.timerId = null;
		clearTimeout(this.timerId);
	},
	
	bindEvents : function() { 
		this.pvParent.onmouseout = function(e) {
			var e = e || window.event; cancelBubbling(e);
			this.restore(e);
		}.bind(this);
		
		
		this.pvParent.onclick = function(e) {return false;}
		
		this.pvParent.onmouseup =function(e) { 
				var e = e || window.event; cancelBubbling(e); 
				if(e.button == 0)
					setTimeout(function() {
						if(this.allowLeftClick){
							this.reset();
							window.location.href = this.pvParent.getAttribute("href");
						} 
						else 
							this.allowLeftClick = true;
					}.bind(this),5);
		}.bind(this);
		
				
		document.observe("click", function(e) {
			if(this.menuOpened) {
				e = e || window.event;
				if(e.button == 0) 
					this.reset();
			}
		}.bind(this));
	},
	
	unbindEvents : function() {
		this.pvParent.onclick = null;
		this.pvParent.onmouseup = null;
		this.pvParent.onmouseout = null;
	}
};

