var searchmore = false;
var inprogress = false;

function handleForum() {
	$$('.showAddSubmission').each(function(el){
			
		searchmore = new Fx.Slide('add_submission');
		
		searchmore.hide();
				
		el.addEvent('click',function() {
			searchmore.toggle();	
		});
				
	});
	
	if($('forum_add_submit'))
	$('forum_add_submit').addEvent('click',function(){
	
		if($('forum_add_clue').get('value') != 'axioma') {
			$('forum_add_clue').setStyle('border','1px solid red');
			return;
		}
		
		getPage('/forum/13',{
			'm_forum_submit': 'Pridať',
			'm_forum_email': $('forum_add_email').get('value'),
			'm_forum_text': $('forum_add_text').get('value'),
			'm_forum_clue': $('forum_add_clue').get('value') 
		});
	
	});

}

function setGalMovement() {
	$('gal_left').addEvent('click', function() {
		var active = $('gallery').getFirst('.selected');
		active.removeClass('selected');
		active.setStyle('display', 'none');
		var newEl = false;
		if (!(newEl = active.getPrevious('img')))
			newEl = active.getParent().getLast('img');
		
		newEl.setStyle('display', 'block');
		if ((size = newEl.getSize().y) != 550) {
			size = ((550 - size)/2).round();
			newEl.setStyle('margin-top', size+'px');
		} else {
			size = ((722 - newEl.getSize().x)/2).round();
			newEl.setStyle('margin-left', size+'px');
		}
		newEl.addClass('selected');
	});
	
	$('gal_right').addEvent('click', function() {
		var active = $('gallery').getFirst('.selected');
		active.setStyle('display', 'none');
		active.removeClass('selected');
		var newEl = false;
		if (!(newEl = active.getNext('img')))
			newEl = active.getParent().getFirst('img');
		newEl.setStyle('display', 'block');
		if ((size = newEl.getSize().y) != 550) {
			size = ((550 - size)/2).round();
			newEl.setStyle('margin-top', size+'px');
		} else {
			size = ((722 - newEl.getSize().x)/2).round();
			newEl.setStyle('margin-left', size+'px');
		}
		newEl.addClass('selected');
	});
}


function getPage(href,obj) {

	if(!obj)
		obj = new Object();

	var myHTMLRequest = new Request.HTML({
		url:href,
		onSuccess : function (responseTree, responseElements, responseHTML, responseJavaScript) {
			var insideContent = $('insidecontent');
			
			insideContent.set('html', responseHTML);
			var myScroll = new MooScroll({
				disabledOpacity : 0
			});
			
			if ($('gal_left') && $('gal_right')) {
				setGalMovement();
			}
			
			insideContent.getElements('a').each(function(el) {
				if (el.getProperty('target') && el.getProperty('target').toLowerCase() == '_blank')
					return;
				
				el.addEvent('click', function(e) {
					e.stop();
					getPage(this.get('href'));
				});
			});
			
			ReMooz.assign('#gallery_pop span', {
					'origin': 'img',
					'zIndex' : 500,
					'shadow': 'onOpenEnd', // fx is faster because shadow appears after resize animation
					'resizeFactor': 0.8, // resize to maximum 80% of screen size
					'cutOut': false, // don't hide the original
					'opacityResize': 0.4, // opaque resize
					'dragging': false, // disable dragging
					'centered': true // resize to center of the screen, not relative to the source element
				});
			
			handleForum();
			
		}
	}).post(obj);
}


function closeWindow() {
	if (morphbg)
		morphbg.cancel();
	
	if (spotmorph) {
		spotmorph.cancel();
		//soundManager.stop('beep');
		/*soundManager.stop('helloWorld')*/
		
		if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]){
			//mySound.stop();
		}
		
	}
	var size = window.getSize();
	spotmorph = new Fx.Morph($('contentdiv'));
	spotmorph.start({
		'margin-top' : -(((size.y/2)-120)+570)
	}).chain(function() {
		if($('contentdiv'))
		$('contentdiv').destroy();
		morphbg = new Fx.Morph($('bgdiv'), {
			'duration' : 300
		}).start({'opacity': 0}).chain(function() {
			$('bgdiv').destroy();
		});
	});
	
	$$('#anchors a').each(function(element) { addEventAnchors(element); });
}

var colors = new Array();
colors['/domov/2'] = '97A6D1';
colors['/novinky/4'] = 'FFB782';
colors['/menu/9'] = 'DBFF66';
colors['/linky/7'] = 'F96423';

function getBrowsers() {

if(Browser.Engine.trident) {

	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
 	var ieversion=new Number(RegExp.$1)
	 if (ieversion>=8)
	  return false;
	 else if (ieversion>=7)
	  return true;
	 else if (ieversion>=6)
	  return true;
	 else if (ieversion>=5)
	  return true;
	}
	
} else if(Browser.Engine.presto)
	return true;
	
return false;

}



