// ISO - image_soft_open

function makeISOwindow (imgWidth, imgHeight, sized) {
	var El = document.createElement('div');
	El.id = 'image_window';
	El.style.zIndex = 9999;
	El.style.position = 'absolute';
	El.style.background = '#EEE';
	El.style.border = '1px solid #CCC';
	El.style.width = imgWidth+ISO.bordePadding+'px';
	El.style.height = imgHeight+ISO.bordePadding+'px';	
	
	var windowSize = getWindowSize();
	var windowOffset = getWindowOffset();
	var top = (windowOffset.y + (windowSize.height - (imgHeight+ISO.bordePadding))/2);
	var left = (windowOffset.x + (windowSize.width - (imgWidth+ISO.bordePadding))/2);	
	El.style.top = (top>0 ? top : 0)+'px';
	El.style.left = (left>0 ? left : 0)+'px';	
	El.innerHTML = ''+
		"		<div id='imgContainer'>"+
		"			<div id='ISOtitleContainer'>"+
		"				<table id='ISOheadTable'><tr>"+
		"					<td id='ISOtitleL'>"+
		"					<td id='ISOtitle'>"+
		"					<td id='ISOtitleR'>"+
		"				</table>"+
		"				<div id='ISOheadL'><span id='ISOpager'></span></div>"+
		"				<div id='ISOclose'></div>"+
		"			</div>"+
		"			<div id='hoverNav'>"+
		"				<a href='javascript:void(0)' id='prevLink'></a>"+
		"				<a href='javascript:void(0)' id='nextLink'></a>"+
		"				<a href='javascript:void(0)' id='closeLink'></a>"+
		"			</div>"+
		"			<div id='ISOsizing'>"+
		"				Изображение уменьшено,<br>чтобы уместиться в<br>вашем браузере.<br>Нажмие здесь,<br>чтобы посмотреть его<br>в реальном масштабе"+
		"			</div>"+		
		"			<a href='javascript:void(0)' id='ISOaimg'><img src='' id='ISOimg'></a>"+
		"		</div>";
	document.body.appendChild (El);
	
	id('nextLink').onclick = showNext;
	id('prevLink').onclick = showPrev;
	id('ISOaimg').onclick = id('ISOclose').onclick = id('closeLink').onclick = closeWindow;
	id('ISOsizing').onclick = resetSizing;
	if (!ISO.ISO_quick) {
		setElementOpacity(El, 0);
		if (!fadeOpacity.aRules['imgRule']) fadeOpacity.addRule('imgRule', 0, 1, 30); 	
		fadeOpacity('image_window', 'imgRule');
	} else setElementOpacity(El, 1);
	id('ISOimg').style.display = 'inline';	
}

function reISOwindow (imgWidth, imgHeight) {
	var El = id('image_window');
	El.style.width = imgWidth+ISO.bordePadding+'px';
	El.style.height = imgHeight+ISO.bordePadding+'px';
	
	var windowSize = getWindowSize();
	var windowOffset = getWindowOffset();
	var top = (windowOffset.y + (windowSize.height - (imgHeight+ISO.bordePadding))/2);
	var left = (windowOffset.x + (windowSize.width - (imgWidth+ISO.bordePadding))/2);	
	El.style.top = (top>0 ? top : 0)+'px';
	El.style.left = (left>0 ? left : 0)+'px';
}

function openISO (group, ID) {
	ISO.setActive(group, ID);
	var A = ISO.items[group][ID].item;
	showShadow ('ISO_back');
	showISOLoading ('ISO_loading', closeLoading);
	
	ISO.items[group][ID].isoitem = new Image();
	ISO.items[group][ID].isoitem.onload = function () {ISO.items[group][ID].loaded = true; showImage();}
	ISO.items[group][ID].isoitem.src = A.href;
	
	if (!ISO.ISO_quick) {
		setElementOpacity(bigEl, 0);	
		if (!fadeOpacity.aRules['backRule']) fadeOpacity.addRule('backRule', 0, 0.5, 30);
		fadeOpacity('shadowISO_back', 'backRule');		
	} else setElementOpacity(bigEl, 0.5);
	bigEl.style.display = 'block';
}

