	// Work Space Width
function getClientWidth () {
	return (document.compatMode == 'CSS1Compat' && !window.opera) ? document.documentElement.clientWidth : document.body.clientWidth;
}

	// Work Space Height
function getClientHeight () {
	return (document.compatMode == 'CSS1Compat' && !window.opera) ? document.documentElement.clientHeight : document.body.clientHeight;
}

	// Scroll Top
function getClientSTop () {
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

	// Scroll Left
function getClientSLeft () {
	return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

	// Add/Remove event

function IIS_addEvent (obj, evn, hnd) {
	if (navigator.appName == 'Microsoft Internet Explorer' || window.opera) {
		obj.attachEvent ('on' + evn, hnd);
	} else {
		obj.addEventListener (evn, hnd, false);
	}
	return true;
}

function IIS_removeEvent (obj, evn, hnd) {
	if (navigator.appName == 'Microsoft Internet Explorer' || window.opera) {
		obj.detachEvent ('on' + evn, hnd);
	} else {
		obj.removeEventListener (evn, hnd, false);
	}
	return true;
}

	// Information

function Info (element) {
		
	var w = element.offsetWidth;
	var h = element.offsetHeight;
	var l = 0;
	var t = 0;
	while (element) {
		l += element.offsetLeft;
		t += element.offsetTop;
		element = element.offsetParent;
	}
	return {'l':l, 't':t, 'w': w, 'h':h};
	
}

// Козерог и Лев

/* --- */

var PopUp = null;

var GalleryElement = null;

window.addEvent ('domready', function () {
	
	var temp, tmp;
	
	var ax, bx, cx, dx;
	
	/* --- */
		
		// Capricorn
		
	temp = $$('div.capricorn');
	if (temp.length) {
		tmp = temp[0];
		tmp.getElement ('img').setStyle ('opacity', 0);
		tmp.addEvent ('mouseover', function () {
			$$('div.capricorn')[0].getElement ('img').set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'opacity':1 } );
		} );
		tmp.addEvent ('mouseout', function () {
			$$('div.capricorn')[0].getElement ('img').set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'opacity':0 } );								 
		} );
	}
	
		// Lion
	
	temp = $$('div.lion');
	if (temp.length) {
		tmp = temp[0];
		tmp.getElement ('img').setStyle ('opacity', 0);
		tmp.addEvent ('mouseover', function () {
			$$('div.lion')[0].getElement ('img').set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'opacity':1 } );
		} );
		tmp.addEvent ('mouseout', function () {
			$$('div.lion')[0].getElement ('img').set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'opacity':0 } );								 
		} );
	}
	
		// Link Information
	
	temp = $$('div.element');
	if (temp.length) {
		PopUp = document.createElement ('div');
		bx = false;
		if (document.documentElement) {
			document.documentElement.appendChild (PopUp);
			bx = true;
		} else {
			if (document.body) { document.body.appendChild (PopUp); bx = true; }
		}
		for (cx = 0; cx != temp.length; cx ++) {
			temp[cx].set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#FFFFFF' } );
			temp[cx].addEvent ('mouseover', function () {
				this.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#F6F1C5' } );
			} );
			temp[cx].addEvent ('mouseout', function () {
				this.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#FFFFFF' } );
			} );
			if (bx) {
				ax = temp[cx].getElements ('a')[1];
				ax.set ('my', cx);
				IIS_addEvent (ax, 'click', showPopUp);
				IIS_addEvent (ax, 'mouseout', hidePopUp);
				ax.onclick = function () { return false; }
			}
		}
		if (bx) {
			PopUp.setStyles ( {
				'position':'absolute','left':'0px','top':'0px','z-index':99,'background-color':'#FFFFFF','border':'1px dotted Gray',
				'background-image':'url(' + IMAGEURL + 'loader.gif)','background-position':'center','background-repeat':'no-repeat',
				'display':'none','width':'5px','height':'5px','overflow':'hidden'
			} );
		}
	}
	
	/* --- */
	
	if ($('LC') && $('RC')) {
		
		var lch = Info ($('LC').getElement ('div.inside')).h;
		var rch = Info ($('RC').getElement ('div.inside')).h;
		
		var mh = (lch > rch) ? lch : rch;
		
		$('LC').getElement ('div.inside').setStyle ('height', mh + 'px');
		
		if (rch > lch) {
			$('LC').getElement ('div.inside').setStyle ('height', (mh - 20) + 'px');
			$('RC').getElement ('div.inside').setStyle ('height', (mh) + 'px');
		} else {
			$('LC').getElement ('div.inside').setStyle ('height', mh + 'px');
			$('RC').getElement ('div.inside').setStyle ('height', (mh + 20) + 'px');
		}
		
	}
	
	/* --- */
	
	if ($('Gallery')) GalleryElement = new IIS_Gallery ();
	
	/* --- */
	
	if ($('vk_like')) {
		VK.init ( { apiId: 2411749, onlyWidgets: true } );
		/*
		VK.Widgets.Comments ('vk', {
			
			limit: 50,
			attach: false,
			
		} );
		*/
		VK.Widgets.Like ('vk_like', { width: 180, type: 'button' } );
	}
	
} );

