// -- NAVIGATION ROUTINES ---------------------------
//
//
function gotoSelectedImageAlbum() {
	window.location = 'http://www.kingnitram.com/image.php?ig=' + document.imageAlbumsSelectForm.imageAlbumsSelect.value;
}

function gotoSelectedAudioAlbum() {
	window.location = 'http://www.kingnitram.com/audio.php?ag=' + document.audioAlbumsSelectForm.audioAlbumsSelect.value;
}


// -- FACEBOOK --------------------------------------
//
//
function onFbLogin() {
	var fbInfo = FB.Facebook.apiClient.users_getInfo( 
		FB.Facebook.apiClient.get_session().uid, 
		['uid', 'name', 'pic_square_with_logo'],
		renderFbCommentForm
	);
}

function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}


function fbRequestStreamPublish( message, mediaType, mediaData, action, actionLink, description, caption ) {
	var media;
	if( mediaType == 'image' ) {
		media = [{
			'type': 			'image',
			'src': 				mediaData.source,
			'href': 			mediaData.link
		}];
	}
	else if( mediaType == 'audio' ) {
		media = [{
			'type':				'mp3',
			'src': 				mediaData.source,
			'title':			mediaData.title,
			'artist':			mediaData.artist,
			'album':			mediaData.album
		}];
	}
	var attachments = {
		'name': 			'King Nitram & the Merry Universe!',
		'href': 			actionLink,
		'description': 		description,
		'caption': 			caption,
		'media': 			media
	};
	var actionLinks = [{ 
		"text": 			action, 
		"href": 			actionLink
	}];
	FB.Connect.streamPublish( message, attachments, actionLinks );//, null, null, null, handleStreamPublishResult );
}








// -- AJAX REQUESTS -------------------------------
//
//
// image data
function getHomepageImageDataFor( threadId ) {
	replaceDivWithRequest( 
		'imageContent', 
		'ajax_homepage_imageContent.php?itid='+threadId
	);
}
// image album data
function getImageAlbumDataForSelected() {
	replaceDivWithRequest( 
		'imageContent', 
		'ajax_homepage_imageContent.php?ig='+document.imageAlbumsSelectForm.imageAlbumsSelect.value 
	);
}


// audio data
function getHomepageAudioDataFor( threadId ) {
	replaceDivWithRequest( 
		'audioContent', 
		'ajax_homepage_audioContent.php?atid='+threadId 
	);
}
// audio album data
function getAudioAlbumDataForSelected() {
	var currentURL = new String( location.href );
	location = currentURL.replace(/\?.*/,'') + '?ag='+document.audioAlbumsSelectForm.audioAlbumsSelect.value;
	/*replaceDivWithRequest( 
		'audioContent', 
		'ajax_homepage_audioContent.php?ag='+document.audioAlbumsSelectForm.audioAlbumsSelect.value 
	);
	*/
}

// widget data
function getWidgetSocialDataFor( threadId ) {
	replaceDivWithRequest( 
		'social', 
		'ajax_widgetSocial.php?wtid='+threadId 
	);
}

/* my pure HTML way of playing audio -- gave it up for a flash audio player 
	- works so much nicer, fuck apple and their lack of iPhone flash support
function loadAndPlayMp3( audioFileLocation ) {
	var embedTagForMp3 = 
	'<input type="button" value="Stop Playing This Song" onclick="removeMp3()" />'+
	'<embed src="'+audioFileLocation+'" autostart="true" loop="false" volume="100" hidden="true">';
	$( '#audioPlayer' ).html( embedTagForMp3 );
}

function removeMp3() {
	$( '#audioPlayer' ).html( '&nbsp;' );
}
*/

// -- REPLACE A DIV WITH AJAX REQUEST -----------------------------------------
//
//
function replaceDivWithRequest( div, request ) {
	$.ajax({
		type: "GET",
		url: request,
		success: function( result ) {
			document.getElementById( div ).innerHTML = result;

			//$( '#'+div ).html( result );
			
			// re-render facebook items
			//FB.XFBML.Host.parseDomTree();
			onFbLogin();
		}
	 });
}
	
