var lastDiv = '';
var currentIndex = 0;
var viewCount = 0;
var isOnRandom = 1;
var isOnContinue = 0;
var currentLength = 10;

function getPlayer(gid) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[gid];
	} else {
		return document[gid];
	}
}

function playerReady(obj) {
	var id = obj['id'];
	var version = obj['version'];
	var client = obj['client'];
	try {
		player = getPlayer(id);
		player.sendEvent('playitem',0);
		//setTimeout("getPlayerLength()",10000);
		getUpdate('item',0);
		logVisit();
		player.style.zIndex = 1;
	} catch(err) {
		setTimeout("instancePlayer()",100);
	}
}

function logView(dataArray) {
	logremote = new logging();
	logremote.setCallbackHandler(dummyFunction);
	logremote.logOneOf('view',dataArray[0],dataArray[1],dataArray[2],wsesid,1,dataArray[3]);
}

function logVisit() {
	logremote2 = new logging();
	logremote2.setCallbackHandler(dummyFunction);
	logremote2.logVisit(wsesid,wid);
}

function getUpdate(typ,pr1,pr2,swf) { 
	if(typ == 'item' && !isOnContinue) {
		viewCount++;
		if (viewCount <= currentLength) {
			setTimeout("getPlayerLength()",2000);
			currentIndex = pr1;
			itemDataStuff = player.itemData(pr1);
			logDataArray = itemDataStuff['id'].split('|');
			storeDataArray = itemDataStuff['author'].split('|');
			
			changeTitle(itemDataStuff['title'],storeDataArray[1],1);
			changeInvis('invis' + storeDataArray[0]);
			
			logView(logDataArray);
		} else {
			if (!isOnRandom) {
				changeToRandom();
			} else {
				changeToContinue();
			}
		}
	}
}

function instancePlayer() {
	if (PhotosSlideshowID != undefined) {
		try {
			testVar = player;
		} catch(err) {
			playerReady({id:PhotosSlideshowID,version:'3',client:'huh'});
		}
		
	} else {
		alert('The Slideshow Photo Widget is required!');
	}
}

function loadPhoto(obj) {
	if (player != undefined) {
		newPhotoLoad = 1;
		player.sendEvent('STOP',1);
		player.loadFile(obj);
		player.sendEvent('PLAY',1);
	} else {
		alert('The Slideshow Photo Widget is required!');
	}
}

function changeTitle(title,theLink,aJS){
	if (aJS) {
		document.getElementById('WindowImageLink').href = "javascript:clickLink('" + theLink + "')";
	} else {
		document.getElementById('WindowImageLink').href = theLink;
	}
	document.getElementById('WindowImageLink').innerHTML = title;
}

function changeShop(storeName) {
	viewCount = 0;
	isOnRandom = 0;
	isOnContinue = false;
	loadPhoto({file:'/Playlists/' + wid + '/'+storeName+'Shopping.xml'});
	currentLength = 10;
}

function changeToRandom() {
	viewCount = 0;
	isOnRandom = 1;
	isOnContinue = false;
	loadPhoto({file:'/Playlists/' + wid + '/WindowShopping.xml'});
	currentLength = 10;
}

function changeToContinue() {
	viewCount = 0;
	isOnRandom = 0;
	isOnContinue = true;
	loadPhoto({file:'/Playlists/ContinueWindowShopping.xml'});
	changeTitle('Click to Continue','javascript:changeToRandom();',0);
	changeLastToGrey();
	currentLength = 5;
	
}

function changeInvis(showDiv) {
	document.getElementById(showDiv).childNodes[0].childNodes[0].style.opacity = 1;
	document.getElementById(showDiv).childNodes[0].childNodes[0].style.filter = 'alpha(opacity=100)';
	document.getElementById(showDiv).childNodes[0].childNodes[0].style.MozOpacity = 1;
	if(lastDiv != '' && lastDiv != showDiv)
	{
		document.getElementById(lastDiv).childNodes[0].childNodes[0].style.opacity = '.25';
		document.getElementById(lastDiv).childNodes[0].childNodes[0].style.filter = 'alpha(opacity=25)';
		document.getElementById(lastDiv).childNodes[0].childNodes[0].style.MozOpacity = .25;
	}
	
	lastDiv = showDiv;
}

function changeLastToGrey() {
	document.getElementById(lastDiv).childNodes[0].childNodes[0].style.opacity = '.25';
	document.getElementById(lastDiv).childNodes[0].childNodes[0].style.filter = 'alpha(opacity=25)';
	document.getElementById(lastDiv).childNodes[0].childNodes[0].style.MozOpacity = .25;
}

function clickLink(theLink) {
	linkDataArray = theLink.split('~');
	if (linkDataArray[0] == 1) {
		window.open(linkDataArray[1],'window' + unique());
	}
}

function unique(){
	
	var now = new Date();
	var ms = now.getTime();
	return ms;
}

function jump() {
	window.location.hash = '#bottomLink';
}

function dummyFunction() {}

function getPlayerLength() {
	currentLength = player.getLength();
	if (!isOnRandom) {
		currentLength = currentLength*2;
	}
}

setTimeout("instancePlayer()",1000);