var ie = (document.all) ? true : false;

// Onload des fonctions
function addLoadEvent(func)
{
	var oldonload = window.onload;
	
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else 
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

// Récupération d'éléments par nom de classe
document.getElementsByClassName = function(className)
{
	var children = (document.all) ? document.all : document.getElementsByTagName('*');
	var elements = new Array();
	var counter = 0;
	  
	for (var i = 0; i < children.length; i++)
	{
		var child = children[i];
		var classNames = child.className.split(' ');
		for (var j = 0; j < classNames.length; j++)
		{
			if (classNames[j] == className) 
			{
				elements[counter] = child;
				counter++;
				break;
			}
		}
	}
	
	return elements;
}

// Popup
function popup(windowHref, windowName, windowWidth, windowHeight)
{
	var attrs = 'width='+windowWidth+',height='+windowHeight+',top=' + ((screen.availHeight - windowHeight)/2) + ',left=' + ((screen.availWidth - windowWidth)/2) + ',toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no';
	
	if (oNewPopup = open(windowHref, windowName, attrs))
	{
		oNewPopup.focus();
		oNewPopup.moveTo(((screen.availWidth - windowWidth)/2), ((screen.availHeight - windowHeight)/2));
		return false;
	}
	else
	{
		//ouverture en target _blank
		return true;
	}
}

//
function rolloverProductBlock()
{
	var form = window.document.forms;
	var input;
	
	// Boucle dans les formulaires
	for(var i = 0; i < form.length; i++)
	{
		// Si le formulaire a comme ID "prod_more_form"
		if(form[i].id == "prod_more_form")
		{
			input = form[i].getElementsByTagName("input");
			
			// Boucle dans les inputs
			for(var j = 0; j < input.length; j++)
			{
				if(input[j].type == "checkbox")
				{
					input[j].onclick = function()
					{	
						var divBlock = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
						
						if(this.checked)
						{
							divBlock.className = "block1_on";
						} else
						{
							divBlock.className = "block1";
						}
					};
				}
			}
		}
	}
}

function openImage(imgPath)
{	

	var sPath = imgPath.substring(0, imgPath.lastIndexOf("/") + 1);
	var sImageFile = imgPath.substr(imgPath.lastIndexOf("/") + 1);
	
	var imgWidth = 10;
	var imgHeight = 10;
	var winPosX = 10;
	var winPosY = 10;
	
	var regExp = new RegExp("^_[0-9]{1,4}_[0-9]{1,4}_.*","g");
	if(regExp.test(sImageFile))
	{
		sImageFile = sImageFile.substr(sImageFile.indexOf("_") + 1);
		sImageFile = sImageFile.substr(sImageFile.indexOf("_") + 1);
		sImageFile = sImageFile.substr(sImageFile.indexOf("_") + 1);
		
		imgPath = sPath + sImageFile;
	}	
	
	var preview = window.open("","","resizable=yes,toolbar=no,scrollbars=no,menubar=no,status=no,directories=no,width="+imgWidth+",height="+imgHeight+",left="+winPosX+",top="+winPosY+"");
	
	preview.document.write('<html><head><title>Cliquez sur l\'image pour fermer cette fenêtre<\/title><\/head>'
	+'<body onBlur="self.focus();" onLoad="centerPopup();" style="margin: 0;">'
	+'<table style="margin: 0; width: 100%; height: 100%;" cellspacing="0" cellpadding="0">'
	+'<tr>'
	+'<td align="center" valign="middle">'
	+'<a href="javascript: self.close()">'
	+'<img id="image" border="0" alt="Chargement de l\'image" src="'+imgPath+'" onLoad="centerPopup();" />'
	+'</a>'
	+'</td>'
	+'</tr>'
	+'</table>'
	+'<scri'+ 'pt type="text/javascript">'
	+'function centerPopup()'
	+'{'
	+'	var target = window.document.getElementById("image");'
	+'		var winPosY = ((screen.height - target.height) / 2);'
	+'		var winPosX = ((screen.width - target.width) / 2);'
	+'		window.resizeTo(target.width + 80, target.height + 140);'
	+'		window.moveTo(winPosX, winPosY);'
	+'}'
	+'</scri'+ 'pt>'
	+'</body></html>');
	preview.focus();
}

function initLightboxPopup(json)
{
	if(json.error)
	{
		alert(json.errorMessage);
	} 
	else
	{
		var caption = "";
		var filename = json.filename;
		var width = json.width;
		var height = json.height;
		
		var objLink = document.createElement('a');
	
		objLink.setAttribute('href',filename);
		objLink.setAttribute('rel','lightbox');
		objLink.setAttribute('title',caption);
		if(typeof width != 'undefined')
		{
			objLink.setAttribute('width', width);
		}
		if(typeof height != 'undefined')
		{
			objLink.setAttribute('height', height);
		}
		Lightbox.prototype.start(objLink);
	}
	
}

function lightboxPopupFailed()
{
	alert("Error");	
}

function SWFDelegate(sImagePath)
{
	var width = 400;
	var height = 400;
	var caption = "";
	
	new Ajax.Request
	(
	 	rPath + "imageinfo_json.php",
		{
			method: 'get',
			parameters: {image: sImagePath},
			onSuccess: function(transport, json) {initLightboxPopup(json); },
			onFailure: function(transport, json) {lightboxPopupFailed(json); }
		}
	);
}
