var multiProduitHome = "/catalog/multi-produit/" ;

function computeDuration() {
	alert( BrowserDetect.browser ) ;
	alert( BrowserDetect.version ) ;
	return 1.0 ;
}

function showMultiProduit( productsId , popupDivId , osid , cssPrefix , fromZoom , hideBuy ) {
	
	
	new Effect.Fade(
			popupDivId ,
			{
				afterFinish : function ( obj ) {
					
					new Ajax.Updater( 
						popupDivId , 
						multiProduitHome + 'multi-produit.php', 
						{
							asynchronous:true ,
							parameters: { 
								products_id : productsId ,
								popup_div_id : popupDivId ,
								osCsid : osid ,
								css_prefix : cssPrefix,
								from_zoom : fromZoom ,
								hide_buy : hideBuy
							} ,
							onComplete : function() {
								
   								new Effect.Appear( popupDivId , { duration: 0.5 }) ;
   								new Effect.Center(popupDivId) ;
							}
						}
					) ;
					
					
	      		} ,
	      		duration: 0.5
			}
		);
	
}

function showZoomProduit( productsId , popupDivId , osid , cssPrefix ) {
	
	new Effect.Fade(
			popupDivId ,
			{
				afterFinish : function ( obj ) {
					
					new Ajax.Updater( 
						popupDivId , 
						multiProduitHome + 'zoomProduit.php', 
						{
							asynchronous:true ,
							parameters: { 
								products_id : productsId ,
								popup_div_id : popupDivId ,
								osCsid : osid ,
								css_prefix : cssPrefix 
							} ,
							onComplete : function() {
								
   								new Effect.Appear( popupDivId , { duration: 0.5 }) ;
   								new Effect.Center(popupDivId) ;
							}
						}
					) ;
					
					
	      		},
	      		duration: 0.5
			}
		);
	
}

function hideMultiProduit( popupDivId , callback ) {
	new Effect.Fade( 
		popupDivId , 
		{
			afterFinish: callback
		} ) ;
}

function ajouterAuPanier( frm , popupDivId , catalog_repository ) {
	stock = parseInt( frm.elements['stock'].value ) ;
	qty = parseInt( frm.elements['products_qty'].value ) ;
	if( qty > stock ) {
		document.getElementById( 'ajout-panier-alerte' ).innerHTML = "la quantité demandée est supérieure au stock" ;
		frm.elements['products_qty'].value = '' ;
		return false ;
	} else {
		
		osCsid = frm.elements['osCsid'].value ; 
		products_id = frm.elements['products_id'].value ; 
		products_qty = frm.elements['products_qty'].value ;
		
		
		new Ajax.Request(
			multiProduitHome + "addCart.php?osCsid=" + osCsid + "&products_id=" + products_id + "&products_qty=" + products_qty ,
			{
				onSuccess: function( t ) {
					hideMultiProduit( popupDivId , function() {
						if( window.location.href.indexOf( 'index.php' ) < 0 || window.location.href.indexOf( 'cPath' ) >= 0 ) {
							window.location.reload() ;
						} 
					} ) ;
				} ,
				onFailure: function() {
					document.getElementById( 'ajout-panier-alerte' ).innerHTML = "le produit n'a pas été ajouté au panier" ;
				}
			}
		) ;
		
		
		return false ;
	}
}

function initMultiProduit( divId ) {
	
	//new Draggable(divId) ;
	//new Effect.Center(divId) ;
}




Effect.Center = function(element)
{
    try
    {
        element = $(element);
    }
    catch(e)
    {
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' )
    {

        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }
    else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {

        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
    {

        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    element.style.position = 'absolute';
    //element.style.display  = 'block';
    element.style.zIndex   = 99;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop )
    {
        scrollY = document.documentElement.scrollTop;
    }
    else if ( document.body && document.body.scrollTop )
    {
        scrollY = document.body.scrollTop;
    }
    else if ( window.pageYOffset )
    {
        scrollY = window.pageYOffset;
    }
    else if ( window.scrollY )
    {
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

}





/**
 *	Browser detection
 */
 
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();













