//jquery scripts
$(function() {
	//twilight stock behaviour & get the absolute path
	$absPath = $('body').attr('id');
	//$tabs = $('#tabs').tabs({ fx: { opacity:'toggle', duration:'1000' } });
	//$tabs.bind("tabsshow", function(event, ui) { window.location.hash = ui.tab.hash; });
		
	// set the transparent pixel
	//if ( $.browser.msie ) { $.ifixpng('/images/pixel.gif'); $('img, .bgPng').ifixpng(); }
	
	//$('#intro_slideshow').cycle ({ fx:'fade', timeout: 2000, speed: 500, next:'#intro_slideshow' });
	if($('#nav_projects a').hasClass('active')){
		$numProjImgs = $('#left #text_area img').size();
		if($numProjImgs > 1){	
			$('#left #text_area img').hide().css('position','absolute');
			$curProjImg = -1;
			$.nextProjImg();
		}
	}

	
	//pictures pages
	if ($('#pictures').length > 0) {
		$("#pictures a").each(function(){
			$link = $(this).attr('href');
			$(this).attr('href','#'+$link);
		});
		$hashImg = location.hash.split("#")[1];
		if ($hashImg) {
			$("#content #left").html('<img src="'+$hashImg+'" style="display:none;"/>');
			$("#content #left img").load(function(){ $(this).fadeIn(); });
			$('#pictures a[href:attr="#'+$hashImg+'"]').addClass('active');
		} else {
			$pic = $('#pictures a:first').attr('href').substring(1);
			$('#pictures a:first').addClass('active');
			$("#content #left").html('<img src="'+$pic+'" style="display:none;"/>');
			$("#content #left img").load(function(){ $(this).fadeIn(); });
			location.hash = $pic;
		}
			
		$("#pictures a").click(function(){
			$("#pictures a").removeClass('active');
			$pic = $(this).attr('href').substring(1);
			$("#content #left").html('<img src="'+$pic+'" style="display:none;"/>');
			$("#content #left img").load(function(){ $(this).fadeIn(); });
			$(this).addClass('active');
		});	
		$('#pictures a').tooltip({ showURL:false, track:true, delay:0, fade:250 });
		$('#pictures_text_area').css('margin','0');
	}
	
	//categories pages
	if ($('#categories').length > 0) {
		$('#categories a').tooltip({ showURL:false, track:true, delay:0, fade:250 });
	}
	
	//galleries pages
	if ($('#galleries').length > 0) {
		$('#galleries a').tooltip({ showURL:false, track:true, delay:0, fade:250 });
	}
		
	/* image popup
	$("#pictures a").photoViewer();
	$(".hoverContainer").hover(function(){ 
			$(this).css('background-color','#333333'); 
			$(this).find('p').show(); 
		}, function() { 
			$(this).css('background-color',''); 
			$(this).find('p').hide(); 
		}
	);
	$(".expand").tooltip({ showURL:false, track:true, delay:0, fade:250 });	
	$('#pictures a').tooltip({ showURL:false, track:true, delay:0, fade:250 });
	*/
		
	//dropdown nav
	/*if ($('.category_list').length > 0) {
		$(".category_list").find("ul").hide();
		$(".category_list").hover(
			function(){ $(this).find("ul").slideDown("fast"); $(this).addClass("ieHover"); }, 
			function(){ $(this).find("ul").slideUp("fast").removeClass("ieHover"); } 
		);
	}*/
	
	$('#footer a[title=""]').attr('title','mailto');
	
	//homepage blocks
	if ($('#blocks').length > 0) {
		$('#blocks li').css('background-image','none');
		$('#blocks li').hide();
		$.showBlocks();
	
		//blocks behaviour
		$('#blocks li').each(function(){
			$(this).attr('alt',$(this).css('background-color'));
		});
		$('#blocks li').hover(function() { 
				$(this).animate({backgroundColor:'#fedd00'});
				$tl = $(this).attr('title');
				$('#nav a[title='+$tl+']').animate({color:$('#nav a[title='+$tl+']').data('hoverColor')});
				$('img[id='+$tl+'_normal]').fadeOut();
			},function(){
				$(this).animate({backgroundColor:$(this).attr('alt')});
				$('#nav a[title='+$tl+']').animate({color:$('#nav a[title='+$tl+']').attr('alt')});
				$('img[id='+$tl+'_normal]').fadeIn();
			}
		);
		$('#nav a').hover(function() { 
				$tl = $(this).attr('title'); 
				$(this).animate({color:$(this).data('hoverColor')});
				$('img[id='+$tl+'_normal]').fadeOut();
			},function(){ 
				$(this).animate({color:$(this).attr('alt')});
				$('img[id='+$tl+'_normal]').fadeIn();
			}
		);	
	}
	
	//home blocks
	if ($('#blocks_home').length > 0) {
		$.navHovers();
		
		//blocks behaviour
		$('#blocks_home li').each(function(){
			$(this).attr('alt',$(this).css('background-color'));
		});
		$('#blocks_home li').hover(function() { 
				$(this).animate({backgroundColor:'#fedd00'});
				$tl = $(this).attr('title');
				$('#nav a[title='+$tl+']').animate({color:$('#nav a[title='+$tl+']').data('hoverColor')});
				$('img[id='+$tl+'_normal]').fadeOut();
			},function(){
				$(this).animate({backgroundColor:$(this).attr('alt')});
				$('#nav a[title='+$tl+']').animate({color:$('#nav a[title='+$tl+']').attr('alt')});
				$('img[id='+$tl+'_normal]').fadeIn();
			}
		);
		$('#nav a').hover(function() { 
				$tl = $(this).attr('title'); 
				$(this).animate({color:$(this).data('hoverColor')});
				$('img[id='+$tl+'_normal]').fadeOut();
			},function(){ 
				$(this).animate({color:$(this).attr('alt')});
				$('img[id='+$tl+'_normal]').fadeIn();
			}
		);	
		//$('#blocks_home a').tooltip({ showURL:false, track:true, delay:0, fade:250 });
	}

	//nav behaviour
	$('#nav').hide();
	$('#nav').slideDown('slow');	
	$('#nav a').each(function(){
		$(this).attr('alt',$(this).css('color'));
	});
	
	//scrollbars
	$('#pictures_text_area, #left').jScrollPane( { showArrows:true, wheelSpeed:30 } );
	$activeNav = $('#nav .active').attr('title');
	if ($activeNav == 'projects') {
		$('.jScrollPaneDrag, .jScrollArrowUp, .jScrollArrowDown').css('background-color','#53caf5');
	} else if ($activeNav == 'contact') {
		$('.jScrollPaneDrag, .jScrollArrowUp, .jScrollArrowDown').css('background-color','#a154a0');
	} else if ($activeNav == 'profile') {
		$('.jScrollPaneDrag, .jScrollArrowUp, .jScrollArrowDown').css('background-color','#f36c24');
	} else if ($activeNav == 'news') {
		$('.jScrollPaneDrag, .jScrollArrowUp, .jScrollArrowDown').css('background-color','#8dc542');
	}
	$('.jScrollArrowUp').css('background-image','url('+$absPath+'/images/arrowUp.gif)');
	$('.jScrollArrowDown').css('background-image','url('+$absPath+'/images/arrowDown.gif)');
});

