

String.prototype.startsWith = function(str)
		{return (this.match("^"+str)==str)};
		
String.prototype.endsWith = function(str)
		{return (this.match(str+"$")==str)}


/*******************************************************/

$(document).ready(function () {
	manageNavigationBar();
	initBrowsePage();
	initProjectsPage();
	initLightBox();
	registerDailyPhoto();
	
	setSectionHeader();
});

/*********************************************************/


function appearNavigationBar() {
	var fadeIn  = function() {
					$("#inner-navigation-bar").fadeIn("slow");
					}
	
	setTimeout(fadeIn, 1000);
}


function setSectionHeader() {
	var title = $("#section-header-hidden").attr("innerHTML");
	$("#section-header").attr("innerHTML",title);
	if (title==null || title.length==0) $("#section-header").hide();
}

function initBrowsePage() {
	//if ($("#BROWSE-PAGE").length==0) return;
	
	var idPattern = /.+showimage=([0-9]+).*/;
	
	var items = $('#thumbnail-container a');
	for (var i = 0; i < items.length; i++) {
		var item = $(items).get(i);
		var imageId = $(item).attr("href").replace(idPattern,"$1");
		var src= $(item).find("img").attr("src").replace("thumbnails/thumb_", "images/");
		$(item).attr("href", src);
		$(item).attr("imageId", imageId);
		var title = $(item).attr("title");
		if (title!=null && title.toLowerCase().indexOf(".jpg")>0) var title = $(item).attr("title","[untitled]");
	}
	
	$('#categorylist li a[title^="_"]').parent().remove();
	// Manage tag button:
	if($("#BROWSE-PAGE").length>0 && document.location.href.indexOf("category")<0) {
		$('#tags-button').fadeIn("slow");

	}
}

function initProjectsPage() {
	if ($("#PROJECTS-PAGE").length>0)  {
	    var boxes = $('.project-box');
		for (var i = 0; i < boxes.length; i++) {
			var box = $(boxes).get(i);
			var src= $(box).find("input[type=hidden]").attr("value");
			if (src!=null && src!="") {
				$(box).find('img').wrap('<a href="'+src+'" />');
				$(box).find('.project-description').append('<a href="'+src+'" > See the gallery </a>');
			}
		}
		
	}
}

function manageNavigationBar() {
  var home ="<a href='/'>home</a> <span> &nbsp; &bull; &nbsp;</span> ";
  if ($("#IMAGE-PAGE").length>0)  {
		$("#inner-navigation-bar").hide();
		$("#inner-navigation-bar").append($("#menu-items"));
		$("#footer-info").css("border","none");
		
		$("#img-border").addClass("home-splash-image");
		
  } else {
		$("#menu-items").prepend($(home));	
		$("#navigation-bar").show();	
  }
  
  $("#footer-info").show();
}



function initLightBox() {
	$('#thumbnail-container a').lightBox();
	}


//********************* UTILS ***************************/

function getPage(pageName, target, callback) {
	$.ajax( {
		type :"POST",
		async :"TRUE",
		dataType :"text/html",
		url :pageName,
		data :"",
    	error: function(){
        	showMessage("Sorry, unable to load the requested content.",target);
    	},
		success : function(response) {
			callbackGetPage(target, response);
			if (callback!=null) callback();
		}
	});
}



function callbackGetPage(target, content) {
	$(target).attr("innerHTML", content);
}


/**************** light-opinion.com Integration ********************/

// http://www.light-opinion.com/latest_image.php
// {"id","datetime","headline","image"}


function registerDailyPhoto() {
	try {
		if (latestLOImage!=null) {
		
			$("#daily-photo").attr("href","http://www.light-opinion.com/images/"+latestLOImage.image);
			var caption= latestLOImage.headline + ' <br><span style="font-size:smaller;font-weight:normal">posted @ light-opinion.com on ' +latestLOImage.datetime+ ' </span>'
			$("#daily-photo").attr("title",caption);
			$("#daily-photo").lightBox();
		}
	} catch (e) { }
}

/*******************************************************************/

function hideCumulus() {
$("#cumulus-overlay").fadeOut();
$("#cumulus-container").fadeOut();
}

function tagCloudCumulus() {	
	$("#cumulus-overlay").fadeIn("slow");  
	
	if ($("#tag_cloud").length==0) return;
	$("#tag_cloud").find("a").removeAttr('class');
	$("#tag_cloud").find("a").removeAttr('style');
	
	var tags =$("#tag_cloud").attr("innerHTML");
	
	tags=tags.toLowerCase();
	tags= tags.replace(/<a/g,'<a style="9" ');
	tags= tags.replace(/&amp;/g,'%26');
	tags= tags.replace(/&/g,'%26');
	tags= tags.replace(/"/g,'\'');
	
	var so = new SWFObject("/graphics/tagcloud.swf", "tagcloud", "600", "400", "7", "#ffffff");

	so.addVariable("tcolor", "0x555555");
	so.addVariable("mode", "tags");
	so.addVariable("distr", "true");
	so.addVariable("tspeed", "100");
	so.addVariable("tagcloud", "<tags>"+tags+"</tags>");
	so.write("flashcontent");
	$("#cumulus-container").fadeIn("slow"); 
}
