//var scriptPath='../';
function openUrl(url) {
	window.location = url;	
}
function ifEnterSubmit(e, form) {
	if 	(getKeyCode(e)==13) {
		form.submit();	
	}
}
function getKeyCode(e) {
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    var s = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    var s = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    var s = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
  return s;
}
function toogleMenu() {
	ddiv =	$('#menufla');
	dimg =	$('#menuimg');
	if (ddiv) {
		if (ddiv.css('display') == 'none' || ddiv.css('display') == '') {
			ddiv.css('display','block');
			if (dimg) dimg.attr('src', dimg.attr('src').split('.gif').join('_.gif'));
		}
		else {
			ddiv.css('display','none');
			dimg.attr('src', dimg.attr('src').split('_.gif').join('.gif'));
		}
	}
};

var timeOut;
function showMenu() {
	var ddiv =	$('#menufla');
	var dimg =	$('#menuimg');
	ddiv.css('display','block');
	if (dimg) dimg.attr('src', dimg.attr('src').split('xxo.gif').join('xxo_.gif'));
	// dimg.src = dimg.src.split('xxo.gif').join('xxo_.gif');
	waitToHide();
};
function waitToHide() {
	clearTimeout(timeOut);
	timeOut = setTimeout(hideMenu, 3000);	
}
function enterMenu() {
	clearTimeout(timeOut);
}
function outMenu() {
	timeOut = setTimeout(hideMenu, 1000);
}
function hideMenu() {	
	clearTimeout(timeOut);
	var ddiv =	$('#menufla');
	var dimg =	$('#menuimg');
	ddiv.css('display','none');
	if (dimg) dimg.attr('src', dimg.attr('src').split('xxo_.gif').join('xxo.gif'));	
};
function resize( width, height, posx, posy){
	window.focus();
	if (posx=='') posx=100;
	if (posy=='') posy=50;
	window.moveTo(posx, posy)
	window.resizeTo(width, height)	
}

/* FORM */
/* select a select option by value */
function rwnFormSelectSelByValue(id, value) {  	
	if (el = document.getElementById(id) ) {
		if (el.options)
		{
			for (num=0; num<el.options.length; num++) {
				if (el.options[num].value == value) {
					el.options[num].selected = 1;
				}
			}
		}
		else {
			if(value == '1')
			{
				el.checked=true;
			}
			else
			{
				el.checked=false;
			}
		}
	}
}

function display(id) {
	dnxt =	$('#newsNext'+id);
	detc =	$('#newsEtc'+id);
	dimg =	$('#puce'+id);
	if (dnxt) {
		if (dnxt.css('display') == 'none' || dnxt.css('display') == '') {
			dnxt.css('display','block');
			if (detc)	detc.css('display','none');
			if (dimg) dimg.attr('src', dimg.attr('src').split('.gif').join('_.gif'));
		}
		else {
			dnxt.css('display','none');
			if (detc)	detc.css('display','block');
			if (dimg) dimg.attr('src', dimg.attr('src').split('_.gif').join('.gif'));
		}
	}
};

var crediTimeout;
function showCredit () {
	changeClassName('credit','on');
	clearTimeout(crediTimeout);
	crediTimeout = setTimeout("changeClassName('credit','')",7000);
}

function changeClassName(id, classname) {
	$('#'+id).attr('className', classname);
	
}
/*
function ajaxUpdateFunc(html){
	if ($(this)) $(this).html(html);
	
}
*/
function showPhoto(number) {
	for (var i=1; i<=4; i++) {
		if (elmt = $('#photo'+i) ) {
			if (i==number)  $(elmt).css('display','block');
			else  $(elmt).css('display','none');
		}
		if (elmt = $('#thumb'+i) ) {
			
			if (i==number) $(elmt).attr('className', 'sel');
			else $(elmt).attr('className', '');
		}
		
	}
}
function over(strId) {
	if (elmt=$('#'+strId)) {
		 $(elmt).attr('className', 'zoom_');
	}
	
}