$.showBlocks = function() {
	$.sequence($('#block_0'));
	$.sequence($('#block_10'));
	$.sequence($('#block_20'));
	$.sequence($('#block_30'));
	$.sequence($('#block_40'));
	$.sequence($('#block_50'));
};

$.sequence = function(e) {
	e.fadeIn('fast',function(){
		$(this).next().length && $.sequence($(this).next());
		if(!$(this).next().length) { $.navHovers(); }
	});
	
};

$.navHovers = function() {
	$nav0 = $('#nav a:eq(0)').attr('title');
	$('#nav a:eq(0)').data('hoverColor','#fedd00');
	$('#nav a:eq(0)').data('bkgImg',$nav0);
	//$('#block_0').attr('title',$nav0).append('<a href="'+$absPath+$nav0+'" style="position:absolute; z-index:3; top:0; left:0;"><img style="position:absolute; z-index:2; top:0; left:0;" id="'+$nav0+'_normal" src="images/nav_'+$('#nav a:eq(0)').data('bkgImg')+'.gif"><img style="position:absolute; z-index:1; top:0; left:0;" id="'+$nav0+'_hover" src="images/nav_'+$('#nav a:eq(0)').data('bkgImg')+'_hover.gif"></a>');
	
	$nav1 = $('#nav a:eq(1)').attr('title');
	$('#nav a:eq(1)').data('hoverColor','#53caf5');
	$('#nav a:eq(1)').data('bkgImg',$nav1);
	$('#block_16').attr('title',$nav1).append('<a href="'+$absPath+$nav1+'" title="'+$nav1+'" style="position:absolute; z-index:3; top:0; left:0;"><img style="position:absolute; z-index:2; top:0; left:0;" id="'+$nav1+'_normal" src="'+$absPath+'images/nav_'+$('#nav a:eq(1)').data('bkgImg')+'.gif"><img style="position:absolute; z-index:1; top:0; left:0;" id="'+$nav1+'_hover" src="'+$absPath+'images/nav_'+$('#nav a:eq(1)').data('bkgImg')+'_hover.gif"></a>');
	
	$nav2 = $('#nav a:eq(2)').attr('title');
	$('#nav a:eq(2)').data('hoverColor','#a154a0');
	$('#nav a:eq(2)').data('bkgImg1','nyc');
	$('#nav a:eq(2)').data('bkgImg2','dc');
	$('#block_21').attr('title',$nav2).append('<a href="'+$absPath+$nav2+'" title="'+$nav2+'" style="position:absolute; z-index:3; top:0; left:0;"><img style="position:absolute; z-index:2; top:0; left:0;" id="'+$nav2+'_normal" src="'+$absPath+'images/nav_'+$('#nav a:eq(2)').data('bkgImg1')+'.gif"><img style="position:absolute; z-index:1; top:0; left:0;" id="'+$nav2+'_hover" src="'+$absPath+'images/nav_'+$('#nav a:eq(2)').data('bkgImg1')+'_hover.gif"></a>');
	$('#block_47').attr('title',$nav2).append('<a href="'+$absPath+$nav2+'" title="'+$nav2+'" style="position:absolute; z-index:3; top:0; left:0;"><img style="position:absolute; z-index:2; top:0; left:0;" id="'+$nav2+'_normal" src="'+$absPath+'images/nav_'+$('#nav a:eq(2)').data('bkgImg2')+'.gif"><img style="position:absolute; z-index:1; top:0; left:0;" id="'+$nav2+'_hover" src="'+$absPath+'images/nav_'+$('#nav a:eq(2)').data('bkgImg2')+'_hover.gif"></a>');
	
	$nav3 = $('#nav a:eq(3)').attr('title');
	$('#nav a:eq(3)').data('hoverColor','#f36c24');
	$('#nav a:eq(3)').data('bkgImg1','beth');
	$('#nav a:eq(3)').data('bkgImg2','mark');
	$('#block_32').attr('title',$nav3).append('<a href="'+$absPath+$nav3+'" title="'+$nav3+'" style="position:absolute; z-index:3; top:0; left:0;"><img style="position:absolute; z-index:2; top:0; left:0;" id="'+$nav3+'_normal" src="'+$absPath+'images/nav_'+$('#nav a:eq(3)').data('bkgImg1')+'.gif"><img style="position:absolute; z-index:1; top:0; left:0;" id="'+$nav3+'_hover" src="'+$absPath+'images/nav_'+$('#nav a:eq(3)').data('bkgImg1')+'_hover.gif"></a>');
	$('#block_38').attr('title',$nav3).append('<a href="'+$absPath+$nav3+'" title="'+$nav3+'" style="position:absolute; z-index:3; top:0; left:0;"><img style="position:absolute; z-index:2; top:0; left:0;" id="'+$nav3+'_normal" src="'+$absPath+'images/nav_'+$('#nav a:eq(3)').data('bkgImg2')+'.gif"><img style="position:absolute; z-index:1; top:0; left:0;" id="'+$nav3+'_hover" src="'+$absPath+'images/nav_'+$('#nav a:eq(3)').data('bkgImg2')+'_hover.gif"></a>');
	
	$nav4 = $('#nav a:eq(4)').attr('title');
	$('#nav a:eq(4)').data('hoverColor','#8dc542');
	$('#nav a:eq(4)').data('bkgImg',$nav4);
	$('#block_59').attr('title',$nav4).append('<a href="'+$absPath+$nav4+'" title="'+$nav4+'" style="position:absolute; z-index:3; top:0; left:0;"><img style="position:absolute; z-index:2; top:0; left:0;" id="'+$nav4+'_normal" src="'+$absPath+'images/nav_'+$('#nav a:eq(4)').data('bkgImg')+'.gif"><img style="position:absolute; z-index:1; top:0; left:0;" id="'+$nav4+'_hover" src="'+$absPath+'images/nav_'+$('#nav a:eq(4)').data('bkgImg')+'_hover.gif"></a>');
			
	//$('#blocks a').tooltip({ showURL:false, track:true, delay:0, fade:250 });
};	

$.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

$.nextProjImg = function(){
	/* sequential */
	if ($curProjImg+1 < $numProjImgs){ 
		$curProjImg += 1; 
		$('#left #text_area img:eq('+($curProjImg-1)+')').fadeOut(3000);
	} else { 
		$curProjImg = 0; 
		$('#left #text_area img:eq('+($numProjImgs-1)+')').fadeOut(3000);
	}	
	$('#left #text_area img:eq('+$curProjImg+')').fadeIn(3000, function(){ 
		$(this).animate({ 'opacity':1}, 2000, function(){
			$.nextProjImg();
		});
	});
	
	/* random
	$('#intro_slideshow li:eq('+$curIntroImg+')').fadeOut('slow');
	$nextIntroImg = Math.floor(Math.random() * $numIntroImgs);
	$('#intro_slideshow li:eq('+$nextIntroImg+')').fadeIn('slow', function(){ 
		$(this).animate({ 'opacity':1}, 2000, function(){
			$curIntroImg = $nextIntroImg;
			$.nextIntroImg();
		});
	});*/
};

