/*
***********************************************************
*** Jetpack name: LangLadder ***
*** Version: 0.0.8 ***
*** Authors:Erik Larson
*** Contact: planeterik@gmail.com ***
*** Last changes: 3/18/2010 ***
***********************************************************/

jetpack.future.import("selection");
jetpack.future.import("menu");
jetpack.future.import("storage.simple");
jetpack.future.import("slideBar");

var langladder={};
langladder.config={};
langladder.util={};
langladder.remote={};
langladder.quiz={};
langladder.slidebar={};
langladder.config.rooturl="http://langladder.com";
langladder.config.debugverbose=true;
/*
* Imports a document over the web
*/
langladder.util.webImport=function (filename){
	var script=$.ajax({url:langladder.config.rooturl+"/"+filename, 
		type: "GET",cache: false,async: false
  	}).responseText;
	return script;
}


var slidebar_context=null; //global variable to point to the slidebar context
var widget_context=null;
var highlightWord=false; //global variable to set the tool to only highlight words once
var gFilteredItems=[]; //global variable used to keep track of which items are filtered

// I changed this part to support only connected mode.  I will add offline mode back later
//jetpack.storage.simple.flashcards = jetpack.storage.simple.flashcards || {};
var flashcards = {};//jetpack.storage.simple.flashcards;

//jetpack.storage.simple.fcperf = jetpack.storage.simple.fcperf || {};
var fcperformance = {};//jetpack.storage.simple.fcperf;
/*for(elem in flashcards){
	fcperformance[elem]=fcperformance[elem]||[];
}*/

jetpack.storage.simple.settings = jetpack.storage.simple.settings || {};
var settings = jetpack.storage.simple.settings;

//jetpack.storage.simple.savedsites = jetpack.storage.simple.savedsites || [];
var savedsites = {};//jetpack.storage.simple.savedsites;

var currentCard;

function clearGlobals(){
	flashcards = {};
	fcperformance = {}
	savedsites = {};
	gFilteredItems=[];
}


// If there are no languages set, the defaul speak English and learning Spanish is used
if(settings["learninglang"]==undefined){
	settings["learninglang"]="es";
}
if(settings["nativelang"]==undefined){
	settings["nativelang"]="en";
}

eval(langladder.util.webImport("javascripts/jetpack/langutil.js"));
eval(langladder.util.webImport("javascripts/jetpack/langremote.js"));
eval(langladder.util.webImport("javascripts/jetpack/langtest.js"));
eval(langladder.util.webImport("javascripts/jetpack/langdatatypes.js"));
eval(langladder.util.webImport("javascripts/jetpack/dateformat.js"));
eval(langladder.util.webImport("javascripts/jetpack/editable.js"));
eval(langladder.util.webImport("javascripts/jetpack/slidebar.js"));
slidebarhtml=langladder.util.webImport("slidebar.html");

var gDefaultText="enter search here in either " + langladder.util.getLangByCode(settings["learninglang"]) + " or " + langladder.util.getLangByCode(settings["nativelang"]);
var currentURL;


/**
* Tracks which page the user is reading
*/
jetpack.tabs.onFocus(function(event){
	currentURL=this.url;
});

/**
* When pages are loaded the text is highlighted
*/
jetpack.tabs.onReady(function(event){
	currentURL=this.url;

	if(highlightWord){
		highlightWord=false;
		langladder.util.highlightVocabWords(this.contentDocument);	
	}
});

/**
* Add buttons to the status bar. 
* 
*/
jetpack.statusBar.append({ 
	html: '<img style="" src='+langladder.config.rooturl+'/images/iconlangladnoback.png height=15 width=15/><span style="float:right;padding-top:0px; padding-bottom:20px; margin-right:3px; cursor:pointer;">Add Site</span>', 
	width: 70,
	onReady: function(widget){	
		widget_context=widget;
		//there is a bug here I don't understand
		$("P",widget).click(function(){
			if(savedsites[jetpack.tabs.focused.url]){
				langladder.remote.addBookmark(jetpack.tabs.focused.contentDocument.title, jetpack.tabs.focused.url, settings["learninglang"]);
				refreshFavorites();
				langladder.util.showJetpackNote(jetpack.tabs.focused.contentDocument.title + " saved.","Saved");
			}else{
				langladder.util.showJetpackNote("You already saved "+ jetpack.tabs.focused.contentDocument.title + ".","Not Saved");
			}
		});
	}
});

/**
* Adds context menu item
* 
*/
jetpack.menu.context.page.add({	 
	label: "Add to LangLadder",	 
	icon: langladder.config.rooturl+"/images/iconlangladnoback.png",
   command: function (event) { 
		var sentence="";
		var vocabWord=jetpack.selection.text.trim();
		jetpack.selection.html="<span id='"+ vocabWord.toLowerCase() +"' class='vocabword' style='background: red; color: black'>" +
		jetpack.selection.html + "</span>";
		var doc=jetpack.tabs.focused.contentDocument;
		var parents=$("#"+vocabWord.toLowerCase(),doc).parents();

		for(i=1;i<parents.length;i++){
			if(parents[i].tagName=="P"||parents[i].tagName=="DIV"||parents[i].tagName=="BODY"||parents[i].tagName=="HTML"){
				var innertext=parents[i].textContent;
				var regexTxt=new RegExp("[^.。।]+"+vocabWord+"[^.。।]+","im");
				sentence=regexTxt.exec(innertext)[0];
				/*sentence=new String(sentence);
				sentence=sentence.trim();
				sentence=sentence.split('\n')[0];
				if(sentence.length>100)
					sentence=sentence.substring(100)*/
				break;
			}
		}
		langladder.remote.translateStringFromPage(vocabWord.toLowerCase(), settings["learninglang"], settings["nativelang"],sentence);
	 }
}); 

/**
* Add the perf info to a given flashcard
*/
function FCPerfAdd(flashcard, wascorrect){
	var d=new Date();
	fcperformance[flashcard.forword]=fcperformance[flashcard.forword]||[];
	fcperformance[flashcard.forword].push({quiz_dt: d, correct: wascorrect}); 
	langladder.remote.addPerformance(flashcard.id,wascorrect, d);
}



/**
*Sets up the slidebar
*/
jetpack.slideBar.append({
	icon: langladder.config.rooturl+"/images/iconlangladnoback.png",
	width: 360,
	persist: true,
	onClick: slidebarsetup,
  	html: slidebarhtml
});


