var featuredVids = [];

function VideoObject() {

	this.contentId = "";
	this.imgURL = "";
	this.videoURL = "";
	this.topic = "";
	this.title = "";
	this.name = ""
	this.description = ""
	
}

function initFeaturedVideos() {
	
	var ajaxFeaturedVideos = new AjaxObject()
	var url = "http://community.landofwineandfood.com/SearchECM-Proxy.aspx?TAG=Insider,Featured,Video";

    ajaxFeaturedVideos.request(url, "", this.callbackFeaturedVideos);
}

function callbackFeaturedVideos(xml) {
    
	var items = xml.getElementsByTagName("CONTENT");
	
	for(var i=0; i<items.length; i++) {
	
	    featuredVids[i] = new VideoObject;
	
        featuredVids[i].contentId = items[0].getElementsByTagName("CONTENTID")[0].firstChild.nodeValue;
        featuredVids[i].title = items[0].getElementsByTagName("NAME")[0].firstChild.nodeValue;

        
		var media = items[i].getElementsByTagName("MEDIA");
		var files = media[0].getElementsByTagName("FILE");
		
		//First File record
		featuredVids[i].description = files[0].getElementsByTagName("DESCRIPTION")[0].firstChild.nodeValue;
		
		var type = files[0].getElementsByTagName("TYPE")[0].firstChild.nodeValue;
		if (type == "JPG") {
			featuredVids[i].imgURL = files[0].getElementsByTagName("URL")[0].firstChild.nodeValue;
		} else {
			featuredVids[i].videoURL = files[0].getElementsByTagName("URL")[0].firstChild.nodeValue;
		}
		
		//Move to next File record
		featuredVids[i].name = files[1].getElementsByTagName("DESCRIPTION")[0].firstChild.nodeValue;
		type = files[1].getElementsByTagName("TYPE")[0].firstChild.nodeValue;
		
		if (type == "JPG") {
			featuredVids[i].imgURL = files[1].getElementsByTagName("URL")[0].firstChild.nodeValue + "&TYPE=JPG&WIDTH=95&HEIGHT=65";
		} else {
			featuredVids[i].videoURL = files[1].getElementsByTagName("URL")[0].firstChild.nodeValue;
		}
	}
	
    //put info in callout
	obj = document.getElementById("insiders_callout_center");
	var innerhtml = "";
    	
	for(i=0; i<featuredVids.length; i++) {
		innerhtml += "<div class=\"callout_video\"><div class=\"callout_vid_img\">";
		innerhtml += "<img src=\"" + featuredVids[i].imgURL +  "\" /></div><div class=\"callout_vid_info\">";
		innerhtml += "<div class=\"callout_vid_name\">" + featuredVids[i].name + "</div>";
		innerhtml += "<div class=\"callout_vid_title\">" + featuredVids[i].description + "</div>";
		innerhtml += "<div class=\"callout_vid_watch\"><a href=\"http://www.landofwineandfood.com/California_Insiders.aspx?VIDEO=" + featuredVids[i].contentId + "\">Watch more...</a></div></div>";
		innerhtml += "<div style=\"float: none; clear: both;\"></div></div>";
	}
	innerhtml += "</div>";
	obj.innerHTML = innerhtml;
}

function AjaxObject() {
	this.createRequestObject = function() {
		var ro = null;
		// Firefox, Safari, Opera, et al.
		if ( window.XMLHttpRequest ) {
			ro = new XMLHttpRequest();
			if (ro.overrideMimeType) {
				ro.overrideMimeType('text/xml');
			}
		//IE
		} else if (window.ActiveXObject) {
			try {
				ro = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					ro = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (ro == null) {
			alert('Cannot create XMLHTTP instance');
			return false;
		}
		return ro;
	}
	this.request = function(url, data,_funcCallback) {
		if(_funcCallback != null) me.funcCallback = _funcCallback;
		this.http.open("GET",url, true);
		this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		this.http.onreadystatechange = this.handleResponse;
		this.http.send(data);
	}
	this.handleResponse = function() {
		
		if ( me.http.readyState == 4) {
			
			if ( me.http.status == 200 ) {
				// IE
				
				if (window.ActiveXObject)
				{
					var doc=new ActiveXObject("Microsoft.XMLDOM");
					doc.async="false";
					doc.loadXML(me.http.responseText);
				}
				// Firefox, Safari, Opera, et al.
				else
				{
					//var parser=new DOMParser();
					var doc=me.http.responseXML;//parser.parseFromString(me.http.responseText,"text/xml");
				}
				var xmlNode=doc.documentElement;
				if(typeof me.funcCallback=='function')
					me.funcCallback(xmlNode);
				else
					return xmlNode;
			}
		}
		if ((me.http.readyState == 1) && (typeof me.funcFail == 'function')) { 
				me.funcFail(); 
		}
	}

	var me = this;
	this.http = this.createRequestObject();
	var funcCallback = null;
	var funcFail = null;
}

initFeaturedVideos(); 

