var rectangles = [];
var albums = [];

window.addEvent('domready', function() {
	
	//create our Accordion instance
	var myAccordion = new Accordion($('accordion'), 'h3.item_clicker', 'div.item_content', {
		opacity: true,
		duration: 200
	});

});




function pm_initialize()
{
	pm_initializeBackground();
//	pm_initAccordions();
	
}
	

//	Utility
	
function pm_contact(inName, inDomain)
{
	address='mailto:' + inName + '@' + inDomain;
	location = (address);
}

//	Stretchy Boxes
	
function pm_initAccordions()
{
	//create our Accordion instance
	var myAccordion = new Accordion($('accordion'), 'h3.item_clicker', 'div.item_content', {
	
		opacity: true,
		duration: 200
	});

}
	
	
	
function x_pm_initAccordions(){

	var stretchers = document.getElementsByClassName('item_content'); // element that can stretch
	var toggles = document.getElementsByClassName('item_clicker'); // element that can be clicked on

	//accordion effect
	var myAccordion = new fx.Accordion(
		toggles, stretchers, {opacity: true, duration: 200}
	);

	//hash function

	function checkHash(){
		var found = false;
		toggles.each(function(h3, i){
			if (window.location.href.indexOf(h3.title) > 0) {
				myAccordion.showThisHideOpen(stretchers[i]);
				found = true;
			}
		});
		return found;
	}

	if (!checkHash()) myAccordion.showThisHideOpen(stretchers[0]);	
}
	

//	Background animation
	
function pm_initializeBackground()
{
	if (rectangles.length == 0)
	{
		//	Set up array of floating rectangles...
		rectangles.push({width: 100, height: 280, x:0, y:200, vx:0.5, vy:-0.5, va:-0.005, cr:113, cg:31, cb:98, ca: 0.2});
		rectangles.push({width: 20, height: 280, x:0, y:200, vx:0.1, vy:0.25, va:0.005, cr:113, cg:15, cb:17, ca: 0.15});
		rectangles.push({width: 240, height: 200, x:120, y:280, vx:0.25, vy:0.75, va:-0.025, cr:96, cg:85, cb:61, ca: 0.2});
		rectangles.push({width: 80, height: 180, x:120, y:300, vx:0.5, vy:-0.25, va:0.005, cr:113, cg:90, cb:35, ca: 0.2});
		rectangles.push({width: 80, height: 80, x:120, y:400, vx:-0.5, vy:0, va:0, cr:120, cg:18, cb:20, ca: 0.2});
		rectangles.push({width: 120, height: 80, x:240, y:400, vx:-0.25, vy:0.5, va:-0.005, cr:120, cg:85, cb:61, ca: 0.12});
		rectangles.push({width: 120, height: 140, x:340, y:340, vx:0.25, vy:0.25, va:0.005, cr:120, cg:85, cb:32, ca: 0.25});
		rectangles.push({width: 360, height: 20, x:180, y:460, vx:0.25, vy:-0.25, va:-0.015, cr:113, cg:15, cb:17, ca: 0.25});
		rectangles.push({width: 100, height: 180, x:480, y:300, vx:-0.75, vy:0.75, va:0.015, cr:111, cg:31, cb:98, ca: 0.15});
		rectangles.push({width: 480, height: 100, x:300, y:380, vx:-0.5, vy:-0.1, va:-0.005, cr:96, cg:39, cb:160, ca: 0.12});
		rectangles.push({width: 240, height: 200, x:600, y:280, vx:-0.5, vy:0.1, va:0.005, cr:83, cg:39, cb:78, ca: 0.12});
		rectangles.push({width: 480, height: 60, x:560, y:420, vx:-0.25, vy:0, va:0, cr:113, cg:18, cb:80, ca: 0.2});
	}
	
	//	Draw background...
	pm_refreshBackground();
}


function pm_refreshBackground()
{
	//	Update and redraw all background rectangles...
	pm_updateBackground();
	pm_drawBackground();
}


function pm_drawBackground()
{
	//	Get canvas to draw in...
	var backgroundCanvas = document.getElementById("background_canvas");
	if (backgroundCanvas.getContext)
	{
		var context = backgroundCanvas.getContext("2d");
		
		//	Clear...
		context.clearRect(0, 0, 840, 480);
		
		//	Draw all the rectangles...
		var rectCount = rectangles.length;
		for (var rectIndex = 0; rectIndex < rectCount; rectIndex++)
		{
			var currentRect = rectangles[rectIndex];
			context.fillStyle = currentRect.color;
			context.fillRect(currentRect.x, currentRect.y, currentRect.width, currentRect.height);
		}
	}
}


