/*
Script: Browser.Extras.js
	Extends the Window native object to include methods useful in managing the window location and urls.

License:
	http://www.clientcide.com/wiki/cnet-libraries#license
*/

//fake object:
gdata = { io: { handleScriptLoaded: function(data)
{ 

var myObject = JSON.decode(data); 
//console.log(data.feed.entry[0]);
} } };

$extend(Browser, {
	getHost:function(url){
		url = $pick(url, window.location.href);
		var host = url;
		if(url.test('http://')){
			url = url.substring(url.indexOf('http://')+7,url.length);
			if(url.test(':')) url = url.substring(0, url.indexOf(":"));
			if(url.test('/')) return url.substring(0,url.indexOf('/'));
			return url;
		}
		return false;
	},
	getQueryStringValue: function(key, url) {
		try { 
			return Browser.getQueryStringValues(url)[key];
		}catch(e){return null;}
	},
	getQueryStringValues: function(url){
		var qs = $pick(url, window.location.search, '').split('?')[1]; //get the query string
		if (!$chk(qs)) return {};
		if (qs.test('#')) qs = qs.substring(0, qs.indexOf('#'));
		try {
      if (qs) return qs.parseQuery();
		} catch(e){
			return null;
		}
		return {}; //if there isn't one, return null
	},
	getPort: function(url) {
		url = $pick(url, window.location.href);
		var re = new RegExp(':([0-9]{4})');
		var m = re.exec(url);
	  if (m == null) return false;
	  else {
			var port = false;
			m.each(function(val){
				if($chk(parseInt(val))) port = val;
			});
	  }
		return port;
	},
  redraw: function(element){
    var n = document.createTextNode(' ');
    this.adopt(n);
    (function(){n.dispose()}).delay(1);
    return this;
	}
});
window.addEvent('domready', function(){
	var count = 0;
	//this is in case domready fires before string.extras loads
	function setQs(){
		function retry(){
			count++;
			if (count < 20) setQs.delay(50);
		}; 
		try {
			if (!Browser.getQueryStringValues()) retry();
			else Browser.qs = Browser.getQueryStringValues();
		} catch(e){
			retry();
		}
	}
	setQs();
});

/*
Script: Hash.Extras.js
	Extends the Hash native object to include getFromPath which allows a path notation to child elements.

License:
	http://www.clientcide.com/wiki/cnet-libraries#license
*/

