// NOTE
// CURRENT ERRORS IN IE because of hoverIntent
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function() {
	Cufon.replace('h1');
	Cufon.replace('#footer ul li a', {
		hover: true
	});
	
	$.preloadImages("../images/shop.jpg", "../images/work.jpg", "../images/learn.jpg", "../images/play.jpg", "../images/refit.jpg", "../images/make.jpg");
	
	if( jQuery.browser.mozilla ) {
		$('#page_title').css({lineHeight: '27px'});
	}
		
	$('#wrapper').centerInClient();
	$(window).bind('resize', function() {
		doLayout();
	});
	
	$('.toggle').click(function() {
		$('.toggle').fadeOut('fast', function() {
			$(this).toggleClass('info');
			$(this).fadeIn('400');
		});
		
		$('#page_description').slideToggle(
			{
				duration: 400,
				queue: true,
				easing: 'easeInOutExpo'
			}
		);
	});
	
	lastBlock = $("#accordion_container #a1");
    maxWidth = 451;
    minWidth = 74;	

	$("#accordion_container li").click(
		function(e){
			$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:0 });
			$(this).animate({width: maxWidth+"px"}, {  queue:false, duration:0 });
			destroyGallery(lastBlock);
			lastBlock = this;
			var xmlfile = $(this).attr('rel');
			loadGallery(this, xmlfile);
		}
	);

	$('embed').click(function(e) {
		e.stopPropagation();
		e.PreventDefault;
		return false;
	});	
	
	$(lastBlock).click(function(e) {
		e.stopPropagation();
		e.PreventDefault;
		return false;
	});
	
	$('.option').hoverIntent(function() {
		$(this).children().fadeIn('fast');
	}, function() {
		$(this).children().fadeOut('fast');
	});
		
});

function doLayout(){
	$('#wrapper').centerInClient();
}

function destroyGallery(divid){
	$(divid).html('');
}

function loadGallery(divid, xmlfile){
	var i = Math.round(10000*Math.random()); 
	// Generate a random number so IE doesn't cache the result
	$().ajaxSend(function(r,s){  
		$('.toggle').fadeOut('fast', function() {
			$(this).addClass('loading');
			$(this).fadeIn('400');
		});
	});

	$.ajax({
	    type: "GET",
		url: "xml/"+xmlfile+"?uid="+i,
		dataType: "xml",
		success: function(xml) {
			 $(this).delay(200,function(){
				$(xml).find('album').each(function(){
					$('#project_title').text($(this).attr('title') );
					$('#project_description').text($(this).attr('description') ); 
					$('li#architect').text($(this).attr('architect') ); 
					$('li#period').text($(this).attr('period') ); 
					$('li#value').text($(this).attr('value') ); 
					$('.toggle').fadeOut('fast', function() {
						$(this).removeClass('loading');
						$(this).fadeIn('400');
					});
					Cufon.replace('h1');					
				 });
			});
		}
	});
	
	var s1 = new SWFObject('flash/carousel.swf','ply3','451','378','9','#3a3a3c');
	s1.addParam('allowfullscreen','true');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('wmode','opaque');
	s1.addVariable('file','xml/'+xmlfile);
	s1.write(divid);
	$('embed').click(function(e) {
		e.stopPropagation();
		e.PreventDefault;
		return false;
	});
}

