/* UTILITY FUNCTIONS */
function ifEnter(event,callback) {
	if(event.keyCode == 13) { callback(); }
}

var Utils = new function() {
	this.param = function(key, default_) {
		if (default_===null) default_="";
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		var qs = regex.exec(window.location.href);
		if(qs === null) return default_;
		else return qs[1];
	};
};

/* ARRAY COLLECTION CLASS */
function ArrayCollection(source) {
	this.rawArray = source;
	if(this.rawArray == null) {
		this.rawArray = [];
	}
	this.addRange = function(array) {
		this.rawArray = this.rawArray.concat(array);
	};
	this.addItem = function(item) {
		this.rawArray.push(item);
	};
	this.length = function() {
		return this.rawArray.length;
	};
	this.getRandomItem = function() {
		return this.rawArray[Math.floor(Math.random() * this.rawArray.length)];
	};
}

function OnokoCollection(source) {
	var superclass = new ArrayCollection(source);
	return superclass;
}

/* ANALYTICS LIBRARIES */
var _gaq = _gaq || [];
var Analytics = new function() {
	this.init = function(analytics) {
		_gaq.push(['_setAccount', analytics]);
		_gaq.push(['_trackPageview']);
		(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		})();
	};
	this.trackEvent = function(category, action, label, value) {
		if (label) {
			_gaq.push(['_trackEvent',category,action,label,value]);
		} else {
			_gaq.push(['_trackEvent',category,action]);
		}
	};
}

/* FACEBOOK LIBRARIES */
var Facebook = new function() {
	this.user = null;
	this.applicationID = '';
	this.init = function(appid) {
		if ($('#fb-root').length == 0) {
			$('body').append($('<div id="fb-root"></div>'));
		}
		if (window.location.hostname == '127.0.0.1') {
			window.location.href = window.location.href.replace('127.0.0.1','localhost');
			return;
		} else if (window.location.hostname == 'localhost') {
			this.applicationID = '168240883245486';
		} else {
			this.applicationID = appid;
		}
		FB.init({appId:this.applicationID,cookie:true,status:true,xfbml:true});
	};
	this.login = function(permissions,callback,redirectMode) {
		if(redirectMode === null) {
			redirectMode = false;
		}
		FB.getLoginStatus(function(response) {
			if(response.session){
				callback(true);
			}else{
				if(redirectMode){
					var redirect_url = escape(window.location);
					var auth_url = 'https://graph.facebook.com/oauth/authorize?client_id=' + Facebook.applicationID + "&redirect_uri=" + redirect_url + "&cancel_url=" + redirect_url + "&scope=" + permissions;
					window.location.href = auth_url;
				}
				else{
					FB.login(function(response) {
						if (response.authResponse) {
							callback(true);
						}
						else{
							callback(false);
						}
					},{scope: permissions});
				}
			}
		});
	};
	this.fql = function(query, callback) {
		FB.api({method: 'fql.query',query: query},callback);
	};
};

var AlbumManager = new function() {
	this.albumID = '';
	this.ensureAlbum = function(albumName,description,callback){
		if(AlbumManager.albumID === ""){
			Facebook.fql("SELECT object_id,name FROM album WHERE owner = me() AND name = '" + albumName + "'",function(results){
				if(results.length === 0){
					var album = {name:albumName,message:description};
					FB.api('/me/albums', 'post', album, function(response) {
					  if (!response || response.error) {
						  callback(false);
					  } else {
						  AlbumManager.albumID = response.id;
						  callback(true);
					  }
					});
				}
				else{
					AlbumManager.albumID = results[0].object_id;
					callback(true);
				}
			});
		}
		else{
			callback(true);
		}
	};
	this.copyPhoto = function(url,caption,callback){
		var photo = {
			url:url,
			link:url,
			message:caption
		};
		FB.api('/' + AlbumManager.albumID + '/photos', 'post', photo, function(response) {
			if(!response || response.error){
				callback(false,0);
			}
			else{
				callback(true,response.id);
			}
		});
	};
};