function out(strId) {
	if (elmt=$('#'+strId)) {
		 $(elmt).attr('className', 'zoom');
	}	
}
/*
- - - - - - - - - -
COMMANDE
Require prototype.js
- - - - - - - - - -
*/
var currentRef;
var cadAddLoaded = function(json) {
	var a=json;
	var objectId = a['userWish']['id'];
	var type = a['type'];
	//alert(type+' '+objectId+'(cadAddLoaded)');
	if (a['content'+type][objectId]!=undefined)	var nb = a['content'+type][objectId]['nb'];
	else var nb = 0;
	
	if (nb>0) {
		$('#bt_'+objectId).attr('className', 'btajt_');
		//changeClassName('bt_'+objectId, 'btajt_');
		$('#barrePage').addClass('active');
		$('#barrePage2').addClass('active');
	}
	else {
		$('#bt_'+objectId).attr('className', 'btajt');
		//changeClassName('bt_'+objectId, 'btajt');
		
		if (a['empty']=='true') {
			$('#barrePage').removeClass('active');
			$('#barrePage2').removeClass('active');
		}
	}
	$('#inp_'+objectId).val(nb);
	if (a['msg']==undefined) {
		if (nb==0)  a['msg'] = new Array('validation','Le produit a bien été retiré de votre sélection.');
		else a['msg'] = new Array('validation','Le produit a bien été ajouté à votre sélection.');
	}
	displayMessage('msg', a['msg']);
}
var errFunc = function(XMLHttpRequest, textStatus, errorThrown) {
    alert('Error ' + textStatus + ' -- ' + errorThrown);
}

var cadDelLoaded = function(json) {
	var a = json;
	var type = a['type'];
	window.location.reload();
}

var errFunc = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}

//For an XML response:
/*
var handlerFunc = function(t) {
    var xmlDoc = t.responseXML.documentElement;
    //Handle data.
}
*/

function querystringToArray(str) {
	var arr = str.split('&');
	var retour = new Array();
	for (i=0; i<arr.length; i++) {
		couple = arr[i].split('=');
		retour[couple[0]] = couple[1];		
	}
	return retour;
}
function cadAdd(obj, type, caddieValue) {
	var nb = false;
	if (!caddieValue)
	{
		caddieValue = 0;
	}
	if (elt=$('#inp_'+obj.ref))
	{
		if (elt.val() > caddieValue)
		{
			nb= elt.val() - caddieValue;
		}
	}
	if (nb==false)
	{
		nb = 1;
	}
	var postData = '&ref='+ obj.ref + '&name=' + obj.name + '&qte=' + obj.qte + '&price=' + obj.price + '&location=' + obj.location + '&achat=' + obj.achat +'&nb='+nb+'&type='+type;
	currentRef = obj.ref ;
	// new Ajax.Updater('msg', '../ajax/cad-add.php', {asynchronous:true, parameters:postData, method:'post',  onComplete:cadAddLoaded, onFailure:errFunc});
	$.ajax({
		url: scriptPath+'ajax/cad-add.php',
		type : 'post',
		dataType: 'json',
		context : $('#msg'),
		data: postData,
		success: cadAddLoaded,
		error: errFunc
	});
}

function cadDel(ref, type) {
	var postData = '&ref='+ ref + '&type='+type;
	//new Ajax.Request('../ajax/cad-del.php', {asynchronous:true, parameters:postData, method:'post',  onComplete:cadDelLoaded, onFailure:errFunc});
	$.ajax({
		url: scriptPath+'ajax/cad-del.php',
		type : 'post',
		data: postData,
		success: cadDelLoaded,
		error: errFunc
	});
	
}

function cadUpd(obj, elt, type) {
	var postData = '&ref='+ obj.ref + '&name=' + obj.name + '&qte=' + obj.qte + '&price=' + obj.price + '&location=' + obj.location + '&achat=' + obj.achat +'&nb='+ parseInt(elt.value)+'&type='+type;
	currentRef = obj.ref ;
	//new Ajax.Updater('msg', '../ajax/cad-update.php', {asynchronous:true, parameters:postData, method:'post',  onComplete:cadAddLoaded, onFailure:errFunc});
	$.ajax({
		url: scriptPath+'ajax/cad-update.php',
		type : 'post',
		dataType: 'json',
		context : $('#msg'),
		data: postData,
		success: cadAddLoaded,
		error: errFunc
	});
}

