var dropdown = 0;
var resetbutton = 0;
var my_select;

window.addEvent('domready', function() {
	fetch_newsfeed(1);
	
	if($('frm_search_site').value == 0) {
		if(($('frm_search_public').getProperty('checked')) && ($('frm_search_private').getProperty('checked'))) {
			$$('.hiddensearchstuff').tween('opacity', [0,0]);
		} else {
			$$('.hiddensearchstuff').tween('opacity', [0,1]);
			display_reset_button();
		}
	} else {
		$$('.hiddensearchstuff').tween('opacity', [0,1]);
		display_reset_button();
		$('frm_search_pubpri').tween('opacity', [0,0]);
	}
	
	$('frm_search_reset').addEvent('click', function() { 
		search_reset();
	});

	$('frm_search_public').addEvent('click', function() {
		if(($('frm_search_public').getProperty('checked')) && ($('frm_search_private').getProperty('checked'))) {
			hide_reset_button();
		}
		if($('frm_search_public').getProperty('checked') == false) {
			if($('frm_search_private').getProperty('checked') == true) {
				display_reset_button();
			}
		}
		if($('frm_search_public').getProperty('checked') != true) {
			$('frm_search_private').setProperty('checked', 'checked');
		}
	});
	
	$('frm_search_private').addEvent('click', function() {
		if(($('frm_search_public').getProperty('checked')) && ($('frm_search_private').getProperty('checked'))) {
			hide_reset_button();
		}
		if($('frm_search_private').getProperty('checked') == false) {
			if($('frm_search_public').getProperty('checked') == true) {
				display_reset_button();
			}
		}
		if($('frm_search_private').getProperty('checked') != true) {
			$('frm_search_public').setProperty('checked', 'checked');
		}
	});
	
	$('frm_search_site').addEvent('change', function() {
		on_search_site_change();
	});
	
	$('frm_search_site').addEvent('click', function() { 
		if(dropdown == 0) {
			dropdown = 1;
		} else {
			dropdown = 0;
		}
	});
	
	$('SearchBoxBasic').addEvent('mouseenter', function() { 
		inside = 1;
		$$('.hiddensearchstuff').set('tween', {duration: 300});
		$$('.hiddensearchstuff').fade('in');
	});
	
	$('SearchBoxBasic').addEvent('mouseleave', function() { 
		inside = 0;
		$$('.hiddensearchstuff').set('tween', {duration: 375});
		if($('frm_search_site').value == 0) {
			if(dropdown == 0) {
				if(($('frm_search_public').getProperty('checked')) && ($('frm_search_private').getProperty('checked'))) {
					$$('.hiddensearchstuff').fade('out');
				}
			}
		}
	});
	
	/*
	my_select = new MavSelectBox({
		elem: $('frm_search_site'),
		alternate: true,
		onSelect: function(_elem) {
			on_search_site_change();
		}
	});
	*/

});

function on_search_site_change() {
	if($('frm_search_site').value != 0) {
		if(($('frm_search_public').getProperty('checked')) && ($('frm_search_private').getProperty('checked'))) {
			display_reset_button();
		}
		$('frm_search_private').setProperty('checked', 'checked');
		$('frm_search_public').setProperty('checked', 'checked');
		$('frm_search_pubpri').set('tween', {duration: 400});
		$('frm_search_pubpri').fade('out');
	} else {
		$('frm_search_pubpri').set('tween', {duration: 200});
		$('frm_search_pubpri').fade('in');
		hide_reset_button();
	}
	if(Browser.Engine.webkit) { dropdown = 0; }
	if(Browser.Engine.gecko) { dropdown = 1; }
}

function display_reset_button() {
	$('frm_search_reset').set('html','<a href="javascript:search_reset();">Reset</a>');
	if(resetbutton == 0) {
		$('frm_search_pubpri').set('tween', {duration: 400});
		$('frm_search_reset').tween('opacity', [0,1]);
	}
	resetbutton = 1;
}
function hide_reset_button() {
	$('frm_search_reset').set('html',"");
	$('frm_search_reset').set('tween', {duration: 200});
	$('frm_search_reset').tween('opacity', [1,0]);
	resetbutton = 0;
}
function search_reset() {
	$('frm_search_query').value = "";
	$('frm_search_site').value = 0;
	$('frm_search_public').setProperty('checked', 'checked');
	$('frm_search_private').setProperty('checked', 'checked');
	$('frm_search_pubpri').set('tween', {duration: 400});
	$('frm_search_pubpri').fade('in');
	hide_reset_button();
}


var htmlReqNewsfeed;

