var Pop = Behavior.create({
	initialize: function() {
		// get the post_id from a hidden field		
		var id = this.element.getElementsByClassName('post_id');
		if(id.length > 0) {
			this.post_id = $F(id[0]);		
		}
	},
	onclick : function(e) {
		if(this.post_id) {
			var source = Event.element(e);

			if($(source).hasClassName('pop')) return this.__pop(e);
			if($(source).hasClassName('unpop')) return this.__unpop(e);
			if($(source).hasClassName('popped')) return false;
			if($(source).hasClassName('unpopped')) return false;
			if($(source).hasClassName('share')) return this.__share(e);
		}
		//return false;
	},
	__pop : function(e) {
		var source = Event.element(e);
		Event.stop(e);		
		
		// ajax parameters 
		var url = $F('base_url') + '/posts/pop';		
		var post = $H({
			post_id: this.post_id
		}).toQueryString();		

		//ajax request
		new Ajax.Request(url, {
			method: 'post',
			postBody: post,
			onComplete: function(transport) {
				source.update(transport.responseText);
			}
		});
		
		source.removeClassName('pop');
		source.addClassName('popped');
		return false;
	},
	__unpop : function(e) {
		var source = Event.element(e);
		Event.stop(e);
		
		// ajax parameters 
		var url = $F('base_url') + '/posts/pop';
		var post = $H({
			post_id: this.post_id,
			delta: -1
		}).toQueryString();

		//ajax request
		new Ajax.Request(url, {
			method: 'post',
			postBody: post
		});		
		source.removeClassName('unpop');
		source.addClassName('unpopped');
		source.update('sorry');
		return false;
	},
	__share : function(e) {
		$('email_friend-' + this.post_id).toggle();
		return false;
	}
});

var DefaultValue = Behavior.create({
	initialize: function(default_value) {
		this.default_value = default_value;
	},
	onfocus : function(e) {
		if(this.element.value == this.default_value){
			this.element.value = '';
		}
	},
	onblur : function(e) {		
		if(this.element.value == ''){
			this.element.value = this.default_value;
		}
	}
});

// var RandomVideo = Behavior.create({
// 
// });

var Archive = Behavior.create({
	initialize: function() {
	},
	onclick: function(e) {
		var element = Event.element(e);
			
		if(element.hasClassName('month')) {this.toggleMonth(element); return false;}
	},
	toggleMonth : function(e) {
		var ul = e.next();
		ul.toggle();
		//Effect.toggle(ul, 'blind',  {duration: .3}); 
	}
});

// Functions ===================================================================
//==============================================================================


// Events ======================================================================
//==============================================================================
Event.addBehavior({
	'#search_category:change' : function(e) {
		var cat = this.value;
		if(this.value == '---') {
			cat = 'art';
		}
		
		window.location = $F('base_url') + '/posts/category/' + cat;
	},
	'.post' : Pop(),
	 '#PostSearchKeyword' : DefaultValue('search by keyword'),	
	'#uploadPhoto:click' : function(e) {
		if($('PhotoUpload').readAttribute('disabled') != null){
			$('PhotoUpload').enable();
			$('photo_fields').show();
			$('VideoYoutubeUrl').disable();
			$('video_fields').hide();
		} else {
			$('PhotoUpload').disable();
			$('photo_fields').hide();
		}
	
	},
	'#uploadVideo:click' : function(e) {
		if($('VideoYoutubeUrl').readAttribute('disabled') != null){
			$('VideoYoutubeUrl').enable();
			$('video_fields').show();
			$('PhotoUpload').disable();
			$('photo_fields').hide();
		} else {
			$('VideoYoutubeUrl').disable();
			$('video_fields').hide();
		}		
	},
	'#sortOrder:change' : function(e) {
		var sortOrder = $F('sortOrder');
		var url = $F('root_url');
		
		// check to see if you are on the post index page
		if(url.substring(url.length - 7, url.length) == '/posts/') {
			url = url + 'index';
		}
		
		document.location = url + '/sort:' + sortOrder + '/direction:desc';
		return false;
	},
	'#PostSearchForm:submit' : function(e) {
		var url = $F('base_url');
		var search_term = escape($F('PostSearchKeyword'));
		document.location = url + '/posts/search/' + search_term;
		return false;
	},
	'ul.questionArchive' : Archive(),
	'ul#topNav li:mouseover' : function(e) {
		if (this.down().next())
			this.down().next().show();
		if (this.previous()) {
			this.previous().setStyle({
				backgroundImage: 'none'
			});
		}
	},
	'ul#topNav li:mouseout' : function(e) {
		if (this.down().next())
			this.down().next().hide();
		if (this.previous()) {
			if (! this.hasClassName('active')) {
				this.previous().setStyle({
					backgroundImage: 'url("../img/nav-border-thing.gif")',
					backgroundRepeat: 'no-repeat',
					backgroundPosition: '100% 55%'
				});
			}
		}
	},
	'.paging form:submit' : function(e) {
		var page = $('pagingPage').value;
		var href = document.location.href;
		if (href.endsWith('posts/')) { // if url ends in 'posts/' we need to add 'index'
			document.location = href + 'index/page:' + page;
		} else {
			if (href.endsWith('/'))
				document.location = href + 'page:' + page;
			else
				document.location = href + '/page:' + page;
		}
		return false;
	}
});

// OnReady =====================================================================
//==============================================================================
Event.onReady(function() {
	if ($$('ul#topNav li.active')[0].previous())
		$$('ul#topNav li.active')[0].previous().setStyle({ backgroundImage: 'none' });
});
