function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
function getPageSize(){
	 var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
			
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
			
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		//alert(yScroll);
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
			
		}
		var contentHeight = $('main_cont').getHeight();
		if (contentHeight > pageHeight)
			pageHeight = contentHeight;
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		return [pageWidth,pageHeight];
}

var MultiPresent = Class.create();
MultiPresent.prototype = {
	galleryLoaded:false,
	initialize:function()
	{
		
		this.EqHeights();
		this.ApplyEvents();
		this.ApplyEventsSubMenus();
		
		if ($('Screens'))
		{
			Event.observe('Screens', "click", this.ApplyScreens.bind(this));
		}
		/*if ($('WatchScreen'))
		{
			Event.observe('WatchScreen', "click", this.ApplyWatchPresentation.bind(this));
		}*/
		if ($('closer'))
		{
			Event.observe('closer', "click", this.ApplyCloser.bind(this));
		}
		var locat = window.location + '';
		if (locat.substr('portf'))
		{
			this.initializePortfolio();
		}
		if ($('OverlayLoader'))
		{
			$('OverlayLoader').style.display = "none";
		}
	},
	EqHeights:function()
	{
		if ($('DescriptionContent'))
		{
		var MenuHeight = $('DescriptionContent').getHeight() - 21;
		//if (MenuHeight > 261)
			$('SubMenuContent').style.height = MenuHeight + "px";
		}
		if ($('VideoContMain'))
		{
			var innerHeight = $('VideoContMainInner').getHeight();

			if (innerHeight > 144)
				$('VideoContMain').style.height = innerHeight + "px";
		}
	},
	ApplyEvents:function()
	{
		if (window.IDs == undefined)
			return;
		for (var i=0; i<IDs.length; i++)
		{
			var img = $('img'+IDs[i]);
			var id = IDs[i];
			
			Event.observe(img, "click", function(event){
				$A($('ProjectList').getElementsByTagName('IMG')).each(function(elem){
					elem.className = "";
				});
				var elem;
				Try.these(function(){
					elem = event.findElement();
				},
				function(){
					elem = Event.element(event);
				});
				elem.className = "active";
				var id = /imga(\d+)/.exec(elem.id)[1];
				this.loadElements(id);
			}.bind(this));
		}
	},
	loadElements:function(id)
	{
		new Ajax.Request(
						 "/js/multi.php",
						 {
							method:"post",
							encoding:"WINDOWS-1251",
							postBody:$H( { id:id } ).toQueryString(),
							onFailure:function(res){
								//alert("error"+res.responseText);
								//this.Message(res.responseText);	
								},
							onSuccess:function(res){
								//alert(res.responseText);	
								var json = res.responseJSON;
								var Content = '<h3>'+json.Caption+'</h3>'+json.Description;
								$('ProjectContent').innerHTML = Content;
								Event.observe('Screens', "click", this.ApplyScreens.bind(this));
								if (json.BigVideo)
								{
									$('WatchScreen').style.display = "inline";
									//Event.observe('WatchScreen', "click", this.ApplyWatchPresentation.bind(this));
									$('WatchScreen').onclick = function(event) { this.ApplyWatchPresentation(event, '', json.BigVideo, json.imgsrc)}.bind(this);
								}else
								{
									$('WatchScreen').style.display = "none";
								}
								
								if ($('images') && json.Screens)
								{
									var ImagesInner = '';
									for (var i = 0; i<json.Screens.length; i++)
									{
										ImagesInner += '<img src="'+json.Screens[i].img+'" alt="'+json.Screens[i].txt+'" />';
									}
									$('images').innerHTML = ImagesInner;
									this.galleryLoaded = false;
									setVariables();
									refresh(false);
								}
								
								var flashvars = {};
		
								flashvars.source=json.videosrc;
		
								flashvars.cover=json.imgsrc;
		
								var params = {
									menu:'false',
									scale:'noScale',
									bgcolor:'#000000',
									allowFullScreen:'true',
									wmode:'transparent'
								};
		
								swfobject.embedSWF("/flashpleer_new/playerbase.swf", "main_video", "495", "240", "9.0.0", "/flashpleer_new/expressInstall.swf", flashvars, params);
									}.bind(this)
							}
						 );
	},
	ApplyEventsSubMenus:function()
	{
		//alert("regre");
		if (!$('SubMenuUL')) return;
		
		$A($('SubMenuUL').getElementsByTagName('A')).each(function(elem){
			Event.observe(elem, "click", function(event)
			{
				var elem = event.findElement();
				var aHref = elem.href.split("/");
				var id = (aHref[aHref.length-1] != undefined ? aHref[aHref.length-1] : aHref[aHref.length-2]);
				$A($('SubMenuUL').getElementsByTagName('LI')).each(function(elem){
					elem.className = "";
				});
				elem.parentNode.className = "active";
				this.loadChapter(id);
			}.bind(this));
			}.bind(this));
	},
	loadChapter:function(id)
	{
		//alert(window.location);
		var Path = window.location+'';
		var aPath = Path.split('/');
		
		new Ajax.Request(
						 "/js/multi.php",
						 {
							method:"post",
							encoding:"WINDOWS-1251",
							postBody:$H( { ChapterID:id, path1:aPath[aPath.length-2], path2:aPath[aPath.length-1], on_main:window.on_main } ).toQueryString(),
							onFailure:function(res){
								alert("error"+res.responseText);
								//this.Message(res.responseText);	
								},
							onSuccess:function(res){
								//alert(res.responseText);
								
								var json = res.responseJSON;
								//alert(json.Videos[0].videosrc);
								var Content = '<h3>'+json.Info.name+'</h3>'+json.Info.text;
								$('SubContent').innerHTML = Content;
								if (!Prototype.Browser.IE)
								this.EqHeights();
								//alert(json.Videos);
								var Content = '<h3>'+json.Videos[0].name+'</h3>'+json.Videos[0].text;
								
								if ($('ProjectList'))
								{
									$('ProjectList').innerHTML = "";
									if (json.Images.length>0)
									{
										for (var i=0; i<json.Images.length; i++)
										{
											$('ProjectList').innerHTML += json.Images[i];
										}
										
									}
									//alert("wregeeg");
									IDs = json.IDs;
									this.ApplyEvents();
									$('ProjectContent').innerHTML = Content;
								}
								if ($('images') && json.Screens)
								{
									//alert(json.Screens);
									var ImagesInner = '';
									for (var i = 0; i<json.Screens.length; i++)
									{
										ImagesInner += '<img src="'+json.Screens[i].img+'" alt="'+json.Screens[i].txt+'" />';
									}
									$('images').innerHTML = ImagesInner;
									this.galleryLoaded = false;
									setVariables();
									refresh(false);
									//alert("rger");
									Event.observe('Screens', "click", this.ApplyScreens.bind(this));
									//Event.observe('WatchScreen', "click", this.ApplyWatchPresentation.bind(this));
									if (json.Videos[0].file2)
									{
										$('WatchScreen').style.display = "inline";
										//Event.observe('WatchScreen', "click", this.ApplyWatchPresentation.bind(this));
										$('WatchScreen').onclick = function(event) { this.ApplyWatchPresentation(event, '', json.Videos[0].file2, json.Videos[0].imgsrc)}.bind(this);
									}else
									{
										$('WatchScreen').style.display = "none";
									}
								}
								//alert("ewgwegwe");
								if (json.Info.title)
								{
									//alert("wregeeg");
									
									$('ProjectAttention').innerHTML = json.Info.title;
									
									$('ProjectAttention').style.display = "block";
									/*if (!Prototype.Browser.IE)
									$('SubContent').style.width = "380px";
									else
									$('SubContent').style.width = "354px";*/
								}
								else
								{
									$('ProjectAttention').style.display = "none";
									/*if (!Prototype.Browser.IE)
									$('SubContent').style.width = "606px";
									else
									$('SubContent').style.width = "590px";*/
								}
								if ($('ChapterContainer') && json.Info.info1)
								{
									$('ChapterContainer').innerHTML = json.Info.info1;
								}
								//alert("ewgwegwe");
							if ($('ProjectList'))
								{
								//alert("ewgwegwe");
									var flashvars = {};
								//alert("reger");
								flashvars.source=json.Videos[0].videosrc;
		
								flashvars.cover=json.Videos[0].imgsrc;
		
								var params = {
									menu:'false',
									scale:'noScale',
									bgcolor:'#000000',
									allowFullScreen:'true',
									wmode:'transparent'
								};
								
								swfobject.embedSWF("/flashpleer_new/playerbase.swf", "main_video", "495", "240", "9.0.0", "/flashpleer_new/expressInstall.swf", flashvars, params);
								}
								
								if (json.Info.bgImage)
								{
									$('ProjectAttention').style.backgroundImage = json.Info.bgImage;
								}
								}.bind(this)
							}
						 );
	},
	ApplyLibrary:function(load, ScreenButton)
	{
		if($(conf_imageflow) || ScreenButton)
		{
			if (ScreenButton)
				var id = /Screens(\d+)/.exec(ScreenButton)[1];
				else var id = '';
			setVariables();
			hide(conf_loading+id);
			if (ScreenButton) load = true;
			refresh(load, ScreenButton);
			if (ScreenButton) show(conf_imageflow+id);
			show(conf_images+id);
			//alert(conf_images+id);
			show(conf_scrollbar+id);
			initMouseWheel();
			initMouseDrag();
			//alert(id);
		}
	},
	hideLibrary:function(id)
	{
		if (!id) id = '';
		$(conf_imageflow+id).style.display = "none";
		$('overlay').remove();
	},
	ApplyScreens:function(event, ScreenButton)
	{
		var objBody = document.getElementsByTagName("body").item(0);
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
				var objOverlay = document.createElement("div");
	//objOverlay.setAttribute('id','overlay');
				objOverlay.id = 'overlay';
				objOverlay.onclick = function () {this.hideLibrary(); return false;}.bind(this);
				objOverlay.style.display = 'none';
				objOverlay.style.position = 'absolute';
				objOverlay.style.top = '0';
				objOverlay.style.left = '0';
				objOverlay.style.zIndex = '90';
 				objOverlay.style.width = '100%';
				objBody.appendChild(objOverlay);
				var arrayPageSize;
				var arrayPageScroll;
				/*Try.these(function(){
					arrayPageSize = getPageSize();
					alert(arrayPageSize);
					arrayPageScroll = document.viewport.getScrollOffsets();
				},
				function(){
					arrayPageSize = getPageSize();
					arrayPageScroll = getPageScroll();
				}
				);*/
				try {
					arrayPageSize = getPageSize();
					//alert(arrayPageSize);
					arrayPageScroll = document.viewport.getScrollOffsets();
					if (!arrayPageSize || !arrayPageScroll) throw "efwef";
				}catch(e)
				{
					arrayPageSize = getPageSize();
					arrayPageScroll = getPageScroll();
				}
				//alert(arrayPageScroll);
				objOverlay.style.height = (arrayPageSize[1] + 'px');
				objOverlay.style.display = 'block';
				if (ScreenButton != undefined)
				{
					var id = /Screens(\d+)/.exec(ScreenButton)[1];
					var imageflow = 'imageflow'+id;
				}
				else
				{
					var imageflow = 'imageflow';
				}
				//alert(imageflow);
				$(imageflow).style.display = "block";
				var left; var top;
				//alert(arrayPageSize);
				Try.these(function(){
					left = document.body.getWidth()/2 - 400;
					top = arrayPageScroll[1] + 100;
				},
				function(){
					left = arrayPageSize[0]/2 - 400;
					top = arrayPageScroll[1] + 100;
				}
				);
				$(imageflow).style.left = left + 'px';
				$(imageflow).style.top = top + 'px';
				//alert(imageflow);
				this.ApplyLibrary((this.galleryLoaded ? false : true), ScreenButton);
				//this.ApplyLibrary(true, ScreenButton);
				this.galleryLoaded = true;
	},
	ApplyWatchPresentation:function(event, id, BigVideo, Cover)
	{
		var objBody = document.getElementsByTagName("body").item(0);
	if (!id) id = '';
	//alert(id);
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
				var objOverlay = document.createElement("div");
	//objOverlay.setAttribute('id','overlay');
				objOverlay.id = 'overlay';
				//objOverlay.onclick = function () {this.hidePresentation(id); return false;}.bind(this);
				objOverlay.style.display = 'none';
				objOverlay.style.position = 'absolute';
				objOverlay.style.top = '0';
				objOverlay.style.left = '0';
				objOverlay.style.zIndex = '90';
 				objOverlay.style.width = '100%';
				objBody.appendChild(objOverlay);
	
				/*var arrayPageSize = getPageSize();
				var arrayPageScroll = getPageScroll();
				var arrayPageSize = getPageSize();
				var arrayPageScroll = document.viewport.getScrollOffsets();
				
				objOverlay.style.height = (arrayPageSize[1] + 'px');
				objOverlay.style.display = 'block';
				var left = document.body.getWidth()/2 - 250;
				var top = arrayPageScroll[1] + 100;*/
				var arrayPageSize;
				var arrayPageScroll;
				Try.these(function(){
					arrayPageSize = getPageSize();
					arrayPageScroll = document.viewport.getScrollOffsets();
				},
				function(){
					arrayPageSize = getPageSize();
					arrayPageScroll = getPageScroll();
				}
				);
				//alert(arrayPageScroll);
				objOverlay.style.height = (arrayPageSize[1] + 'px');
				objOverlay.style.display = 'block';
				var left; var top;
				//alert(arrayPageSize);
				Try.these(function(){
					left = document.body.getWidth()/2 - 315;
					top = arrayPageScroll[1] + 20;
				},
				function(){
					left = arrayPageSize[0]/2 - 351;
					top = arrayPageScroll[1] + 20;
				}
				);
				var main_video = document.createElement("DIV");
				main_video.style.width = "650px";
				main_video.style.height = "500px";
				//var main_video = "main_video"+id;
				//alert(main_video);
				
				main_video.style.position = "absolute";
				main_video.style.left = left + 'px';
				main_video.style.top = top + 'px';
				main_video.style.zIndex = 100;
				main_video.id = "main_video_popup";
				var main_video_obj = document.createElement("DIV");
				main_video_obj.id = "main_video_obj";
				main_video.appendChild(main_video_obj);
				main_video.onclick = function() { void(0);}
				objOverlay.appendChild(main_video);
				var closer = document.createElement('DIV');
				closer.className = "closer";
				closer.style.left = left + 630 + 'px';
				closer.style.top = top + 520 + 'px';
				closer.id = "PresentationCloser"+id;
				//alert(id);
				
				objOverlay.appendChild(closer);
				closer.onclick = function(){ this.hidePresentation(id); return false}.bind(this);
				var flashvars = {};
		
				flashvars.source=BigVideo;
		
				flashvars.cover=Cover;
		
				var params = {
					menu:'false',
					scale:'noScale',
					bgcolor:'#000000',
					allowFullScreen:'true',
					wmode:'transparent'
				};
		
				swfobject.embedSWF("/flashpleer_new/playerbase.swf", "main_video_obj", "650", "500", "9.0.0", "/flashpleer_new/expressInstall.swf", flashvars, params);
				/*Event.observe(closer, "click", function(event){
					//alert("sgrege");
					var elem = event.findElement();
					var id = /PresentationCloser(\d+)/.exec(elem.id)[1];
					//alert(id);
					this.hidePresentation(id);}.bind(this));*/
				//alert(id);
				
				
				//alert("MP.hidePresentation('"+id+"'); return false;");
				return false;
	},
	hidePresentation:function(id)
	{
		if (!id) id = '';
		$('overlay').remove();
	},
	ApplyCloser:function()
	{
		if ($('imageflow').style.display == "block")
		{
			this.hideLibrary(); return;
		}
	},
	initializePortfolio:function()
	{
		$A(document.getElementsByClassName("Screens")).each(function(elem){
			Event.observe(elem, "click", function(event){
				this.ApplyScreens(event, elem.id);
			}.bind(this));
		}.bind(this));

	}
}
var MP;
//Event.observe(window, "load", function(){
document.observe("dom:loaded", function() {
	MP = new MultiPresent();
});