var PopUpImage = null;

var showPopUp = function (event) {
	var e = (event) ? event : window.event;
	PopUp.empty ();
	var my = this.get ('my');
	var dx = $$('div.element')[my];
	PopUp.setStyles ( {
		'left':(e.clientX - 3) + 'px','top':(e.clientY - 20 + getClientSTop ()) + 'px',
		'display':'block','width':'6px','height':'6px'
	} );
	var rel = dx.getProperty ('rel');
	var ax;
	ax = rel.indexOf (','); var w = parseInt (rel.substr (0, ax)); rel = rel.substr (ax + 1);
	ax = rel.indexOf (','); var h = parseInt (rel.substr (0, ax)); rel = rel.substr (ax + 1);
	PopUpImage = rel;
	PopUp.set ('morph', { transition: 'cubic:out', duration: 400, onComplete:function () {
		new Asset.image (PopUpImage, {
			onload:function () { PopUp.getElement ('img').set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'opacity':1 } ); }
		}).setStyle ('opacity', 0).inject (PopUp);
	} } ).morph ( { 'width':w + 'px','height':h + 'px','left':(e.clientX - (w / 2)) + 'px','top':(e.clientY - 20 + getClientSTop () - h) + 'px' } );
	return false;
}

var hidePopUp = function (event) {
	var e = (event) ? event : window.event;
	PopUp.setStyles ( { 'left':'0px','top':'0px','display':'none','width':'5px','height':'5px' } );
	PopUp.empty ();
}

function setColor (color) { $('Gallery').getElement ('div.slideShow').setStyle ('background-color', '#' + color); }

/* --- --- --- */