function pm_updateBackground()
{
	//	Go through all rectangles...
	var rectCount = rectangles.length;
	for (var rectIndex = 0; rectIndex < rectCount; rectIndex++)
	{
		var currentRect = rectangles[rectIndex];
		
		//	Update x-position...
		currentRect.x = currentRect.x + currentRect.vx;
		if (((currentRect.x > 840) && (currentRect.vx > 0)) || ((currentRect.x < -currentRect.width) && (currentRect.vx < 0)))
		{
			currentRect.vx = -currentRect.vx;
		}
		
		//	Update height and y-position...
		currentRect.height = currentRect.height + currentRect.vy;
		currentRect.y = currentRect.y - currentRect.vy;
		if ((currentRect.height > 480) || (currentRect.height < 120))
		{
			currentRect.vy = -currentRect.vy;
		}
		
		//	Update alpha...
		currentRect.ca = currentRect.ca + currentRect.va;
		currentRect.color = "rgba(" + currentRect.cr + ", " + currentRect.cg + ", " + currentRect.cb + ", " + currentRect.ca + ")";
		if ((currentRect.ca < 0.05) || (currentRect.ca > 0.5))
		{
			currentRect.va = -currentRect.va;
		}
	}
	
	//	Set timeout for next refresh...
	window.setTimeout("pm_refreshBackground()", 150);
}


//	Fading

function pm_setOpacity(object, opacity)
{
	if ((opacity >= 0) && (opacity <=100) && (object != null))
	{
		opacity = (opacity == 100 ? 99.999 : opacity);

		//	Safari 1.2, Firefox, Mozilla, CSS3...
		object.style.opacity = opacity / 100;

		//	Old Mozilla, Firefox...
		object.style.MozOpacity = opacity / 100;

		//	Old Safari, Konqueror...
		object.style.KHTMLOpacity = opacity / 100;

		//	IE/Win...
		object.style.filter = "alpha(opacity:"+opacity+")";
	}
}


