// JavaScript Document
var topic = "";
var videos = [];
var playHomeVideo = false;

function callbackInsiderTopics(xml) {
	var selTopics = document.getElementById("select_topic");
	
	var items = xml.getElementsByTagName("TAG");
	
	for (var i=0; i<items.length; i++) {
		selTopics.options[i] = new Option(items[i].getAttribute('NAME'),items[i].getAttribute('VALUE'));
	}
	selTopics.options[i] = new Option("All","ALL");
	if (defaultInsiderTopic == "ALL") {
		selTopics.options[i].selected=true;
	} else {
		selTopics.options[8].selected=true;
	}

}

function initInsiderTopics() {
	
	var ajaxTopics = new AjaxObject()
	var url = API_URL + GET_INSIDER_TOPICS;

    ajaxTopics.request(url, "", this.callbackInsiderTopics);
	
}

function initVideos(value) {

	
	var ajaxVideos = new AjaxObject()
	var url = API_URL + GET_INSIDER_VIDEOS;

	if (value != "ALL") {
		url += "&TAG=" + value;
	}
    ajaxVideos.request(url, "", this.callbackVideos);
}

//shuffle an array using Fisher-Yates algorithm: v1.0
function shuffle (o) {
  for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
  return o;
}

function callbackVideos(xml) {

	var items = xml.getElementsByTagName("MEDIA");
	for(var i=0; i<items.length; i++) {
		
		videos[i] = new VideoObject;
		
		var files = items[i].getElementsByTagName("FILE");
		
		//First File record
		videos[i].contentId = files[0].getElementsByTagName("CONTENTID")[0].firstChild.nodeValue;
		videos[i].description = files[0].getElementsByTagName("DESCRIPTION")[0].firstChild.nodeValue;
		videos[i].title = files[0].getElementsByTagName("NAME")[0].firstChild.nodeValue;
		
		var type = files[0].getElementsByTagName("TYPE")[0].firstChild.nodeValue;
		if (type == "JPG") {
			videos[i].imgURL = files[0].getElementsByTagName("URL")[0].firstChild.nodeValue;
		} else {
			videos[i].videoURL = files[0].getElementsByTagName("URL")[0].firstChild.nodeValue;
		}
		
		//Move to next File record
		videos[i].name = files[1].getElementsByTagName("DESCRIPTION")[0].firstChild.nodeValue;
		type = files[1].getElementsByTagName("TYPE")[0].firstChild.nodeValue;
		
		if (type == "JPG") {
			videos[i].imgURL = files[1].getElementsByTagName("URL")[0].firstChild.nodeValue + "&TYPE=JPG&WIDTH=95&HEIGHT=65";
		} else {
			videos[i].videoURL = files[1].getElementsByTagName("URL")[0].firstChild.nodeValue;
		}
	}
	
	if (defaultInsiderTopic != "TheRealBottleShock") {
		videos = shuffle(videos);
	}
    // Attach thumbnails to scroller
	obj = document.getElementById("scrollinner");
	var width = ((videos.length) * 130);
	width = width + "px";
	obj.style.width =  width;
	var innerhtml = "";
    	innerhtml += "<div>";
	for(i=0; i<videos.length; i++) {
		innerhtml += "<div class=\"scrollObj\">";
		innerhtml += "<div><img onclick=\"stillClick(" + i + ");\" src=\"" + videos[i].imgURL +  "\" /></div>";
		//innerhtml += "<div>" + videos[i].name + "</div>";
		innerhtml += "<div>" + videos[i].description + "</div>";
		innerhtml += "</div>";
		innerhtml += "<div style=\"float: left;\"><img src=" + spacer + " /></div>";
		innerhtml += "</div>";
	}
	innerhtml += "</div>";
	obj.innerHTML = innerhtml;



    // Set default video for page
	var n = Math.floor(videos.length*Math.random());
	var searchText;
	switch(getURLParam('VIDEO'))
	{
	case "FV1":
		searchText = "Bill Harlan: The Best Time to Do California Wine Country";
	break;
	case "FV2":
		searchText = "Suzanne Goin and Caroline Styne: The Best Taco Stand in Santa Barbara";
	break;
	default:
		searchText = "Thomas Keller: Best Rising Star Chefs in the Bay Area";
	break;
	}
	stillClick(getVideoId(searchText),"false");
	
	if (playHomeVideo) {
		playHomeVideo = false;
		playWineAndFood();
	}
	
}

// Returns the index from the videos array based on the title
function getVideoId(searchText) {
	for (i=0; i<videos.length; i++) 
	{
		if (videos[i].description == searchText) {
			return i;
		}
	}
	return 0;
}

function VideoObject() {

	this.contentId = "";
	this.imgURL = "";
	this.videoURL = "";
	this.topic = "";
	this.title = "";
	this.name = ""
	this.description = ""
	
}


//Varietal.. on Home page / Map Page and Callout

function callbackVarieties(xml) {
	
		var selVariety = document.getElementById("select_variety");
		
		var items = xml.getElementsByTagName("TAG");
		
		for (var i=0; i<items.length; i++) {
			selVariety.options[i] = new Option(items[i].getAttribute('NAME'),items[i].getAttribute('VALUE'));
		}

}


function initVarieties() {

	var ajaxVarieties = new AjaxObject()
	var url = API_URL + "/GetTags.aspx?CATEGORY=varietal";
    ajaxVarieties.request(url, "", this.callbackVarieties);
	
}

function getVideos(value) {
	videos.length = 0;
	initScroller();
	initVideos(value);
}

//Scroller

var id;
var movestatus;

function initScroller(width) {
	scrollObj = document.getElementById("scrollinner").style;
	scrollObj.top = "0px";
	scrollObj.left = "0px";
}

function stopit () {  //stop moveit loop
	if (movestatus==1) {
		window.clearTimeout(id);
		movestatus=0;
	}
}

function moveit(dirx){ //move icon group stopping at the ends

	if(videos.length <= 3) {
		return false;
	}
	
	movestatus=1;
	menuscrollerwidth=document.getElementById("scrollinner").offsetWidth;
	menuscrollerwidth=(menuscrollerwidth - scrollWidth)*-1;
	menuscrollerwidth=menuscrollerwidth + 100;
	
	moveobj=document.getElementById("scrollinner").style;
	oldposx=parseInt(moveobj.left);
	newposx = oldposx + dirx;
	if (newposx >0) newposx=0;
	if (newposx < menuscrollerwidth) newposx=menuscrollerwidth;
	moveobj.left=newposx + "px";
	id=window.setTimeout('moveit('+dirx+')', 20);
}

function initCallouts() {
	
	if (document.getElementById("map_callout") != null) {
		initVarieties();
		document.getElementById("select_variety").style.width = "140px";
	}
}

//Print

function printPage()
{
	window.print();
}





function getURLParam (name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return null;
	else
		return results[1];
}


///Select a wine region from the map callout

function wineRegionSelect() {

	url="./Wine_Explorer.aspx?REGION=";
	
	var obj = document.getElementById("map_callout_select");

	url += obj.options[obj.selectedIndex].value;
	
	window.location = url;

}

