//
//
//  JAVASCRIPT FUNCTIONS FOR AJAX
//
//
//

//
// sets the stage for the loading bar to work it's magic
//
 

// makes it so when pressing enter, it does not submit it
function noenter() {
	if( window.event.keyCode == 13 ){
		changeZipCode();
	}
  return !(window.event && window.event.keyCode == 13); 
}

function enter_pressed(e){
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return false;
        return (keycode == 13);
}

//
// Adding links to homepage
//
function add_link(){
	var qs = Form.serialize($("submit_link"));
 Element.show("loading");
  	DivLoading('link_list');
	new Ajax.Updater('links', '../pages/main/links.php?'+qs, {asynchronous:true, evalScripts:true});
	
	
}

function delete_that_link(){
	DivLoading('link_list');
	var qs = Form.serialize($("remove_link"));
 Element.show("loading");
	new Ajax.Updater('links', '../pages/main/links.php?'+qs, {asynchronous:true, evalScripts:true});

	
}

function preview_link(){
	DivLoading('link_preview');
	var qs = Form.serialize($("submit_link"));
 Element.show("loading");
	new Ajax.Updater('link_preview', '../pages/main/link_preview.php?' + qs , {asynchronous:true, evalScripts:true});

}


function add_todo(){
	var qs = Form.serialize($("submit_todo"));

  	DivLoading('todo');
	new Ajax.Updater('todo', '../pages/db/todo.php?'+qs, {asynchronous:true, evalScripts:true});
	
	
}

function remove_todo(todo_id){
	DivLoading('todo'); 
	new Ajax.Updater('todo', '../pages/db/todo.php?delete_todo_id='+todo_id, {asynchronous:true, evalScripts:true});
}
function update_todo(todo_id){
	
	var text_todo_id = 'text_todo_' + todo_id;
	if(document.getElementById(text_todo_id).style.textDecoration == 'none'){
			document.getElementById(text_todo_id).style.textDecoration = 'line-through';
			new Ajax.Updater('hidden', '../pages/db/todo.php?update_todo_id='+todo_id+ 'status=0', {asynchronous:true, evalScripts:true});
	}else{
			document.getElementById(text_todo_id).style.textDecoration = 'none';
			new Ajax.Updater('hidden', '../pages/db/todo.php?update_todo_id='+todo_id+ 'status=1', {asynchronous:true, evalScripts:true});
	}
	
}


//
// Adding rss to homepage
//
function add_rss(){
	var qs = Form.serialize($("submit_rss"));
 	DivLoading('rss_results');
	new Ajax.Updater('rss', '../pages/main/rss.php?'+qs, {asynchronous:true, evalScripts:true});
}

function remove_rss(){
	DivLoading('rss_results');
	var qs = Form.serialize($("remove_rss"));
 Element.show("loading");
	new Ajax.Updater('rss', '../pages/main/rss.php?'+qs, {asynchronous:true, evalScripts:true});
}

function switch_rss(){
	DivLoading('rss_results');
	var rss_id = $F('rss_id');
	new Ajax.Updater('rss', '../pages/main/rss.php?rss_id='+rss_id, {asynchronous:true, evalScripts:true});

	
}

function switch_rss_num(){
	DivLoading('rss_results');
	var rss_num = $F('rss_num');
	new Ajax.Updater('rss', '../pages/main/rss.php?rss_num='+rss_num, {asynchronous:true, evalScripts:true});		

	
}

//
// link functions
//
function switch_link_category(category_id){
	
	category = category_id; 
	DivLoading('link_list');
	new Ajax.Updater('links', '../pages/main/links.php?category='+category, {asynchronous:true, evalScripts:true});

	
	
}

function switch_game_category(category_id){
	DivLoading('games_results');
		new Ajax.Updater('games', '../pages/main/games.php?games_category='+category_id, {asynchronous:true, evalScripts:true});
}

function switch_sports_category(category_id){
	DivLoading('sports_results');
		new Ajax.Updater('sports', '../pages/main/sports.php?sports_category='+category_id, {asynchronous:true, evalScripts:true});
}



//
// Calendar Stufff
//

function add_event(){
	var qs = Form.serialize($("submit_event"));
	DivLoading('events');
	Ajax.Updater('events', '../pages/main/events.php?'+qs, {asynchronous:true, evalScripts:true});
		
	
	
}

