YAHOO.namespace("cx.expedia.widgetlibrary");
YAHOO.cx.expedia.widgetlibrary.virtualTour = function(cfg) {
	this.panToX = 0;
	this.timerID = 0;
	this.zIndex = 500;
	this.dragok = false;
	this.leftToRight = true;
	this.mouseX = 0;
	this.dx = 0;
	
	// if no config is provided we use these defaults
	this.divId = "panoramaImgDiv";
	this.imgDropDownList = "imgDropdownList";
	this.baseURL = "http://media.expedia.com";
	
	// else use whats in config
	if (cfg != null) {
		this.divId = cfg.divId;
		this.imgDropDownList = cfg.imgDropDownList;
		this.baseURL = cfg.baseURL;
	}
	
	var fixOpera = function() {
		// Stop Opera selecting anything whilst dragging.
	    if (window.opera) {
			document.write("<input type='hidden' id='Q' value=' '>");
	    }
	}

	init = function() {
	    fixOpera();
	}();
	
	this.setupImageDrag = function() {
	    YAHOO.util.Event.addListener(this.divId, "mousedown", this.down, this, true);
	    YAHOO.util.Event.addListener(this.divId, "mouseup", this.up, this, true);
	}

	this.move = function(e, vt) {
	    if (!e) e = window.event;
	    if (vt.dragok) {
			var elem = document.getElementById(vt.divId);
			vt.panToX = vt.dx + e.clientX - vt.mouseX;
			elem.style.backgroundPosition = vt.panToX + "px 0px";
		    return false;
	    }
	}
	    
	this.down = function(e, vt) {
	    if (!e) e = window.event;
		
	    var element = document.getElementById(vt.divId);
	    fixOpera();
	    
	    vt.dragok = true;
	    element.style.zIndex = vt.zIndex++;
	    vt.dx = parseInt(vt.panToX);
	    vt.mouseX = e.clientX;
	    
	    YAHOO.util.Event.addListener(vt.divId, "mousemove", vt.move, vt, true);
	    return false;
	}
	    
	this.up = function(vt) {
	    vt.dragok = false;
	    YAHOO.util.Event.removeListener(vt.divId, "mousemove", vt.move, vt, true);
	}

	this.panImage = function(vt){
	    return function() {
			var elem = document.getElementById(vt.divId);
			if (vt.leftToRight) 
			    vt.panToX++;
			else
			    vt.panToX--;
			elem.style.backgroundPosition = vt.panToX + "px 0px";
	    };
	}

	this.startVirtualTour = function() {
	    if (this.timerID == 0) {
			var ptr = this.panImage(this);
			this.timerID = setInterval(ptr, 10);
	    }
	}
	
	this.stopVirtualTour = function() {
	    if (this.timerID != 0) {
			clearInterval(this.timerID);
			this.timerID = 0;
	    }
	}
	
	this.goLeftToRight = function() {
	    this.stopVirtualTour();
	    this.leftToRight = true;
	    this.startVirtualTour();
	}
	
	this.goRightToLeft = function() {
	    this.stopVirtualTour();
	    this.leftToRight = false;
	    this.startVirtualTour();
	}

	this.changePanoramaImage = function() {
	    var startAgain = false;
	    if (this.timerID != 0) {
			this.stopVirtualTour();
			startAgain = true;
	    }
	    
	    var dropDownElem = document.getElementById(this.imgDropDownList);
	    var idx = dropDownElem.selectedIndex;
	    
	    if (idx == 0) {
	    	document.getElementById(this.divId).style.backgroundImage = "";
	    } else {
    		var imgUrl = this.baseURL + dropDownElem[idx].value;
    		document.getElementById(this.divId).style.backgroundImage = "url(" + imgUrl + ")";
	    }
	    
	    if (startAgain) this.startVirtualTour();
	}
	
	this.changePanoramaImageTo = function(url) {
		var startAgain = false;
	    if (this.timerID != 0) {
			this.stopVirtualTour();
			startAgain = true;
	    }
	    
   		var imgUrl = this.baseURL + url;
   		var element = YAHOO.util.Dom.get(this.divId);
   		if (element != null) 
   		{
   			element.style.backgroundImage = "url(" + imgUrl + ")";
   		}
	    
	    if (startAgain) this.startVirtualTour();
	}
};

