var player = null;
var active_player = null;

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	addListeners();
}


function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}


function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
}

function callPlayer(xml_url,landing,container, p_width, p_height, _autoplay, _thumbnail){
	/* Determine what kind of pre-roll is supposed to be played here */
	var adTypes_adtonomy = 1;
	var adTypes_ova = 2;

	/* Set the default */
	var adType = adTypes_adtonomy;

	if (xml_url.match("/ova.xml$") == "/ova.xml")
		adType = adTypes_ova;

	//getting the loader for helper letter
	if (adType == adTypes_adtonomy)
		xml_url = xml_url + "&calledfrom="+document.URL; 

	//encoding the restricted chars
        var v_xml_url = xml_url.replace(/&/g,"%26");
        v_xml_url = v_xml_url.replace(/=/g,"%3D");
	v_xml_url = v_xml_url.replace("?","%3F");
	
	if ( !container ){
		container = "galleryPlayer";  
	}

	//just be sure landing is not carrying a full address. http in this string causes crossdomain messages!
	landing = landing.replace("http://www.autoguide.com","");

	var v_file = "/gallery/playlist.php?theMovie%3D" + landing; 
	var p_obj = container+"_obj";
	// if it is not set we consider it autoplay
	// with any negative number we made it stop

        //***************************************************************************
        // new forced condition: autoplay all players, except for landing of blog and 
	// just the first in case of more than one
	if (document.URL.toLowerCase() != "http://www.autoguide.com/auto-news/"  && document.URL.toLowerCase().indexOf("http://www.autoguide.com/auto-news/page") == -1 ) 
	{ 
                _autoplay = true;
	}
        //***************************************************************************

  	if ( _autoplay && _autoplay < 1){
		_autoplay = false;
	}
	else{
		_autoplay = true;
		//keep tracking of active one
		if ( active_player == null ){
			active_player = container;
		}
		else{
			//only one auto play in each page
			_autoplay = false;
		}	
	}	

	if ( !p_width ){
                p_width = '474';
        }
        if ( !p_height ){
                p_height = '384';
        }


	var so = new SWFObject('/gallery/modules/flashvideo/lib/player56.swf',p_obj,p_width,p_height,'9');
	if ( so['installedVer']['major'] >= 9 ){
	        so.addParam('allowscriptaccess','always');
	        so.addParam('allowfullscreen','true');
		so.addParam('wmode','transparent');
		so.addVariable('playlistfile', v_file);
       	 	so.addVariable('dock','true');
		so.addVariable('stretching','fill');	
       	 	so.addVariable('skin','/gallery/modules/flashvideo/lib/modieus.zip');

		if (adType == adTypes_adtonomy)
		{
			so.addVariable('autostart',_autoplay);
			so.addVariable('plugins','adtvideo');
			so.addVariable('adtvideo.config', v_xml_url);
		}
		else if (adType == adTypes_ova)
		{
			so.addVariable('autostart', 'true');
			so.addVariable('plugins', 'ova-trial');
			so.addVariable('config', v_xml_url);
			so.addVariable('provider', 'video');
			so.addVariable('duration', '30');
		}

	        so.write(container);
	}
	else{
		clearList(container);
		if ( landing.length == 11){
			var txt = '<object width="'+p_width+'" height="'+p_height+'"><param name="movie" value="http://www.youtube.com/v/'+landing+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+landing+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+p_width+'" height="'+p_height+'"></embed></object>';
		}
		else{
			var txt = 'Click <a href="'+document.domain+landing+'" >here</a> to download the file ';
		}
		document.write(txt);
	}		
}

function clearList(elm){
	var area = gid(elm);
	while (area.firstChild) 
	 {
		area.removeChild(area.firstChild);
	 }		
}

function gid(name)
{
  return document.getElementById(name);
}