function switch_calendar(month, year){
	DivLoading('calendar');
	new Ajax.Updater('calendar', '../pages/db/calendar.php?month='+ month + '&year=' + year, {asynchronous:true, evalScripts:true});
	
	
	
}

//
//
//

function insert_review(){
	
	var qs = Form.serialize($("insert_food_review"));
	 Element.show("loading");
	new Ajax.Updater('reviews', '../pages/db/food_review.php?'+qs, {asynchronous:true, evalScripts:true});
		
	
}

function changeZipCode(){
	
	var qs = Form.serialize($("weather_form"));
	new Ajax.Updater('weather', '../pages/db/weather.php?'+qs, {asynchronous:true, evalScripts:true});
	
	
}

//
// AJAX to get drink special search
//
function drinkspecial_search(input){
	DivLoading('innerb');
	var search = $F('searcher');
	new Ajax.Updater('drinkspecials', '../pages/main/drinkspecials.php?search='+search, {asynchronous:true, evalScripts:true});

	
}
//
// AJAX to get drink special date
//		drinkspecial_date('monday');
function drinkspecial_date(date){
		
	DivLoading('innerb');
	new Ajax.Updater('drinkspecials', '../pages/main/drinkspecials.php?date='+date, {asynchronous:true, evalScripts:true});
	
	
	
	//
	// Loading gif when ajax is processing
	//
	
	
}
//
// AJAX to get drink special venue
//
function drinkspecial_venue(input){
		
	var venue = $F('venue');
	DivLoading('innerb');
	new Ajax.Updater('drinkspecials', '../pages/main/drinkspecials.php?venue='+venue, {asynchronous:true, evalScripts:true});
	
	
}

//
// AJAX Loading functions
//
function displayLoading()
{
         Element.show("loading");
}

function hideLoading ()
{
         Element.hide("loading");
}

//
// used to show and hid divs, a miracle
//
//
// used to show and hid divs, a miracle
//
function hideshowDiv(element)
{
        var tagname = element;
		var display;
		var imagename = 'image_'+element;
		
		if(document.getElementById(tagname).className == 'none'){
				//new Effect.BlindDown(tagname);
				//new Effect.Appear(tagname);
				document.getElementById(tagname).style.display = 'block';
				document.getElementById(tagname).className = 'block';
				document.getElementById(imagename).src = '../images/box_red_plus.gif';
				
				
				display = '1';
		}else if(document.getElementById(tagname).className == 'block'){
				//new Effect.BlindUp(tagname);
				//new Effect.Fade(tagname);
				document.getElementById(imagename).src = '../images/box_red_minus.gif';
				document.getElementById(tagname).style.display = 'none';
				document.getElementById(tagname).className = 'none';
				
				display = '0';
		}else{
			alert('do not really konw whats going on');
		}
		 Element.hide("loading");
		new Ajax.Updater('error', '../pages/db/boxes.php?tagname=' + tagname + '&display=' + display, {asynchronous:true, evalScripts:true});
				
}

function wikipedia_showMore(){
	    if(document.getElementById('second_body').style.display == "none"){
			document.getElementById('second_body').style.display = "block";
			document.getElementById('wiki_hideshow').innerHTML = 'Hide';
		}else if(document.getElementById('second_body').style.display == "block"){
			document.getElementById('second_body').style.display = "none";
			document.getElementById('wiki_hideshow').innerHTML = 'More..';
		}
	
}

function show_featured_review_form(){
		if(document.getElementById('featured_review_form').style.display == "none"){
			document.getElementById('overlay').style.display = "block";
			document.getElementById('featured_review_form').style.display = "block";
			document.getElementById('review_form_link').innerHTML = 'Hide Review Form';
		}else if(document.getElementById('featured_review_form').style.display == "block"){
			document.getElementById('overlay').style.display = "none";
			document.getElementById('featured_review_form').style.display = "none";
			document.getElementById('review_form_link').innerHTML = 'Show Review Form';
		}
}


function hideShowID(element){
	
        var tagname = element;
		var imagename = 'image_' + element;
		
		if(document.getElementById(tagname).style.display == 'none'){
				
				document.getElementById(tagname).style.display = 'block';
				document.getElementById(imagename).src = '../images/box_minus.gif';				

		}else if(document.getElementById(tagname).style.display == 'block'){
				
				document.getElementById(imagename).src = '../images/box_plus.gif';
				document.getElementById(tagname).style.display = 'none';

		}else{
			alert('do not really konw whats going on');
		}	

}

