// File:	vmsclip.js
// Author:	Angus McIntyre <amcintyre@vmsinfo.com>
// Date:	03/10/2003
// Updated:	08/20/2003
// For:		VMS <http://www.vmsinfo.com/>
// Version:	1.1
//
// Library functions for manipulating the Media Player and in-page images.

// ======================================================================
//								CLIP MANAGER
// ======================================================================

// currentClipManager
//
// Global variable storing the current ClipManager object. This will be
// null for QuickView subscribers, who have unhindered access to clips.
// In the case of non-subscribers, it will be bound to a ClipManager
// object which tracks the clips licensed by the user. The manager will
// be created dynamically when the page is loaded.

var currentClipManager;

// addClip
//
// Record that a clip, identified by segment index, has been licensed
// by the user. This only updates the local ClipManager object; update
// of the remote records is handled when the clip is played.

function addClip(index) {
	this.licensed[index] = 1;
}

// checkClip
//
// Check the status of a given clip, identified by index (this is its
// index within the list of QuickView clips on the page). If the clip
// has not been previously played, the user will be warned that they
// are about to be charged and given the option to cancel.

function checkClip(index) {
	if (this.licensed[index] == 0) {
		if (top.confirm("There is a charge for viewing " +
						"this clip. If you click the 'OK' button, you will " +
						"be billed $" + this.price + ". If you do not wish " +
						"to license this clip, please click 'Cancel' now.")) {
			this.licensed[index] = 1;
			return true;
		}
		else {
			return false;
		}
	}
	else {
		return true;
	}
}

// ClipManager
//
// The ClipManager is an object that determines whether to warn the user
// about the potential cost of the clips they intend to view. It 'knows'
// which clips they have previously licensed, and the price at which
// clips are 'sold' to the user (all clips must be priced the same).

function ClipManager(clips,price) {
	this.addClip = addClip;
	this.checkClip = checkClip;
	this.price = price;
	if (clips > 0) {
		this.licensed = new Array(clips);
		for(i=0;i<clips;i++) {
			this.licensed[i] = 0;
		}
	}
}

// ======================================================================
//								MAIN
// ======================================================================

// runclip
//
// Play the selected clip in the Media Player.

function runclip(path, item) {
	if (top.header.MediaPlayer21) {
		top.header.MediaPlayer21.URL= path
	}
	if (navigator.userAgent.indexOf('Mac') != -1 || navigator.userAgent.indexOf('Firefox') != -1) {
		window.open(path.replace('playclip.asp', 'playmacclip.asp'), 'player', 'width=260,height=264')
	}
	else {

			if (item == "QL") {

				top.header.QLtest.style.display = "block";
				top.header.QLtest.style.position = "absolute";
				top.header.QLtest.style.top = "19px";
				top.header.QLtest.style.left = "22px";

				top.header.MediaPlayer21.style.position = "absolute";
				top.header.MediaPlayer21.style.height = "65px";
				top.header.MediaPlayer21.style.top = "185px";

			}
			else {
				top.header.QLtest.style.display = "none";
/*
				top.header.QLtest.style.position = "absolute";
				top.header.QLtest.style.top = "18px";
				top.header.QLtest.style.left = "0px";
*/
				top.header.MediaPlayer21.style.position = "absolute";
				top.header.MediaPlayer21.style.height = "240px";
				top.header.MediaPlayer21.style.top = "00px";
			}
		}
	
}

// canManipulateImages
//
// check if the browser we're using can do clever stuff with document
// images.

function canManipulateImages() {
	if (document.images)
		return true;
	else
		return false;
}

// preloadImage
//
// pre-create and pre-load an image so that it's ready when we want it.

function preloadImage(imageURL) {
	if (gImageCapableBrowser) {
		image = new Image();
		image.src = imageURL;
		return image;
	}
}

// updateQuickViewLinks
//
// Walk the page and switch all the images in it to show the default
// 'QuickView' text. Then set the specified image to show the "Now
// Playing" text.
//
// 03/10/2003	AM	Note that this will lose if there are any other
//					images in the page besides the QuickView links.
//					If that ever becomes the case, this script will
//					need to be rewritten.

function updateQuickViewLinks(active) {
	for(var i = 0; i < document.images.length; i++) {
	  if (document.images[i].alt == "QuickView[SM]") {
		document.images[i].src = QVimage.src;
	  }
	  else if (document.images[i].alt == "QuickListen[SM]") {
		document.images[i].src = QLimage.src;
	  }
	  else {
		document.images[i].src = BLOGimage.src;
	  }
	}
document.images[active].src = imageNowPlaying.src;
	document.images[active].src = imageNowPlaying.src;
}

// playClip
//
// Update the link image, and then call the 'runclip' function to
// actually play the video.

function playClip(button,clip, item) {
	if ((currentClipManager == null) || 
		(currentClipManager.checkClip(button))) {
		updateQuickViewLinks(button);
		runclip(clip, item);
	}
}

// gImageCapableBrowser - is this browser hip to images? Set up
// a global variable so that we don't have to keep calling a function
// (useful if the function becomes costly to compute).

gImageCapableBrowser = canManipulateImages();

// Preload the necessary images

imageDefault = preloadImage('../../../shared/textQuickView.jpg');
QVimage = preloadImage('../../../shared/textQuickView.jpg');
QLimage = preloadImage('../../../shared/textQuickListen.jpg');
BLOGimage = preloadImage('../../../shared/textViewBlog.jpg');
imageNowPlaying = preloadImage('../../../shared/textNowPlaying.jpg');

