var theRequest;
var infoLightbox = false;
var infoWaitTimer = null;
var vendorWindow = null;

function openSite ( theUrl ) {

	if (window.vendorWindow && !vendorWindow.closed) {
		vendorWindow.location = theUrl;
	} else {

		theWidth = 850;
		theHeight = 750;
		leftCoord = (screen.width / 2) - (theWidth / 2);
		topCoord = (screen.height / 2) - (theHeight / 2);

		vendorWindow = window.open(theUrl,"vendorwindow",
			"width="+theWidth+",height="+theHeight+",top="+topCoord+",left="+leftCoord+
			",menubar,toolbar,location,status,directories,resizable,scrollbars");

	}
	vendorWindow.focus();
	return false;

}

function shareListing (theUrl) {

	theWidth = 340;
	theHeight = 515;
	leftCoord = (screen.width/2) - (theWidth/2);
	topCoord  = (screen.height/2) - (theHeight/2) - 20;

	shareWindow = window.open(theUrl,"sharewindow","width="+theWidth+
	",height="+theHeight+",top="+topCoord+",left="+leftCoord+
	",menubar=no,toolbar=no,location=no,status=no,directories=no,resizable,scrollbars=no");
	shareWindow.focus();

	return false;

}

function getVideoHtml ( theVideo, isVendor, showPlaying, autoPlay, clickImage ) {

	theUrl = '/videos/video.swf?video='+theVideo+'&amp;autoplay='+autoPlay+'&amp;isvendor='+isVendor+'&amp;showplaying='+showPlaying;
	if (clickImage) theUrl += '&amp;clickimage='+clickImage;
	theWidth = '320';
	theHeight = '260';

	var theHtml = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
	theHtml += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" ';
	theHtml += 'width="'+theWidth+'" height="'+theHeight+'" id="video-object">';
	theHtml += '	<param name="allowScriptAccess" value="sameDomain" />';
	theHtml += '	<param name="movie" value="'+theUrl+'" />';
	theHtml += '	<param name="loop" value="false" />';
	theHtml += '	<param name="menu" value="false" />';
	theHtml += '	<param name="quality" value="autohigh" />';
	theHtml += '	<param name="scale" value="noscale" />';
	theHtml += '	<param name="salign" value="lt" />';
	theHtml += '	<param name="wmode" value="transparent" />';
	theHtml += '	<embed src="'+theUrl+'" loop="false" menu="false" quality="autohigh" ';
	theHtml += 'scale="noscale" salign="lt" wmode="transparent" width="'+theWidth+'" height="'+theHeight+'" ';
	theHtml += 'id="video-embed" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" ';
	theHtml += 'pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	theHtml += '</object>';

	return theHtml;

}

function loadVideo ( theVideo, clickImage ) {
	var autoPlay = (clickImage) ? false : true;
	var theHtml = getVideoHtml(theVideo,'false','true',autoPlay,clickImage);
	document.getElementById('video').innerHTML = theHtml;
}

function watchVideo ( theVideo, isVendor, theText ) {

	isVendor = (isVendor && theText && document.getElementById('videoheading'))?true:false;

	if (isVendor) {
		var theHtml = getVideoHtml(theVideo,'true','false','true','');
		document.getElementById('video').innerHTML = theHtml;
		document.getElementById('videoheading').innerHTML = 'You are watching:';
		document.getElementById('videotext').className = 'large';
		document.getElementById('videotext').innerHTML = theText;
	} else {
		var theHtml = getVideoHtml(theVideo,'false','true','true','');
		document.getElementById('video').innerHTML = theHtml;
	}

	theScroll = getPageScroll();
	if (theScroll[1] > 245) window.scroll(0,225);

	return false;

}

function resetVideo ( isVendor ) {

	document.getElementById('video').innerHTML = '<img src="/images/video.jpg" alt="Please choose a video below" width="320" height="260" />';

	if (isVendor) {
		document.getElementById('videoheading').innerHTML = 'Please choose a video';
		document.getElementById('videotext').className = '';
		document.getElementById('videotext').innerHTML = 'Select a video below to view it. Click the &#8220;View Video&#8221; button and the video will display here.';
	} else nowPlaying('');

}