function hideShowSearch(){
	var tagname = 'search_main';
	var imagename = 'search_main_minimize_button';
	if(document.getElementById(tagname).style.display == 'none'){
				
				document.getElementById(tagname).style.display = 'block';
				document.getElementById(imagename).src = 'images/box_search_down.gif';				

		}else if(document.getElementById(tagname).style.display == 'block'){
				
				document.getElementById(imagename).src = 'images/box_search_up.gif';
				document.getElementById(tagname).style.display = 'none';

		}else{
			alert('do not really konw whats going on');
		}	
}


function refreshDiv(element)
{
        var tagname = element;
		
		new Ajax.Updater(tagname, '../pages/db/' + tagname+'.php', {asynchronous:true, evalScripts:true});
		
		
		
}

function switchFeaturedCategory(category_id){
	 	DivLoading('featured_results');
		new Ajax.Updater('featured_food', '../pages/db/featured.php?featured_category_id='+ category_id, {asynchronous:true, evalScripts:true});		
}

function getFeaturedDetails(place_id){
		new Effect.Appear('featured_top');
		new Effect.Appear('overlay', { to: .7 } );
		new Effect.Appear('featured_details');
		document.getElementById('featured_back').style.display = 'block';
		DivLoading('featured_details');
		new Ajax.Updater('featured_details', '../pages/db/featured_details.php?place_id='+ place_id, {asynchronous:true, evalScripts:true});		
}

function hideFeatured(){
	new Effect.Fade('overlay');
	new Effect.Fade('featured_details');	
	document.getElementById('featured_top').style.display = 'none';
	document.getElementById('featured_back').style.display = 'none';	
}


function submit_rating(place_id, rating){
	 Element.show("loading");
	new Ajax.Updater('rating', '../pages/db/rating.php?place_id='+place_id+ '&rating=' + rating, {asynchronous:true, evalScripts:true});
	
}

function main_switch(switcheroo){
	DivLoading('main_results');
	new Ajax.Updater('main', '../pages/db/main.php?page='+switcheroo, {asynchronous:true, evalScripts:true});
	
	
}

function show_popup(switcheroo){
	new Effect.Appear('overlay' , { to: .7 });
	new Effect.Appear('inner_pop' , { to: 1 });
	DivLoading('inner_pop');
	new Ajax.Updater('inner_pop', '../pages/db/popup.php?page='+switcheroo, {asynchronous:true, evalScripts:true});
}

function showgame(game_id){
	new Effect.Appear('overlay' , { to: .7 });
	new Effect.Appear('game_pop' , { to: 1 });
	new Ajax.Updater('game_pop', '../pages/db/games.php?game_id='+game_id, {asynchronous:true, evalScripts:true});
	
}

function switch_game(game_id){
        new Effect.Appear('overlay' , { to: .7 });
        new Effect.Appear('game_pop' , { to: 1 });
        // DivLoading('game_code');
        new Ajax.Updater('game_pop', '../pages/db/games.php?game_id='+game_id, {asynchronous:true, evalScripts:true});

}
function hidegame(game_id){
	new Effect.Fade('game_pop');
	new Effect.Fade('overlay');
	document.getElementById('game_pop').innerHTML = '';	
}

function hide_popup(){
	new Effect.Fade('overlay');
	new Effect.Fade('inner_pop');
	
}


//
// SEARCH STUFF HERE
//
function switch_search(switcheroo){
	if(document.getElementById('search_results')){
		document.getElementById('search_results').innerHTML = '';
	}
	DivLoading('search_main');
	new Ajax.Updater('search', '../pages/db/search.php?search='+switcheroo, {asynchronous:true, evalScripts:true});
	
	
}

function search_results(switcheroo){
	DivLoading('search_results');
	var qs = Form.serialize($("search_form"));
	
	new Ajax.Updater('search_results', '../pages/search/results/' + switcheroo + '.php?' + qs, {asynchronous:true, evalScripts:true});
	
	

}

