mainFader = new Object();
mainFader.stopFade = function () {}
mainFader.setFade = function () {}
pageLoad = true;

// --- PAGE INITIALIZATION ---
document.observe("dom:loaded", function() {
	if($('saveToSelection')) {
		go = true;
		try {
			test = detailId;
		} catch(e) {
			go = false;
		}

		if(go) {
			blankHref('saveToSelection');
			$('saveToSelection').observe('click', function() {
				add = new Array();
				add.push(detailId);
				addToSelection(add);
			});
		}
	}

	if($('printLink')) {
		$('printLink').observe('click', function() {
			window.print();
		});
	}

	if($('sendToEmail')) {
		$('emailPageForm').showed = false;
		$('sendToEmail').observe('click', function() {
			if($('emailPageForm').showed == false) {
				$('emailPageForm').setStyle({
					display: 'block',
					opacity: 0
				});

				new Effect.Opacity('emailPageForm', {duration: 0.5, from: 0, to: 1});
				$('emailPageForm').showed = true;
			}
		});
	}

	if($('plan') && $('dot')) {
		planPos = Position.cumulativeOffset($('plan'));

		$('dot').setStyle({
			top: planPos[1]+'px',
			left: planPos[0]+'px'
		});

		$('dot').baseX = planPos[0];
		$('dot').baseY = planPos[1];
	}

	if($('housesTable')) {
		rows = $('housesTable').getElementsByTagName('TR');

		for(x = 0; x < rows.length; ++x) {
			Element.extend(rows[x]);
			//rows[x].pos = x;

			//alert(row.id);
			rowLinks = rows[x].getElementsByTagName('A');

			if(rowLinks.length > 0) {
				houseNumber = parseInt(rowLinks[0].innerHTML);
				rows[x].id = 'row_'+houseNumber;
			}

			rows[x].observe('click', function(event) {
				links = Event.element(event).up('TR').getElementsByTagName('A');
				document.location.href = links[0].href;
			});

			rows[x].observe('mouseover', function(event) {
				allRowsOff();
				row = Event.element(event).up('TR');
				row.setStyle({
					backgroundColor: '#fedc89'
				});
				positionDot(row);

				if($('navigator')) {
					parts = row.id.split('_');
					if(parts.length == 2) {
						flashes = $('navigator').getElementsByTagName('OBJECT');
						ids = getFlashIds(parts[1]);

						//alert(ids['section']+' '+ids['number']);
						/**/
						flashes[0].SetVariable('sekceId', ids['section']);
						flashes[0].SetVariable('dumId', ids['number']);
						flashes[0].SetVariable('evnt', 1);

						/*
						sekceId
						dumId
						evnt(1=mouseOver, 2=mouseOut, 3=press)
						*/
					}
				}
			});

			rows[x].observe('mouseout', function(event) {
				allRowsOff();
				row = Event.element(event).up('TR');
				if($('dot')) {
					$('dot').setStyle({
						display: 'none'
					});
				}

				if($('navigator')) {
					parts = row.id.split('_');
					if(parts.length == 2) {
						flashes = $('navigator').getElementsByTagName('OBJECT');
						ids = getFlashIds(parts[1]);

						flashes[0].SetVariable('sekceId', ids['section']);
						flashes[0].SetVariable('dumId', ids['number']);
						flashes[0].SetVariable('evnt', 2);
					}
				}
			});
		}
	}

	if($('images')) {
		stopFader = false;
		try {
			test = startFader
		} catch(e) {
			stopFader = true;
		}

		mainFader = new faderController('images');
		if(stopFader == true)mainFader.stopFade();
	}

	if($('fotoSwitch') && $('textSwitch') && $('textBcg') && $('content')) {
		blankHref('fotoSwitch');
		$('content').packed = false;

		$('fotoSwitch').observe('click', function() {
			packContent();
		});

		$('textSwitch').observe('click', function() {
			unSlideContent();
		});

		//na zacatku stranky rozbalit
		$('textBcg').setStyle({height: '0px', width: '0px', overflow: 'hidden'});
		$('content').setStyle({height: '0px', width: '0px', overflow: 'hidden'});
	}

	if($('fotoNav') && $('fotoBack') && $('fotoNext')) {
		blankHref('fotoBack');
		blankHref('fotoNext');

		$('fotoNext').observe('click', function(){
			mainFader.nextImage();
		});

		$('fotoBack').observe('click', function(){
			mainFader.previousImage();
		});
	}

	if($('imageTools') && $('imageToolsBcg')) {
		$('imageTools').setStyle({width: '0px', overflow: 'hidden', display: 'block'});
		$('imageToolsBcg').setStyle({width: '0px', overflow: 'hidden', display: 'block'});
	}

	if($('imageSave')) {
		$('imageSave').observe('click', function() {
			imgUrl = mainFader.getShowedPicture();
      if(imgUrl.search('admin') != -1) {
				cPath = 'flashpic';
			} else {
				cPath = 'flashdef';
			}
			parts = imgUrl.split('/');
			getUrl = baseUrl+'get_file.php?file='+parts[parts.length - 1]+'&path='+cPath+'&name=terasy_unhost.jpg';
      document.location.href = getUrl;
		});
	}

	if($('imageEmail') && $('emailPageForm')) {
		$('imageEmail').observe('click', function() {
			mainFader.stopFade();

			if(!$('mailImageForm')) {
				newFormCont = document.createElement('DIV');
				newFormCont.id = 'mailImageForm';
				newFormCont.innerHTML = $('emailPageForm').innerHTML;

				document.body.appendChild(newFormCont);

				bodyMailImage = $('mailImageForm');
				formTitle = bodyMailImage.getElementsByTagName('H5');
				formTitle[0].innerHTML = dict['send_image'];
			}

			imgUrl = mainFader.getShowedPicture();
			if(imgUrl.search('admin') != -1) {
				cPath = 'flashpic';
			} else {
				cPath = 'flashdef';
			}
			parts = imgUrl.split('/');

			formTitle = bodyMailImage.getElementsByTagName('H5');
			if($('sendImage'))$('sendImage').remove();
			if($('sendPath'))$('sendPath').remove();

			new Insertion.After(formTitle[0], '<input type="hidden" name="sendImage" id="sendImage" value="'+parts[parts.length - 1]+'" /><input type="hidden" name="sendPath" id="sendPath" value="'+cPath+'" />');

			$('mailImageForm').setStyle({
				display: 'block',
				opacity: 0
			});

			new Effect.Opacity('mailImageForm', {duration: 0.5, from: 0, to: 1});
		});
	}

	if($('imagePrint')) {
		$('imagePrint').observe('click', function() {
			imgUrl = mainFader.getShowedPicture();
			if(imgUrl.search('admin') != -1) {
				cPath = 'flashpic';
			} else {
				cPath = 'flashdef';
			}
			parts = imgUrl.split('/');
			getUrl = baseUrl+'print_file.php?file='+parts[parts.length - 1]+'&path='+cPath;
			window.open(getUrl, 'printwindow', 'width=700,height=500');
		});
	}

	if($('imageGalleries') || $('photoGaleries')) {

		if($('imageGalleries'))imgs = $('imageGalleries').getElementsByTagName('IMG');
		else if($('photoGaleries'))imgs = $('photoGaleries').getElementsByTagName('IMG');

		for(x = 0; x < imgs.length; ++x) {
			tid = 'tOver' + x;
			imgs[x].id = tid;

			Element.extend(imgs[x]);

			imgLink = imgs[x].up();

			if(imgLink.rel != '') {
				blankHref(imgLink);
				imgLink.onclick = function() {
					openImageGallery(this.rel);
				}
			}

			if($('imageGalleries'))new textOver(tid, 'textOver', '<div>'+imgs[x].alt+'</div>');
		}
	}

	if($('catalogueDetail') && $('foot')) {
		$('foot').setStyle({display: 'none'});
	}
});