var IIS_Gallery = new Class ( {
	
	WDTH:null,
	HGHT:null,
	
	BlackMaxWDTH:null,
	BlackMaxHGHT:null,
	
	WhiteMaxWDTH:null,
	WhiteMaxHGHT:null,
	
	Slide:null,
	
	PButton:null,
	NButton:null,
	FButton:null,
	
	Black:null,
	White:null,
	
	Current:null,
	
	Photo:null,
	
	Flag:false,
	
	Implements: [Options, Events, Chain],
	
	initialize: function () {
		
		this.Slide = $('Gallery').getElement ('div.slideShow');
		
		this.PButton = $('Gallery').getElement ('div.l_button');
		this.NButton = $('Gallery').getElement ('div.r_button');
		this.FButton = $('Gallery').getElement ('div.full');
		
		this.Black = $('Gallery').getElement ('div.black');
		this.White = $('Gallery').getElement ('div.white');
		
		var sh = getClientHeight ();
		
		if (sh > 812) sh = 812;
		if (sh < 492) sh = 492;
		
		this.WDTH = 978;
		this.HGHT = sh - 44;
		
		this.BlackMaxWDTH = this.WDTH - 10;
		this.BlackMaxHGHT = this.HGHT - 10;
		
		this.WhiteMaxWDTH = this.BlackMaxWDTH - 4;
		this.WhiteMaxHGHT = this.BlackMaxHGHT - 4;
		
		$('Gallery').setStyle ('height', (sh - 2) + 'px');
		
		this.Slide.setStyle ('height', (sh - 44) + 'px');
		
		this.PButton.addEvent ('mouseover', function () { GalleryElement.PButton.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#F6F1C5' } ); } );
		this.PButton.addEvent ('mouseout', function () { GalleryElement.PButton.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#FFFFFF' } ); } );
		this.NButton.addEvent ('mouseover', function () { GalleryElement.NButton.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#F6F1C5' } ); } );
		this.NButton.addEvent ('mouseout', function () { GalleryElement.NButton.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#FFFFFF' } ); } );
		this.FButton.addEvent ('mouseover', function () { GalleryElement.FButton.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#F6F1C5' } ); } );
		this.FButton.addEvent ('mouseout', function () { GalleryElement.FButton.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'background-color':'#FFFFFF' } ); } );
		
		this.Black.setStyles ( { left:parseInt ((this.WDTH - 100) / 2) + 'px', top:parseInt ((this.HGHT - 100) / 2) + 'px' });
		
		var b = null;
		
		if (document.documentElement) {
			b = document.documentElement;
		} else {
			if (document.body) {
				b = document.body;
			}
		}
		
		if (b) {
			
			IIS_addEvent (b, 'keypress', this.key);
			
			b.focus ();
			
		}
		
		this.Current = this.real_id ();
		
		this.show ();
		
	},
	
	hash_id:function () { return parseInt (location.hash.substr (1)); },
	
	real_id:function () {
		var id = this.hash_id ();
		for (cx = 0; cx != GalleryPhoto.length; cx ++) {
			if (id == GalleryPhoto[cx]['id']) return cx;
		}
		return 0;
	},
	
	show:function () {
		
		this.White.empty ();
		
		$('Gallery').getElement ('div.head').getElement ('div.name').getElement ('div').empty ();
		$('Gallery').getElement ('div.head').getElement ('div.name').setStyle ('opacity', 0);
		
		var w = GalleryPhoto[this.Current]['w'];
		var h = GalleryPhoto[this.Current]['h'];
		if (w > this.WhiteMaxWDTH || h > this.WhiteMaxHGHT) {
			var kw = w / this.WhiteMaxWDTH;
			var kh = h / this.WhiteMaxHGHT;
			var k = ((kw > kh) ? kw : kh);
			w = parseInt (w / k);
			h = parseInt (h / k);
		}
		
		this.Photo = new Asset.image (GalleryPhoto[this.Current]['url'], {
			onload:function () {
				
				var obj = GalleryElement;
				
				var w = GalleryPhoto[obj.Current]['w'];
				var h = GalleryPhoto[obj.Current]['h'];
				if (w > obj.WhiteMaxWDTH || h > obj.WhiteMaxHGHT) {
					var kw = w / obj.WhiteMaxWDTH;
					var kh = h / obj.WhiteMaxHGHT;
					var k = ((kw > kh) ? kw : kh);
					w = parseInt (w / k);
					h = parseInt (h / k);
				}
				
				var bw = w + 4;
				var bh = h + 4;
				
				obj.Black.set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( {
					width:bw, height:bh,
					left:parseInt ((obj.WDTH - bw) / 2), top:parseInt ((obj.HGHT - bh) / 2)
				} );
				
				obj.White.set ('morph', { transition: 'cubic:out', duration: 300, onComplete:function () {
					GalleryElement.Slide.getElement ('img').set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'opacity':1 } );
					var obj = GalleryElement;
					if (GalleryPhoto[obj.Current]['name'] != 'no name') {
						$('Gallery').getElement ('div.head').getElement ('div.name').getElement ('div').innerHTML = GalleryPhoto[obj.Current]['name'];
					} else {
						$('Gallery').getElement ('div.head').getElement ('div.name').getElement ('div').innerHTML = '...';
					}
					$('Gallery').getElement ('div.head').getElement ('div.name').set ('morph', { transition: 'cubic:out', duration: 300 } ).morph ( { 'opacity':1 } );
					if (GalleryPhoto[obj.Current]['w'] > obj.WhiteMaxWDTH || GalleryPhoto[obj.Current]['h'] > obj.WhiteMaxHGHT) {
						var a = $('Gallery').getElement ('div.head').getElement ('div.full').getElement ('a');
						a.setProperty ('href', GalleryPhoto[obj.Current]['url']);
						a.setProperty ('target', '_blank');
						a.setStyle ('background-position', '0px -21px');
					}
					$$('div.links')[0].getElements ('a')[obj.Current].setStyle ('border', '1px solid #FFA200');
					location.hash = '#' + GalleryPhoto[obj.Current]['id'];
					obj.Flag = true;
				} } ).morph ( { width:w, height:h } );
				
			}
		} ).setStyles ( { 'opacity':0, 'width':w, 'height':h }).inject (this.White);
		
	},
	
	prepare:function (id) {
		
		if (id == this.Current) return;
		if (!this.Flag) return;
		this.Flag = false;
		
		var a = $('Gallery').getElement ('div.head').getElement ('div.full').getElement ('a');
		a.setProperty ('href', '#');
		a.setProperty ('target', '_self');
		a.setStyle ('background-position', '0px 0px');
		
		$$('div.links')[0].getElements ('a')[this.Current].setStyle ('border', '1px dotted Silver');
		
		location.hash = '#?';
		
		this.Current = id;
		
		GalleryElement.Slide.getElement ('img').set ('morph', { transition: 'cubic:out', duration: 300, onComplete:function () {
			GalleryElement.show ();
		} } ).morph ( { 'opacity':0 } );
		
	},
	
	prev:function () {
		if (!this.Flag) return;
		if (this.Current > 0) this.prepare (this.Current - 1);
	},
	
	next:function () {
		if (!this.Flag) return;
		if (this.Current < GalleryPhoto.length - 1) this.prepare (this.Current + 1);
	},
	
	index: function (id) {
		if (!this.Flag) return;
		id --;
		if (id < 0 || id > GalleryPhoto.length - 1) return;
		this.prepare (id);
	},
	
	key:function (event) {
		
		e = (event) ? event : ((window.event) ? window.event : false);
		
		if (e) {
			
			if (e.keyCode) {
				
				if (e.keyCode == 37) { GalleryElement.prev (); };
				if (e.keyCode == 39) { GalleryElement.next (); };
				
				if (e.keyCode == 27) {
					
					if ($$('div.back_link')) {
						
						var url = $$('div.back_link').getElement ('a').getProperty ('href').toString ();
						
						if (url.toString) url = url.toString ();
						
						window.location.href = url;
						
					}
					
				}
				
			}
			
		}
		
	}
	
} );
