var TutorialVideoTagName = "tutorialvideo";
var StartPlayTagName = "startplay";
var VideoItemTagName = "videoitem";
var VideoItemTitleTagName = "title";
var VideoItemAdsDataTagName = "adsdata";
var VideoItemImageTagName = "image";

var VideoItemNumber = 5;
var VideoItemImageLinkPrefix = "ImgLink";
var VideoItemImagePrefix = "Img";
var VideoItemTextLinkPrefix = "TextLink";

var DefaultVideoItemTitle = "Coming soon ...";
var DefaultVideoItemImage = "comingsoon.jpg";
var ActiveVideoItemTitleColor = "#FFC800";
var InactiveVideoItemTitleColor = "#895500";

var PlayVideoFunctionName = "playVideo2";

var DefaultPlaylistFilename = "playlist.xml"

var xmlinstance;
var playItemCollection = new Array();
var startIndex = -1;
var currentIndex;
var tutorialVideo = null;
var isPlayingTutorialVideo = false;

var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;

function initPlaylist(filename){
    var playlist = DefaultPlaylistFilename;
    if(filename != null){
        playlist = filename;
    }
    
    xmlinstance = createXmlObj();
    xmlinstance.onreadystatechange =stateChangedHandler;
    try{
	    xmlinstance.open('GET', playlist, true);
	    xmlinstance.send(null);
    }
    catch(exception){
	    alert(exception);
    }
}

function createXmlObj(){
	var httprequest = false;
	if(window.XMLHttpRequest) {
		httprequest = new XMLHttpRequest();
		if(httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml');
	}
	else if(window.ActiveXObject){
		try{
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
			}
		}
	}
	return httprequest;
}

function stateChangedHandler(){
	if(xmlinstance.readyState == 4){
		if(xmlinstance.status != 200){
			alert("Loading playlist xml file failed!");
		}
		
		parsePlaylist(xmlinstance.responseXML);
		setPlayItems();
	}
}

function parsePlaylist(xmlDoc){
    if(xmlDoc != null){
        parseTutorialVideo(xmlDoc);
        parseStartIndex(xmlDoc);
        parsePlayItems(xmlDoc);
    }
}

function parseTutorialVideo(xmlDoc){
    if(xmlDoc != null){
        var node = xmlDoc.getElementsByTagName(TutorialVideoTagName);
        if(node.length > 0
            && node[0].firstChild != null
            && node[0].firstChild.data != ""){
            tutorialVideo = node[0].firstChild.data;
        }
    }
}

function parseStartIndex(xmlDoc){
    if(xmlDoc != null){
        var node = xmlDoc.getElementsByTagName(StartPlayTagName);
        if(node.length > 0
            && node[0].firstChild != null
            && node[0].firstChild.data != ""){
            startIndex = parseInt(node[0].firstChild.data, 10);
        }
    }
}

function parsePlayItems(xmlDoc){
    var itemCollection;
    var item;
    var title;
    var adsdata;
    var image;
    
    if(xmlDoc != null){
        itemCollection = xmlDoc.getElementsByTagName(VideoItemTagName);
    }else{
        itemCollection = new Array();//To let itemCollection.length be valid
    }
    
    for(var i=0;i<VideoItemNumber;i++) {
        if(i < itemCollection.length){
            item = itemCollection[i];
            title = item.getElementsByTagName(VideoItemTitleTagName);
            adsdata = item.getElementsByTagName(VideoItemAdsDataTagName);
            image = item.getElementsByTagName(VideoItemImageTagName);
        }else{
            title = null;
            adsdata = null;
            image = null;
        }
        
        var itemData = new Object();
        
        if(title != null
            && title.length > 0
            && title[0].firstChild != null
            && title[0].firstChild.data != ""){
            itemData.title = title[0].firstChild.data;
        }else{
            itemData.title = DefaultVideoItemTitle;
        }
        
        if(adsdata != null
            && adsdata.length > 0
            && adsdata[0].firstChild != null
            && adsdata[0].firstChild.data != ""){
            itemData.adsdata = adsdata[0].firstChild.data;
        }
                
        if(image != null
            && image.length > 0
            && image[0].firstChild != null
            && image[0].firstChild.data != ""){
            itemData.image = image[0].firstChild.data;
        }else{
            itemData.image = DefaultVideoItemImage;
        }
        
        playItemCollection.push(itemData);
	}
}