Hash.implement({
	getFromPath: function(notation) {
		var source = this.getClean();
		notation.replace(/\[([^\]]+)\]|\.([^.[]+)|[^[.]+/g, function(match) {
			if (!source) return;
			var prop = arguments[2] || arguments[1] || arguments[0];
			source = (prop in source) ? source[prop] : null;
			return match;
		});
		return source;
	},
	cleanValues: function(method){
		method = method||$defined;
		this.each(function(v, k){
			if (!method(v)) this.erase(k);
		}, this);
		return this;
	},
	run: function(){
		var args = $arguments;
		this.each(function(v, k){
			if ($type(v) == "function") v.run(args);
		});
	}
});

/*
Script: String.Extras.js
	Extends the String native object to include methods useful in managing various kinds of strings (query strings, urls, html, etc).

License:
	http://www.clientcide.com/wiki/cnet-libraries#license
*/
String.implement({
	stripTags: function() {
		return this.replace(/<\/?[^>]+>/gi, '');
	},
	parseQuery: function(encodeKeys, encodeValues) {
		encodeKeys = $pick(encodeKeys, true);
		encodeValues = $pick(encodeValues, true);
		var vars = this.split(/[&;]/);
		var rs = {};
		if (vars.length) vars.each(function(val) {
			var keys = val.split('=');
			if (keys.length && keys.length == 2) {
				rs[(encodeKeys)?encodeURIComponent(keys[0]):keys[0]] = (encodeValues)?encodeURIComponent(keys[1]):keys[1];
			}
		});
		return rs;
	},
	tidy: function() {
		var txt = this.toString();
		$each({
			"[\xa0\u2002\u2003\u2009]": " ",
			"\xb7": "*",
			"[\u2018\u2019]": "'",
			"[\u201c\u201d]": '"',
			"\u2026": "...",
			"\u2013": "-",
			"\u2014": "--",
			"\uFFFD": "&raquo;"
		}, function(value, key){
			txt = txt.replace(new RegExp(key, 'g'), value);
		});
		return txt;
	},
	cleanQueryString: function(method){
		return this.split("&").filter(method||function(set){
			return $chk(set.split("=")[1]);
		}).join("&");
	},
	findAllEmails: function(){
			return this.match(new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", "gi")) || [];
	}
});


/*
Script: Browser.Extras.setQueryStringValues.js
License:
	http://www.clientcide.com/wiki/cnet-libraries#license
*/

$extend(Browser, {
	setQueryStringValues: function(values){
		var base = $pick(window.location.href, window.location.search, '').split('?')[0]; //get the base string
		var qs = this.getQueryStringValues(); // get the query string
		var merged = $merge(qs, values);
		var query = '?';		
		for (key in merged) {
			query += '&' + key + '=' +merged[key]
		}
		return base + query;
	}
});






/*
	picasaViewer v1.0 - mooTools script to embed a picasa gallery in your website
	Lennart Pilon (http://pilon.nl)	
*/

window.addEvent('domready', function() {
var gdata={};
	var username	= 'australianskydive';

	/*
	The following values are valid for the thumbsize and imgmax query parameters and are embeddable on a webpage. 
	These images are available as both cropped(c) and uncropped(u) sizes by appending c or u to the size. As an 
	example, to retrieve a 72 pixel image that is cropped, you would specify 72c, while to retrieve the uncropped 
	image, you would specify 72u for the thumbsize or imgmax query parameter values.

	32, 48, 64, 72, 144, 160
	*/
	var thumbsize	= '160c';
	
	/*
	The following values are valid for the thumbsize and imgmax query parameters and are embeddable on a webpage. 
	These images are available as only uncropped(u) sizes by appending u to the size or just passing the size 
	value without appending anything.

	200, 288, 320, 400, 512, 576, 640, 720, 800

	*/
	var imgmax = '800';

	var url = (Browser.getQueryStringValue('albumid')) ? 'http://picasaweb.google.com/data/feed/base/user/'+ username +'/albumid/'+ Browser.getQueryStringValue('albumid') +'?category=photo&alt=json&callback=viewPhotoList&thumbsize=' + thumbsize +'&imgmax='+imgmax : 'http://picasaweb.google.com/data/feed/base/user/' + username + '?category=album&alt=json&callback=viewAlbumList&access=public&thumbsize=' + thumbsize;
	new Element('script', {'src': url}).inject($('photos'));	

/*
gdata.io.handleScriptLoaded=function(list){
//console.log(list);
};

//fake object:

= { io: { handleScriptLoaded: function(data)
{ 
console.log(data);
//JSONparser(data); 

} } };

*/

	var gdataURL ="http://gdata.youtube.com/feeds/base/users/Skydivecairns/uploads?alt=json-in-script&amp;v=2&amp;callback=viewVideoList&amp;orderby=published&amp;client=ytapi-youtube-profile";
	
//	new Element('script', {'src': url}).inject($('desktop'));	


// insert the script:
var s = document.createElement('script');
s.src = gdataURL;
document.getElementsByTagName('head')[0].appendChild(s);






function viewVideoList(list) {
//console.log(list);
}



});




function viewAlbumList(list) {
	
	href  = list.feed.author[0].uri.$t;
	user = list.feed.author[0].name.$t;
	
	$('navigate').adopt(new Element('a', {'href': href, 'html': 'To Picasa', 'title': 'View '+user+'\'s gallery at Picasa', 'class': 'picasalink'}));

	list.feed.entry.each(function(item) {
		var title	= item.title.$t;
		var description	= item.media$group.media$description.$t;
		//console.log(item);
		var thumb	= item.media$group.media$thumbnail[0].url;
	
		id_begin	= item.id.$t.indexOf('albumid/')+8;
		id_end		= item.id.$t.indexOf('?');
		var id		= item.id.$t.slice(id_begin, id_end);
		var href	= Browser.setQueryStringValues({'albumid': id});
		d			= item.published.$t;
		var date	= d.substr(8,2) + '-' + d.substr(5,2) + '-' + d.substr(0,4);

		$('photos').adopt(new Element('div', {'class': 'image-container',
		'events': {
        'click': function(){
            window.location.href=href;
        }}}).adopt(new Element('a', {'href': href, 'title': title}).adopt(new Element('img', {'src': thumb, 'alt': title}), new Element('h2', {'html': title})), new Element('p', {'html': description, 'picclass': 'desc'})));
	}); 
}

function viewPhotoList(list) {
	
	var album = list.feed.title.$t;
	var href  = Browser.setQueryStringValues({'albumid': ''});
	
	$('navigate').adopt(new Element('a', {'href': href, 'html': 'Back to the album list', 'class': 'backlink'}), new Element('h2', {'html': album}));
	
	list.feed.entry.each(function(item, size) {

		var title	= item.title.$t;
		var link	= item.media$group.media$content[0].url;
		size		= item.media$group.media$content[0].width;
		var thumb	= item.media$group.media$thumbnail[0].url;
		
		id_begin	= item.id.$t.indexOf('albumid/')+8;
		id_end		= item.id.$t.indexOf('?');
		var id		= item.id.$t.slice(id_begin, id_end);
		
		$('photos').adopt(new Element('a', {'href': link, 'title': title, 'class': 'lightbox', 'rel': 'lightbox-album'}).adopt(new Element('img', {'src': thumb, 'alt': title})));
	});

	//$$("a")
	$('photos').getElements('a').filter(function(el) {
		return el.href && el.firstChild && el.firstChild.src;
	}).slimbox({}, function(el) {	
		return [el.href, (el.title || el.firstChild.alt) + '<br /><a href="' + el.href + '">Album: '+ album +' </a>'];
	});
}