function showImage (sizeCancel) {
	var img = ISO.getActive().isoitem;	
	// CHECK MIN SIZE
	var width = img.width;
	var height = img.height;
	var MINHEIGHT = 240;
	var MINWIDTH = 320;
	var MAXSIZING = false;
	if (height < MINHEIGHT && width < MINWIDTH) {
		var k1 = MINHEIGHT / height;
		var k2 = MINWIDTH /  width;
		var k = k1 > k2 ? k2 : k1;
		width = width*k;
		height = height*k;
	}
	if (!sizeCancel) {
		var windowSize = getWindowSize();
		var MAXHEIGHT = windowSize.height-20;
		var MAXWIDTH = windowSize.width-20;		
		if (height > MAXHEIGHT || width > MAXWIDTH) {
			var k1 = MAXHEIGHT / height;
			var k2 = MAXWIDTH / width;
			var k = k1 > k2 ? k2 : k1;
			width = width*k;
			height = height*k;
			MAXSIZING = true;
		}
	}
	if (!ISO.windowExists) {
		hideLoading ('ISO_loading');
		makeISOwindow(Math.max(width, MINWIDTH), Math.max(height, MINHEIGHT));
		ISO.windowExists = true;
	} else {
		if (id('loadingISO_loading')) hideLoading ('ISO_loading');
		reISOwindow(Math.max(width, MINWIDTH), Math.max(height, MINHEIGHT));
	}

	id('ISOimg').src = img.src;
	id('ISOimg').width = width;
	id('ISOimg').height = height;

	if (height < MINHEIGHT) {
		id('ISOimg').style.top = '50%';
		id('ISOimg').style.marginTop = (-(Math.round(height/2)))+'px';		
	} else {
		id('ISOimg').style.top = '0px';
		id('ISOimg').style.marginTop = '5px'
	}
	if (width < MINWIDTH) {
		id('ISOimg').style.left = '50%';
		id('ISOimg').style.marginLeft = (-(Math.round(width/2)))+'px';		
	} else {
		id('ISOimg').style.left = '0px';
		id('ISOimg').style.marginLeft = '5px'
	}
	ISO.setTitle(width);
	ISO.setPager();
	ISO.setNav();	
	id('ISOsizing').style.display = (MAXSIZING ? 'block' : 'none');
}
function resetSizing () {
	//ISO.getActive(). = true;
	showImage (true);
}
function showNext () {
	ISO.setActive(ISO.active.group, ISO.active.ID+1);
	if (ISO.getActive().loaded) showImage();
	else {
		showISOLoading ('ISO_loading', function () {ISO.setActive(ISO.active.group, ISO.active.ID-1); hideLoading ('ISO_loading')});
		ISO.interval = setInterval(function(){
			if (ISO.getActive()===null) {clearInterval(ISO.interval);return;}  
			if (ISO.getActive().loaded) {
				clearInterval(ISO.interval);
				showImage();
			}
		}, 100);
	}
}
function showPrev () {
	ISO.setActive(ISO.active.group, ISO.active.ID-1);
	if (ISO.getActive().loaded) showImage();
	else {
		showISOLoading ('ISO_loading', function () {ISO.setActive(ISO.active.group, ISO.active.ID+1); hideLoading ('ISO_loading')});
		ISO.interval = setInterval(function(){
			if (ISO.getActive()===null) {clearInterval(ISO.interval);return;}  
			if (ISO.getActive().loaded) {
				clearInterval(ISO.interval);
				showImage();
			}
		}, 100);
	}
}

function closeLoading() {
	if (!ISO.ISO_quick) fadeOpacity.backQ('shadowISO_back');
	hideShadow ('ISO_back');
	hideLoading ('ISO_loading');
	ISO.setActive(null, null);
}

function closeWindow() {
	if (!ISO.ISO_quick) {
		fadeOpacity.backQ('shadowISO_back');
		fadeOpacity.backQ('image_window'); 
	}
	hideShadow ('ISO_back');
	if (id('loadingISO_loading')) hideLoading ('ISO_loading');
	var El = id('image_window');
	El.parentNode.removeChild(El);
	ISO.setActive(null, null); 
	ISO.windowExists = false;
}
// cISO class ================================
cISO = function () {
	if (detectIE6()) return;
	this.ISO_quick = false;
	this.bordePadding = 12;
	this.items = {}
	this.windowExists = false;
	addLoadEvent(initISO);
	preLoadImages('images/close.png', 'images/prev.png', 'images/next.png', 'images/leftconner.png', 'images/rightconner.png', 'images/closes.png',
		'images/loading_bg.png', 'resizer.png');
}
cISO.prototype.addItem = function (group, item) {
	if (!this.items[group]) this.items[group] = []
	this.items[group].push({'item':item, 'isoitem':null, 'loaded':false});
	return this.items[group].length-1
}
cISO.prototype.setActive = function (group, itemID) {
	this.active = {'group':group, 'ID':itemID}
}
cISO.prototype.getActive = function () {
	return this.active.ID!==null ? this.items[this.active.group][this.active.ID] : null;
}

