var Site = {
	ajaxloader : '<div id="ajax-loader">Loading<br /><img src="/assets/img/ajax_loader.gif" alt="Loading..." width="62" height="13" longdesc="Loading content..." /></div>',
	ajaxloaderexists : 0,
	notifyexists : 0,
	notifyvisible : 0,
	loaded: 0,
	ajaxloaded: 1,

	init : function() {
		Site.ajax();
		Site.loaded = 0;
	},

	/**
 	* loader toggles the fast loader
 	*/
	loader : function(method) {
		// first check if #ajax-loader exists
		if (Site.ajaxloaderexists === 0) {
			if ($('#ajax-loader').length < 1) {
				// create it
				$('body').append(Site.ajaxloader);
				Site.ajaxloaderexists = 1;
			} else Site.ajaxloaderexists = 1;
		}

		// otherwise toggle
		if (method !== undefined) { if (method === 'on') {$('html').css({cursor:'wait'});$('#ajax-loader').css('display','block');} else { $('html').css({cursor:'default'});$('#ajax-loader').fadeOut(250); }return false;}

		if ($('#ajax-loader').is(':hidden')) { $('html').css({cursor:'wait'});$('#ajax-loader').css('display','block'); }
		else { $('html').css({cursor:'default'}); $('#ajax-loader').fadeOut(250); }
		return true;
	},

	notify : function(text, type) {
		var delay = 750;
		if (Site.notifyvisible === 1) return false;
		if (type == '') type = 'default';
		if (Site.notifyexists === 0) {
			if ($('#notify-info').length < 1) {
				// create it
				$('body').append('<div id="notify-info"></div>');
				Site.notifyexists = 1;
			} else Site.notifyexists = 1;
		}
		var clwidth = window.document.body.clientWidth;
		var windowh = (typeof window.innerHeight != 'undefined') ? window.innerHeight : document.body.offsetHeight;
		$('#notify-info').empty().text(text).removeClass().addClass(type);
		Site.notifyvisible =1;
		var w = Math.round($('#notify-info').outerWidth(true) / 2);
		$('#notify-info').css({display:'block',top: ((windowh/2)-100)+'px', left: ((clwidth/2)-w)+'px'})
		.animate({top: (windowh/2)+'px', opacity: 0.8}, 500, 'easeInOutExpo', function() {
			setTimeout(function(){$('#notify-info').animate({top: ((windowh/2)+300)+'px', opacity: 0}, 500, 'easeInOutQuint', function(){$('#notify-info').remove();Site.notifyexists=0;});Site.notifyvisible =0;}, delay);
		});
		return false;
	},

	redirect : function(where){
		//window.location.replace(where);
		window.location = where;
	},

	info : function(id, type, message) {
		$(id).html('<span class="'+type+'">'+message+'</span>');
		return (type == 'error') ? false : true;
	},

	load : function(url){
		Site.loaded = 0;
		setTimeout(function(){if (Site.loaded != 1) { Site.loader('on') }}, 1000);
		$.ajax({
			type: "POST",
			url: url,
			dataType: 'html',
			data: '&ajax=1',
			beforeSend: function(){},
			complete: function(){},
			success: function(data) {
				Site.loaded=1;
				$('#main').html(data);
				$.address.value(url);
				Site.loader('off');
			}
		});
		if (typeof(pageTracker) == 'object') pageTracker._trackPageview(url);
		return false;
	},

	ajax : function() {
		// break out of un-ajax'd path, if the user has followed a direct link
		if (document.location.pathname.length > 1)
			window.location = '/#'+document.location.pathname;

		$.address.externalChange(function(e) {
			var path = $.address.pathNames();
			if (path.length == 0 && Site.ajaxloaded == 1) {
				Site.load(e.path);
			}
			else if (path.length > 0) {
					Site.load(e.path);
			}
			Site.ajaxloaded = 1;
		});
	},
	
	update : function(newtitle, newurl) {
		$.address.title(newtitle);
		$('.sel').removeClass();
		$('#link-'+newurl).addClass('sel');
		$('.fadein').css({opacity:0}).load(function(){ $(this).animate({opacity:1},1000,'easeInOutSine'); });
	},
	
	getHash : function () {
		if (window.location.hash) {
			var hash = window.location.hash;
			return hash.replace('#', '');
		}
		else {
			return "/";
		}
	}
};

var Login = {
	boxy : '',
	holder : '#login-form-holder',
	form : function() {
		var data ='&ajax=1';
		$.ajax({
			type: "POST",
			url: '/admin/login/',
			dataType: 'json',
			data: data,
			beforeSend: function(){},
			complete: function(){},
			success: function(data) {
				Login.boxy = new Boxy('<div style="width:450px" id="login-form-holder">'+data.html+'</div>', {title:'Login',unloadOnHide : true});
			}
		});
		if (typeof(pageTracker) == 'object') pageTracker._trackPageview('/user/login/');
		return false;
	},

	submit : function(form) {
		var data = $(form).serialize();
		var url = $(form).attr('action');
		var redirect = Site.getHash();
		data += '&ajax=1';
		
		var height = $(form).height();
		$(form).remove();
		$(Login.holder).append('<div class="login-loading" style="height:'+height+'px;"><h3>LOGGING IN</h3></div>');

		$.ajax({
			type: "POST",
			url: url,
			data: data,
			dataType: 'json',
			beforeSend: function(){},
			complete: function(){},
			success: function(data) {
				if (data.success == 1) {
					$('.login-loading h3', Login.holder).text('SUCCESS!');
					$('.login-loading',Login.holder).animate({opacity:0},500, function() {
						$(Login.holder).css('opacity',0);
						$(this).remove();
						Site.redirect(redirect);
					});
				} else {
					$('.login-loading h3', Login.holder).text('INCORRECT USERNAME/PASSWORD');
					$('.login-loading',Login.holder).animate({opacity:0},500, function() {
						$(Login.holder).css('opacity',0).append(data.html).animate({opacity:1},500);
						$(this).remove();
					});
				}
			}
		});
		return false;
	}
};

