var startBodyHeight = 0;
function fireHashLink() {
	if (location.hash.length >= 3) {
		var hashLink = location.hash.substring(1);
		var clicked = false;
		$$('a').each(function(el){
			if (!clicked && el.oldLink.replace('http://www.princehall.ru', '') == hashLink) {
				el.fireEvent('click');
				clicked = true;
			}
		})
		
		if (!clicked) loadPage(hashLink);
	}
}
$(window).addEvent('domready', function(){
	initLinks();
	startBodyHeight = document.body.scrollHeight;
	if (Browser.Engine.trident) ie_fix();
	
	new Swiff('/sharedfiles/swf/logo.swf', {container: 'flogo',width: 272,height: 200})
	new Swiff('/sharedfiles/swf/fontan.swf', {container: 'ffontan',width: 218,height: 316})
	new Swiff('/sharedfiles/swf/lebedi.swf', {container: 'flebedi',width: 41,height: 47})		
	new Swiff('/sharedfiles/swf/dver.swf', {container: 'fdver',width: 95,height: 87})		
	new Swiff('/sharedfiles/swf/flam.swf', {container: 'fflam',width: 74,height: 75})		
	new Swiff('/sharedfiles/swf/olen.swf', {container: 'folen',width: 101,height: 174})		
	new Swiff('/sharedfiles/swf/music.swf', {container: 'fmusic',width: 147,height: 77})
	fireHashLink();
});

var currentActiveLink = false;
function initLinks() {
	$$('a').each(function(el){
		el.oldLink = el.href;
		if (el.href.indexOf('princehall.ru/') !== -1 &&
			el.href.indexOf('userfiles/') == -1 && 
			el.href.indexOf('sharedfiles/') == -1 && 
			el.href.indexOf('module.rtiresize.image/') == -1 && 
			el.href !== "http://www.princehall.ru/" && !el.hasClass('direct')) {
			el.removeEvents('click');
			var natHref = el.href.replace('http://www.princehall.ru', '');
			el.addEvent('click', function(e){
				//e.stop();
				if (el !== currentActiveLink) {
					el.addClass('a');
					
					if (currentActiveLink) currentActiveLink.removeClass('a');
					currentActiveLink = el;

					location.hash = natHref;
					loadPage(natHref);
				}
				
				return false;
			})
			el.href = "javascript:loadPage('"+natHref+"')";
		}
	})
	$$('.floatingbox .closer').addEvent('click', function(){
		hidePage();
		return false;
	})
	$$('.floatingbox .back').removeEvents('click').addEvent('click', function(){
		history.go(-1);
		if (location.hash.length >= 3)
			fireHashLink();
		else 
			hidePage();
		return false;
	})	
	if ($$('a[rel]')) Slimbox.scanPage();
	
	$$('.validateForm').removeEvents('submit');
	$$('.validateForm').addEvent('submit', function(e){
		e.stop();
		eval(this.name+'Valid'+'(this);');
		
		if (!this.hasClass('noValid')) {
			var inputB = this.getElement('input[type=submit]');
			var did = 'loader'+Math.round(Math.random()*100000);
			var div = new Element('div', {
				'id': did,
				'styles' : {
					'width': inputB.offsetWidth,
					'height': inputB.offsetHeight,
					'background': 'url(/images/loader.gif) no-repeat center center',
					'float':'right',
					'color':'#070'
				}
			})
			inputB.addClass('hide');
			div.inject(inputB, 'after');
			
			
			this.set('send', {
				url: "/module.request_form/?send",
				method: 'post',
				onSuccess: function(h){
					$(did).setStyles({'background':'none'}).set('html', "<b>отправлено</b>")
				}		
			}).send();
		}
		
		return false;
	})
	
	if (Browser.Engine.trident) ie_fix();
}

function hidePage() {
	document.body.setStyles({
		'height':'auto'
	})

	$$('.floatingbox').morph({
		//opacity:0
		display:'none'
	})
	if (currentActiveLink) currentActiveLink.removeClass('a');
	currentActiveLink = false;
	location.hash = '/';
}

var cRequest = false;
function loadPage(path) {
	$('startContent').setStyles({
		//'display':'none'
	})
	
	document.body.setStyles({
		'height':'auto'
	})
	
	$$('.floatingbox').setStyles({
		display: 'block',
		opacity:0
	}).morph({opacity:1});
	
	$$('.floatingbox .content').set('html', '').setStyles({opacity:0})
	
	if (cRequest) cRequest.cancel();
	cRequest = new Request({
		url: "/module.getpage.contentByURI/",
		method: 'get',
		onSuccess: function(h){
			new Fx.Scroll(window).toTop();
			$$('.content_ly.floating .content').set({'html':h,'morph':{duration:300}}).morph({opacity:1})
			initLinks();
			
			document.body.setStyles({
				'height':document.body.scrollHeight + ((startBodyHeight < (document.body.scrollHeight + 48))?48:0)
			});
		}
	}).send("uri="+path);
}

function gbformValid(f){
	if (
		f['field_10'].value == ""
		||
		f['field_20'].value == ""
	) {
		alert("Пожалуйста, заполните все обязательные поля формы.");
		$(f).addClass('noValid');
		return false;
	} else {
		$(f).removeClass('noValid');
		return true;
	}
}

function rformValid(f){
	if (
		f['field_10'].value == ""
		||
		f['field_20'].value == ""
		||
		f['field_35'].value == ""
	) {
		alert("Пожалуйста, заполните все обязательные поля формы.");
		$(f).addClass('noValid');
		return false;
	} else {
		$(f).removeClass('noValid');
		return true;
	}
}

function ie_fix() {
	$$('.content_ly').each(function(el){
		el.getElement('.rcbox').setStyles({
			height:el.getHeight()
		})
	})
	$$('.rcbox').setStyle('opacity', 1)
}