//example prompt_background('display', 'rss')
function prompt_control(state, prompt_title, vars) {
	if(state == "display") {
		$('prompt_background').setStyle('display', 'block');
		var fxBackgroundFade = new Fx.Morph($('prompt_background'), 
									{
										duration:   300, 
										transition: Fx.Transitions.Quad.easeOut,
										onComplete: function() { 
											$('prompt').setStyle('display', 'block');
											$('prompt').set('class', 'prompt_loading');
											
											var fxPromptFade = new Fx.Morph($('prompt'), 
																	{
																		duration: 300, 
																		transition: Fx.Transitions.Quad.easeOut,
																		onComplete: function() {
																			prompt_fetch_html(prompt_title, vars);
																		}
																	});
											
											
											fxPromptFade.start({'opacity': [0, 1]});
										}
									});
		
		fxBackgroundFade.start({'opacity': [0, 0.65]});
	}
	if(state == "hide") {
		var fxPromptMorph = new Fx.Morph($('prompt'), 
								{
								       duration:   300, 
								       transition: Fx.Transitions.Quad.easeOut,
								       onComplete: function() { 
										       $('prompt').setStyle('display', 'none');
										       $('prompt').set('html', '');
										       var fxBackgroundFade = new Fx.Morph($('prompt_background'), 
																		{
																			duration: 300, 
																			transition: Fx.Transitions.Quad.easeOut,
																			onComplete: function() {
																				$('prompt_background').setStyle('display', 'none');
																			}
																		});
										       fxBackgroundFade.start({'opacity': [0.65, 0]});						
								       }
								});
		fxPromptMorph.start('.prompt_closing');
	}
}

function prompt_fetch_html(prompt_title, vars) {
	if(prompt_title == 'rss') {
		var htmlRegRss = new Request.HTML(
						{
							method: 'get',
							url: 'ajax/rss_html.php?id=' + vars,
							onComplete: function(responseTree, responseElements, responseHTML){ 
								      var fxPromptMorph = new Fx.Morph($('prompt'), 
														{
														       duration:   500, 
														       transition: Fx.Transitions.Quad.easeOut,
														       onComplete: function() { 
															       $('prompt').set('html', responseHTML);
														       }
														});
								      fxPromptMorph.start('.prompt_rss');
								      $('prompt').erase('class', 'prompt_loading');
							}
						});
		htmlRegRss.send();
	}
	if (prompt_title == 'settings') {
		alert("Under construction...");
		prompt_control('hide', 'settings', '')
	}
}


function fetch_newsfeed(page) {
	htmlReqNewsfeed = new Request.HTML({url:'ajax/newsfeed_html.php?page=' + page, onComplete: function(responseTree, responseElements, responseHTML){	
		$('rss_list').set('html', responseHTML);
		
		var customTips = $$('.tooltip');
		
		var toolTips = new Tips(customTips, {
			//this will set how long before 
			//the tooltip will wait to show up
			//when you mouseover the element
			//in milliseconds
			showDelay: 100,    //default is 100
			
			//this is how long the tooltip
			//will delay bofore hiding
			//when you leave
			hideDelay: 100,   //default is 100
			
			//this will add a wrapper div 
			//with the following class to your tooltips
			//this lets you have different styles of tooltips
			//on the same page
			className: 'newstooltip', //default is null
			
			//this sets the x and y offets
			offsets: {
				'x': 16,       //default is 16
				'y': 16        //default is 16
			},
			
			//this determines whether the tooltip
			//remains staitionary or follows your cursor
			//true makes it stationary
			fixed: false,      //default is false
			
			//if you call the functions outside of the options
			//then it may "flash" a bit on transitions
			//much smoother if you leave them in here
			onShow: function(toolTipElement){
				//passes the tooltip element
				//you can fade in to full opacity
				//or leave them a little transparent
				$$('.newstooltip').setStyle('display', 'block');
				toolTipElement.fade(1);
				//toolTipElement.set('tween', {duration: 'long'});
				//toolTipElement.tween('opacity', '1');
			},
			onHide: function(toolTipElement){
				toolTipElement.fade(0);
				//toolTipElement.set('tween', {duration: 'long'});
				//toolTipElement.tween('opacity', '0');
			},
			onAttach: function(toolTipelement){
				//$$('.newstooltip').setProperty('style', 'position: absolute; visibility: hidden; opacity: 0;');
			}
		});
		
		var myTooltips = $$('.newstooltip');
		myTooltips.fade(0);
		myTooltips.setProperty('style', 'position: absolute; visibility: hidden; opacity: 0;')
		
	}});
	
	htmlReqNewsfeed.send();
}