

var fpConf = {
	videoFile: '',
	showPlayList: false,
	baseURL: '',
	autoPlay: false,
	autoBuffering: true,
	startingBufferLength: 5,
	bufferLength: 30,
	loop: false,
	hideControls: false,
	initialScale: 'orig', //- or 'fit'
	showPlayListButtons: true,
	useNativeFullScreen: true,
    useHwScaling: true,
    //splashImageFile:'http://borat/alchemy_reality/images/video_splash.jpg',
    //scaleSplash: false,
    controlBarGloss: 'high'
}



videos = {
    pobj : null,
    videoDir : '',

    videoFiles : null, //- to  be populated onload with list of files within directory above
    videoCategories : null,

	icon : '../images/play_now.jpg',
	isPlayingIcon : '../images/play_now_orange.gif',

	nowPlaying  : '',

    init : function init() {
        if (document.getElementById) {
			this.videoFiles = Videos;
			this.videoCategories = Categories;

			for(var i=0; i<this.videoFiles.length; i++) {
				//console.log('playimg'+i);
				o = document.getElementById('playimg'+i);
				if(o) {
                    o.playSrc = this.isPlayingIcon;
				    o.oSrc = o.src;
                }
			}

            //this.pobj = document.getElementById("videoPlayer");
            this.loadPlayerConfig();
//
			//console.log('INIT:');
			//console.log(this);
			//console.log(this.pobj);

			//window.setInterval('videos.setVideoStatus()', 250);
        }
    },

    loadPlayerConfig : function () {
	    this.pobj.setConfig(fpConf);
        //this.pobj.setConfig( {videoFile: this.videoDir+this.videoFiles[0]['name']
        //                    , loop: false } );
        this.pobj.setConfig( { videoFile: '../images/video_splash.png'
                             , duation:  10
                             , loop: true } );
        this.pobj.DoStop();
    },

    loadVideo : function(ind) {

        if(document.getElementById('videoList') && document.getElementById('videoList').blur) {
            document.getElementById('videoList').blur();
        }
        
        
        //- record that this video was selected
        //this.sendHttp('log_trailer_view.php?trailer_id=' + videos.videoFiles[ind]['id']);
        
        //- Set playing icon for video
		for(var i=0; i<this.videoFiles.length; i++) {
			//console.log('playimg'+i);
			o = document.getElementById('playimg'+i);
            if(o && o.src != o.oSrc) {
				o.src = o.oSrc;
			}
		}
		o = document.getElementById('playimg'+ind)
		if(o) o.src = o.playSrc;

        videos.nowPlaying = videos.videoFiles[ind]['name'];

        //console.log(this);
		///console.log(this.pobj);
		//- Show the Loading.. text
		vt = document.getElementById('videoTitle');
        vt.innerHTML = videos.nowPlaying;
        //vt.innerHTML = 'Loading Video...';
        //vt.style.color = '#ef5500';

		//vp = document.getElementById('videoPlaying');
        //vp.innerHTML = videos.nowPlaying;


		//- Initiate the video
		if(videos.pobj.getIsPlaying()) {
            videos.pobj.DoStop();
            videos.pobj.Reset();
        }
        videos.pobj.setConfig( {videoFile: videos.videoDir+videos.videoFiles[ind]['file']
                                , loop: false} );
        videos.pobj.DoPlay();

    },


    setVideoStatus : function() {
        vt = document.getElementById('videoTitle');
        //console.log( this.pobj.getTime() );
        if(this.pobj.getIsPlaying() || this.pobj.getIsPaused() && this.nowPlaying) {
            vt.innerHTML = this.nowPlaying;
            vt.style.color = '';
        }
    },

	outputVideoList : function() {

		videos.videoFiles = Videos;
		videos.videoCategories = Categories;
		videos.videoDir = videoDir;
		/*Do I add var output to the video object???*/
		
		switch(output) {
			case 'dropdown':
				//out = '<ul id="videoList">';
				out = '<select id="videoList" onchange=\"videos.loadVideo( this.options[this.selectedIndex].value )\">'
					+ '<option value=""></option>';
		
				for(var j=0; j<videos.videoCategories.length; j++) {
				
					out += '<optgroup label="'+videos.videoCategories[j]['name']+'">';
		
					for(var i=0; i<videos.videoFiles.length; i++) {
						if(videos.videoFiles[i]['cat'] != videos.videoCategories[j]['id']) continue;
						//console.log('playimg'+i);
						/*out += '<li><a href="javascript://" onclick="videos.loadVideo('+i+')">'
							+ '<img width="20" height="20" border="0" src="'+videos.icon+'" id="playimg'+i+'" alt="[Play] " title="Play '+videos.videoFiles[i]['name']+'" /></a>'
							+ '<a href="javascript://" onclick="videos.loadVideo('+i+')">'
							+ videos.videoFiles[i]['name']
							+ '</a>'
							+ '</li>';*/
		
						out += '<option value="'+i+'">'
							+ videos.videoFiles[i]['name']
							+ '</option>';
					}
					out += '</optgroup>';
				}
		
				out += '</select>';
				
			break;
			case 'list':
			
				out = '<div id="videoList">\n';
				out += '	<ul>\n';
				for(var j=0; j<videos.videoCategories.length; j++) {
					
					out += '		<li>&nbsp;'+videos.videoCategories[j]['name']+'\n';
					out += '			<ul>\n';
					
					for(var i=0; i<videos.videoFiles.length; i++) {
						
						if(i % 2 == 0) {
							style = 'style="background-color:#eeeeee"';
						} else {
							style = 'style="background-color:white"';
						}	
						
						if(videos.videoFiles[i]['cat'] != videos.videoCategories[j]['id']) continue;
						out += '				<li onClick="videos.loadVideo('+i+')" '+style+'>&nbsp;'+videos.videoFiles[i]['name']+'</li>\n';
					}
					out += '			</ul>\n';
					out += '		</li>\n';
				}
				out += '	</ul>\n';
				out += '</div>';
			
			break;
			case 'biglist':
			
				out = '<div id="videoList">';
				for(var j=0; j<videos.videoCategories.length; j++) {
					
					out += '<h1>'+videos.videoCategories[j]['name']+'</h1>';
					
					for(var i=0; i<videos.videoFiles.length; i++) {
						if(i < videos.videoFiles.length-1) {
							style='style="border-bottom:1px solid #000"';
						} else {
							style="";
						}
						if(videos.videoFiles[i]['cat'] != videos.videoCategories[j]['id']) continue;
						out += '<div class="videoListItem" '+style+'>';
						out += '<div class="videoListItemDesc">';
						out += '<h2>'+videos.videoFiles[i]['name']+'</h2>';
						out += '<p>'+videos.videoFiles[i]['desc']+'</p>';
						out += '</div>';
						out += '<div class="videoListItemLink" style="padding:0">';
						out += '<img src="../images/play.png" style="margin:0" onClick="videos.loadVideo('+i+')" title="Click to play"/>';
						out += '</div>';
						out += '<div class="clear"></div>';
						out += '</div/>';
					}
				}	
				out += '</div>';
		}
		
		document.write( out );
	},


    //- request data from the server via the XML HTTP object
    sendHttp: function(url, func) {
        //if(http_timeout) clearTimeout(http_timeout);
        var response = "";
        var safari = navigator.userAgent.indexOf("Safari")>=0;
        var xo = this._getHttpObj();
        if(!xo || safari && !func) {
            (new Image()).src = url;
        }
        else {
            var to_func = function() {
                xo.abort();
            }		
            xo.open("GET", url, true );
            if(func) { 
                xo.onreadystatechange = function() {
                    if( xo.readyState==4 ) {
                        func( xo );
                    }
                }
            }
            xo.send( null )
        }
    },

    _getHttpObj: function() {
        var o=null;
        if(window.XMLHttpRequest) { 
            o = new XMLHttpRequest()
        }
        else if(window.ActiveXObject) {
            o = new ActiveXObject("Msxml2.XMLHTTP");
            if(!o) {
                o = new ActiveXObject("Microsoft.XMLHTTP")
            }
        }
        return o
    }
}


// wait for the page to fully load before initializing
window.onload = function(e) {
    videos.init();
}