function addEventAnchors(el) {

	el.addEvent('click', function (e) {
		e.stop();
		
		if(inprogress)
			return;
		inprogress = true;
		
		$$('#anchors a').each(function(element) { element.removeEvents(); });
		var size = window.getSize();
		
		if(getBrowsers())
			margin = 0;
		else
			margin = -((size.y/2)-120);
		
		var bgdiv = new Element('div', {
			'id' : 'bgdiv',
			'styles' : {
				'position' : 'absolute',
				'width' : size.x+'px',
				'height' : size.y+'px',
				'background-color' : '#000000',
				'opacity' : 0,
				'margin-top' : margin,
				'cursor' : 'pointer',
				'z-index' : '50'
			},
			'events' : {
				'click' : function(e) {
					e.stop();
					if(inprogress)
						return;
					closeWindow();
				}
			}
		
		});
		
		bgdiv.inject($(document.body), 'top');
		
		morphbg = new Fx.Morph(bgdiv, {
			'duration' : 300
		}).start({'opacity': 0.8}).chain((function() {
			
			var myHTMLRequest = new Request.HTML({
				url:this.get('href'),
				onSuccess : function (responseTree, responseElements, responseHTML, responseJavaScript) {
				
					//if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]){
						soundManager.play('beep');
					//} 
					
					var size = window.getSize();
					
					if(getBrowsers())
						margin = -570;
					else
						margin = -(((size.y/2)-120)+570);

					
					var spot = new Element('div', {
						'id' : 'contentdiv',
						'styles' : {
							'background' : 'url(pub/local/img/bg.png)',
							'margin-top' : margin,
							'margin-left' : ((size.x/2)-450),
							'position' : 'absolute',
							'z-index' : '100'
						}
					});
					var insideContent = new Element('div', {
						'id' : 'insidecontent',
						'html' : responseHTML,
						'class' : 'scroll'
					}); 
					
					if (colors[this.options.url])
						insideContent.setStyle('color', '#'+colors[this.options.url]);
					
					var close = new Element('div', {
						'id' : 'close',
						'events' : {
							'click' : function() {
								closeWindow();
							}
						}
					});
					
					spot.adopt(close, insideContent);
					
					
					spot.inject($(document.body), 'top');
					spotmorph = new Fx.Morph(spot, {
						duration : 1500,
						fps : 60,
						transition : Fx.Transitions.Bounce.easeOut
					});
					
					var myScroll = new MooScroll({
						disabledOpacity : 0
					});
					
					if(getBrowsers())
						margin = 0;
					else
						margin = -((size.y/2)-120);
					
					spotmorph.start({
						'margin-top' : margin
					});
					
					insideContent.getElements('a').each(function(el) {
						if (el.getProperty('target') && el.getProperty('target').toLowerCase() == '_blank')
							return;
						el.addEvent('click', function(e) {
							e.stop();
							getPage(this.get('href'));
						});
					});
					
					if ($('gal_left') && $('gal_right')) {
						setGalMovement();
					}	
					
					/*if(this.options.url == '/panorama/8') {
						 var so = new SWFObject("/pub/local/flash/twviewer.swf", "sotester", "900", "570", "9.0.28", "#310101"); 
						 so.useExpressInstall('/pub/local/flash/expressinstall.swf'); 
						 so.addParam("allowNetworking", "all"); 
						 so.addParam("allowScriptAccess", "always"); 
						 so.addParam("allowFullScreen", "true"); 
						 so.addParam("scale", "noscale"); 
						 so.addVariable("lwImg", "/pub/local/flash/logo.jpg"); 
						 so.addVariable("lwBgColor", "255,255,255,255"); 
						 so.addVariable("lwBarBgColor", "255,255,255,255"); 
						 so.addVariable("lwBarColor", "255,152,203,0"); 
						 so.addVariable("lwBarBounds", "325,368,273,8"); 
						 so.addVariable("lwShowLoadingPercent", "true"); 
						 so.addVariable("iniFile", "/pub/local/flash/axioma.bin"); 
						 so.write("flashcontent"); 	
					}*/
					
					handleForum();
					
					inprogress = false;
					
				}
			}).post({});
			
		}).bind(this));
		
		
	});
}

var morphbg = false;
var spotmorph = false;
var mySound = false;

window.addEvent('domready', function() {

	if(navigator.appName == 'Microsoft Internet Explorer') {
	
		var ua = navigator.userAgent;
    	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    	if(re.exec(ua) != null)
      		rv = parseFloat( RegExp.$1 );
		
		if(rv < 7 || rv >= 9) {
			$('errorwindow').setStyle('display','block');
		}
	}


	//soundManagerInit();
	//if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]){
		//alert('startujem flash');
		
		soundManager.url = '/pub/helpers/js/sm2/swf/'
		soundManager.useHTML5Audio = false;
		soundManager.debugMode = false;
		
		soundManager.onload = function() {
		
			mySound = soundManager.createSound({
				id: 'beep',
				url: '/pub/helpers/js/sm2/audio/beep.mp3',
				volume: 70,
				onload: function() {
				}
			});
	
		}
	
	//}
	
	var sizes = window.getSize();
	
	var x = sizes.x;
	var y = sizes.y;
	
	$('bg').setStyle('height', y);
	$('bg').setStyle('width', x);
	
	$('knihy').setStyle('height', y);
	$('knihy').setStyle('width', x);
	
	//$(document.body).setStyle('margin', 0);
	$('knihy').setStyle('margin', 0);
	
	//$(document.body).setStyle('background-position', 'center '+((y/2)-120)+'px');
	$('knihy').setStyle('background-position', 'center '+((y/2)-120)+'px');
	
	$('anchors').setStyle('margin-top', ((y/2)-120)+'px');
	
	$$('#anchors a').each(function(el) {
		addEventAnchors(el);
	});
	
});