function setPlayItems(){
    var item;
    var link;
    var imageNode;
    var imageLinkNode;
    var textLinkNode;
    for(var i=0;i<playItemCollection.length;i++) {
        item = playItemCollection[i];
        
        if(item.adsdata != undefined){
            //link = "javascript:" + PlayVideoFunctionName + "('" + item.adsdata + "');currentIndex=" + i;
            link = "javascript:playItem(" + i + ")";
        }else{
            link = null;
        }
        imageNode = document.getElementById(VideoItemImagePrefix + i);
        imageLinkNode = document.getElementById(VideoItemImageLinkPrefix + i);
        textLinkNode = document.getElementById(VideoItemTextLinkPrefix + i);
        
        if(imageLinkNode != null && link != null){
            imageLinkNode.href = link;
        }
        
        if(textLinkNode != null){
            if(link != null){
                textLinkNode.href = link;
                textLinkNode.style.color = ActiveVideoItemTitleColor;
            }else{
                textLinkNode.style.color = InactiveVideoItemTitleColor;
            }
            
            if(isIE){
                textLinkNode.innerText = item.title;
            }else{
                textLinkNode.textContent = item.title;
            }
        }
        
        if(imageNode != null){
            imageNode.src = item.image;
        }
    }
}

function FlashCallout(evtObj){
    switch (evtObj.type){
        case "onPlayerInitComplete":
            if(tutorialVideo != null){
                playTutorialVideo();
            }else{
                startPlay();
            }
            break;
        case "onPlayerComplete":
            if(isPlayingTutorialVideo){
                isPlayingTutorialVideo = false;
                startPlay();
            }else{
                nextCurrentIndex();
                playItem(currentIndex);
            }
            break;
        case "onPlayerStateChange":
            if(evtObj.state == "connectionError"){
                if(isPlayingTutorialVideo){
                    isPlayingTutorialVideo = false;
                    startPlay();
                }else{
                    playItemCollection[currentIndex].canPlay = false;
                    nextCurrentIndex();
                    playItem(currentIndex);
                }
            }
            break;
        case "onPlayerError":
            switch (evtObj.msg){
                case "Load ads data file failed":
                case "Invalid ads data file format":
                    playItemCollection[currentIndex].canPlay = false;
                    nextCurrentIndex();
                    playItem(currentIndex);
                    break;
            }
            break;
    }
}

function playTutorialVideo(){
    if(tutorialVideo != null){
        isPlayingTutorialVideo = true;
        playFLV(tutorialVideo);
    }
}

function startPlay(){
    startIndex = Math.min(startIndex, VideoItemNumber - 1);
    startIndex = Math.max(startIndex, 0);
    currentIndex = startIndex;
    if(!isIndexValid(currentIndex)){
        nextCurrentIndex();
    }
    playItem(currentIndex);
}

function nextCurrentIndex(){
    var oldCurrentIndex = currentIndex;
    do{
        currentIndex++;
        currentIndex = currentIndex % VideoItemNumber;
        if(currentIndex == oldCurrentIndex){
            break;
        }
    }while(!isIndexValid(currentIndex));
}

function isIndexValid(index){
    if(index >= 0 && index < VideoItemNumber){
        var item = playItemCollection[index];
        if(item != undefined && item.adsdata != undefined && (item.canPlay == undefined || item.canPlay)){
            return true;
        }
    }
    return false;
}

function playItem(index){
    if(index >= 0 && index < VideoItemNumber){
        var item = playItemCollection[index];
        if(item != undefined && item.adsdata != undefined && (item.canPlay == undefined || item.canPlay)){
            isPlayingTutorialVideo = false;
            currentIndex = index;
            playVideo2(item.adsdata);
        }
    }
}
