var bUpdatingFeed = false;		
var sVideoFeed   = '';
var sPhotoFeed   = '';
var sCurrentFeed = '';
var sCurrentlyViewing = '';
var sCurrentVideo = 0;
var xmlhttpvideofeed;
var xmlhttpphotofeed;

var sVideoLink1;
var sVideoLink2;
var sVideoLink3;
var sVideoLink4;

var sVideoTitle1;
var sVideoTitle2;
var sVideoTitle3;
var sVideoTitle4;

var sVideoDescription1;
var sVideoDescription2;
var sVideoDescription3;
var sVideoDescription4;

var sVideoThumb1;
var sVideoThumb2;
var sVideoThumb3;
var sVideoThumb4;

var iFeedStart = 0;


function get_photofeed(sArea){
	sCurrentlyViewing = 'photos';
	reset_video_variables;
	
	sCurrentFeed = '';

	if(sPhotoFeed!=''){
		sCurrentFeed = sPhotoFeed;
		
		iFeedStart = 0;
		update_video_div();
	} else {
		if(bUpdatingFeed==false){
			bUpdatingFeed = true;
			show_video_loading_div();
			var d = new Date();
			load_photofeed('../common/photo_feed.asp?a=' + sArea + '&random=' + d.getFullYear() + d.getMonth() + d.getDate());
			//load_photofeed('../common/photo_feed.asp?a=' + sArea + '&random=' + d);
		}
	}
}

function get_videofeed(sYoutubeUsername){
	sCurrentlyViewing = 'videos';
	reset_video_variables;

	sCurrentFeed = '';

	if(sVideoFeed!=''){
		sCurrentFeed = sVideoFeed;
		
		iFeedStart = 0;
		update_video_div();
	} else {
		if(bUpdatingFeed==false){
			bUpdatingFeed = true;
			show_video_loading_div();
			load_videofeed('../common/video_feed.asp?u=' + sYoutubeUsername);
		}
	}
}

function load_videofeed(url) {
	xmlhttpvideofeed=null;

	if (window.XMLHttpRequest) {
		// code for Mozilla, etc.
		xmlhttpvideofeed=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// code for IE
		xmlhttpvideofeed=new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlhttpvideofeed!=null) {
		xmlhttpvideofeed.onreadystatechange=videofeed_state_Change;
		xmlhttpvideofeed.open("GET",url,true);
		xmlhttpvideofeed.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.");
	}
}			

function videofeed_state_Change() {

	// if xmlhttpvideofeed shows "loaded"
	if (xmlhttpvideofeed.readyState==4) {
		// if "OK"
		if (xmlhttpvideofeed.status==200) {
			// ...some code here...		 
			sVideoFeed = xmlhttpvideofeed.responseText;
			sCurrentFeed = sVideoFeed;			
			update_video_div();
			hide_video_loading_div();
		} else {
			document.getElementById('div_video_main').innerHTML = 'Error receiving video feed';
		}
	}

	bUpdatingFeed = false;
}


function load_photofeed(url) {
	xmlhttpphotofeed=null;

	if (window.XMLHttpRequest) {
		// code for Mozilla, etc.
		xmlhttpphotofeed=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// code for IE
		xmlhttpphotofeed=new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlhttpphotofeed!=null) {
		xmlhttpphotofeed.onreadystatechange=photofeed_state_Change;
		xmlhttpphotofeed.open("GET",url,true);
		xmlhttpphotofeed.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.");
	}
}			

function photofeed_state_Change() {

	// if xmlhttpphotofeed shows "loaded"
	if (xmlhttpphotofeed.readyState==4) {
		// if "OK"
		if (xmlhttpphotofeed.status==200) {
			// ...some code here...		 
			sPhotoFeed = xmlhttpphotofeed.responseText;
			sCurrentFeed = sPhotoFeed;
			update_video_div();
			hide_video_loading_div();
		} else {
			document.getElementById('div_photo_main').innerHTML = 'Error receiving video feed';
		}
	}

	bUpdatingFeed = false;
}



