var _flight_path = "/flights/getDeals.aspx?dealType=flight";
//var _hotel_path = "/UBF/getDeals.aspx?dealType=hotel";

function TravelDeals(type, output, loader, message, error,origin) {
	this.type = type;
	this.output = output;
	this.loader = loader;
	this.message = message;
	this.error = error;
	this.from = origin;
}

TravelDeals.prototype.getDeals = function(from, to, noLoader) {
  //this.hideOutput();
  if (noLoader == null)
    //this.displayLoader();
	var url = "";
	switch (this.type) {
		case "flight":
		  url = _flight_path;
			break;
		case "hotel":
		  url = _hotel_path;
			break;
	}
	if (this.from != null)
	  url += "&origin=" + this.from
	if (to != null)
	  url += "&destination=" + to;
	this.getResults(url);
}

TravelDeals.prototype.xmlLoaded = function(data) {
	this.output.innerHTML = data.responseText;
	this.hideLoader();
        this.displayOutput();
        this.onload();
        //initLightbox();
        initRestrictionsModal();
	// display number of results in selection panel
    }



TravelDeals.prototype.onload = function() {}

TravelDeals.prototype.getResults = function(url) {
	var o = this;
	var callback = {
		success: o.xmlLoaded,
		failure: o.displayError,
		scope: o
	}
	url += "&= " + Math.random();
	YAHOO.util.Connect.asyncRequest("GET", url, callback);
}

TravelDeals.prototype.displayOutput = function() {
	this.output.className = this.output.className.replace("off-left", "");
}

TravelDeals.prototype.hideOutput = function() {
	this.output.className = this.output.className.replace("off-left", "") + "off-left";
}

TravelDeals.prototype.displayLoader = function() {
	this.loader.className = this.loader.className.replace("off-left", "");
}

TravelDeals.prototype.hideLoader = function() {
	//this.loader.className = this.loader.className.replace("off-left", "") + "off-left";
}

TravelDeals.prototype.displayError = function() {
  //this.hideLoader();
  //this.error.className = this.error.className.replace("off-left", "")
}

TravelDeals.prototype.hideError = function() {
	this.error.className = this.error.className.replace("off-left", "") + "off-left";
}

function initRestrictionsModal() {
    if (!document.getElementsByTagName) { return; }
    var objBody = document.getElementsByTagName("body").item(0);
    var anchors = document.getElementsByTagName("a");

    // loop through all anchor tags
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];

        if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")) {

            //anchor.onclick = showLightbox;
            new JB.Class.Modal({
                openTogglers: anchor, // i.e "a.clickme"
                url: "/partials/modal.restriction.html",
                onOpen: function (e, instance) {
                    var $modal = instance.modal;
                    var detailHeader = $(e.target).parents('.description').children('.detailsBubble').children('h4').text();
                    var detail = $(e.target).parents('.description').children('.detailsBubble').children('p').text();

                    var txt = "Other restrictions apply.";
                    detail += "<a href='/legal/flights-terms' target='_blank' >" + txt + "</a>";

                    $modal.find('.detail-header').text(detailHeader);
                    $modal.find('.details').html(detail);                   

                },
                onClose: function (e, instance) {
                    // on close callback.  (optional)
                }
            });
        }
    }
}