function nowPlaying ( theVideo ) {
	var timeStamp = new Date();
	xmlRequest('/videos/?cmd='+timeStamp.getTime()+'&watching='+theVideo,1);
}

function updateNowPlaying () {
	if (theRequest.readyState == 4 && theRequest.status == 200 && theRequest.responseText) {
		document.getElementById('videocontent').innerHTML = theRequest.responseText;
	}
}

function displayInfo ( theUrl ) {
	var timeStamp = new Date();
	xmlRequest(theUrl+'?cmd='+timeStamp.getTime(),2);
	return false;
}

function updateDisplayInfo () {
	if (theRequest.readyState == 4 && theRequest.status == 200 && theRequest.responseText) {

		if (infoWaitTimer != null) clearTimeout(infoWaitTimer);
	
		if (lightboxReady == false) {
			infoWaitTimer = setTimeout("updateDisplayInfo()",1000);
		} else {
	
			// prep objects
	
			var infoWidth = 600;
			var infoHeight = 580;
	
			var objLightbox = document.getElementById('infolightbox');
			var objLoadingImage = document.getElementById('loadingImage');
			var arrayPageSize = getPageSize();
			var arrayPageScroll = getPageScroll();
			
			objOverlay.onclick = function () {hideInfoLightbox(); return false;}
	
			if (!infoLightbox) {
				var objBody = document.getElementsByTagName("body").item(0);
				objLightbox = document.createElement("div");
				objLightbox.setAttribute('id','infolightbox');
				objLightbox.style.display = 'none';
				objLightbox.style.position = 'absolute';
				objLightbox.style.zIndex = '100';	
				objBody.insertBefore(objLightbox, objOverlay.nextSibling);
				infoLightbox = true;
			}	
		
			// center loadingImage if it exists
			if (objLoadingImage) {
				objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 50) / 2) + 'px');
				objLoadingImage.style.left = (((arrayPageSize[0] - 100) / 2) + 'px');
				objLoadingImage.style.display = 'block';
			}
		
			// set the Overlay to take up the whole page and show
			objOverlay.style.width = '100%';
			objOverlay.style.height = (arrayPageSize[1] + 'px');
			objOverlay.style.display = 'block';

			// Center the info lightbox
			lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 37 - infoHeight) / 2);
			var lightboxLeft = ((arrayPageSize[0] - 22 - infoWidth) / 2);
			objLightbox.style.top = (lightboxTop < 0) ? "0" : lightboxTop + "px";
			objLightbox.style.left = (lightboxLeft < 0) ? "0" : lightboxLeft + "px";
			objLightboxDetails.style.width = infoWidth + 'px';
	
			// A small pause between the image loading and displaying is required with IE,
			// this prevents the previous image displaying for a short burst causing flicker.
			if (navigator.appVersion.indexOf("MSIE")!=-1) pause(250);
	
			if (objLoadingImage) objLoadingImage.style.display = 'none';
			objLightbox.style.display = 'block';
			adjustOverlay(true);
		
			document.getElementById('infolightbox').innerHTML = theRequest.responseText;

		}

	}
}

function hideInfoLightbox () {

	var objLightbox = document.getElementById('infolightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	objOverlay.style.width = '1px';
	objOverlay.style.height = '1px';

	lightboxTop = 0;

	objOverlay.onclick = function () {hideLightbox(); return false;}

}

function xmlRequest ( theUrl, onReady ) {

	theRequest = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			theRequest = new XMLHttpRequest();
        } catch(e) {
			theRequest = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	theRequest = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		theRequest = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		theRequest = false;
        	}
		}
    }
	if (theRequest) {
		if (onReady == 1) {
			theRequest.onreadystatechange = updateNowPlaying;
		} else theRequest.onreadystatechange = updateDisplayInfo;
		theRequest.open("GET",theUrl,true);
		theRequest.send("");
	}

}