pageMenu = new Object();

Event.observe(window, 'load', function() {
	if($('mainMenu')) {
		pageMenu = new subMenu('mainMenu', 'submenu', 'lowerleft');
		pageMenu.setMenuShadow('menuTrans');
		pageMenu.registerAfterPosiCallback(afterPosi);
	}

	if($('fotoSwitch') && $('textSwitch') && $('textBcg') && $('content')) {
		keepHidden = true;

		try {
			test = hiddenContent;
		} catch(e) {
			keepHidden = false;
		}


		if(keepHidden == true) {
			pageLoad = false;
			afterPack();
			$('content').packed = true;
		} else {
			$('content').packed = true;
			unSlideContent();
			pageLoad = false;
		}
	}

	if($('plan') && $('dot')) {
		inDetail = true;

		try {
			test = detailNumber;
		} catch(e) {
			inDetail = false;
		}

		if(inDetail == true) {
			setDetailFlash();
			//window.setTimeout(setDetailFlash, 3500);
		}

		/*
		try {
			test = detailId;
		} catch(e) {
			inDetail = false;
		}

		if(inDetail == true) {
			haveCoords = true;
			try {
				test = dotCoords;
			} catch(e) {
				haveCoords = false;
			}

			if(haveCoords == true) {
				coords = dotCoords.pop();

				if(coords['x'] != 0 && coords['y'] != 0) {
					$('dot').setStyle({
						display: 'block',
						top: ($('dot').baseY + coords['y'])+'px',
						left: ($('dot').baseX + coords['x'])+'px'
					});
				}
			}
		}
		*/
	}
});

