function bindAjaxToLinks(root) {
	$('a[href*=PageContent]:not([href$=&hard])', root).each(function() {
		$(this).click(function() {
			document.location.href = document.location.href.replace(/#.*$/, '') + '#' + /PageContent_id=(\d+)/.exec(this.href)[1];
			return false;
		});
	});
}
$(document).ready(function() { 
	var specialTopImages = {167:true, 168: "doradcze", 192:true, 193:true };
	function loadContent(url) {
		$.getJSON(url,{},function(data) { 
			var i = 0;
			var setTopImageToDefault = true;
			var setOrnamentToDefault = true;
			function reloadIthMenu() {
				if(specialTopImages[data.selected[i]]) {
					$('#topImage').each(function() { this.src = '/img/'+data.selected[i]+'.jpg' });
					setTopImageToDefault = false;
				}
				if(specialTopImages[data.selected[i]] == "doradcze") {
					$('#main').css('background-image', 'url(/components/@Page/img/doradcze.jpg)');
					setOrnamentToDefault = false;
				}
				
				$('.ml_'+(i+1)+'.active').removeClass('active');
				var itemToSelect = $('.ml_'+(i+1)+'.id_'+data.selected[i]);
				if(itemToSelect.length == 0 && i > 0) {
					var menuContainer
					if(i == 2) {
						menuContainer = $('.ml_2.active .cml_3');
						$('.cml_3').html("");
					} else {
						menuContainer = $('.cml_'+(i+1));
					}
					menuContainer.load('/?run=AjaxMenu&AjaxMenu_level=' + (i+1) + '&AjaxMenu_root=' + data.selected[i-1] + '&AjaxMenu_selected=' + data.selected[i], {}, function() {
						bindAjaxToLinks(menuContainer);
						if(++i<data.selected.length) {
							reloadIthMenu();
						} else {
							for(;i<4;i++) {
								$('.cml_'+(i+1)).html("");
							}
							$('#contentTitle').text(data.content.title);
							$('#contentArea').load('/?run=ComponentContent&ComponentContent_id='+data.content.id);
							document.title = 'Respect: '+data.content.title;
							if(setTopImageToDefault) {
								$('#topImage').each(function() { this.src = '/img/default.jpg' });
							}
							if(setOrnamentToDefault) {
								$('#main').css('background-image', 'url(/components/@Page/img/rozwojowe.jpg)');
							}
						}
					});
				} else {
					itemToSelect.addClass('active');
					if(++i<data.selected.length) {
						reloadIthMenu();
					} else {
						for(;i<4;i++) {
							$('.cml_'+(i+1)).html("");
						}
						$('#contentTitle').text(data.content.title);
						$('#contentArea').load('/?run=ComponentContent&ComponentContent_id='+data.content.id);
						document.title = 'Respect: '+data.content.title;
						if(setTopImageToDefault) {
							$('#topImage').each(function() { this.src = '/img/default.jpg' });
						}
						if(setOrnamentToDefault) {
							$('#main').css('background-image', 'url(/components/@Page/img/rozwojowe.jpg)');
						}
					}
				}
			}
			
			reloadIthMenu();
		});
	}
	var currentlyLoadedContentId 
	try { currentlyLoadedContentId = /PageContent_id=(\d+)/.exec(document.location.href)[1]; } catch(e) { }
	var idOfContentToLoad = document.location.href.replace(/^.*?(#|$)/, '');
	if(idOfContentToLoad && idOfContentToLoad != currentlyLoadedContentId) {
		loadContent('/?run=AjaxContent&AjaxContent_id='+idOfContentToLoad);
		currentlyLoadedContentId = idOfContentToLoad;
	}
	bindAjaxToLinks(document.body);
	
	setInterval(function() {
		var idOfContentToLoad = document.location.href.replace(/^.*?(#|$)/, '');
		if(!idOfContentToLoad) {
			try { idOfContentToLoad = /PageContent_id=(\d+)/.exec(document.location.href)[1]; } catch(e) { }
		}
		if(idOfContentToLoad != currentlyLoadedContentId) {
			loadContent('/?run=AjaxContent&AjaxContent_id='+idOfContentToLoad);
			currentlyLoadedContentId = idOfContentToLoad;
		}
	}, 200);
	$('#flash').flash({ src: "/flash/anim_globe.swf", width: 491, height: 181, wmode:'transparent' });
});

