var Site = {

	start: function(){
		Site.setHeaderBg();
		Site.postFromFront();
		Site.catHighlight();
		Site.searchFocus();
		Site.ratingTips();
		new SmoothScroll();
	},


	setHeaderBg: function(){ //this sets the background for the header based on the time of the day (called in body onLoad)
		var t = new Date();
		t = t.getHours();
		if(t >= 6 && t < 12){
			$('header').setStyle('background', 'url(http://www.maponomy.com//images/logo.jpg)');
		}
		if(t >= 12 && t < 20){
			$('header').setStyle('background', 'url(http://www.maponomy.com//images/logo.jpg)');
		}
		if(t >= 20 && t < 22){
			$('header').setStyle('background', 'url(http://www.maponomy.com//images/logo.jpg)');
		}
		if(t >= 22 || t < 6){
			$('header').setStyle('background', 'url(http://www.maponomy.com//images/logo.jpg)');
		}
	},

	ratingTips: function(){
		if($$('.disabled')){
			var rTips = new Tips($$('.disabled, .gimme-a-tip'), { showDelay: 250});
		}
	},

	frontPostValidate: function(){
		if($('post-front-title').value != '' && $('post-front-content').value != ''){
			return true;
		}
		else{
			alert('Please fill in all fields.');
			return false;
		}
	},

	searchFocus: function(){
		if(s = $('s')){
			form = s.getParent().getParent();
			oAction = form.action;
			oValue = s.value;
			if (oAction.match("public")) {
				if(oValue.match('Buscar'))
					oAction = '/community/es/'
				else
					oAction = '/community/';
			}
			s.addEvents({
				'focus': function(){
					s.value = '';
				},
				'blur': function(){
					if(s.value == '')
						s.value = oValue;
				}
			});
			$$('#search ul li a').each(function(e){
				e.addEvent('click',function(){
					if(!e.hasClass('active')){
						e.addClass('active');
						loc = form.action;
						if(loc.match("public")){
							form.action = oAction;
						}
						else{
							form.action = '/community/public/';
						}
						$$('#search ul li a').each(function(o){
							if(o != e && o.hasClass('active'))
								o.removeClass('active');
						});
					}
					s.focus();
				});
			});
		}
//		if($('s')){
//			s = $('s').getParent().getParent();
//			searchButtons = $$('#search ul li a');

//			sField = $('s');
//			sField.addEvent('focus', function(){
//				if(sField.value == 'Search...')
//					sField.value = '';
//			});
//			sField.addEvent('blur', function(){
//				if(sField.value == '')
//					sField.value = 'Search...';
//			});
//		}
	},

	postFromFront: function(){
		if($('post-from-front')){
			pfFx = new Fx.Slide($('post-from-front'));
			pfFx.hide();
			$('post-from-front').setStyle('display','block');
			if($('post-from-front').hasClass('editting')){
				pfFx.show();
			}
			else{
				$('new-post').addEvent('click', function(){
					pfFx.toggle();
				});
			}
		}
	},

	jaxer: function(action, form, target){
		var url = String(window.location);
		if(url.search(/https/) != -1){
			action = (action.replace(/http/,"https"));
		}
		if(url.search(/www/) == -1){
			action = (action.replace(/www./,""));
		}
		new Ajax(action, {
			postBody: $(form),
			update: $(target),
			onFailure: function(){
				$(target).setText('An error has occured.');
			}
		}).request();
	},

	catHighlight: function(){
		var cats = $$('#categories label');
		cats.each(function(e){
			e.addEvent('click', function(){
				e.addClass('active-category');
				cats.each(function(c){
					if(c != e){
						c.removeClass('active-category');
					}
				});
			});
		});
	}
}

window.addEvent('domready', Site.start);
