//
//
//  JAVASCRIPT FUNCTIONS FOR AJAX
//
//
//

function clearSearch(){
   search = document.getElementById("search_query");
   search.value = "";
   tvshow_search();
}


// makes it so when pressing enter, it does not submit it
function noenter(event) {
	var keycode = event.keyCode;
	
	if(event && keycode == 13){
			tvshow_search();
	}else{
		return false;	
	}
}

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);
}


function show_link_form(){
	tagname = 'add_link';

	if( document.getElementById(tagname).style.display == 'block' ){
		document.getElementById(tagname).style.display = 'none';
		document.getElementById('add_link_link').innerHTML = "Add Link";
	}else{
		document.getElementById('add_link_link').innerHTML = "Hide Link Form";
		document.getElementById(tagname).style.display = 'block';
	}

}



//
// AJAX Loading functions
//
function displayLoading()
{
         Element.show("loading");
}

function hideLoading ()
{
         Element.hide("loading");
}

function hideshowResult(element)
{
        var tagname = 'ul_' +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(tagname).style.display = 'none';
				document.getElementById(imagename).src = '../images/box_plus.gif';

		}else{
			alert('do not really konw whats going on');
		}
	
}

// used to show and hid divs, a miracle
//
function hideshowDiv(element)
{
		var imagename = 'image_element';
       if(document.getElementById(element).style.display == 'none'){

				document.getElementById(element).style.display = 'block';
				//document.getElementById(imagename).src = '../images/box_minus.gif';
				
		}else if(document.getElementById(element).style.display == 'block'){

				
				document.getElementById(element).style.display = 'none';
				//document.getElementById(imagename).src = '../images/box_plus.gif';

		}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 tab_switch(switcheroo){
	
	new Ajax.Updater('housing_holder', '../pages/db/housing.php?switch='+switcheroo, {asynchronous:true, evalScripts:true});
	
	Ajax.Responders.register(	{		onCreate : displayLoading, 		onComplete : hideLoading	}	);
	
}

function submit_rating(place_id, rating){
	
	new Ajax.Updater('rating', '../pages/db/rating.php?place_id='+place_id+ '&rating=' + rating, {asynchronous:true, evalScripts:true});
	
	Ajax.Responders.register(	{		onCreate : displayLoading, 		onComplete : hideLoading	}	);
	
}

function main_switch(switcheroo){
	
	new Ajax.Updater('main', '../pages/db/main.php?page='+switcheroo, {asynchronous:true, evalScripts:true});
	
	Ajax.Responders.register(	{		onCreate : displayLoading, 		onComplete : hideLoading	}	);
	
}

function show_popup(switcheroo){
	
	new Effect.Appear('popup');	
	
	new Ajax.Updater('popup', '../pages/db/popup.php?page='+switcheroo, {asynchronous:true, evalScripts:true});
	
	Ajax.Responders.register(	{		onCreate : displayLoading, 		onComplete : hideLoading	}	);
	
	
	
}

function hide_popup(){
	
	new Effect.Fade('popup');
	
}


function tvshow_keypress_search(){
		//Element.show("loading");
		document.getElementById('real_search_results').innerHTML='<img src="/images/loader.gif" />';
	//	document.getElementById('wiki_hideshow').innerHTML = '<img src="" />';
 		setTimeout( 'tvshow_search();', 750 );	
}

//
// SEARCH STUFF HERE
//
var busy = 0;
var last_query;
var query;
function tvshow_search(){
	
	  query = $F("search_query");

//	alert(busy+ " last_query:" + last_query + " new_query:" + query + " counter:" + counter);
	
	if(query != last_query){
		
		new Ajax.Updater('search_results', '../pages/db/search.php?search_query='+query, 
						{asynchronous:true});
		
	    Ajax.Responders.register(	{ onComplete : hideLoading	}	);
		last_query = query;

	}


}
function inner_content(show_id, ul_id){
	Element.show("loading");
/*	
	if(document.getElementById('ul_' + ul_id).style.display == 'none'){
				new Effect.BlindDown('ul_' + ul_id);
				new Effect.Appear('ul_' + ul_id);
				document.getElementById('ul_' + ul_id).style.display = 'block';	
				
	}else if(document.getElementById('ul_' + ul_id).style.display == 'block'){
				new Effect.BlindUp('ul_' + ul_id);
				new Effect.Fade('ul_' + ul_id);
				
				document.getElementById('ul_' + ul_id).style.display = 'none';
	}
	*/
	new Ajax.Updater('inner_content', '../pages/db/show.php?show_id='+show_id, {asynchronous:true, evalScripts:true});
	
				Ajax.Responders.register(	{		onCreate : displayLoading, 		onComplete : hideLoading	}	);

}

function search_result_type(search_type){
		var query = document.search_form.search_query.value;
		document.getElementById('real_search_results').innerHTML='<img src="/images/loader.gif" />';
		new Ajax.Updater('search_results', '../pages/db/search.php?search_query='+ query +'&search_type='+search_type, 
						{asynchronous:true});
		
		Ajax.Responders.register(	{		onCreate : displayLoading, 		onComplete : hideLoading	}	);
}

function inner_content_category(show_id, category_id){
					
		new Ajax.Updater('inner_content', '../pages/db/show.php?show_id='+show_id + '&category_id=' + category_id, {asynchronous:true, evalScripts:true});
	
		Ajax.Responders.register(	{		onCreate : displayLoading, 		onComplete : hideLoading	}	);
				
}

function tvshow_letter_search(letter){
		
	document.getElementById('real_search_results').innerHTML='<img src="/images/loader.gif" />';
	
	document.search_form.search_query.value= letter;
	
	//var qs = Form.serialize($("search_form"));
	
	new Ajax.Updater('search_results', '../pages/db/search.php?search_query='+letter, {asynchronous:true, evalScripts:true});
	
}

function insert_new_link(){

	var qs = Form.serialize($("form_add_link"));
	new Ajax.Updater('add_a_link', '../AddLink.php?'+qs, {asynchronous:true, evalScripts:true});

}

function insert_new_comment(){

        var qs = Form.serialize($("form_add_comment"));
        new Ajax.Updater('add_a_comment', '../AddComment.php?'+qs, {asynchronous:true, evalScripts:true});

}


function new_category(){

        document.getElementById('new_category').innerHTML = "<input type=\"hidden\" name=\"new_category\" value=\"set\" /><input type =\"text\" name=\"category\" /> ex) Season 3";
        }


function reportBroken(videoID){

	new Ajax.Updater('hidden', '../pages/db/report.php?video_id='+videoID, {asynchronous:true, evalScripts:true});

        Ajax.Responders.register(       {               onCreate : displayLoading,              onComplete : hideLoading        }       );

}

function submitVideoRating(rating ,videoID){

        new Ajax.Updater('hidden', '../pages/db/rating.php?rating=' + rating + '&video_id='+videoID, {asynchronous:true, evalScripts:true});

        Ajax.Responders.register(       {               onCreate : displayLoading,              onComplete : hideLoading        }       );

}

function showcomments(comment_id){

       

}



