$(document).ready(function(){
	
	
	
	$('.closeSearches').live("click",function() {
		$("div#searchWrapper").fadeOut();
	 });
	 
	 $("#magic_search").live("click", function(){

		$("body").append('<div id="searchWrapper" style="display:none;"></div>');
		$("div#searchWrapper").fadeIn();
		$("div#searchWrapper").css("height",$(document).height());
		$("div#searchWrapper").load("/forum/lexx_resources/searches/index.php");
		
		
		
		return false;

	});

	$("body.iframe_results ul li").first().addClass("active");
	$("body.iframe_results .tab").first().addClass("active");
	$("body.iframe_results .path").first().addClass("active");
	
	
	$(".iframe_results ul li").live('click', function(){
		$(".iframe_results ul li,.tab, .path").removeClass("active");
		$(this).addClass("active");
		$(".iframe_results .tab#site_"+$(this).attr("id")).addClass("active");
		$(".iframe_results .path."+$(this).attr("id")).addClass("active");
		
	});
	
	$(".clear").live('click', function(){
		$(this).prevAll("input[type=checkbox]").attr('checked', false);
	});
	$(".select_all").live('click', function(){
		$(this).prevAll("input[type=checkbox]").attr('checked', true);
	});
	
	$("#engr input[type=submit]").live('click', function(){
		loop_checkbox("en", "gr", $(this));
		return false;
	});
	$("#gren input[type=submit]").live('click', function(){
		loop_checkbox("gr", "en", $(this));
		return false;
	});
	$("#enen input[type=submit]").live('click', function(){
		loop_checkbox("en", "en", $(this));
		return false;
	});
	$("#grgr input[type=submit]").live('click', function(){
		loop_checkbox("gr", "gr", $(this));
		return false;
	});
	
	
	
});

$(".tabs").text($(".tabs").text().replace("undefined", ""));

function loop_checkbox( src, trg, the_this)
{
		term = the_this.prevAll(".term").val();
		var content = '';
		var labels = '<ul id="menu">';
		
		var the_checked = new Array();
		
		
		
		
		$("#" + src+trg + " input[type=checkbox]").each(function(){
			if ($(this).is(":checked")) {
				
					function_name = "submit_form_" + $(this).attr("name");
					content = content + window[function_name](term, src, trg);
					if($(this).attr("name") != "teleterm")
					{
						labels = labels + '<li id="'+$(this).attr("name")+'">'+$(this).next().text()+'</li>';			
						
					}
					
			}
		});
		labels = labels + '</ul>';
	
	   myWindow=window.open(); 
       myWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>All searches</title><script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script><script type="text/javascript" src="http://lexilogia.gr/forum/lexx_resources/lexx.js"></script><link rel="stylesheet" type="text/css" media="all" href="http://lexilogia.gr/forum/lexx_resources/lexx.css" /> </head><body class="iframe_results"> ' + labels + '<div class="tabs">' +content +'</div></body></html>'); 
       myWindow.document.close(); 
       myWindow.focus(); 
       
     
	 
}

function set_iframe(url, id)
{
	output = '<input type="text" class="path '+id+'" value="'+url+'"/>';
	output +=  '<div id="site_'+id+'" class="tab"><iframe width="100%" height="100%" src="'+url+'"></iframe></div>'; 
	return output;
}

function replace_quotes(term)
{
	term = term.replace('"','');
	term = term.replace("'","");
	term = term.replace('"','');
	term = term.replace("'","");
	return term;
}

function replace_blank(term, symbol)
{
	term = term.replace(' ',symbol);
	return term;
}

function submit_form_magenta(term, src, target)
{
	term = replace_quotes(term);
	term = encodeURIComponent(term);
	class_name = 'magenta';
	var url =  'http://www.magentadb.gr/magenta-dictionaries/translate.php?SIP='+term ;
	
	
	output = set_iframe(url , class_name); 
	return output;
}
function submit_form_microsoft(term, src, target)
{
	term = replace_quotes(term);
	class_name = 'microsoft';
	output = set_iframe('http://www.microsoft.com/Language/en-US/Search.aspx?sString='+term+'&langID=el-gr', "microsoft"); 
	return output;
}
function submit_form_iate(term, src, target)
{
term = encodeURIComponent(term);

if(src == "gr"){src = 'el';}
if(target == "gr"){target = 'el';}

class_name = 'iate';
var url =  'http://iate.europa.eu/iatediff/SearchByQuery.do?method=search&query='+term+'&sourceLanguage='+src+'&targetLanguages='+target+'&domain=0';

output = set_iframe(url , class_name); 

	return output;
	
}
function submit_form_teleterm(term, src, target)
{
	term = replace_quotes(term);
	if(src == "gr")
	{	
		
		$('#teleterm .cbotype').val("TERMGR");
	}
	if(src == "en")
	{	
		
		$('#teleterm .cbotype').val("TERMEN");
	}

	$("#teleterm .txt-Input").val(term);
	$("#teleterm input[type=submit]").click();
}

