//----------------------------------
//     Define Adswizz variables
//----------------------------------

var embedCode='<object type="application/x-shockwave-flash" data="http://www.optionauto.com/wp-content/themes/optionauto/preview.swf" width="610" height="332" id="videoH" style="visibility: visible;"><param name="menu" value="false"><param name="allowScriptAccess" value="always"><param name="scale" value="default"><param name="allowFullScreen" value="true"><param name="wmode" value="transparent"><param name="flashvars" value="videoWidth=610&amp;videoHeight=332&amp;orderBy=PublishedDate&amp;maxVideos=40&amp;searchVisible=off&amp;predefinedSearch=&amp;predefinedUser=optionauto"></object>';
var qadswizzComponent;
var remaining_seconds;
var counter_span;
var COUNTER_ID = "adswizzcompcountdown";
var countdown_message;
var countdownid;
var container;


//----------------------------------
//        Debugging tool 
//-----------------------------------
// You can activate the Javascript console for debugging 
//if (!window['console']) {
	// Enable console
	//if (window['loadFirebugConsole']) {
		//window.loadFirebugConsole();
	//} else {
		// No console, use Firebug Lite
		//var firebugLite = function(F,i,r,e,b,u,g,L,I,T,E){if(F.getElementById(b))return;E=F[i+'NS']&&F.documentElement.namespaceURI;E=E?F[i+'NS'](E,'script'):F[i]('script');E[r]('id',b);E[r]('src',I+g+T);E[r](b,u);(F[e]('head')[0]||F[e]('body')[0]).appendChild(E);E=new Image;E[r]('src',I+L);};
		//firebugLite(document,'createElement','setAttribute','getElementsByTagName','FirebugLite','4','firebug-lite.js','releases/lite/latest/skin/xp/sprite.png','https://getfirebug.com/','#startOpened');
	//}
//} else {
	// console is already available, no action needed.
//}


//-----------------------------------------------------------
//   Adswizz Sample code for Video-on-display integration
//------------------------------------------------------------
function adswizzReady(adswizzComponent) {
	if(adswizzComponent)
	{
		try
		{
			adswizzComponent.addEventListener("AdStarted", "onAdStarted");
			adswizzComponent.addEventListener("AdStopped", "onAdStopped");
			adswizzComponent.addEventListener("AdRemainingTimeChange", "onAdRemainingTimeChange");
			adswizzComponent.addEventListener("RequestPlayerPlay", "onRequestPlayerPlay");
			qadswizzComponent=adswizzComponent;
			container = document.getElementById("myad");
		}
		catch(e)
		{
			console.log(e);
		}
	}
	else
	{
		console.log('Adswizz Component not found!');
	
	}
}		
	
// 	
function adswizzFailed(adswizzComponent) {
        if(!qadswizzComponent)
        {
           	// implement page behavior if VIP is not available
           	// i.e. display player and play content or display image/game 
         	var xw = document.getElementById('myad');
			xw.innerHTML=embedCode;	
        }
}

function onAdStarted(event) {
	var message = 'onAdStarted';
	for (var key in event) {
		message += ' ' + key + '=' + event[key];
	}
	//console.log(message);
	if(showCountDown()){
    	//console.log(event.secondsRemaining);
    	addCountDown(event.secondsRemaining);	
	}
}


function onAdStopped(event) {
	var message = 'onAdStopped';
	for (var key in event) {
		message += ' ' + key + '=' + event[key];
	}
	//console.log(message);
	//alert(message);	

}

function onAdRemainingTimeChange(event) {
	var message = 'onAdRemainingTimeChange';
	for (var key in event) {
		message += ' ' + key + '=' + event[key];
	}
	//console.log(message);
	//enter code if necessary
	remaining_seconds = event.secondsRemaining + 1;
	
}
function onRequestPlayerPlay(event) {
	var message = 'onRequestPlayerPlay';

	for (var key in event) {
		message += ' ' + key + '=' + event[key];
	}
	//console.log(message);
		removeCountDown();
	var xw = document.getElementById('myad');
   	xw.innerHTML=embedCode;	
}

//----------------------------------------
//  4.Countdown utilities
//----------------------------------------	
function showCountDown(){
	//console.log("show countdown");
	//var msg = 'The video will start in [seconds] seconds';
	var msg = 'La video commence dans [seconds] secondes';
			
	countdown_message = document.createElement('p');
	countdown_message.style.color = "white";
	countdown_message.style.margin = "0";
	countdown_message.style.padding = "3px";
	countdown_message.style.backgroundColor = "black";
	countdown_message.style.position = "absolute";
	countdown_message.style.bottom = "0px";
	countdown_message.style.right = "5px";
	countdown_message.style.font = "10px arial,sans-serif";
			
	if(msg.indexOf('[seconds]') != -1)
		{
			msg = msg.replace(/\[seconds\]/i, "<span id=\"" + COUNTER_ID + "\"></span>");
		}
	else
		{
			msg = msg + ' <span id="' + COUNTER_ID + randm + '"></span>';
		}
			
	countdown_message.innerHTML = msg;
	container.appendChild(countdown_message);
	counter_span = document.getElementById(COUNTER_ID);
	return true;
}
	
function countDown (){
	if(remaining_seconds > 0){
		remaining_seconds--;
	}
	counter_span.innerHTML = remaining_seconds;
}
	
function addCountDown(seconds){
	remaining_seconds = seconds;
	counter_span.innerHTML = seconds;
	countdownid = setInterval("countDown()", 1000);
}
	
function removeCountDown(){
	clearInterval(countdownid);
	var container = document.getElementById("myad");
		
	if(container.getElementsByTagName('p').length > 0){
		container.removeChild(countdown_message);
	}
}
	