cISO.prototype.setNav = function () {
	if (ISO.active.group=='!' || typeof(ISO.items[ISO.active.group][ISO.active.ID+1])=='undefined') {
		id('nextLink').style.display = 'none';
	} else {
		id('nextLink').style.display = 'block';
		var nextItem = ISO.items[ISO.active.group][ISO.active.ID+1];
		if (nextItem.isoitem===null) { // ПРЕДЗАГРУЗКА
			nextItem.isoitem = new Image;
			nextItem.isoitem.onload = function (){nextItem.loaded = true;}
			nextItem.isoitem.src = nextItem.item.href;			
		}
	}
	if (ISO.active.group=='!' || typeof(ISO.items[ISO.active.group][ISO.active.ID-1])=='undefined') {
		id('prevLink').style.display = 'none';
	} else {
		id('prevLink').style.display = 'block';
		var prevItem = ISO.items[ISO.active.group][ISO.active.ID-1];
		if (prevItem.isoitem===null) { // ПРЕДЗАГРУЗКА
			prevItem.isoitem = new Image;
			prevItem.isoitem.onload = function (){prevItem.loaded = true;}
			prevItem.isoitem.src = prevItem.item.href;			
		}
	}
}
cISO.prototype.setPager = function () {
	id('ISOpager').innerHTML = (ISO.active.ID+1)+' из '+ISO.items[ISO.active.group].length;	
}
cISO.prototype.setTitle = function (width) {
	if (title = ISO.items[ISO.active.group][ISO.active.ID].item.title) {
		id('ISOheadTable').style.display = '';
		title = title.substr(0, Math.round((width - 190)/ 12));
		id('ISOtitle').innerHTML = title;	
	} else id('ISOheadTable').style.display = 'none';
}
ISO = new cISO();
// ================================================
function initISO() {
	if (!document.getElementsByTagName) { return; }
	var anchors = document.getElementsByTagName('a');

	for (i=0,len=anchors.length; i<len; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && (rel = anchor.getAttribute('rel')) && rel.match(/^ISO/)) {
			var ISOgroup = (rel.match(/ISO\[(.*)\]/));
			ISOgroup = (ISOgroup!==null && ISOgroup[1]) ? ISOgroup[1] : '!';
			var itemID = ISO.addItem(ISOgroup, anchor);
			anchor.onclick = function(ISOgroup, itemID) { return function () {openISO(ISOgroup, itemID); return false;}}(ISOgroup, itemID)
		}
	}
	anchor = null;
	//print_r(ISO.items);
}

function showISOLoading (id, onclose) {
	var width = 46;
	var height = 46;
	theLoading = document.createElement('div');
	theLoading.id = 'loading'+id;
	theLoading.style.zIndex = 9999;
	theLoading.style.position = 'absolute';
	theLoading.style.backgroundImage="url(js/iso/images/loading_bg.png)";
	theLoading.style.width = width+'px';
	theLoading.style.height = height+'px';	
	var windowSize = getWindowSize();
	var windowOffset = getWindowOffset();	
	theLoading.style.top = (windowOffset.y + (windowSize.height - height)/2)+'px';
	theLoading.style.left = (windowOffset.x + (windowSize.width - width)/2)+'px';
	theLoading.innerHTML = '<img src="well/pics/big/loading.gif" title="ЗАГРУЗКА\nНажмите для отмены" style="border:0px;margin:7px;cursor:pointer;">';
	if (onclose) theLoading.onclick = onclose;
	document.body.appendChild(theLoading);
}

function getWindowSize () {
	var width, height;
	if (self.innerHeight) {	
		width = self.innerWidth;	
		height = self.innerHeight;	
	// IE 6 Strict Mode	
	} else if (document.documentElement && document.documentElement.clientHeight) {	
		width = document.documentElement.clientWidth;	
		height = document.documentElement.clientHeight;	
	// Остальные версии IE	
	} else if (document.body) {	
		width = document.body.clientWidth;	
		height = document.body.clientHeight;	
	}
	return {width:width, height:height}
}
function getWindowOffset () {
	var scrOfX = 0, scrOfY = 0;
	if( document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		// DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		// IE6 Strict
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	} else if(typeof(window.pageYOffset) == 'number' ) {
		// Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	return {x:scrOfX, y:scrOfY}
}
function detectIE6() {
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		var b_version = navigator.appVersion;
		var res = b_version.match(/\MSIE\s+(\d\.\d\b)/);
		if (res[1] <= 6){return true;}
	}
	return false;
}