// -- SUBMIT A COMMENT --------------------------------------------------------
//
//
// render fb comment form
function renderFbCommentForm( userInfo ) {
	// ids for each comment form
	var commentForms = [{
			addCommentTemplateId: 'addImageCommentTemplate',
			fbAddCommentTemplateId: 'fbuAddImageCommentTemplate'
		},
		{	addCommentTemplateId: 'addAudioCommentTemplate',
			fbAddCommentTemplateId: 'fbuAddAudioCommentTemplate'
		},
		{	addCommentTemplateId: 'addWidgetCommentTemplate',
			fbAddCommentTemplateId: 'fbuAddWidgetCommentTemplate'
		}
	];
	for( var i=0; i < commentForms.length; i++ ) {
		var addCommentTemplate = document.getElementById( commentForms[i].fbAddCommentTemplateId );
		if( addCommentTemplate != null ) {
			addCommentTemplate = addCommentTemplate.innerHTML;
			addCommentTemplate = addCommentTemplate.replace(
				/\[FBUID\]/g,
				userInfo[0]['uid']
			);
			addCommentTemplate = addCommentTemplate.replace(
				/\[NAME\]/g,
				userInfo[0]['name']
			);
			addCommentTemplate = addCommentTemplate.replace(
				/\[AVATAR\]/g,
				userInfo[0]['pic_square_with_logo']
			);
			addCommentTemplate = addCommentTemplate.replace(
				/\[FB_THUMB\]/g,
				"<fb:profile-pic uid='loggedinuser' facebook-logo='true'></fb:profile-pic>"
			);
			addCommentTemplate = addCommentTemplate.replace(
				/\[FB_WELCOME\]/g,
				"<div style='font-size:13px;'>Welcome, <fb:name uid='loggedinuser' useyou='false'></fb:name>.</div>"+
				"<div style='font-size:11px;margin-bottom:5px;'>You are signed in with your Facebook account.</div>"
			);
			addCommentTemplate = addCommentTemplate.replace(
				/\[FB_NOTES\]/g,
				"<div style='font-size:11px;margin-top:8px;font-style:italic;'>Comments left here will NOT appear on your Facebook Wall<br />unless you grant permission afterwards. Thank you!</div>"
			);
			//alert( addCommentTemplate );
			document.getElementById( commentForms[i].addCommentTemplateId ).innerHTML = addCommentTemplate;
		}
	}
	// because this is XFBML, we need to tell Facebook to re-process the document 
	FB.XFBML.Host.parseDomTree();
}

// submit comment type for thread id
function submitCommentTypeForThreadId( type, threadId ) {
	// data
	var query = "?action=submitComment"+
		"&threadId="+document.getElementById( type ).threadId.value+
		"&uid="+document.getElementById( type ).uid.value+
		"&fbuid="+document.getElementById( type ).fbuid.value+
		"&name="+document.getElementById( type ).name.value+
		"&comment="+document.getElementById( type ).comment.value;

	// add guest attributes
	if( type == 'guestAddImageCommentForm' || type == 'guestAddAudioCommentForm' || type == 'guestAddWidgetCommentForm' ) {
		query += "&email="+document.getElementById( type ).email.value;
	}
	// add facebook attributes
	else {
		query += "&fbuid="+document.getElementById( type ).fbuid.value+
			"&avatar="+escape( document.getElementById( type ).avatar.value );
	}

	// web service
	$.ajax({
		type: "GET",
		url: '_scripts/api.php'+query,
		success: function( result ) {
			if( type == 'guestAddImageCommentForm' || type == 'fbuAddImageCommentForm' ) {
				getHomepageImageDataFor( threadId );
			}
			else if( type == 'guestAddAudioCommentForm' || type == 'fbuAddAudioCommentForm' ) {
				getHomepageAudioDataFor( threadId );
			}
			else if( type == 'guestAddWidgetCommentForm' || type == 'fbuAddWidgetCommentForm' ) {
				getWidgetSocialDataFor( threadId );	
			}
			
			// prompt to publish to facebook
			var comment = document.getElementById( type ).comment.value;
			var mediaData;
			var action = document.getElementById( type ).callbackAction.value;
			var actionLink = document.getElementById( type ).callbackURL.value;
			
			var description = document.getElementById( type ).mediaDescription.value;
			var caption = document.getElementById( type ).mediaCaption.value;
			
			if( type == 'fbuAddImageCommentForm' || type == 'fbuAddWidgetCommentForm' ) {
				mediaData = {
					source: document.getElementById( type ).mediaSource.value,
					link: actionLink
				};
				fbRequestStreamPublish( comment, 'image', mediaData, action, actionLink, description, caption );
			}
			else if( type == 'fbuAddAudioCommentForm' ) {
				mediaData = {
					source: document.getElementById( type ).mediaSource.value,
					title: document.getElementById( type ).mediaTitle.value,
					artist: document.getElementById( type ).mediaArtist.value,
					album: document.getElementById( type ).mediaAlbum.value
				};
				fbRequestStreamPublish( comment, 'audio', mediaData, action, actionLink, description, caption );
			}
			
		}
	 });
}






