/*	
	functions.js
	===============================================================
	Panoramica
	_______________________________________________________________
*/

/*	
	@ to_hex
	@ rgb_to_hex
	@ get_theme_color
  ---------------------------------------------------------------
	Gets an element's colour from the stylesheet.
	_______________________________________________________________
*/			

	function to_hex(N) 
	{
		// http://www.linuxtopia.org/online_books/javascript_guides/javascript_faq/rgbtoHex.htm		
	 if (N === null) 
	 {
		 return "00";
	 }
	 N = parseInt(N, 10); 
	 if (N === 0 || isNaN(N)) 
	 {
		 return "00";
	 }
	 N = Math.max(0,N); 
	 N = Math.min(N,255); 
	 N = Math.round(N);
	 return ("0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16));
	}		
	
	function rgb_to_hex(color)
	{	
		//console.debug( "rgb_to_hex('", color, "')");
	
		// FF formats the color as RGB, so we need to convert it to hex for sIFR
		if (color.indexOf('rgb') != -1)		
		{
			// rgb(193, 19, 30)
			var colors = color.replace('rgb(', '');
					colors = colors.replace(')', '');
					colors = colors.replace(' ', '');		
			
			// 193,19,30
			var colors_array = colors.split(',');
			var r = colors_array[0];
			var g = colors_array[1];						
			var b = colors_array[2];
	
			// http://www.linuxtopia.org/online_books/javascript_guides/javascript_faq/rgbtoHex.htm
			var hex_string = ('#' + to_hex(r) + to_hex(g) + to_hex(b));
			
			return (hex_string.toLowerCase());
		}
		else
		{			
			// IE already formats the color as hex
			return (color.toLowerCase());
		}
	}	

	function get_theme_colour(what)
	{				
		var theme_colour;
		
		if (what == 'bg')
		{			
			theme_colour = rgb_to_hex( glow.dom.get('#shell').css('background-color') ).substring(1); // substring strips leading '#'		
		}
		else if (what == 'fg')
		{
			theme_colour = rgb_to_hex( glow.dom.get('#soundcloud-player').css('border-top-color') ).substring(1); // substring strips leading '#'		
		}

		return theme_colour;			
	}
	
/*	
	@ embed_soundcloud_player
  ---------------------------------------------------------------
	color - uses theme link color:
	play button background (gradient), 
	unbuffered waveform foreground,
	buffered waveform background (gradient),
	hovered set track title background,
	info text link color
	
	Always #fff / grey??:
	text color,
	button foreground / icon color
	
	comments_color:
	not sure as no comments yet, could be the text colour, doesn't look the background color is affected
	
	theme_color:
	background color - uses page bg color	
	
	soundcloud_player is a global here so that we can control it via the SC API outisde of this function
	
	http://www.schillmania.com/projects/soundmanager2/doc/				

	Methods tested and working in FF3.5:
	soundcloud_player.movie.play()
	soundcloud_player.movie.stop()	
	soundcloud_player.movie.pause()	
	soundcloud_player.movie.prev()				
	soundcloud_player.movie.next() - can call multiple times to skip to a certain track to fake 'randomisation'
	soundcloud_player.movie.setVolume(50) - does not update slider tho
	
	soundcloud_player.movie.load(trackNum - 1)		
	_______________________________________________________________
*/		

	var soundcloud_player;

	function embed_soundcloud_player()
	{
		soundcloud_player = new glow.embed.Flash('http://player.soundcloud.com/player.swf', 
		'#soundcloud-player', 
		'9.0.124',
		{
			height: soundcloud_height,
			width: '99%', /* else last couple of px sometimes not rendered, hiding RHS of player */
			message: '', /* noflash - 'false' sans quotes also seems to work ok here */
			id: 'scPlayer1',
			params: {
				allowscriptaccess: 'always',
				bgcolor: get_theme_colour('bg'), /* bgcolor while loading */
				flashVars: {     
					url: soundcloud_url,
					auto_play: 'true',
					color: get_theme_colour('fg'),						
					download: 'false',			
					sharing: 'true',						
					show_artwork: 'false',						
					show_comments: 'false',
					show_playcount: 'false',
					theme_color: get_theme_colour('bg')				
				}
			}				
		});
		soundcloud_player.embed();
		
		// heading
		if (soundcloud_player.movie) // or, soundcloud_player.isSupported
		{
			glow.dom.create('<h3/>').text('Listen:')
			.insertBefore('#soundcloud-player');
		}
	}
	
	function soundcloud_player_play(track_number)
	{
		soundcloud_player.movie.load(track_number - 1);
	}
	
	
/*	
	move_footer()
	---------------------------------------------------------------
	Ensure that the footer bg image sits at the bottom of the viewport
	
	if (shell_liner_height != viewport_height) // < or >

	// <
	// main height is less than the browser height
	// so stretch the height of main to push the footer down.
	// increasing the height by the amount that the shell is shorter than the browser window		
	
	// >
	// main height is stretched to push footer down
	// this makes it too high when the window is resized down
	// so reduce the height by the amount that is now taller than the browser window		
	_______________________________________________________________
*/	
	
	function move_footer() 
	{
		if ( glow.dom.get('#shell').length > 0 )
		{
			glow.dom.get('body').addClass('stretched'); // remove noscript btm borders/extra padding below p.top, before calculating height
			
			// Push the footer down to the foot of the page, by setting a minimum height on the preceding div#main			
			var shell_liner_height = glow.dom.get('#shell > div > div').height();			

			// Push the footer down to the foot of the page, by setting a minimum height on the preceding div#main
			var viewport_height = glow.dom.get(window).height();

			var div_body = glow.dom.get('#body');
			
			if (shell_liner_height != viewport_height)
			{	
				new_height = ( div_body.height() + (viewport_height - shell_liner_height) );
				new_height_px = (new_height + 'px');
			
				if (glow.env.ie < 7)
				{						
					div_body.css('height', new_height_px);
				}
				else 
				{
					div_body.css('min-height', new_height_px);			
				}					
			}
		}
	}
	
/*	
	activate_faux_links()
	---------------------------------------------------------------
	Make elements with 'a blur' classname act like links
	_______________________________________________________________
*/		
	
	function activate_faux_links()
	{	
		var faux_links = glow.dom.get('.a');
		
		glow.events.addListener( faux_links, 'mouseover', 
			function () 
			{
				glow.dom.get(this).removeClass('blur');
				glow.dom.get(this).addClass('hover');				
			}
		);	
		
		glow.events.addListener( faux_links, 'mouseout', 
			function () 
			{
				glow.dom.get(this).removeClass('hover');
				glow.dom.get(this).addClass('blur');				
			}
		);					
	}
	
/*	
	request_theme_image_details()
	---------------------------------------------------------------
	Get theme details from Flickr and display
	Originally from Dan Smith - Life 
	_______________________________________________________________
*/		

	function request_theme_image_details(serverside_script)
	{
		create_theme_image_details_holder();		
		
		glow.net.get(serverside_script, {
			onLoad: 
				function(response) 
				{			
					// is selector syntax causing an error in IE8??
					glow.dom.get('#themeinfo > .section > .liner').html( response.text() ); // replace loading msg contents	
					
				},
			onError: 
				function(response) {
					// response.statusText());
					// is this causing an error in IE8??
					glow.dom.get('#status').text('Sorry, theme information not available - please check your internet connection or try again later. ');		
				}
		});			
	}
	
/*	
	create_theme_image_details_holder() - Glow
	---------------------------------------------------------------
	Get theme details from Flickr and display
	Originally from Dan Smith - Life
	_______________________________________________________________
*/			
	
	function create_theme_image_details_holder()
	{				
		glow.dom.create('<div/>').addClass('panel').attr('id', 'themeinfo' )
			.append(
				glow.dom.create('<h3/>')
				.append(
					glow.dom.create('<span/>' ).text('Theme image:' )
				)
			)
//			.append(
//				glow.dom.create('<p/>' ).addClass('note').text('Background info about this theme. ')
//			)
			.append(
				glow.dom.create('<div/>').addClass('section')
					.append(
						glow.dom.create('<div/>').addClass('liner')
						.append(
							glow.dom.create('<p/>').attr('id', 'status')
								.append(								
									glow.dom.create('<img/>').attr({ src : '/_resources/v2/ui/images/inline/animations/loader/gray_busy.gif', width : '29', height : '5', alt : 'Loading...' })
								)
								.append(
									glow.dom.create('<span/>').text('Loading theme details..')			
								)
								.append(							
									glow.dom.create('<div/>').addClass('clear')		
								)											
						)
					)			
			)			
			.appendTo( 
				glow.dom.get('#related') 
			)		
	}
	
/*	
	make_frames_clickable()
	---------------------------------------------------------------
	Make index .frames clickable, so that even empty frames
	(ie with no image) can be clicked	
	_______________________________________________________________
*/

	function make_frames_clickable() 
	{	
		glow.dom.get('#main > div > div').each(
			function()
			{
				var this_div = glow.dom.get(this);
				
				if ( this_div.hasClass('post') )
				{
					this_div.addClass('a blur');
					
					glow.events.addListener( this_div, 'click', 
						function () 
						{
							window.location = glow.dom.get(this_div).get('a').attr('href');
						}
					);
					
					glow.events.addListener( this_div, 'mouseover', 
						function () 
						{							
							window.status = glow.dom.get(this_div).get('a').attr('href');
						}
					);																	
					
					glow.events.addListener( this_div, 'mouseout', 
						function () 
						{
							window.status = '';
						}
					);						
				}
			}
		)
	}		