function pm_setOpacityForId(objectId, opacity)
{
	if (document.getElementById)
	{
		object = document.getElementById(objectId);
		pm_setOpacity(object, opacity);
	}
}

	
//	Album covers
	
	
function pm_initAlbums()
{
	albums = new Array();
	index = -1;
	albums[index++] = "afi_december_underground.jpg";
	albums[index++] = "all_saints_pure_shores.jpg";
	albums[index++] = "arcade_fire_neon_bible.jpg";
	albums[index++] = "bjork_earth_intruders.jpg";
	albums[index++] = "bjork_homogenic.jpg";
	albums[index++] = "bjork_medulla.jpg";
	albums[index++] = "bjork_selmasongs.jpg";
	albums[index++] = "bjork_vespertine.jpg";
	albums[index++] = "bjork_volta.jpg";
	albums[index++] = "black_eyed_peas_elephunk.jpg";
	albums[index++] = "black_eyed_peas_monkey_business.jpg";
	albums[index++] = "black_kids_partie_traumatic.jpg";
	albums[index++] = "blur_think_tank.jpg";
	albums[index++] = "craig_armstrong_as_if_to_nothing.jpg";
	albums[index++] = "craig_armstrong_the_quiet_american.jpg";
	albums[index++] = "crystal_castles_crystal_castles.jpg";
	albums[index++] = "css_donkey.jpg";
	albums[index++] = "cut_chemist_whats_the_altitude.jpg";
	albums[index++] = "daddy_yankee_cartel.jpg";
	albums[index++] = "dave_matthews_some_devil.jpg";
	albums[index++] = "dave_matthews_stand_up.jpg";
	albums[index++] = "fischerspooner_odyssey.jpg";
	albums[index++] = "foals_antidotes.jpg";
	albums[index++] = "foo_fighters_echoes.jpg";
	albums[index++] = "get_cape_find_the_time.jpg";
	albums[index++] = "get_cape_keep_singing_out.jpg";
	//	albums[index++] = "get_cape_searching.jpg";
	albums[index++] = "goldfrapp_supernature.jpg";
	//	albums[index++] = "guster_satellite.jpg";
	albums[index++] = "gwen_stefani_love_angel_music_baby.jpg";
	albums[index++] = "gwen_stefani_the_sweet_escape.jpg";
	albums[index++] = "hadouken_declaration_of_war.jpg";
	//	albums[index++] = "hadouken_music_for_an_accelerated_culture.jpg";
	albums[index++] = "harry_g_williams_the_number_23.jpg";
	albums[index++] = "jimmy_eat_world_futures.jpg";
	albums[index++] = "keane_hopes_and_fears.jpg";
	albums[index++] = "keane_perfect_symmetry.jpg";
	albums[index++] = "keane_under_the_iron_sea.jpg";
	albums[index++] = "land_of_talk_drawn.jpg";
	albums[index++] = "linkin_park_reanimation.jpg";
	albums[index++] = "lykke_li_youth_novels.jpg";
	albums[index++] = "madonna_american_life.jpg";
	albums[index++] = "madonna_bedtime_stories.jpg";
	albums[index++] = "madonna_confessions_on_a_dancefloor.jpg";
	albums[index++] = "madonna_evita.jpg";
	albums[index++] = "madonna_hard_candy.jpg";
	albums[index++] = "madonna_music.jpg";
	albums[index++] = "maroon_5_it_wont_be_soon_before_long.jpg";
	albums[index++] = "massive_attack_100th_window.jpg";
	albums[index++] = "massive_attack_mezzanine.jpg";
	albums[index++] = "massive_attack_protection.jpg";
	albums[index++] = "natasha_bedingfield_nb.jpg";
	albums[index++] = "natasha_bedingfield_unwritten.jpg";
	albums[index++] = "nine_black_alps_everything_is.jpg";
	albums[index++] = "no_doubt_hella_good.jpg";
	albums[index++] = "no_doubt_hey_baby.jpg";
	albums[index++] = "no_doubt_rock_steady.jpg";
	albums[index++] = "oasis_familiar_millions.jpg";
	albums[index++] = "oasis_heathen_chemistry.jpg";
	albums[index++] = "oasis_standing_on_the_shoulder_of_giants.jpg";
	albums[index++] = "patrick_wolf_magic_position.jpg";
	albums[index++] = "patti_smith_gung_ho.jpg";
	albums[index++] = "perry_farrell_satellite_party.jpg";
	//	albums[index++] = "pink_feel_good_time.jpg";
	//	albums[index++] = "placebo_album.jpg";
	albums[index++] = "pussycat_dolls_when_I_grow_up.jpg";
	albums[index++] = "shakira_oral_fixation_vol_2.jpg";
	//	albums[index++] = "spice_girls_spice.jpg";
	//	albums[index++] = "spice_girls_spiceworld.jpg";
	albums[index++] = "sugababes_one_touch.jpg";
	albums[index++] = "the_feeling_join_with_us.jpg";
	albums[index++] = "the_feeling_twelve_stops_and_home.jpg";
	albums[index++] = "the_rascals_rascalize.jpg";
	albums[index++] = "the_script_the_script.jpg";
	albums[index++] = "the_teenagers_reality_check.jpg";
	albums[index++] = "the_veronicas_when_it_all_falls.jpg";
	albums[index++] = "u2_best_of_1990_2000.jpg";
	albums[index++] = "u2_kiss_me_kill_me.jpg";
	albums[index++] = "u2_pop.jpg";
	albums[index++] = "william_orbit_hello_waveforms.jpg";	
}
	
function pm_randomAlbum(inDivId, inInitialDelay, inFadeIncrement, inFadeDelay, inHoldDelay)
{
	//	pick a random number for the album image...
	now = new Date();
	seed = now.getSeconds();
	index = Math.floor(Math.random(seed) * albums.length);
	imageObject = document.getElementById("album_placeholder_front");
	pm_setOpacity(imageObject, 0);
	imageSource = "/_resources/images/albums_splash/" + albums[index];
	imageObject.src = imageSource;
	
	window.setTimeout("pm_fadeInAlbum('album_placeholder_front', 0, " + inFadeIncrement + ", " + inFadeDelay + ");", inInitialDelay);
}


function pm_fadeInAlbum(objectId, opacity, inFadeIncrement, inFadeDelay)
{
	if (document.getElementById)
	{
		object = document.getElementById(objectId);
		if (opacity <= 100)
		{
			//	keep fading up image...
			pm_setOpacity(object, opacity);
			opacity += inFadeIncrement;
			window.setTimeout("pm_fadeInAlbum('" + objectId + "', " + opacity + ", " + inFadeIncrement + ", " + inFadeDelay + ")", inFadeDelay);
		}
		else
		{
			//	swap this into back placeholder...
			placeholderObject = document.getElementById("album_placeholder_back");
			placeholderObject.src = object.src;
			
			//	reset the opacity on the foremost placeholder...
			pm_setOpacity(object, 0);
			
			//	and repeat...
			pm_randomAlbum("album", 2000, 1, 10, 700);
		}
	}
}
