var LoaderAnim = "<DIV ID='LoaderBlog'><CENTER><IMG SRC='a/loadinfo.net.gif'><BR><SPAN STYLE='font-variant:small-caps;'>loading</SPAN></CENTER></DIV>";
var Page = "";

//	--------------------------------------------------
$(document).ready(function() {
//	--------------------------------------------------
	window.fbAsyncInit = function() {
		FB.init({appId: '129374933803323', status: true, cookie: true, xfbml: true});
		FB.Event.subscribe('auth.login', function(response) {login();});
		FB.Event.subscribe('auth.logout', function(response) {logout();});
		FB.getLoginStatus(function(response) {
			if (response.session) {login();}
		});
	};(function() {
		var e = document.createElement('script');
		e.type = 'text/javascript';
		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
		e.async = true;
		document.getElementById('fb-root').appendChild(e);
	}());

	try {Page = getValue("page");} catch (err) {}
	if (Page == "") {
		try {Page = getValue("Page");} catch (err) {}
	}

	if (Page == "") {
		doBlog();
	} else {
		doPage();
	}

	doVideo();
	doFlickr();
	//doTweets('Me');
	//doFacebook();
});

//  ==================================================
//	--  Wordpress Blog Reader -   - AndyJ May 2011  --
//	--------------------------------------------------
function doBlog() {
//	--------------------------------------------------
	var txM = $('#txMonth').val();
	var txY = $('#txYear').val();
	var txS = $('#txSearch').val();

	var txP = "";
	try {
		txP = getValue("p");
	} catch (error) {}

	$('#blog-container').html(LoaderAnim);

	if ( txP == "" ) {
		var feedURL = "xml/blog_echo.php?txM=" + txM + "&txY=" + txY + "&txS=" + txS;
	} else {
		var feedURL = "xml/blog_echo.php?txP=" + txP + "&withoutcomments=1";
	}

	$.get(feedURL, function(theXML) {

		var Content = "";

		$('entry',theXML).each(function(i){

			var blog_link = 	$(this).find('id').text();
			var blog_title = 	$(this).find('title').text();
			var blog_body = 	$(this).find('content').text();
			var blog_date = 	$(this).find('published').text();
				blog_date = 	blog_date.substr(0,10);

			var Admin = "<a HREF='/blog/wp-admin/post.php?post=" + txP + "&action=edit'>Edit</a>";
			// /blog/wp-admin/post.php?post=3031&action=edit

			Content+= "<DIV CLASS='blog-entry-head'>" + blog_date + " : " + blog_title + "</DIV>";
			Content+= "<DIV>" + blog_body + "<BR></DIV>";

		});

		$('#blog-container').html(Content);

	});
}
function blogEnter( evt, frm ) {
	var keyCode = null;
	if(evt.which) {keyCode = evt.which;} else if (evt.keyCode) {keyCode = evt.keyCode;}
	if( keyCode == 13 ) {
		$('#BlogButton').click();
		return false;
	}
	return true;
}



// ==============================================
// --  YouTube Feed Reader - AndyJ - Feb 2011  --
// ==============================================
function doVideo() {
// ==============================================

	$('#video-container').html(LoaderAnim);

	$.getJSON('http://gdata.youtube.com/feeds/users/andyjpartridge/uploads?alt=json-in-script&callback=?&max-results=6', function(data) {

		var Content = "<A HREF='http://youtube.com/andyjpartridge/'>Visit my YouTube Channel</A>";
		Content += "<BR><CENTER>";

		$.each(data.feed.entry, function(i, item) {

			var yt_url = item['media$group']['media$content'][0]['url'];
			var yt_thumb = item['media$group']['media$thumbnail'][0]['url'];
			var yt_title = item['title']['$t'];

			Content += ""
					+	"<A HREF='" + yt_url + "' TITLE='" + yt_title + "'>"
					+	    "<IMG CLASS=VideoImage BORDER=1 SRC='" + yt_thumb + "'>"
					+	"</A>";
		});

		Content += "</CENTER>";

		$('#video-container').html(Content);
	});

}

// ==============================================
function doFlickr() {
// ==============================================
	$('#flickr-container').html(LoaderAnim);

	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=37642540@N04&lang=en-us&format=json&jsoncallback=?", function(data){

		var Content = "<A HREF='http://flickr.com/andyjpartridge/'>Visit me on Flickr</A>";
		Content += "<BR><CENTER>";

		$.each(data.items, function(i,item){

			if (i <6) {
				Content += "<SPAN><a href='" + item.link + "' BORDER=0>";
				Content += "<IMG CLASS='flickr-img' BORDER=0 SRC='"+ item.media.m + "'></a></SPAN>&nbsp;";
			}

		});

		Content += "</CENTER>";

		$('#flickr-container').html(Content);
	});

}


// =====================
function doTweets(Qry) {
// ---------------------
	var q="andyj72";
	if (Qry == "From") 	{var q="from:andyj72";}
	if (Qry == "To") 	{var q="to:andyj72";}

	var options = 		{query: q}
	$('#twitter-container').empty();
	$('#twitter-container').tweets(options);
}


// =====================
function doFacebook() {
// ---------------------
	//$('#facebook-container').facebook();
}


// -----------------------
function dump(arr,level) {
// -----------------------
//USE: $('#aj-dump').append('<PRE>' + dump(data) + '</PRE>');
// -----------------------
	var dumped_text = "";
	if(!level) level = 0;
		var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
		if(typeof(arr) == 'object') {
		for(var item in arr) {
			var value = arr[item];
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else {	dumped_text = "===>"+arr+"<===("+typeof(arr)+")";}
	return dumped_text;
}



// -------------------------
function getValue(varname) {
// -------------------------
	var url = window.location.href;
	var qparts = url.split("?");	if (qparts.length == 0) {return "";}
	var query = qparts[1];
	var vars = query.split("&");
	var value = "";

	for (i=0;i<vars.length;i++) {
		var parts = vars[i].split("=");
		if (parts[0] == varname) {
			value = parts[1];
		break;
		}
	}
	value = unescape(value);
	value.replace(/\+/g," ");

	return value;
}


// ====================
// -- Facebook Stuff --
// ====================
function login(){
// ====================
	FB.api('/me', function(response) {
		document.getElementById('login').style.display = "block";
		document.getElementById('login').innerHTML = "Hi <font color='#9080c0'>" + response.name + "</font>";
	});
}
function logout(){
	document.getElementById('login').style.display = "none";
}
function fqlQuery(){
	var Output = '';
	FB.api('/me', function(response) {

		for(var propertyName in response) {
			Output += propertyName + ': ' + response[propertyName] + "<br />";
			// propertyName is what you want
			// you can get the value like this: myObject[propertyName]
		}

		var ProfilePic = response.name;
		ProfilePic = ProfilePic.split(' ').join('');
		Output = Output + '<img src="http://graph.facebook.com/' + ProfilePic + '/picture" alt="" />' + "<br />";

		document.getElementById('name').innerHTML = Output;
	});
}



//  =======================================
//	--  Page Reader -  - AndyJ May 2011  --
//	---------------------------------------
function doPage() {
//	---------------------------------------

	$('#blog-container').html(LoaderAnim);

	var feedURL = "xml/page_echo.php?page=" + Page;

	$.get(feedURL, function(theXML) {


		//Content+= "<DIV>" + theXML + "<BR></DIV>";


		$('#blog-container').html(theXML);




	});
}