var Page = {
	mytextarea : null,

	init : function() {
		Page.mytextarea = null;
	},

	titleKeys : function(e) {
		switch (e.keyCode) {
			case 27:
				Page.hideTitleTextArea();
			break;
			case 13:
				return Page.saveTitleText();
			break;
		}
		return true;
	},

	hideTitleTextArea : function() {
		if (!Page.mytextarea) return false;
		var text = (Page.mytextarea).parent().find('.text');
		text.css('display','block');
		var textarea = Page.mytextarea;
		textarea.css('display','none');
		$(textarea).unbind('keydown');
		return false;
	},

	showTitleTextArea : function(text) {
		var textarea = Page.mytextarea = $(text).parent().find('textarea');
		var h = $(text).height();
		$(text).css({display:'none'});
		textarea.css({display:'block',height: h+'px'});
		$(textarea).keydown(Page.titleKeys).focus();
	},

	saveTitleText : function() {
		var text = Page.name = Page.mytextarea.val();
		var id = Page.mytextarea.attr('rel');
		var data = '&id='+id+'&text='+text;
		$.ajax({
			type: "POST",
			url: '/admin/updatePageTitle',
			data: data,
			dataType: 'json',
			beforeSend: function(){},
			success: function(data) {
				if (data.worked == 0) {
					Site.notify('Couldn\'t Update Page Title: "'+text+'"', 'error');
				} else {
					Site.notify('Updated Page Title: "'+text+'"', 'success');
				}
			}
		});
		Page.hideTitleTextArea();
		var disp = (Page.mytextarea).parent().find('.text');
		disp.text(text);
		if (typeof(pageTracker) == 'object') pageTracker._trackPageview('/admin/updatepagetitle/');
		return false;
	},

	saveContentText : function() {
		var text = Page.mytextarea.val();
		text = text.replace(/\n/g, '<br />');
		var id = Page.mytextarea.attr('rel');
		var data = '&id='+id+'&text='+text;
		$.ajax({
			type: "POST",
			url: '/admin/updatePageContent',
			data: data,
			dataType: 'json',
			beforeSend: function(){},
			success: function(data) {
				if (data.worked == 0) {
					Site.notify('Couldn\'t Update Page Content: "'+text+'"', 'error');
				} else {
					Site.notify('Updated Page Content: "'+data.content+'"', 'success');
				}
			}
		});
		Page.hideContentTextArea();
		var disp = (Page.mytextarea).parent().find('.text');
		disp.html(text);
		if (typeof(pageTracker) == 'object') pageTracker._trackPageview('/admin/updatepagecontent/');
		return false;
	},

	contentKeys : function(e) {
		if (e.ctrlKey && e.keyCode == 13)
			Page.saveContentText();
		switch (e.keyCode) {
			case 27:
				Page.hideContentTextArea();
			break;
		}
		return true;
	},

	showContentTextArea : function(text) {
		var textarea = Page.mytextarea = $(text).parent().find('textarea');
		var w = $(text).width();
		$(text).css({display:'none'});
		textarea.css({display:'block',width: w+'px'});
		$('#gallerytexthelper').css('display','block');
		$('#gallerytextsave').css('display','block');
		$(textarea).elastic().keydown(Page.contentKeys).focus();
	},

	hideContentTextArea : function() {
		if (!Page.mytextarea) return false;
		var text = (Page.mytextarea).parent().find('.text');
		text.css('display','block');
		var textarea = Page.mytextarea;
		textarea.css('display','none');
		$('#gallerytextsave').css('display','none');
		$('#gallerytexthelper').css('display','none');
		$(textarea).unbind('keydown');
		return false;
	}

};

var Image = {
	boxy : '',
	holder : '#image-form-holder',
	form : function(imageid) {
		var data ='&imageid='+imageid+'&redirect='+Site.getHash();
		$.ajax({
			type: "POST",
			url: '/admin/imageForm/',
			dataType: 'json',
			data: data,
			beforeSend: function(){},
			complete: function(){},
			success: function(data) {
				Image.boxy = new Boxy('<div style="width:450px" id="image-form-holder">'+data.html+'</div>', {title:'Change Image: '+imageid,unloadOnHide : true});
			}
		});
		return false;
	}

};

var Form = {
	sendForm : function(formid, holder) {
		var url = $(formid).attr('action');
		var data = $(formid).serialize();
		$.ajax({
			type: "POST",
			url: url,
			data: data,
			dataType: 'html',
			beforeSend: function(){$('#'+holder).fadeOut(150);},
			complete: function(){$('#'+holder).fadeIn(450);},
			success: function(html) {
				$('#'+holder).html(html);
			}
		});
        if (typeof(pageTracker) == 'object') pageTracker._trackPageview(url);
        return false;
	}
}

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

$(function(){
	Site.init();
});