// --- FUNCTIONS ---
function setDetailFlash() {
	flashes = $('navigator').getElementsByTagName('OBJECT');
	ids = getFlashIds(detailNumber);
	flashes[0].SetVariable('sekceIdStart', ids['section']);
	flashes[0].SetVariable('dumIdStart', ids['number']);
	/*
	flashes[0].SetVariable('evnt', 1);
	flashes[0].SetVariable('sekceId', ids['section']);
	flashes[0].SetVariable('dumId', ids['number']);
	*/
}

function getFlashIds(seekNumber) {
	resSKey = 0;
	resNKey = 0;
	fNumber = 0;
	found = 0

	for(y = 1; y < flashIds.length; ++y) {
		section = flashIds[y+''];
		for(x = 1; x < section.length; ++x) {
			if(seekNumber == section[x+'']) {
				resSKey = y;
				resNKey = x;
				break;
			}
		}
	}

	res = {'section': resSKey, 'number': resNKey};
	return res;
}

function getHouseNumber(section, house) {
	go = true;

	try {
		test = flashIds[section][house];
	} catch(e) {
		go = false;
	}

	if(go == true) {
		return flashIds[section][house];
	} else {
		return 0;
	}
}

function flashHover(section, house, fEvent) {
	houseNumber = getHouseNumber(section, house)

	if(houseNumber != 0) {
		if(fEvent == 1) {
			// over
			if($('row_'+houseNumber)) {
				allRowsOff();
				$('row_'+houseNumber).setStyle({
					backgroundColor: '#fedc89'
				});
			}
		} else if(fEvent == 2) {
			// out
			allRowsOff();
		} else if(fEvent == 3) {
			// click
			document.location.href = flatUrls[houseNumber];
		}
	}
}

function positionDot(row) {
	dott = $('dot');
	cells = row.getElementsByTagName('TD');
	if(cells[0]) {
		Element.extend(cells[0]);
		cislo = parseInt(cells[0].down().innerHTML);

		havePosition = true;
		try {
			test = dotCoords[cislo]
		} catch(e) {
			havePosition = false;
		}

		if(havePosition == false) {
			dott.setStyle({
				display: 'none'
			});
		} else {
			if(dotCoords[cislo]['y'] != 0 && dotCoords[cislo]['x'] != 0) {
				dott.setStyle({
					display: 'block',
					top: (dott.baseY + dotCoords[cislo]['y'])+'px',
					left: (dott.baseX + dotCoords[cislo]['x'])+'px'
				});
			} else {
				dott.setStyle({
					display: 'none'
				});
			}
		}
	}
}

var onImage = 0;

function closeImageGallery() {
	new Effect.Opacity($('imageGallery'), {duration: 1, from: 1, to: 0, afterFinish: function() {
		$('imageGallery').setStyle({
			opacity: 0,
			display: 'none'
		});
	}})

	new Effect.Opacity($('pageCover'), {duration: 1, from: 0.7, to: 0, afterFinish: function() {
		$('pageCover').setStyle({
			opacity: 0,
			display: 'none'
		});
	}});
}

