/*
	Lavender - data connection
*/
function LavConn(){
    this.baseURL = "datasvc/";
	this.caseStudyURL = "casestudylistsvc.ashx";
	this.caseStudyDetailURL = "casestudysvc.ashx";
	this.mapPOIURL = "mapPOISVC.ashx";

    this.processingStarted = new Signal(this, "processstarted");
    this.processingEnded = new Signal(this, "processingended");
}
var $lconn = LavConn.prototype;

/* DATA CONNECTION FUNCTIONS */
$lconn.sendAndLoad = function(url, data, success, fault){ 
	try{
		$(document).css({cursor:"wait"});
	}catch(err){}
    if(this.processingStarted){
		try{
			this.processingStarted.dispatch();
		}catch(err){}
    }
    var ref = this;
	$.ajaxSetup({
		success: function(result, status, httprequest){
			success(result, result.type);
			$(document).css({cursor:"auto"});
			if(this.processingEnded){
				try{
					this.processingEnded.dispatch();
				}catch(err){}
			}
		},
        error:function(httprequest, status, error){
			fault(status, error);
			$(document).css({cursor:"wait"});
			if(this.processingEnded){
				try{
					this.processingEnded.dispatch();
				}catch(err){}
			}
		}
    });
    $.ajax({
        url: url,
		data: data,
        dataType: "json",
        contentType: "application/json"
    });
}
$lconn.loadTemplateFile = function(success, fault, url){
	$.ajax({
        url: url || "templates/temp_cs.html",
        dataType: "html",
        success: function(result, status, httprequest){
    		var $template = $(result);
			var divs = {};
			for (var i =0;i<$template.length;i++){
				if($template[i].nodeName.toLowerCase() == "div"){
					divs[$($template[i]).attr("id")] = $template[i];
				}
			}
			success(divs);			
        },
        error:function(httprequest, status, error){
			fault(status, error);
        }   
    });
}
/* RETRIEVAL */
$lconn.loadHomePageList = function(data, success, fault){
	if(data){
		this.sendAndLoad(this.baseURL+this.caseStudyURL, data, success, fault);
	}else{
		this.sendAndLoad(this.baseURL+this.caseStudyURL, null, success, fault);
	}
}
$lconn.loadCaseStudyDetails = function(data, success, fault){
	this.sendAndLoad(this.baseURL+this.caseStudyDetailURL, data, success, fault);
}
$lconn.loadNextBatch = function(data, success, fault){
	//data = null;
	this.sendAndLoad(this.baseURL+this.caseStudyURL, data, success, fault);
}
$lconn.loadPOI = function (data, success, fault) {
    this.sendAndLoad(this.baseURL + this.mapPOIURL, data, success, fault);
}