function update_video_div(){
	
	if (sCurrentFeed+''==''){
		if(sCurrentlyViewing == 'videos'){
			document.getElementById('video_loading_div').innerHTML = "<table width='100%' height='100%' border='0'><tr><td class='txt11p' align='center'>There are no videos to show</td></tr></table>";
		} else {
			document.getElementById('video_loading_div').innerHTML = "<table width='100%' height='100%' border='0'><tr><td class='txt11p' align='center'>There are no photos to show</td></tr></table>";
		}
		iVideoTimeout = setTimeout('show_video_loading_div()', 500)
	} else {
	
		reset_video_variables()

		var arrFeed = sCurrentFeed.split("\n");

		var j = 0;

		for(i = iFeedStart; i <= arrFeed.length-1; i++){
			var arrSingleVideo = arrFeed[i].split("|");

			arrSingleVideo[3] = trimString(arrSingleVideo[3]+'');

			switch(j){
				case 0:
					sVideoTitle1       = arrSingleVideo[0];
					sVideoLink1        = arrSingleVideo[1];
					sVideoThumb1       = arrSingleVideo[2];
					sVideoDescription1 = arrSingleVideo[3];
					break;
				case 1:
					sVideoTitle2       = arrSingleVideo[0];
					sVideoLink2        = arrSingleVideo[1];
					sVideoThumb2       = arrSingleVideo[2];
					sVideoDescription2 = arrSingleVideo[3];
					break;
				case 2:
					sVideoTitle3       = arrSingleVideo[0];
					sVideoLink3        = arrSingleVideo[1];
					sVideoThumb3       = arrSingleVideo[2];
					sVideoDescription3 = arrSingleVideo[3];
					break;
				case 3:
					sVideoTitle4       = arrSingleVideo[0];
					sVideoLink4        = arrSingleVideo[1];
					sVideoThumb4       = arrSingleVideo[2];
					sVideoDescription4 = arrSingleVideo[3];
					break;
				default:
					i = arrFeed.length;
					break;
			}

			j += 1;

		}

		if (sVideoThumb1==undefined){
			sVideoThumb1 = '';
		}
		if (sVideoThumb2==undefined){
			sVideoThumb2 = '';
		}
		if (sVideoThumb3==undefined){
			sVideoThumb3 = '';
		}
		if (sVideoThumb4==undefined){
			sVideoThumb4 = '';
		}

		if (sVideoThumb1!=''){
			if(sCurrentlyViewing == 'videos'){
				document.getElementById('div_video_thumb_1').innerHTML = "<img src='" + sVideoThumb1 + "' width='45' height='31' title='" + sVideoTitle1 + "'>";
			} else {
				document.getElementById('div_video_thumb_1').innerHTML = "<img src='../inc/ThumbGenerate.asp?VFilePath=../" + sVideoThumb1 + "&Width=45&Height=31&Quality=90' title='" + sVideoTitle1 + "'>";
			}
		} else {
			document.getElementById('div_video_thumb_1').innerHTML = "<img src='img/clr.gif' width='45' height='31' />";
		}
		if (sVideoThumb2!=''){
			if(sCurrentlyViewing == 'videos'){
				document.getElementById('div_video_thumb_2').innerHTML = "<img src='" + sVideoThumb2 + "' width='45' height='31' title='" + sVideoTitle2 + "'>";
			} else {
				document.getElementById('div_video_thumb_2').innerHTML = "<img src='../inc/ThumbGenerate.asp?VFilePath=../" + sVideoThumb2 + "&Width=45&Height=31&Quality=90' title='" + sVideoTitle2 + "'>";
			}
		} else {
			document.getElementById('div_video_thumb_2').innerHTML = "<img src='img/clr.gif' width='45' height='31' />";
		}
		if (sVideoThumb3!=''){
			if(sCurrentlyViewing == 'videos'){
				document.getElementById('div_video_thumb_3').innerHTML = "<img src='" + sVideoThumb3 + "' width='45' height='31' title='" + sVideoTitle3 + "'>";
			} else {
				document.getElementById('div_video_thumb_3').innerHTML = "<img src='../inc/ThumbGenerate.asp?VFilePath=../" + sVideoThumb3 + "&Width=45&Height=31&Quality=90' title='" + sVideoTitle3 + "'>";
			}
		} else {
			document.getElementById('div_video_thumb_3').innerHTML = "<img src='img/clr.gif' width='45' height='31' />";
		}
		if (sVideoThumb4!=''){
			if(sCurrentlyViewing == 'videos'){
				document.getElementById('div_video_thumb_4').innerHTML = "<img src='" + sVideoThumb4 + "' width='45' height='31' title='" + sVideoTitle4 + "'>";
			} else {
				document.getElementById('div_video_thumb_4').innerHTML = "<img src='../inc/ThumbGenerate.asp?VFilePath=../" + sVideoThumb4 + "&Width=45&Height=31&Quality=90' title='" + sVideoTitle4 + "'>";
			}
		} else {
			document.getElementById('div_video_thumb_4').innerHTML = "<img src='img/clr.gif' width='45' height='31' />";
		}


		show_video(1)

		var sPreviousLink = "<img src='img/clr.gif' width='9' height='9' />";
		var sNextLink     = "<img src='img/clr.gif' width='9' height='9' />";

		for(i = 0; i < roundup(arrFeed.length/4); i++){
			sIplus1 = i+1;
			sTempFeedStart = i*4;

			if (sTempFeedStart==iFeedStart-4){
				sPreviousLink = "<a href='#' style='font-size:11px;' onclick='iFeedStart=" + sTempFeedStart + "; update_video_div(); return false;' /><img src='img/com/orarrow-lft.gif' width='9' height='9' border='0' /></a>"
			}
			if (sTempFeedStart==iFeedStart+4){
				sNextLink     = "<a href='#' style='font-size:11px;' onclick='iFeedStart=" + sTempFeedStart + "; update_video_div(); return false;' /><img src='img/com/orarrow-rht.gif' width='9' height='9' border='0' /></a>"
			}
		}

		document.getElementById('div_video_nav_previous').innerHTML = sPreviousLink;
		document.getElementById('div_video_nav_next').innerHTML     = sNextLink;
	}
}

