var videoUpdate = function(url, param) {
	jQuery('.video-listing').html('<img src="../rss/ajax-loader.gif" border="0" style="margin:30px;" />');

	if (!url) {
		url = '../rss/youtube.php';
	}
	if (!param) {
		param = {};
	}
	jQuery.getJSON(url, param, function(json){
			if (json.success) {
				var html = [];
				html.push('<br /><b>Categories:</b><br /><br /><a href="javascript:;" onclick="videoUpdate();">All</a>');
				for (var categoryIndex in json.categories) {
					html.push('<a href="javascript:;" onclick="videoUpdate(false, {\'category\': \'' + json.categories[categoryIndex] + '\'});">' + json.categories[categoryIndex] + '</a>');
				}
				html.push('<br />');
				jQuery('.video-category').html(html.join('<br />'));
				html = [];
				
				//html.push('<div class="video-items">');
                html.push('<div id="js-container">');
				var index = 0;
				for (var itemIndex in json.items) {
					var item = json.items[itemIndex];
					item.duration = parseInt(item.duration, 10);
					var min = Math.floor(item.duration / 60);
					var second = item.duration - min * 60;
					/*
					html.push('<div class="video-item"><table cellspacing="0" cellpadding="0" border="0"><tr>');
					html.push('<td width="140" valign="top">');
					html.push('<a href="#preview" alt="' + item.title + '" onclick="videoDisplay(\'' + item.id + '\');">');
					html.push('<img src="' + item.thumbnail + '" border="0" />');
					html.push('</a>');
					html.push('<br />Length: ' + min + '\'' + second + '\"</td>');
					html.push('<td width="256" valign="top">');
					html.push('<div><a href="' + item.link + '" target="_blank">' + item.title + '</a><br />' + item.datetime + '</div>');
					html.push('<div><br />' + item.content.replace(/\n/g, "\n<br />") + '</div>');
					html.push('</td></tr></table></div>');

					if (index % 2 == 1) {
						html.push('<div class="video-separator"></div>');
					}
                    */
                    html.push('<div class="js-image" rank="' + item.score + '">');
                    html.push('<a rel="prettyPhoto" href="' + item.link + '" target="_blank"><img class="js-small-image" src="' + item.thumbnail + '"/></a>');
                    html.push('<div class="js-small-caption">');
                    html.push('<span>' + item.title + '</span>');
                    html.push('</div>');
                    html.push('<div class="js-overlay-caption-content">');
                    html.push('<h4><a href="#preview" alt="' + item.title + '" onclick="videoDisplay(\'' + item.id + '\');">' + item.title + '</a></h4>');
                    html.push('<p>Length: ' + min + '\'' + second + '\"<br />' + item.datetime + '</p>');
					html.push('<p>' + item.content.replace(/\n/g, "\n<br />") + '</p>');
                    html.push('</div>');
                    html.push('</div>');
                    
					index++;
				}

				html.push('</div>');

				/*
				html.push('<script type="text/javascript" charset="utf-8">');
				html.push('$(document).ready(function(){');
				html.push('$("a[rel^=\'prettyPhoto\']").click(function() { ');
				html.push('this.prettyPhoto({ hideflash: true });');
				html.push('});');
				html.push('});');
				html.push('</script>');

				html.push('<script type="text/javascript" charset="utf-8">');
				html.push('$(document).ready(function(){');
				html.push('$("a[rel^=\'prettyPhoto\']").prettyPhoto({ hideflash: true });');
				html.push('});');
				html.push('</script>');
				*/
	

				/*
                if (json.max > json.length) {
					html.push('<div class="video-nav">');
					var begin = json.start + 1;
					var end = ((json.next > json.max) ? json.max : json.next);
					if (json.url_previous != '') {
						html.push('<a href="javascript:;" onclick="videoUpdate(\'' + json.url_previous + '\');">Previous</a> | ');
					}
					html.push('Video ' + begin + ' - ' +  end + ' out of ' + json.max);
					if (json.url_next != '') {
						html.push(' | <a href="javascript:;" onclick="videoUpdate(\'' + json.url_next + '\');">Next</a>');
					}
					html.push('</div>');
				}
                */
				jQuery('.video-listing').html(html.join(''));
                jQuery('#js-container').jsquares();
				
				var mh = 0, mt = 0;
				jQuery('.js-image').each(function(){
					var top = parseInt(jQuery(this).css('top'), 10);
					if (top > mt) {
						mt = top;
						mh = top + jQuery(this).height();
					}
				});
				jQuery('#js-container').height(mh);
			} else {
				jQuery('.video-listing').html('<b>Error loading viedo list.</b><br />' + json.message).css({"text-align":"center","height":"200px","color":"red"});
			}
		});
};
var videoDisplay = function ( video_id ) {
	jQuery('.video-preview .preview').html('<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/' + video_id + '&hl=en_US&fs=1&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + video_id + '&hl=en_US&fs=1&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>');
};

var albumUpdate = function () {
	jQuery('.album-listing').html('<img src="../rss/ajax-loader.gif" border="0" style="margin:30px;" />');

	var url = '../rss/picasa.php';
	jQuery.getJSON(url, {}, function(json){
			if (json.success) {
				var html = [];
				html.push('<div class="album-items">');
				var index = 0;
				var firstAlbum = '';
				for (var itemIndex in json.items) {
					var item = json.items[itemIndex];
					if (parseInt(item.num_photos, 10) > 0) {
						if (firstAlbum == '') {
							firstAlbum = item.name;
						}

						html.push('<div class="album-item">');
						html.push('<a href="javascript:;" alt="' + item.title + '" onclick="photoUpdate(\'' + item.name + '\');">');
						html.push('<img src="' + item.thumbnail + '" border="0" />');
						html.push('</a>');
						html.push('<br /><a href="' + item.link_html + '" target="_blank">' + item.title + '</a>');
						html.push('<br />' + item.num_photos + ' photo(s)');
						html.push('<br />' + item.datetime);
						html.push('</div>');

						if (index % 2 == 1) {
							html.push('<div class="album-separator"></div>');
						}
						index++;
					}
				}
				if (index == 0) {
					html.push('<b>Comming soon.</b>');
				}
				html.push('</div>');

				jQuery('.album-listing').html(html.join(''));

				photoUpdate( firstAlbum );
			} else {
				jQuery('.album-listing').html('<b>Error loading album list.</b><br />' + json.message).css({"text-align":"center","height":"200px","color":"red"});
			}
		});
}
var photoUpdate = function (album) {
	if (!album) {
		return false;
	}

	jQuery('.photo-listing').html('<img src="../rss/ajax-loader.gif" border="0" style="margin:30px;" />');

	var url = '../rss/picasa.php?data=photo&album=' + album;
	jQuery.getJSON(url, {}, function(json){
			if (json.success) {
				var html = [];
				html.push('<div class="photo-items">');
				var index = 0;
				for (var itemIndex in json.items) {
					var item = json.items[itemIndex];

					html.push('<a href="' + item.content + '" title="' + item.title + '" rel="album_photos">');
					html.push('<img src="' + item.thumbnail + '" border="0" alt="' + item.title + '" />');
					html.push('</a>');

					if (index % 7 == 6) {
						//html.push('<div class="photo-separator"></div>');
					}
					index++;
				}
				html.push('</div>');

				jQuery('.photo-listing').html(html.join(''));

				$("a[rel=album_photos]").fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over'
				});
			} else {
				jQuery('.photo-listing').html('<b>Error loading photo list.</b><br />' + json.message).css({"text-align":"center","height":"200px","color":"red"});
			}
		});
}

jQuery(function(){
	if (jQuery('.video-preview').size() > 0) {
		// set preview area
		jQuery('.video-preview').append('<a name="preview"></a><div class="preview">Click on thumbnails to watch video</div>');
		// load listing
		videoUpdate();
	}
	if (jQuery('.album-listing').size() > 0) {
		// load listing
		albumUpdate();
	}
});

