var  seccion_activa;
var  section_hash;
var  sub_section_hash;
var sub_sub_section_hash;
var  old_s;

seccion_activa = null;
section_hash = null;
sub_section_hash = null;

function getQueryVidValue(){
	var q = document.location.search || document.location.hash;
	if(q){
		var startIndex =  q.indexOf("#");
		var endIndex = q.length;
		var story_id =  q.substring(q.indexOf("#", startIndex)+1, endIndex);
 		return story_id;
	}
	return "";
}



function setExternalLink() { 
	// some application vars
	var stateVar = "nothin'", displayDiv = document.getElementById("HistoryState");
	//NEW LUIS
	this.applyNew = function(linkMessage) {
		
		if(linkMessage != ""){
			// alert("linkMessage = "+linkMessage);
			unFocus.History.addHistory(linkMessage); 
		}else{
			document.title = "| CAF | "+linkMessage;
			displayDiv.innerHTML = "Current History: ";
			unFocus.History.addHistory("");
		}
	}
	//LUIS

	// This is the method that will recieve notifications from the History Keeper,
	// which will then update the state of the app.
	// :NOTE: This will be called when a new entry is added.
	// :NOTE: This will be called if another app on the page sets a history,
	//        so you will need to watch out for this, if you have more than
	//        one script using the history keeper, by doing some kind of 
	//        check to make sure that the new hash belongs to this app.
	//        I wouldn't expect this to be a problem all that frequently.
	this.historyListener = function(historyHash) {
		old_s = seccion_activa;
	
		if(historyHash == old_s){
			//alert("historyHash = "+historyHash + " /n old_s = "+old_s)
			return;
		}
		seccion_activa = historyHash;
		// update the stateVar
		stateVar = historyHash;
 		//alert(stateVar);
		// update display content
		displayDiv.innerHTML = "Current History: " + historyHash; 
		// update document title
		var historyHash_t = historyHash.split("/");
		section_hash = historyHash_t[1];
		sub_section_hash = historyHash_t[2];
		sub_sub_section_hash = historyHash_t[3];
		
		historyHash_t = historyHash_t.join(" | ");
		
		document.title = "| CAF | " + historyHash_t;

		if(clicked == false){
			clickHist_Button(); 
		 }
		 clicked = false;
	};
	/*this.getLink = function(){
	     //alert("stateVar= "+stateVar);
		pushDeeplink(stateVar);
	}*/
	// subscribe to unFocus.History
	unFocus.History.addEventListener('historyChange', this.historyListener);
	
	// Check for an initial value (deep link).
	// In this demo app, the historyListener can handle the task.
	this.historyListener(unFocus.History.getCurrent());
	
};
// instantiate and inialize the app. DOM has to be ready so we can get a ref to
// the HistoryState DOM element (aka the view), so we use QuickLoader to make
// sure it's ready.
//		<body onLoad="demoApp = new setExternalLink();">

//var demoApp;
//demoApp = new setExternalLink();