function openImageGallery(dir) {
	url = baseUrl+lan+'/ajax.php?type=imageGallery&dir='+dir;

	new Ajax.Request(url, {
		onSuccess: function(transport) {
			if($('imageGallery')) {
				$('imageGallery').replace(transport.responseText);
				$('imageGallery').setStyle({
					opacity: 0,
					display: 'block'
				});

				$('pageCover').setStyle({
					opacity: 0,
					display: 'block'
				});

				new Effect.Opacity($('imageGallery'), {duration: 1, from: 0, to: 1})
				new Effect.Opacity($('pageCover'), {duration: 1, from: 0, to: 0.7})

				//prohazovani fotek
				if($('mainImage') && $('thumbnails')) {
					$('mainImage').observe('click', function() {
						closeImageGallery();
					});

					if($('closeGallery')) {
						$('closeGallery').observe('click', function() {
							closeImageGallery();
						});
					}

					subImages = $('thumbnails').getElementsByTagName('IMG');

					for(x = 0; x < subImages.length; ++x) {
						subImages[x].imgOrder = x;
						Element.extend(subImages[x]);
						subImages[x].id = 'galThumb_'+x;
						subImages[x].observe('click', function(event){
							ele = Event.element(event);
							onImage = ele.imgOrder;
							$('mainImage').down().src = ele.src.replace('galsm', 'gal').replace('sm', 'big');
						});
					}

					//posun sipkama
					if($('leftArrow') && $('rightArrow')) {
						blankHref('leftArrow');
						blankHref('rightArrow');

						$('leftArrow').observe('click', function(event){
							if((onImage - 1) >= 0) {
								--onImage;
								ele = new Image();
								ele.src = $('galThumb_'+onImage).src.replace('galsm', 'gal').replace('sm', 'big');
								ele.onload = function() {
									$('mainImage').down().src = this.src;
								}
							}
						});

						$('rightArrow').observe('click', function(event){
							if((onImage + 1) < subImages.length) {
								++onImage;
								ele = new Image();
								ele.src = $('galThumb_'+onImage).src.replace('galsm', 'gal').replace('sm', 'big');
								ele.onload = function() {
									$('mainImage').down().src = this.src;
								}
							}
						});
					}
				}
			}
		}
	});
}

function hideImageForm() {
	$('mailImageForm').setStyle({
		display: 'none'
	});
}

function afterPack() {
	//v ie6 to nezmizne uplne
	$('textBcg').setStyle({height: '0px', overflow: 'hidden'});
	$('content').setStyle({height: '0px', overflow: 'hidden'});

	$('textSwitch').setStyle({
		opacity: 0,
		display: 'block'
	});

	new Effect.Opacity($('textSwitch'), {duration: 0.5, from: 0, to: 1});

	new Effect.Parallel(
    [
		new Effect.Scale('imageTools', 100, {scaleY: false, scaleFrom: 0, scaleMode: { originalHeight: 93, originalWidth: 57}}),
		new Effect.Scale('imageToolsBcg', 100, {scaleY: false, scaleFrom: 0, scaleMode: { originalHeight: 93, originalWidth: 57}})
	],
	{
		duration: 0.5
	});

	if($('fotoNav') && $('fotoBack') && $('fotoNext')) {
		$('fotoNav').setStyle({
			opacity: 0,
			display: 'block'
		});

		new Effect.Opacity($('fotoNav'), {duration: 0.5, from: 0, to: 1});
	}

	mainFader.setFade();
}

function hideFotoNav() {
	$('fotoNav').setStyle({
		opacity: 0,
		display: 'none'
	});
}

function beforeUnSlide() {
	if(!pageLoad) {
		if($('mailImageForm')) {
			new Effect.Opacity('mailImageForm', {duration: 0.5, from: 1, to: 0, afterFinish: hideImageForm});
		}

		new Effect.Opacity($('textSwitch'), {duration: 0.5, from: 1, to: 0});

		new Effect.Parallel(
	    [
			new Effect.Scale('imageTools', 0, {scaleY: false}),
			new Effect.Scale('imageToolsBcg', 0, {scaleY: false})
		],
		{
			duration: 0.5
		});

		if($('fotoNav') && $('fotoBack') && $('fotoNext')) {
			new Effect.Opacity($('fotoNav'), {duration: 0.5, from: 1, to: 0, afterFinish: hideFotoNav});
		}
	}
}

function slideContent() {
	if($('content').packed == false) {
		new Effect.Parallel(
	    [
			new Effect.Scale('textBcg', 0, {scaleY: false}),
			new Effect.Scale('content', 0, {scaleY: false})
		],
		{
			duration: 1,
			afterFinish: afterPack
		});

		$('content').packed = true;
	}
}