var timerId;
function doCadUpd(obj, elt, type){
clearTimeout(timerId);
timerId = window.setTimeout(function () {cadUpd(obj, elt, type);}, 1000);
}

function selectionRrefresh(type) {	
	var postData = '&type='+ type;
	//new Ajax.Updater('selection', '../ajax/cad-selection-refresh.php', {asynchronous:true, parameters:postData, method:'post',  onComplete:cadAddLoaded, onFailure:errFunc});
	$.ajax({
		url: scriptPath+'ajax/cad-selection-refresh.php',
		type : 'post',
		dataType: 'json',
		context : $('#selection'),
		data: postData,
		success: cadAddLoaded,
		error: errFunc
	});
	
}

/*
- - - - - - - - - -
MSG EFFECT
- - - - - - - - - -
*/
var msgType = {validation:'Confirmation', attention:'Attention'};
var timeout;
function displayMessage(strId, arrMsg) {
	if (elt = $('#'+strId)) { 
		elt.html('<div class="msgIn"><p><strong>'+msgType[arrMsg[0]]+' :</strong> '+arrMsg[1]+'</p></div>');
		$('#'+strId).attr('classname',arrMsg[0]);
		elt.css('display','block');
		elt.show(1);
		clearTimeout(timeout);
		timeout = setTimeout("hideBlock('" + strId + "')",10000);
	}
}
function getElementHeight(elem) {
		var xPos = elem.offsetHeight;
		return xPos;
}
function hideBlock (strId) {
	$('#'+strId).hide(2)
	/*if (elt = document.getElementById(strId)) {
		if (elt.startHeight == undefined) {
			elt.startHeight = getElementHeight(elt) ;
			elt.style.overflow = 'hidden';
		}
		if (getElementHeight(elt)>2) {
			elt.startHeight -= 8;
			if (elt.startHeight<0) elt.startHeight = 1;
			elt.style.height = String(elt.startHeight)+'px';
			elt.style.backgroundImage = 'none';
			setTimeout("hideBlock('" + strId + "')",50);
		}
		else {
			elt.style.display = 'none';
			elt.startHeight = undefined;
		}
		
	}
	*/
}
/*
- - - - - - - - - -
GALERY
- - - - - - - - - -
*/
function showImage(image, divid) {
	//new Ajax.Updater(divid, '../ajax/showImage.php', {asynchronous:true, parameters:'image='+image.split('.').join('|'), method:'post'});
	$('#'+divid).load(
		scriptPath+'ajax/showImage.php',
		{
		type : 'post',
		data: 'image='+image.split('.').join('|')
		}
	);
}
function firstSpecificChild(elt, type) {
	for (i=0; i<elt.childNodes.length; i++) {
		if (type == elt.childNodes[i].nodeName) return elt.childNodes[i];
	}
	return false;
}
function rwnPhotoNext(divId) {
		// Get img element
		if (elt = document.getElementById(divId)) {
			var img = firstSpecificChild(elt, 'IMG');
		}
		if (img) {
			// get Array Index
			var imgName = img.getAttribute('src').split('/');
			var index = $.inArray( imgName[imgName.length-1], rwnPhotoArray[divId].arr );
			index++;
			if (index>=rwnPhotoArray[divId].arr.length) index = 0;
			showImage(rwnPhotoArray[divId].path+rwnPhotoArray[divId].arr[index], divId);
		}	
}
function rwnPhotoPrevious(divId) {
		// Get img element
		if (elt = document.getElementById(divId)) {
			var img = firstSpecificChild(elt, 'IMG');
		}
		if (img) {
			// get Array Index
			var imgName = img.getAttribute('src').split('/');
			var index = $.inArray( imgName[imgName.length-1], rwnPhotoArray[divId].arr );
			index--;
			if (index<0) index = rwnPhotoArray[divId].arr.length-1;
			showImage(rwnPhotoArray[divId].path+rwnPhotoArray[divId].arr[index], divId);
		}
}