function submit_form_proz(term, src, target)
{
	term = replace_quotes(term);
	term = encodeURIComponent(term);
	if(src == "gr"){src = 'ell';}
	if(target == "gr"){target = 'ell';}
	if(src == "en"){src = 'eng';}
	if(target == "en"){target = 'eng';}
	output = set_iframe( 'http://www.proz.com/?sp=ksearch&term='+term+'&whole_words=y&edited=y&unedited=y&glossaries=y&glosspost=y&og_team=&wikiwords=&from='+src+'&to='+target+'&all_encodings=y&float=&language_depth=1&submit=1', "proz"); 
	return output;
}

function submit_form_georgaka(term, src, target)
{
	term = replace_quotes(term);
	term = encodeURIComponent(term);
	output = set_iframe('http://www.greek-language.gr/greekLang/modern_greek/tools/lexica/georgakas/search.html?lq='+term, "georgaka"); 
	return output;	
}

function submit_form_oxford(term, src, target)
{
	term = replace_quotes(term);
	output = set_iframe('http://oxforddictionaries.com/search?searchType=dictionary&isWritersAndEditors=true&searchUri=All&q='+term+'&_searchBtn=Search&contentVersion=WORLD', "oxford"); 
	return output;	
}

function submit_form_longman(term, src, target)
{
	term = replace_quotes(term);
	output = set_iframe('http://www.ldoceonline.com/search/?q='+term, "longman"); 
	return output;
}

function submit_form_macmillan(term, src, target)
{
	term = replace_quotes(term);
	term = replace_blank(term, '-');
	output = set_iframe('http://www.macmillandictionary.com/dictionary/british/'+term, "macmillan"); 
	return output;
}

function submit_form_wikipedia(term, src, target)
{
	term = replace_quotes(term);	
	output = set_iframe('http://en.wikipedia.org/wiki/'+term, "wikipedia"); 
	return output;
}

function submit_form_onelook(term, src, target)
{
	term = replace_quotes(term);
	term = replace_blank(term, '+');

	output = set_iframe('http://onelook.com/?w='+term, "onelook"); 
	return output;
}

function submit_form_answers(term, src, target)
{
	output = set_iframe('http://www.answers.com/topic/'+term, "answers"); 
	return output;
}

function submit_form_urban(term, src, target)
{
	term = replace_quotes(term);
	term = replace_blank(term, '+');
	output = set_iframe('http://www.urbandictionary.com/define.php?term='+term, "urban"); 
	return output;
}

function submit_form_thesaurus(term, src, target)
{
	term = replace_quotes(term);
	term = replace_blank(term, '+');
	output = set_iframe('http://thesaurus.com/browse/'+term, "thesaurus"); 
	return output;
}

function submit_form_gbooks(term, src, target)
{
	term = encodeURIComponent(term);
	output = set_iframe('http://www.google.com/search?q='+term+'&btnG=%CE%91%CE%BD%CE%B1%CE%B6%CE%AE%CF%84%CE%B7%CF%83%CE%B7+%CE%92%CE%B9%CE%B2%CE%BB%CE%AF%CF%89%CE%BD&tbm=bks&tbo=1&hl=el', "gbooks"); 
	return output;
}
function submit_form_lkn(term, src, target)
{
	term = encodeURIComponent(term);
	output = set_iframe('http://www.greek-language.gr/greekLang/modern_greek/tools/lexica/triantafyllides/search.html?lq='+term+'&dq=', "lkn"); 
	return output;
}

function submit_form_lexiscope(term, src, target)
{
	term = replace_quotes(term);
	term = encodeURIComponent(term);
	output = set_iframe('http://www.neurolingo.gr/online_tools/lexiscope.htm?term='+term, "lexiscope"); 
	return output;
}

function submit_form_elwikipedia(term, src, target)
{
	term = encodeURIComponent(term);
	output = set_iframe('http://el.wikipedia.org/w/index.php?title=%CE%95%CE%B9%CE%B4%CE%B9%CE%BA%CF%8C%3A%CE%91%CE%BD%CE%B1%CE%B6%CE%AE%CF%84%CE%B7%CF%83%CE%B7&search='+term, "elwikipedia"); 
	return output;
}

function submit_form_grnews(term, src, target)
{
	term = encodeURIComponent(term);
	output = set_iframe('http://www.google.gr/search?q='+term+'%20site%3Atovima.gr+OR+site%3Atanea.gr+OR+site%3Akathimerini.gr+OR+site%3Aenet.gr+OR+site%3Aethnos.gr', "grnews"); 
	return output;
}
function submit_form_ennews(term, src, target)
{
	term = encodeURIComponent(term);
	output = set_iframe('http://www.google.com/search?q='+term+'%20site%3Abbc.co.uk+OR+site%3Aindependent.co.uk+OR+site%3Aguardian.co.uk', "ennews"); 
	return output;
}
function submit_form_biblionet(term, src, target)
{
	term = replace_quotes(term);
	term = encodeURIComponent(term);
	output = set_iframe('http://www.biblionet.gr/main.asp?page=results&title='+term+'&TitleSplit=1&summary=&titlesKind=&person=&person_ID=&PerKind=0&com=&com_ID=&isbn=&from=&untill=&subject=&subject_ID=&series=&low=&high=&OrigLang=&PagesFrom=&PagesTo=&avail_stat=', "biblionet"); 
	return output;
}




