function createRequestObject() {
  if (typeof XMLHttpRequest === 'undefined') {
      XMLHttpRequest = function() {
            try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
                    catch(e) {}
                          try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
                                  catch(e) {}
                                        try { return new ActiveXObject("Msxml2.XMLHTTP"); }
                                                catch(e) {}
                                                      try { return new ActiveXObject("Microsoft.XMLHTTP"); }
                                                              catch(e) {}
                                                                    throw new Error("This browser does not support XMLHttpRequest.");
                                                                        };
                                                                          }
                                                                            return new XMLHttpRequest();
                                                                            }


var atv ={
	
	container: null,
	shiftTimer: null,
	scrollingOffset: 0,
	scrollingOffsetDelta: 0,
	scrollingOffsetMaximum: 3,
	
	getContainer: function() {if(this.container)return this.container; return this.container=document.getElementById('banners')},
	
	
	
	shiftContainer: function() {

		if(atv.getContainer().offsetLeft + atv.getContainer().offsetWidth < atv.getContainer().parentNode.offsetWidth) {
			atv.getContainer().style.left= atv.getContainer().parentNode.offsetWidth-atv.getContainer().offsetWidth+'px';
			return atv.stopBanners();
		}

		if(atv.getContainer().offsetLeft > 0) {
			atv.getContainer().style.left= '0px';
			return atv.stopBanners();
		}

		atv.getContainer().style.left=atv.getContainer().offsetLeft + atv.scrollingOffset+'px';
		atv.scrollingOffset=atv.scrollingOffset + atv.scrollingOffsetDelta;
		if(Math.abs(atv.scollingOffset) > atv.scrollingOffsetMaximum)
			atv.scrollingOffset+=((atv.scrollingOffset>0)?1:-1)*atv.scrollingOffsetDelta;


		if(Math.abs(atv.scrollingOffset) < Math.abs(atv.scrollingOffsetDelta) / 2 || Math.abs(atv.scrollingOffset) < 1) {
			return atv.stopBanners();
		}
		
		atv.shiftTimer=window.setTimeout(atv.shiftContainer, 45);

		return false;
	},
	
	pushBanners: function(delta) {
		this.scrollingOffsetDelta=delta;
		this.shiftContainer();
		return null;
	},

	stopBanners: function () {
			window.clearTimeout(atv.shiftTimer);
			atv.shiftTimeout=null;
			atv.scrollingOffset=0;
			atv.scrollingOffsetDelta=0;
			return false;
	},

	releaseBanners: function() {
		this.scrollingOffsetDelta=-this.scrollingOffsetDelta*1.2;
		return null;
	},
	
	popupDiv: null,
	
	showVideoPopUp: function(title, description, URL) {
		if(!atv.popupDiv) {
			atv.popupDiv=document.createElement('div');
			atv.popupDiv.style.cssText='overflow: auto; position: absolute; width: 50%; left:25%; top:20%; height: 40%; background: #f0f0f0; border: #999999 4px solid';
			atv.popupDiv.setAttribute('id', 'popup');
			document.body.appendChild(atv.popupDiv);
		}
		
		atv.popupDiv.style.display="";
		
		//alert(URL);
		url=URL;
		var pattern1=/.flv/;
		var pattern2=/http:\/\/arctic-tv.ru\//;
		p1=url.replace(pattern2,'')
		path=p1.replace(pattern1,'');

		var ext='.flv';
		var xmlhttp = createRequestObject();
		xmlhttp.open('GET', '/chfile.php?f='+path+'.mp4', false);
	        xmlhttp.send(null);
		if(xmlhttp.status == 200) {
	          if (xmlhttp.responseText == 1)
	    	        {
			     ext='.mp4';
		        }
		}
//		alert(ext);

	        URL=url.replace(pattern1,ext);
	
		
		
		
		
		atv.popupDiv.innerHTML='<div style="font-size: 150%; float:right; padding: 0px 10px 0 0; font-weight: bold;"><a href="#" onclick="return atv.hideVideoPopUp()" style="color: #ff9900; text-decoration: none">×</a></div>';
		if(URL)
			atv.popupDiv.innerHTML+='<div style="padding: 10px; float:left"><embed src="/site-specific/arctic-tv.ru/flash/player.swf" height="240" width="300" allowscriptaccess="always" allowfullscreen="true" flashvars="file='+URL+'&plugins=viral-2d"/></div>';
		atv.popupDiv.innerHTML+='<p style="padding: 10px 15px 10px 15px">'+description+'</p>';
		return false;		
	},
	
	hideVideoPopUp: function() {
		if(atv.popupDiv) {
			atv.popupDiv.innerHTML='';
			atv.popupDiv.style.display="none";	
		}
		return false;
	},
	buildflashvars: function(flashvars) {
		var fa=new Array();
		for (var i in flashvars) {
				fa[fa.length]=i+'='+encodeURIComponent(flashvars[i]);
		}
		return fa.join('&');	
	},
	programme: {
		siteName: null,
		videoNavigationContainer: null,
		videoDescriptionContainer: null,
		setSiteName: function(hostname){
			atv.programme.siteName=hostname;
		},
		setNavigationContainer: function(container){
			atv.programme.videoNavigationContainer=container;
		},
		setDescriptionContainer: function(container){
			atv.programme.videoDescriptionContainer=container;
		},
		player: {
			container: null,
			id: 'atv-player',
			setContainer: function(node) {
				atv.programme.player.container=node;
			},
			remove: function() {
				try{
					swfobject.removeSWF(atv.programme.player.id);
				}
				catch(e) {
					
				}
			},
			create: function(where, url) {
			var pattern1=/.flv/;
			var pattern2=/http:\/\/arctic-tv.ru\//;
			var pattern3=/http:\/\/www.arctic-tv.ru\//;
			p1=url.replace(pattern2,'')
			p2=p1.replace(pattern1,'');
			path=p2.replace(pattern3,'');
			var ext='.flv';
			var xmlhttp = createRequestObject();
//			alert(path);
			xmlhttp.open('GET', '/chfile.php?f='+path+'.mp4', false);
			xmlhttp.send(null);
//			alert(xmlhttp.responseText);
			if(xmlhttp.status == 200) {
			  if (xmlhttp.responseText == 1)
			    {
//				 alert(xmlhttp.responseText);
				 ext='.mp4';
			    }
			}
//			alert(ext);
			
			
			url=url.replace(pattern1,ext);
			
				var so = new SWFObject('/site-specific/arctic-tv.ru/flash/player.swf','ply','320','240','9','#ffffff');
				so.addParam('allowfullscreen','true');
				so.addParam('allowscriptaccess','always');
				so.addVariable('provider','video');
				so.addVariable('config', 'http://'+atv.programme.siteName+'/site-specific/arctic-tv.ru/upload/player-config.xml');
//				so.addVariable('image','http://]]><xsl:value-of select="$siteName"/><xsl:value-of select="concat(previewURL, '%3Fe%3D', /document/@entropy)" /><![CDATA[');
				so.addVariable('file',url);
				so.addVariable('autostart', true);

				var flashvars=atv.buildflashvars({config: 'http://'+atv.programme.siteName+'/site-specific/arctic-tv.ru/upload/player-config.xml',provider:'video',file: url});
				var embed=escape('embed width="320" height="240" src="/site-specific/arctic-tv.ru/flash/player.swf" flashvars="'+flashvars+'" allowscriptaccess="always" allowfullscreen="true"');
				
				so.addVariable('viral.embed', '<'+escape(embed)+'></embed>');
				so.write(where);
			},
			play: function(url) {
				atv.programme.player.remove();
				atv.programme.player.create(atv.programme.player.container, url);
				return false;
			} 
		},
		videoCache: new Array(),
		checkVideoCacheHit: function(date) {
			if(atv.programme.videoCache[date])
				return true;
			return false;
		},
		updateVideoCache: function (startDate, endDate){
			var data=flamework.json.mod_atv.getRecentVideosByProgrammeName(atv.programme.programmeName, startDate, endDate);
			if(data) {			
				eval ("var videos="+data);
				if(!videos.length)
					videos=new Array(videos);
				for(var i in videos) {
					for(j in videos[i]){
						var d=videos[i][j].date;
						if(!atv.programme.videoCache[d])
							atv.programme.videoCache[d]=new Array();
						atv.programme.videoCache[d][videos[i][j].id]=videos[i][j];
					}
				}
				
			}
		},
		getVideos: function(date) {
			if(atv.programme.videoCache[date])				
					return atv.programme.videoCache[date];
			return null;
		},
		programmeName:null,
		setProgrammeName:function(name){
			atv.programme.programmeName=name;
		},
		setDescription: function(text) {
			atv.programme.videoDescriptionContainer.innerHTML=text;
			return false;
		},
		calendar: {	
			place: function(where) {
				var cal_obj = new RichCalendar();
				cal_obj.auto_close = false;
				cal_obj.language = 'ru';
				cal_obj.user_onchange_handler = atv.programme.calendar.onDateChange;
				cal_obj.user_onclose_handler = function(){};
				cal_obj.show_at_element(where, "child");
				return null;
			},
			onDateChange: function(cal, object_code){
				if (object_code == 'day') {
					var date=cal.get_formatted_date('%Y-%m-%d');
					
					var videos=atv.programme.getVideos(date);
					
					var str='';
					var c=0;
					if(videos){
						var first=true;
						for(var i in videos) {
							if(first) {
								obj=videos[i];
								atv.programme.player.play('http://'+atv.programme.siteName+'/site-specific/arctic-tv.ru/upload/video/archive/'+obj.date+'/'+obj.id+'.flv');
								atv.programme.setDescription(obj.description);
								first=false;
							}
							str+='<a href="#" onclick="atv.programme.player.play(\'http://'+atv.programme.siteName+'/site-specific/arctic-tv.ru/upload/video/archive/'+videos[i].date+'/'+videos[i].id+'.flv\');return atv.programme.setDescription(\''+videos[i].description+'\')">'+videos[i].title+'</a><br />';
							c=c+1;						
						}
						cal.show_date();
					}
					if(c>1)
						atv.programme.videoNavigationContainer.innerHTML=str;
					else
						atv.programme.videoNavigationContainer.innerHTML='';
					
				}
			}
			
		}
	}
}

