$(document).ready(function() {
	fade_speed = 1000;
	

	// fiding urls for purpose of link highlights and such
	//-----------------------------------------=
	current_url = window.location.href;
	site_url = "http://"+location.host+"/";
	trimmed_url = location.protocol + "//" + location.host + location.pathname.slice(0, location.pathname.indexOf("/", 1)) + "/";
	//==========================================
	
	
	$("#navigation a").removeClass('active');
	$("#navigation").find("a[href='"+trimmed_url+"']").addClass('active');
	$("#artist-list").find("a[href='"+current_url+"']").addClass('active');
	
	$("a").live("click", function(){
		url = $(this).attr('href');
		urchinTracker(url);
		$(this).blur();
	});
	
	
	$("#artist-list a").live("click", function() {
		loadArtistContent($(this).attr('href'));
		$("#artist-list a").removeClass('active');
		$(this).addClass('active');
		return false;
	});
	
	$('#boxes .box').each(function(){
		$(this).find('div.element:first').show();
		$(this).find('div.element:not(:first)').each(function(){
			$(this).find('h3').hide();
			$(this).find('p').hide();
			$(this).find('img').hide();
			
		});
		$(this).find('div.element:last').attr('last', 'true');
	});
	
	timeDelay = 8000;
	image_fade_speed = 2500;
	text_fade_speed = 2000;
	window.setTimeout('toggle_box("#box-left",timeDelay, image_fade_speed, text_fade_speed)', timeDelay);
	
	attachPlayer();
	attachFlash();
	attachSlideShowArtist();
	generalAttach();
	attachScroll("#artist-text");
	attachScroll("#text");

	
	$("#navigation a").add("#boxes a").click(function(){
		url = $(this).attr('href');
		
		current_if_artist = current_url.search("artysci");
		link_if_artist = url.search("artysci");
		
		if(current_if_artist == "-1" && link_if_artist != "-1"){
			
			loadArtistContent(url);
		};
		
		if(current_if_artist != "-1" && link_if_artist == "-1"){
			$("#artist-changing-content").remove();
			$("#middle").append("<div id=\"photos\"></div>");
			attachFlash();
			generalAttach();
		};
		
		if(current_if_artist != "-1" && link_if_artist != "-1") {
			loadArtistContent(url);
		}
		
 		$("#content").fadeOut(fade_speed, function(){
			$("#content").load(url  + " #inner", function(){
				$("#content").fadeIn(fade_speed);
				generalAttach();
				attachScroll("#text");
				
				element = $("#artist-list a[href='"+url+"']");
				if(element.length == 1) {
						//alert(element.position().top);
						$('#text')[0].scrollTo("a[href='"+url+"']");
				}
				
				$("#content").find("a[href='"+url+"']").slice(0, 1).addClass('active');
			});
		});
		
		
		$("#navigation a").removeClass('active');
		$(this).addClass('active');

		if(link_if_artist != "-1") {
			$("#navigation").find("a[href*='artysci']").slice(0, 1).addClass('active');
		}
		
		current_url = $(this).attr('href');
		return false;	
	});
	
	//Border hover on images
	$('#boxes img').hover(function() {
		$(this).removeClass('img-not-hover').addClass('imghover');
	}, function() {
		$(this).removeClass('imghover').addClass('img-not-hover');
	});
	
	//Hover on footer paragraphs
	$('#footer p').hover(function() {
		$(this).addClass('p_hover');
	}, function() {
		$(this).removeClass('p_hover');
	});
	

	// scroll to anchor after direct page access
	//------------------------------------------
	$('#text')[0].scrollTo("a[href='"+current_url+"']");
	//=========================================
	
});

	
function toggle_box(boxID, timeDelay, image_fade_speed, text_fade_speed) {
	if($(boxID+" div.element").length > 1) {
		$(boxID).find("div.element:visible").each(function() {
				fadeOutElement = $(this);
				faster_text_fade_speed = text_fade_speed/2;
				
				if($(this).attr('last') == 'true') {
					fadeInElement = $(this).parent().find('div.element:first');
				}
				else {
					fadeInElement  = $(this).next();
				}
		
		
				$(this).find('h3').fadeOut(faster_text_fade_speed, function(){
					fadeInElement.find('h3').fadeIn(faster_text_fade_speed);
				});
				$(this).find('p').fadeOut(faster_text_fade_speed, function(){
					fadeInElement.find('p').fadeIn(faster_text_fade_speed);
				});
				$(this).find('img').fadeOut(image_fade_speed, function(){fadeOutElement.hide();});
				
				fadeInElement.show();
				
				
				fadeInElement.find('img').fadeIn(image_fade_speed);

		});
	}
	nextBoxID = "#box-left";
	if(boxID == "#box-left") nextBoxID = "#box-right";
	window.setTimeout('toggle_box(nextBoxID, timeDelay, image_fade_speed, text_fade_speed)', timeDelay);
}

function generalAttach() {
	//Magda Gessler as link
	$("strong:contains('MAGDA GESSLER')").wrap("<a href='http://www.ufukiera.pl/' target='_blank''></a>");
	$('ul.zaufali li').addClass("clearfix");
}



function loadArtistContent(url) {
	$("#photos").remove();
	if($("#artist-changing-content").length == 0) {
		$("#middle").append("<div id=\"artist-changing-content\"></div>");
		$("#artist-changing-content").append("<div id=\"artist-content\"></div>");
		$("#artist-changing-content").append("<div id=\"artist-photos\"></div>");
	}


	$("#artist-content").fadeOut(fade_speed);
	$("#artist-photos").fadeOut(fade_speed, function(){
		$("#artist-changing-content").load(url + " #artist-changing-content-inner", function(){
			attachScroll("#artist-text");
			attachSlideShowArtist();
			$("#artist-content").hide();
			$("#artist-photos").hide();
			$("#artist-photos").fadeIn(fade_speed);
			$("#artist-content").fadeIn(fade_speed);
			$("#artist-photos").fadeIn(fade_speed);
			
		});
	});
}

function attachScroll(string) {
	$(string).jScrollPane({
		scrollbarMargin: 4,
		scrollbarWidth: 8,
		showArrows:true,
		wheelSpeed: 30,
		arrowSize: 16
	});
}

function attachPlayer() {
	$('#player').flash(
		{
		src: site_url+'player.swf',
		width: 90,
		height: 14
		},
		{ update: false }
	);
}

function attachFlash() {
	$('#photos').flash(
		{
		src: site_url+'photos.swf',
		width: 650,
		height: 270
		},
		{ expressInstall: true }
	);
}


function attachSlideShowArtist() {
	$('#artist-photos-inner').innerfade({
		speed: 3000,
		timeout: 4500,
		type: 'sequence',
		containerheight: '270px'
	});
}