function packContent() {
	if($('content').packed == false) {
		new Effect.Parallel(
	    [
			new Effect.Scale('textBcg', 14.3, {scaleX: false}),
			new Effect.Scale('content', 14.3, {scaleX: false})
		],
		{
			duration: 1,
			afterFinish: slideContent
		});
	}
}

function unPackContent() {
	if($('content').packed == true) {
		new Effect.Parallel(
	    [
			new Effect.Scale('textBcg', 100, {scaleX: false, scaleFrom: 14.3, scaleMode: { originalHeight: 586, originalWidth: 730}}),
			new Effect.Scale('content', 100, {scaleX: false, scaleFrom: 14.3, scaleMode: { originalHeight: 586, originalWidth: 730}})
		],
		{
			duration: 1
		});

		$('content').packed = false;
	}
}

function unSlideContent() {
	if($('content').packed == true) {
		mainFader.stopFade();

		$('textBcg').setStyle({height: '84px', overflow: 'hidden'});
		$('content').setStyle({height: '84px', overflow: 'hidden'});

		new Effect.Parallel(
	    [
			new Effect.Scale('textBcg', 100, {scaleY: false, scaleFrom: 0, scaleMode: {originalHeight: 84, originalWidth: 730}}),
			new Effect.Scale('content', 100, {scaleY: false, scaleFrom: 0, scaleMode: {originalHeight: 84, originalWidth: 730}})
		],
		{
			duration: 1,
			beforeStart: beforeUnSlide,
			afterFinish: unPackContent
		});
	}
}

function allRowsOff() {
	if($('housesTable')) {
		rows = $('housesTable').getElementsByTagName('TR');

		for(x = 0; x < rows.length; ++x) {
			Element.extend(rows[x]);
			rows[x].setStyle({backgroundColor: 'transparent'});
		}
	}
}

function addToSelection(ids) {
	url = baseUrl+lan+'/ajax.php?type=addFavorites&ids='+escape(ids.toJSON());

	new Ajax.Request(url, {
		onSuccess: function(transport) {
			//$('favoritesCount').update(transport.responseText);

			if($('demandSelection')) {
				expl = $('demandSelection').innerHTML.split('<span>');
				$('demandSelection').innerHTML = expl[0]+' <span>('+transport.responseText+')</span>';
			}
		}
	});
}

function blankHref(eleId) {
	$(eleId).href = 'javascript:void(0)';
}

function afterPosi() {
	/*
	if($('submenu-4') && $('submenu-shadow-4')) {
		subMenuWidth = 160;
		mMenu = $('submenu-4').ref.mainMenus[4];

		pos = Position.cumulativeOffset($('images'));
		pLeft = pos[0] + $('images').getWidth() - subMenuWidth;

		$('submenu-4').setStyle({
			width: subMenuWidth+'px',
			//height: '102px',
			height: '68px',
			left: pLeft+'px'
		});

		$('submenu-shadow-4').setStyle({
			width: subMenuWidth+'px',
			//height: '102px',
			height: '68px',
			left: pLeft+'px'
		});
	}
	*/
}

function insertFlash(eleId, swfFile, width, height, alt, wmode, flashVars) {
	directFlashVars = '';
	if(flashVars != '')directFlashVars = '?'+flashVars;

	flashHtml =
	'<!--[if !IE]> -->'+"\n"+
	'<object type="application/x-shockwave-flash" data="'+swfFile+directFlashVars+'" width="'+width+'" height="'+height+'">'+"\n"+
	'<!-- <![endif]-->'+"\n"+
	'<!--[if IE]>'+"\n"+
	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+width+'" height="'+height+'">'+"\n"+
	'<param name="movie" value="'+swfFile+directFlashVars+'" />'+"\n"+
	'<!--><!---->'+"\n"+
	'<param name="loop" value="true" />'+"\n"+
	'<param name="menu" value="false" />'+"\n"

	if(wmode)flashHtml += '<param name="wmode" value="transparent" />'+"\n";
	if(flashVars != '')flashHtml += '<param name="flashvars" value="'+flashVars+'" />'+"\n";

	flashHtml += alt+"\n";
	flashHtml += '</object>'+"\n";
	flashHtml += '<!-- <![endif]-->'+"\n";

	obj = document.getElementById(eleId);
	obj.innerHTML = flashHtml;
}
