//mediaPop.js
//
/*First time a user clicks on a video launcher, store the video asset name/number
in a cookie. Then show them the media choice page and let them choose which type of 
media player they will use for the rest of their interaction with the site. This choice 
is stored in a cookie. When they  click submit, get video asset name/number from 
cookie and serve them the appropriate file. On each video file page, put link to 
'Change Media Preference.' This would kill the media preference cookie and attempt 
to reload the video file. When the cookie isn't found the process begins again.*/


//Pop up window function. Get asset and media pref and then call 
//openMediaWin(); function with url
function mediaPop (dir,topic,asset) {
	document.cookie = "asset=" + dir + '|' + topic + '|' + asset +'; path=/';
	var allCookies = document.cookie;
	var mediaPref = getMediaPref(allCookies);
	var url;
	if (mediaPref == 'no prefs') {
		//path to page for users to choose wmp or qt
		url = dir+'choose.html';
	} else  {
		url = dir+topic+'/'+mediaPref+'/'+asset+'.html';
	}
	openMediaWin(url);
}

//Add media preference cookie and then get asset url and call loadFile function
function addPref (pref) {
	var when = new Date();
	when.setFullYear(when.getFullYear() + 2);
	if (pref == 'qt') {
		document.cookie = "media=qt; path=/; expires=" + when.toGMTString();
		loadFile('qt');
	} else if (pref == 'wmp') {
		document.cookie = "media=wmp; path=/; expires=" + when.toGMTString();
		loadFile('wmp');
	} else {
		error('There was a problem with your directive.');
	}
}

//When 'change media preference' is clicked the media cookie is deleted and the 
//preference page is loaded in the same window
function killCookie (oreo) {
	var when = new Date();
	when.setFullYear(when.getFullYear() - 1);
	document.cookie = oreo+"=not; expires=" + when.toGMTString();
	var url = '../../choose.html'; //Needs to changed dependent upon site
	location.replace(url);
}

//Get url from addPref function and load that page in same window
function loadFile (mediaPref) {
	var url;
	var values = new Array();
	values = getCookieValue();
	if (mediaPref == 'error') {
		//path to page for users to choose wmp or qt
		url = values[0]+'error.html';
	} else  {
		url = values[1]+'/'+mediaPref+'/'+values[2]+'.html';
	}
	location.replace(url);
	
	
}

//Get values of  asset cookie 
function getCookieValue () {
	var allCookies = document.cookie;
	var pos = allCookies.indexOf('asset=');
	if (pos != -1) {
		var start = pos + 6;
		var end = allCookies.indexOf(";", start);
		if (end == -1) {
			end = allCookies.length;
		}
		var value = allCookies.substring(start,end);
		var a = new Array();
		a = value.split('|');
		return a;
	} else {
		return 'No Cookie!';
	}
}

//Get value of media cookie or indicate that it has not been set yet
function getMediaPref (allCookies) {
	var pos = allCookies.indexOf('media=');
	if (pos != -1) {
		var start = pos + 6;
		var end = allCookies.indexOf(";", start);
		if (end == -1) {
			end = allCookies.length;
		}
		var value = allCookies.substring(start,end);
		return value;
	} else {
		return 'no prefs';
	}
}


//Function to make pop-up window
function openMediaWin (loc) {
	//define window parameters
	var w = 400;
	var h = 350;
	var stat = 'no';
	var scroll = 'no';
	var reSize = 'yes';
	var winName = 'VideoPopUp';
	
	//Call window and bring it to foreground
	var mediaWin = window.open(loc,winName,'width='+w+',height='+h+',toolbar=no,status='+stat+',scrollbars='+scroll+',resizable='+reSize+',menubar=no,directories=no');
	mediaWin.focus();
}

//Generic error message in plain text. Should probably change this to some 
//nicer looking message.
function error (msg) {
	document.open();
	document.write(msg);
	document.close();
}