var head = document.getElementsByTagName('head')[0];
var scripts;
var jQueryIsLoaded = false;
var vp_status = 'closed';
var minwidth = 39;
var maxwidth = 444;
var vp_current_size = 39;

/*
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} 
	else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} 
	else { 
		return false; 
	} 
}

newOnLoad = addEvent(window, 'load', fn)
*/
onload = function()
{
	// check if jQuery is loaded.  if not, load it in the page's header.
	if(head && typeof jQuery == 'undefined')
	{
		load_scripts();
	}
	
	else
	{
		//console.log('jQuery is loaded and head is defined. calling jquery_ready()');
		jquery_ready();
	}

}

function load_scripts()
{
	/* chargement de jQuery */
	var script1 = document.createElement('script');
	script1.setAttribute('type', 'text/javascript');
	
	var ie7 = ((navigator.appVersion.indexOf("MSIE 7.") == -1) ? false : true);
	
	if(ie7)
	{
		script1.setAttribute('src', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js');
	}
	else
	{
		script1.setAttribute('src', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js');
	}
	
	eval(head.appendChild(script1));

	attachCode();
}

function gen_iframe_panel(wskey, thisHTMLelem, is_agency)
{
	// ajout du DIV  div containing the open / close button and iFrame
	var vp2011 = document.createElement('div');
	vp2011.setAttribute('name', 'vp2011');
	vp2011.setAttribute('id', 'vp2011');
	try{
		vp2011.style.setAttribute('cssText', 'position:absolute; top:110px; right:10px;; z-index:100000; margin:0; padding:0; height:333px; width:48px; overflow:hidden; white-space:nowrap; border:none; ');
	}
	catch(e)
	{
		vp2011.setAttribute('style', 'position:absolute; top:110px; right:10px; z-index: 100000;  margin:0; padding:0; height:333px; width:48px; overflow:hidden; white-space:nowrap; border:none; ');
	}

	thisHTMLelem.appendChild(vp2011);
	document.getElementById('vp2011').className = "f3eede ";
	
	// ajout du DIV englobant le bouton et le iFrame 
	var wrapper = document.createElement('div');
	wrapper.setAttribute('name', 'vpwrapper');
	wrapper.setAttribute('id', 'vpwrapper');
	try{
		wrapper.style.setAttribute('cssText', 'margin:0; padding:0; height:333px; width:447px; border:none;');
	}
	catch(e)
	{
		wrapper.setAttribute('style', 'margin:0; padding:0; height:333px; width:447px; border:none;');
	}
	
	vp2011.appendChild(wrapper);
	
	// ajout Bouton pour ouvrir / fermer le panneau de vote
	var btn = document.createElement('input');
	btn.setAttribute('type', 'image');
	btn.setAttribute('name', 'open_vp2011');
	btn.setAttribute('id', 'open_vp2011');
	btn.setAttribute('src', 'img/1.jpg');
	try{
		btn.style.setAttribute('cssText', 'float:left; display:inline; margin:0; padding:0; height:333px; width:52px; border:none; cursor:pointer');
	}
	catch(e)
	{
		btn.setAttribute('style', 'float:left; display:inline; margin:0; padding:0; height:333px; width:52px; border:none; cursor:pointer');
	}
	
	// ajout du iFrame
	var iFrame = document.createElement('iframe');
	iFrame.setAttribute('name', 'vp2011iFrame');
	iFrame.setAttribute('id', 'vp2011iFrame');
	iFrame.setAttribute('src', 'img/2.jpg');
	iFrame.setAttribute('height', 333);
	iFrame.setAttribute('width', 395);
	iFrame.setAttribute('frameborder', 0);
	try
	{
		iFrame.style.setAttribute('cssText', 'float:left; display:inline; margin-top:-7px; padding:0; height:333px; width:395px; border:0px solid #000; overflow:hidden;');
	}
	catch(e)
	{
		iFrame.setAttribute('style', 'float:left; display:inline; margin-top:-7px;  height:333px; width:395px; border:none; overflow:hidden;');
	}
	
	wrapper.appendChild(btn);
	wrapper.appendChild(iFrame);
}

function slide_voting_panel()
{	
	//console.log('slide_voting_panel()');
	
	if(vp_status == 'closed')
	{
		slide_open_voting_panel();
	}
	else
	{
		slide_close_voting_panel();
	}
	
}

function slide_open_voting_panel()
{
	jQuery('#vp2011').animate({width:'360px'}, {duration:750});
	vp_status = 'open';
}

function slide_close_voting_panel()
{
	jQuery('#vp2011').animate({width:'52px'}, {duration:750});
	vp_status = 'closed';
}

function attachCode()
{
	if (typeof jQuery == 'undefined')
	{
		setTimeout('attachCode()', 1000);
		//console.log('Reload');
	} 
	else
	{
		//console.log('is loaded, calling jquery_ready().');
		jQueryIsLoaded = true;
		jquery_ready();
	}
	//return jQueryIsLoaded;
}

function jquery_ready()
{
		jQuery(document).ready(function($){
				gen_iframe_panel('ee4a4ef5-fb40-11e0-b072-29e815ab1d8f', document.getElementById('boomerang2011').parentNode, false);
				//console.log('jQuery(document).ready');
		document.getElementById('open_vp2011').onclick = function(){
				
				slide_voting_panel();
				//console.log('slide_voting_panel on click');
				return false;
			};
		
		document.getElementById('vp2011iFrame').onload = function(){
			if(vp_status == 'closed')
			{
				slide_voting_panel();
				//console.log('slide_voting_panel when jQuery is loaded');
				setTimeout('slide_voting_panel()', 2000);
				//console.log('slide_voting_panel after timeout');
			}
		}
	});
}
