/*
 * A Cavallo Application code
 * by Bill Lanides
 * <wlanides AT drazium DOT com>
 */

App = {
	_currentPage: null,
	_currentFilename: null,

	/* settings for image fonts used by jquery.bfont plugin */
	fontSettings : {
			navMenu: {
				imgClass: "navImg",
				family: "CarnevaleeFreakshow.ttf",
				size: 13,
				color:'666666',
				bgcolor:'000000',
				useImg: true,
				imgAttr: 'base'
			},
			navMenuSelected: {
				imgClass: "navImg",
				family: "CarnevaleeFreakshow.ttf",
				size: 13,
				color:'FFCC35',
				bgcolor:'000000',
				useImg: true,
				imgAttr: 'on'
			},
			sectionTitle: {
				imgClass: "",
				family: "BleedingCowboys.ttf",
				size: 24,
				color:'FFCC35',
				bgcolor:'000000'
		},
		callout: {
				imgClass: "",
				family: "BleedingCowboys.ttf",
				size: 24,
				color:'66CCFF',
				bgcolor:'000000'
		}
	},
	inpFocus: function(el) {
		el.style.borderColor = "black";
		//el.select();
	},
	inpBlur: function(el) {
		el.style.borderColor = "#ababab";
	},

	/* build the markup editor interface */
	initEdit: function() {
			// display the content editing information of the current page
			App._currentFilename = STATE.page; //gup('page');

			// reset form to prevent stale content in editor
			$("#markUpForm").get(0).reset();

			// add the editor header information
			$("#precontent").prepend(' \
				<div class="control_bar"> \
					<span class="edit_label">Editing</span> <span id="edit_title"></span> \
					<span class="edit_label">File:</span> /main/content/<span id="edit_filename"></span> \
				</div> \
				<div class="control_bar"> \
					<span id="button_reload" class="button">Load</span> \
					<span id="button_save" class="button">Save As</span> \
					<span id="button_preview" class="button">Preview</span> \
				</div> \
			');

			$("#edit_title").html( App._currentPage.attr("title") );
			$("#edit_filename").html( App._currentFilename );

			// initialize markup editor
			$("#content").markItUp(mySettings);

			// init custom markup buttons
			$(".control_bar > .button").hover(
				function() {
					$(this).css("background-color","#D6D9C1");
				},
				function(){
					$(this).css("background-color", "");
				}
			);
			$("#button_reload").click( function(){
				$(".markItUp .load").mouseup();
				//miu.load(markItUp);
			});
			$("#button_save").click( function(){
				$(".markItUp .save").mouseup();
			});
			$("#button_preview").click( function(){
				$(".markItUpButton7").mouseup();
			});
	}
}

/* simple preload function */
function imgPreload() {
	var args = simplePreload.arguments;
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}

/* get url parameter */
function gup(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if (results == null) {
		return "";
	} else {
		return results[1];
	}
}

/* set url parameter * TODO: redo the big ugly */
function sup(name,value) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if(results == null && value) {  // add the url param
		/* HACK: ugly way to add url param */
		if(window.location.href.indexOf("?")>0) {
			return window.location.href + "&" + name + "=" + value;
		} else {
			return window.location.href + "?" + name + "=" + value;			
		}
	} else {
		if(value) { // change the currently set url param
			var newParam = results[0].substring(0,1) + name + "=" + value;
			return window.location.href.replace(regex, newParam);
		} else { // remove the url param
			// TODO: needs support for changing & to ? when necessary
			return window.location.href.replace(regex, "");
		}
	}
}

/* Initialize the application */
$(document).ready(function() {
	/* create the left side nav menu font images */
	$('ul#navigation .remote').bfont(App.fontSettings.navMenu).bind("focus", function(e) { this.blur() });
		
	/* hack(ish) image preload for the "on" state for of the nav links */
	$('ul#navigation .remote').each(function() {
		var onImg = $(this).attr("on");
		if(onImg) {
			var preloadImg = new Image();
			preloadImg.src = onImg;
		}
	});

	/* rewrite content links to work properly and set/style the current page */
	var current_page = STATE.page; //gup('page');
	$('a.remote').each(function(i){
		var page = this.href.substring( this.href.lastIndexOf("/")+1 );
		if(current_page && current_page == page) {
			/* highlight the current page in the nav */
			App._currentPage = $(this).bfont(App.fontSettings.navMenuSelected);
			/* set the section title to the current page title */
			var title = $(this).attr("title");
			if(title){
				$('#section_title').hide().bfont(App.fontSettings.sectionTitle, title).fadeIn();
			}
		}
		this.href = "index.php?page=" + page;
	});
	if(!App._currentPage) {
		App._currentPage = $("a.remote[@title='About']").bfont(App.fontSettings.navMenuSelected);
	}

	/*
	 * Edit Mode setup
	 */

	/* setup keyboard toggle (ctrl+\) for edit mode */
	//$(document).add("body").keydown( function(e) {
	$(document).keydown( function(e) {
		var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
		if(e.ctrlKey && key == 220) {
			if(STATE.edit) {
				var ok = confirm("MarkUp Editor\n\nDo you want to stop editing this content?\n(All unsaved changes will be lost.)\n-\n");
				if(ok){
					//window.location.href = sup('edit','0');
					window.location.href = sup('edit');
				}
			} else {
				var ok = confirm("MarkUp Editor\n\nDo you want to edit this content?\n-\n");
				if(ok){
					window.location.href = sup('edit','1');
				}
			}
		}
	});

	/* initialize edit mode */
	if(STATE.edit) {
		App.initEdit();
	}
});


/*
 * jQuery Cookie Plugin
 */
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('j.5=u(9,a,2){6(h a!=\'v\'){2=2||{};6(a===m){a=\'\';2.3=-1}4 3=\'\';6(2.3&&(h 2.3==\'n\'||2.3.k)){4 7;6(h 2.3==\'n\'){7=w C();7.B(7.z()+(2.3*A*o*o*E))}l{7=2.3}3=\'; 3=\'+7.k()}4 8=2.8?\'; 8=\'+2.8:\'\';4 b=2.b?\'; b=\'+2.b:\'\';4 c=2.c?\'; c\':\'\';d.5=[9,\'=\',q(a),3,8,b,c].t(\'\')}l{4 g=m;6(d.5&&d.5!=\'\'){4 e=d.5.x(\';\');D(4 i=0;i<e.f;i++){4 5=j.r(e[i]);6(5.p(0,9.f+1)==(9+\'=\')){g=y(5.p(9.f+1));s}}}F g}};',42,42,'||options|expires|var|cookie|if|date|path|name|value|domain|secure|document|cookies|length|cookieValue|typeof||jQuery|toUTCString|else|null|number|60|substring|encodeURIComponent|trim|break|join|function|undefined|new|split|decodeURIComponent|getTime|24|setTime|Date|for|1000|return'.split('|'),0,{}))
