/**
 * -----------------------------------------------------------------------------
 * URI Helpers
 * -----------------------------------------------------------------------------
 * used to complement a CodeIgniter MVC framework
 */

// path to base site directory
//var DOMAIN 			= 'http://localhost';
//var ROOTPATH		= '/crimsonlist/';
var DOMAIN			= 'http://www.crimsonlist.org';
var ROOTPATH		= '/';

var BASE_URL 		= DOMAIN + ROOTPATH;

// uri path to controllers
var SITE_URL = BASE_URL + "index.php/";

// 	forms document url from path
//	@param path	String
function appurl(path) { return BASE_URL + path; }

// 	forms url from codeigniter uri segments
//	@param components	String
function siteurl(components) { return SITE_URL + components; }

function assets(path) { return BASE_URL + "assets/" + path; }

/**
 * -----------------------------------------------------------------------------
 * Application Constants
 * -----------------------------------------------------------------------------
 */
var CSRF = "_nocsrf";
var COOKIE_DOMAIN = DOMAIN;
var COOKIE_PATH = ROOTPATH;
function nocsrf() { return $.cookie(CSRF); }

var Color = { one: "#900", two: "#EEE", three: "#FFF", four: "#000" }	// Enum =D

/**
 * -----------------------------------------------------------------------------
 * Helper Methods
 * -----------------------------------------------------------------------------
 * line-saving, time-saving, thought-saving functions
 */

/*
 * compute size of window; compatible across webkit and non-webkit browsers
 * @return	object
 */
var windowsize = function() {
	var width = 0, height = 0;

	if(!window.innerWidth) {
		if(!(document.documentElement.clientWidth == 0)) {
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		}
		else {
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
	}
	else {
		width = window.innerWidth;
		height = window.innerHeight;
	}
	return {w:width, h:height};
}

/**
 * @param fraction		float
 * @param width			int
 * @param height		int
 * @param foreground	String css background property
 * @param background	String css background property
 * @param borders		boolean
 * @return	Object JQuery
 */
function fracBox(fraction,width,height,fg,bg,html) {
	if (is_undef(fg) || is_empty(fg)) fg = Color.one;
	if (is_undef(bg) || is_empty(bg)) bg = Color.two;
	if (is_undef(html)) html = false;
	if (! html) {
		var container = $('<div></div>')
						.css({'padding':0,'margin':0,'background':bg})
						.width(width).height(height);
		var bar = $('<div></div>')
				.css({'margin':0,'background':fg})
				.height(height).width(width*fraction);
		container.append(bar);
		return container;
	}
	else {
		return "" +
		'<div style="padding:0;margin:0;background:'+bg+';width:'+width+'px;height:'+height+'px;">'+
			'<div style="margin:0;background:'+fg+';width:'+width*fraction+'px;height:'+height+'px;"></div>'+
		'</div>';
	}
}

/**
 * This functions takes a string containing an ISBN (ISBN-10 or ISBN-13) and 
 * @return Boolean	true if it's valid or false otherwise.
 */
function validateISBN(isbn) {
	if(isbn.match(/[^0-9xX\.\-\s]/)) return false;
	isbn = isbn.replace(/[^0-9xX]/g,'');
	if (isbn.length != 10 && isbn.length != 13) return false;
	
	checkDigit = 0;
	
	if(isbn.length == 10) {
	    for (var i = 0; i < 9; i++) checkDigit += (10 - i) * parseInt(isbn.charAt(i));
	    var t = isbn.charAt(9); 
	    checkDigit += parseInt((t == 'x' || t == 'X') ? 10 : t);
	    return checkDigit % 11 == 0;
	}
	else {
		for (var i = 0; i < 13; i+=2) checkDigit += parseInt(isbn.charAt(i));
	    for (var i = 1; i < 12; i+=2) checkDigit += 3 * parseInt(isbn.charAt(i));
	    return checkDigit % 10 == 0;
	}
}

/**
 * -----------------------------------------------------------------------------
 * Extend JQuery with postJSON
 * -----------------------------------------------------------------------------
 * works just like getJSON =)
 */
jQuery.extend({
   postJSON: function( url, data, callback ) {
      return jQuery.post(url, data, callback, "json");
   }
});

/**
 * creates a loading message using the Popup object
 * @param message
 * @param gif	url string of the loading image to be displayed
 * @return	the Popup object
 */
function loading_popup (message, gif) {
	var loader = new Popup();
	loader.style = { 'width':'230px' };
	loader.content =  '<img src="'+gif+'" alt="loading..." /><p>'+message+'</p>';
	loader.show();
	return loader;	
}