function show_video(iVideoNumber){
	
	if (sCurrentVideo!=iVideoNumber){
	
		switch(iVideoNumber){
			case 1:
				sVideoToShow      = sVideoLink1;
				sVideoTitle       = sVideoTitle1;
				sVideoDescription = sVideoDescription1;

				change_opacity( document.getElementById('video_thumb_td_1'), 100 )
				change_opacity( document.getElementById('video_thumb_td_2'), 50 )
				change_opacity( document.getElementById('video_thumb_td_3'), 50 )
				change_opacity( document.getElementById('video_thumb_td_4'), 50 )

				break;
			case 2:
				sVideoToShow      = sVideoLink2;
				sVideoTitle       = sVideoTitle2;
				sVideoDescription = sVideoDescription2;

				change_opacity( document.getElementById('video_thumb_td_1'), 50 )
				change_opacity( document.getElementById('video_thumb_td_2'), 100 )
				change_opacity( document.getElementById('video_thumb_td_3'), 50 )
				change_opacity( document.getElementById('video_thumb_td_4'), 50 )

				break;
			case 3:
				sVideoToShow      = sVideoLink3;
				sVideoTitle       = sVideoTitle3;
				sVideoDescription = sVideoDescription3;

				change_opacity( document.getElementById('video_thumb_td_1'), 50 )
				change_opacity( document.getElementById('video_thumb_td_2'), 50 )
				change_opacity( document.getElementById('video_thumb_td_3'), 100 )
				change_opacity( document.getElementById('video_thumb_td_4'), 50 )

				break;
			case 4:
				sVideoToShow      = sVideoLink4;
				sVideoTitle       = sVideoTitle4;
				sVideoDescription = sVideoDescription4;

				change_opacity( document.getElementById('video_thumb_td_1'), 50 )
				change_opacity( document.getElementById('video_thumb_td_2'), 50 )
				change_opacity( document.getElementById('video_thumb_td_3'), 50 )
				change_opacity( document.getElementById('video_thumb_td_4'), 100 )

				break;
		}
		if (sVideoToShow!=''){
			if(sCurrentlyViewing == 'videos'){

				document.getElementById('div_video_main').innerHTML = 'loading...';

				var objFlash = new FlashObject('http://www.youtube.com/v/' + sVideoToShow + '&rel=1', 'youtubemovie', '210', '147', '5', '#ffffff');
				objFlash.write('div_video_main');		

				document.getElementById('video_tab_1').src = 'img/com/phototab-off.gif';
				document.getElementById('video_tab_2').src = 'img/com/videotab-on.gif';
			} else {
				document.getElementById('div_video_main').innerHTML        = "<img src='../inc/ThumbGenerate.asp?VFilePath=../" + sVideoToShow + "&Width=210&Height=130&Quality=90' style='border:1px #eeeeee solid;' />";
				document.getElementById('video_tab_1').src = 'img/com/phototab-on.gif';
				document.getElementById('video_tab_2').src = 'img/com/videotab-off.gif';
			}
			document.getElementById('div_video_title').innerHTML       = sVideoTitle;
			document.getElementById('div_video_description').innerHTML = sVideoDescription;
		}
		
		sCurrentVideo = iVideoNumber;
	}
}

function reset_video_variables(){
	sVideoLink1  = '';
	sVideoLink2  = '';
	sVideoLink3  = '';
	sVideoLink4  = '';
	sVideoTitle1 = '';
	sVideoTitle2 = '';
	sVideoTitle3 = '';
	sVideoTitle4 = '';
	sVideoThumb1 = '';
	sVideoThumb2 = '';
	sVideoThumb3 = '';
	sVideoThumb4 = '';
	
	sCurrentVideo = 0;
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function show_video_loading_div(){
	document.getElementById('video_loading_div').style.display = '';
}

function hide_video_loading_div(){
	document.getElementById('video_loading_div').style.display = 'none';
}

function roundup(iNumberToRound){
	if(parseInt(iNumberToRound) < iNumberToRound){
		return parseInt(iNumberToRound)+1;
	} else {
		return parseInt(iNumberToRound);
	}
}
