/**
 * @author Jarek Kraśniewski
 */

$(document).ready(function(){
	
	$.history.init(function(hash){
        if(hash == "") {
			url = 'pages/art2010/content/wola-art-2010.html';
			id = 'mainContent';
            $.ajax({
				url: url,
				dataType: 'html',
				cache: false,
				success: function(html){
					$("#"+id).html(html);
					$.history.load(url+"?"+id);
					enableSubSection();
				}
			});
        } else {
            rec = hash.split("?");
			url = rec[0];
			id = rec[1];
			$(".ajaxLink").each(function(){ $(this).removeClass('selected'); });
			$(".ajaxLink").each(function(){
				if ($(this).attr('href') == url) { $(this).addClass('selected'); }
			});
			$.ajax({
				url: url,
				dataType: 'html',
				cache: false,
				success: function(html){
					$("#"+id).html(html);
					$.history.load(url+"?"+id);
					enableSubSection();
				}
			});
        }
    },
    { unescape: ",/?" });

	$(".ajaxLink").each(function(){
		$(this).click(function() {
			slideTo('#topMenu');
			url = $(this).attr('href');
			id = $(this).attr('rel');
			$(".ajaxLink").each(function(){ $(this).removeClass('selected'); });
			$(".ajaxLink").each(function(){
				if ($(this).attr('href') == url) { $(this).addClass('selected'); }
			});
			$.history.load(url+"?"+id);
			$.ajax({
				url: url,
				dataType: 'html',
				cache: false,
				success: function(html){
					$("#"+id).html(html);
					enableSubSection();
				}
			});
			return false;
		});
	});
	
});

function slideTo(id) {
	$('html,body').animate({scrollTop: $(id).offset().top},'slow');
	return false;
}

function enableSubSection(){
	$(".section").each(function(){ $(this).hide(); });
	$(".subLink").each(function(){ 
		if ($(this).attr('href') == "#section1") { $(this).addClass('selected'); }
	});
	$("#section1").show();
	
	$(".subLink").each(function(){
		$(this).click(function() {
			id = $(this).attr('href');
			$(".subLink").each(function(){ $(this).removeClass('selected'); });
			$(".section").each(function(){ $(this).hide(); });
			$(this).addClass('selected');
			$(id).show();
			return false;
		});
	});
}