function professor_searcher(){

	var qs = Form.serialize($("ratemyprofessors"));
	DivLoading('professor_results');
	new Ajax.Updater('professor_results', '../pages/school/professorSearch.php?' + qs, {asynchronous:true, evalScripts:true});
}

function professor_info(professor_id){
	
	DivLoading('professor_results');	
	new Ajax.Updater('professor_results', '../pages/school/professorInfo.php?professor_info=' + professor_id, {asynchronous:true, evalScripts:true});
}


function visitMail(){
	var site = $('select_mail');
	
//	alert(site.value);
	if(site != ""){
		window.open(site.value);
	}
	 new Ajax.Updater('email', '../pages/db/emaillinks.php', {asynchronous:true, evalScripts:true});

}
function visitQuickLinks(){
	var site = $('select_quicklinks');
	if(site != ""){
		window.open(site.value);
	}
	 new Ajax.Updater('quicklinks', '../pages/db/quicklinks.php', {asynchronous:true, evalScripts:true});

}

function googleWebSearch(){
	// Create a search control
        var searchControl = new GSearchControl();
	var ws = new GwebSearch();
 	options = new GsearcherOptions();
 	options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
 	searchControl.addSearcher(ws, options);
	searchControl.setSearchCompleteCallback(this, reloadSnap);
	 
 	searchControl.draw(document.getElementById("searchcontrol"));
	
	
   	
}
function reloadSnap(){
	SNAP_COM.shot.Rescan();
	//document.getElementById('search_results').innerHTML = '';
	
}

// used to insert into db for links
function MyKeepHandler(result) {
	
	// clone the result html node
 	var title = escape(result.titleNoFormatting);
	var url = escape(result.url);
	
 	new Ajax.Updater('links', '../pages/main/links.php?insert_link=true&link='+url + '&name='+title, {asynchronous:true, evalScripts:true});
}


function googleNewsSearch(){
      var searchControl = new GSearchControl();
	  searchControl.addSearcher(new GnewsSearch);	    
	 searchControl.draw(document.getElementById("searchcontrol"));
	 
	 SNAP_COM.shot.Rescan();
}

function googleVideoSearch(keyword){
	
     // var searchControl = new GSearchControl();    
     // searchControl.addSearcher(new GvideoSearch());
	   //searchControl.draw(document.getElementById("searchcontrol"));
	
	   var options = {
        twoRowMode : true, startupDelay : 1000 
      };
      var videoSearch = new GSvideoSearchControl(
                              document.getElementById("searchcontrol"),
                              [{ query : keyword}], null, null, options);
	   
}
/*
function quick_wiscmail_hover_on(){
		document.getElementById('quick_wisc_image').class = "hover";
                document.getElementById('quick_wisc_image').innerHTML = "hover";
}
function quick_wiscmail_hover_off(){
                document.getElementById('quick_wisc_image').class = "hover";
                document.getElementById('quick_wisc_image').innerHTML = "hover";
}
 */

//
// call this when the order is moved so you can update it
//
function order_update(){
		
		//var col1_sorted_ids = Sortable.serialize('col1');
		//var col3_sorted_ids = Sortable.serialize('col3');
		
		//alert();
		
		 Element.hide("loading");
		new Ajax.Updater('error', '../pages/db/box_order.php?col1='+ Sortable.sequence('col1') + '&col3=' + Sortable.sequence('col3') , { asynchronous:true, evalScripts:true});
		
		
}

function updateCraigsSubCategory(){
	
 var meowcat = document.getElementById('catAbbreviation').selectedIndex+1;
 document.getElementById('sub_cat').innerHTML = document.getElementById('sub_cat_'+meowcat).innerHTML;

}

function updateAmazonSubCategory(){
	
 var meowcat = document.getElementById('catAbbreviation').selectedIndex+1;
 document.getElementById('sub_cat').innerHTML = document.getElementById('sub_cat_'+meowcat).innerHTML;

}


function clearSearch(){
	document.getElementById('search_results').innerHTML = '';
}

function accordionSwitch(){
	
}

function DivLoading(div_id){
	document.getElementById(div_id).innerHTML = '<div id="loading"></div>';
	
}

function tabbing(container, show_id){
	var nodes = document.getElementById(div_id).getChildNodes();
	alert(nodes);
	
}

function switchSchools(school_id){
	
 	document.getElementById('css_style').href = 'style_'+ school_id +'.css';
}
