// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
if (!eSpace) {
	var eSpace = {}
}

eSpace.render = function(node, templateUrl, data, callback){
	node = $(node);
	eSpace.load_template(templateUrl, function(res){
		var template = res.responseText;
		var result = template.process(data,{throwExceptions:true});
		node.innerHTML = result;
		if(callback)callback();
	})
}

eSpace.render_to_string = function(templateUrl, data, callback){
	eSpace.load_template(templateUrl, function(res){
		var template = res.responseText;
		callback(template.process(data,{throwExceptions:true}));
	})	
}

eSpace.load_template = function(templateUrl, callback){
	new Ajax.Request(templateUrl, {method:'get', onSuccess:function(res){
		callback(res)
	}})	
}

eSpace.HTML = {
	cascadeLists: function(parentId, childId, callbackFunction){
		$("body").append('<select style="display:none" id="' + parentId + childId + '"></select>');
	  var childOptions = $('#' + childId + ' option');
		$('#' + parentId + childId).html(childOptions);
	
	  $('#' + parentId).change(function(){
	    var parent = $('#' + parentId)[0];
			
			var selectedOptionValue = '';
			if (parent.selectedIndex > -1)
				selectedOptionValue = parent.options[parent.selectedIndex].value;
			
	    if (selectedOptionValue == '') 
	  		$('#' + childId).html($('#' + parentId + childId + ' option[value=""]').clone());
	  	else {
				var childs = $('#' + parentId + childId + ' option[parent="'+selectedOptionValue+'"]');
				if(childs.size() == 0)
					$('#' + childId).html($('#' + parentId + childId + ' option[value=""]').clone());
				else
	  			$('#' + childId).html($('#' + parentId + childId + ' option[parent="' + selectedOptionValue + '"]').clone());
	  	}
	
			$('#' + childId).trigger("change");
			
	    if(callbackFunction != null) callbackFunction(parent.options[parent.selectedIndex]);
		});
		
		$('#' + parentId).trigger("change");
	}
}

/**
 * Use this function to confirm any action you want
 * parameters: message to show
 * return: true if yes chosen, false if no chosen
 */
confirmAction = function(message){
  if(confirm(message))
    return true;
  else
    return false;
}

/**
 * This identifier is globally defined in order to maintain
 * some data used in getUniqueKey function
 */
identifier = 0;

/**
 * This function takes no parameters and uses identifier
 * to generate a unique key within the page it runs inside
 */
getUniqueKey = function(){
	var key = 'adc' + identifier;
	identifier++;
	return key;
}

/**
 * These are the set of functions
 * used to extract info from
 * listing pages to prepare
 * a printer friendly version
 */
extractBreadCrumb = function(){
	crumbs = $('.category-breadcrumb dl').children();
	result = [];
	for(var i =0; i< crumbs.length; i++){
		result.push('<span>'+$(crumbs[i]).text().replace('RSS ','')+'</span>')
	}
	return result.join('&raquo;');
}

extractListings = function(){
	return $('.category-list-main')[0].innerHTML;
}


