

var loading = '<img src = "/images/loading-black.gif" />'; 
var loading2 = '<img src = "/images/loading-white.gif" />'; 
	
$(document).ready( function() 
{
		$(document).pngFix();
		
		box = new Ajaxbox();

        $('#datafiche').jScrollPane( {showArrows:true} );

        displayShippingForm();
		
        if( $('#menu_haut').length > 0 )  menu();
        
        $('a[rel*="lightbox"]').lightBox({
        	imageLoading: '/images/loading-white.gif',
        	imageBtnClose: '/src/icons/silk/cross.png',
        	imageBlank: '/images/blank.gif'
        	
        });
});



function hoverScroll(carousel) {
	carousel.clip.hover(function() {
	       carousel.stopAuto();
	   }, function() {
	       carousel.startAuto();
	   });
}

//init des controls du carousel
function carouselInit(carousel) {
	$('.jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval($(this).text()));
		return false;
	});
}


function popuprnp(){
	var win2 = window.open("http://www.receiveandpay.com/wallet/customer/popup3x.php?logo=10669" ,'popup','height=956,width=616,status=yes,scrollbars=yes,menubar=no,resizable=yes');
}

function addEventDialog(){
	$( 'div#dialog' ).livequery( function(){
		var img = $(this).find( 'img#dial' );
		
		if( img.length > 0 )
		{
			
			img.click( function(){
				box.hide();
			});
			
			
			img.mouseover( function(){
				$(this).attr( 'src', '/images/cross_over.jpg' );
			});
			
			img.mouseout( function(){
				$(this).attr( 'src', '/src/icons/silk/cross.png' );
			});
		}
	});
}

function returnCgv()
{
        if( !$('#checkcondition').is(':checked') )
        {
                box.show();
                box.update('<?php displayBox( AVENUE_CGV_TITLE, AVENUE_CGV_FALSE ); ?>');
                return false;
        }
        return true;
}

function displayPayment( id )
{
        $('div[id*="paiement_"]').each( function( )
        {
	    	if ( this.id != 'paiement_'+id ){
				$( '#'+this.id ).slideUp();
			}else{
				$( '#'+this.id ).slideDown();
			}
        });
        
}

function tabpanel( el, el1 ){
	$(el).show();
	$(el1).hide();
}

function makeActive(id,id2){
document.getElementById(id).className = "tabactive";
document.getElementById(id2).className = "tabinactive";
}
function registerNewsLetter()
{
	box.show();
    box.update( loading );
	$.ajax({
		type : 'POST',
		url  : '/ajax.php',
		data:{
			cmd   : 'registerNewsLetter',
			email : $('#email_news').val()
		},
		success: function( result )
		{
			box.update( result );
		}
	});
	
}






function zoomOption( id )
{
    var element = $( '#zoom_'+id );
    if( element.length > 0  )
    {
        element.fadeIn( 'fast', function(){
            $(this).show();
        })
    }
}

function dezoomOption( id )
{
    var element = $( '#zoom_'+id );
    if( element.length > 0  )
    {
        element.fadeOut( 'fast', function(){
            $(this).hide();
        })
    }
}


function menu()
{
	var loaded = true; //pour eviter que le menu ne disparraise si on on sort du li
	var menu = true;
	
	$( 'li.ligne1 > a' ).each( function()
	{
		var id = $(this).attr( 'id' ).match( /([0-9]+)/ )[1];
		var div = $( 'div#avs_menu_'+id );
		var bgcolor = div.css( 'background-color' ) == 'transparent' || '' ? '#313131' : div.css( 'background-color' );
		var that = $(this);
		var loaded;
		var timeout;
		
		that.bind( 'mouseenter',function()
		{
				var css = {
						'background-color': bgcolor,
						'background-image': 'none'
				} 
				
				//si couleur par d�fault
				if( bgcolor == '#C71851' )
					div.css( css ); // on change le style  
				else
					$( 'li.ligne1' ).removeAttr( 'style' );
					that.parent().css( css );
				
				div.show(); // affiche celle selectionn�e
				
				div.bind( 'mouseenter', function(){
					
					loaded = true;
				}).bind( 'mouseleave', function()
				{
					loaded = false;
					that.parent().removeAttr( 'style' );
					
					//pour eviter d'avoir le menu du haut color� si pas de style
					if(  bgcolor  == '#C71851' )
						div.removeAttr( 'style' );
					
					div.hide();
				})
				
		}).bind( 'mouseleave', function()
		{
				timeout = setTimeout( function(){
					if( !loaded )
					{
						clearTimeout( timeout );
						that.parent().removeAttr( 'style' );

						//pour eviter d'avoir le menu du haut color� si pas de style
						if(  bgcolor  == '#C71851' )
							div.removeAttr( 'style' );

						div.hide();
					}
				}, 50 );
				
		});
	});
	
	$( 'li.ligne2 > a' ).each( function()
	{
		var id = $(this).attr( 'id' ).match( /([0-9]+)/ )[1];
		var div = $( 'div#avs_menu_'+id );
		var bgcolor = div.css( 'background-color' ) == 'transparent' || '' ? '#313131' : div.css( 'background-color' );
		var that = $(this);
		var loaded;
		var timeout;
		
		that.bind( 'mouseenter',function()
		{
				var css = {
						'background-color': bgcolor,
						'background-image': 'none'
				} 
				
				//si couleur par d�fault
				if( bgcolor == '#C71851' )
					div.css( css ); // on change le style  
				else
					$( 'li.ligne2' ).removeAttr( 'style' );
					that.parent().css( css );
				
				div.show(); // affiche celle selectionn�e
				
				div.bind( 'mouseenter', function(){
					
					loaded = true;
				}).bind( 'mouseleave', function()
				{
					loaded = false;
					that.parent().removeAttr( 'style' );
					
					//pour eviter d'avoir le menu du haut color� si pas de style
					if(  bgcolor  == '#C71851' )
						div.removeAttr( 'style' );
					
					div.hide();
				})
				
		}).bind( 'mouseleave', function()
		{
				timeout = setTimeout( function(){
					if( !loaded )
					{
						clearTimeout( timeout );
						that.parent().removeAttr( 'style' );

						//pour eviter d'avoir le menu du haut color� si pas de style
						if(  bgcolor  == '#C71851' )
							div.removeAttr( 'style' );

						div.hide();
					}
				}, 50 );
				
		});
	});
}


function addProductCart()
{
	var data = new Array();
	var display = false;
	
	$('input[name*="opt["]:text').each( function( )
		{
			if( !isNaN( parseInt ( this.value ) ) && parseInt ( this.value ) > 0 )
			{
				display = true;
				data.push({ 
					"opt_id" : this.name.match(/\[([0-9]+)\]/)[1], 
					"opt_qt" : this.value,
					"opt_spl": 0
				});
				
			}
		}
	)
	
	$('input[name*="sample["]:checkbox').each( function( )
		{
			if( this.checked == true )
			{
				display = true;
				data.push({ 
					"opt_id" : this.name.match(/\[([0-9]+)\]/)[1], 
					"opt_qt": 1,
					"opt_spl": 1
				});
				
			}
		}
	)
	
	if( display )
	{
            box.show();
            box.update( loading );

            $.ajax({
                type: 'POST',
                url : '/ajax.php',
                dataType: 'html',
                data: {
                    cmd: 'addProductCart',
                    opt: $.toJSON( data )
                },
                success: function( result ){
                    box.update( result );
                	var script =  new Function( $('script', result ).text() );
                	script();
                }
            });
	}
}


function deletProduct( id )
{
    
    $('#row_'+id).fadeOut( 'slow', function(){
        $(this).remove();
    });
    
    var tab = $('.row_friend tbody tr' );
    if( tab.length == 1 )
    {
       
       $('.cart_prd_linked').fadeOut( 'slow', function(){
            $(this).remove();
        });
    }
}


function addProductLinked()
{
	
	$('.loader').fadeIn();
	var data = [];
	var display = false;
	
	$('input[name*="optp["]:text').each( function( index, element )
		{
			if( !isNaN( parseFloat ( this.value ) ) && parseFloat ( this.value ) > 0 )
			{
				display = true;
				data.push({ 
					"opt_id" : this.name.match(/\[([0-9]+)\]/)[1], 
					"opt_qt" : this.value
				});
				
			}
		}
	)
	
	if( display )
	{
		$.ajax({
			type: 'POST',
			url: '/ajax.php',
			data: 
			{
				cmd: 'addLinkedProduct',
				optp: $.toJSON( data )
			},
			success: function ( result )
			{
				$('.loader').fadeOut( 'slow', function(){
					top.location.href = '/cart.php';
				});
			}
		});
	}
}


function displayShippingForm( )
{
   if( $('#shp_status_shipping_1').is(':checked') )
   {
        $('.sho').slideDown();
   }else{
        $('.sho').slideUp();
   }

}

function displayShippingFormCompany( )
{

   if( $('#shp_status_shipping2_1').is(':checked') )
   {
        $('.shi').slideDown();
   }else{
        $('.shi').slideUp();
   }

}


function displayDetail( id )
{
    box.show();
    box.update( loading );

    $.ajax({
            type: 'POST',
            url : '/ajax.php',
            data: {
                    cmd: 'detailOrder',
                    id: id
            },
            success: function( result ){
                    box.update( result );
            }
    });
}


function loadVideo( id )
{
	$('.player').html( loading2 );
	$.ajax({
		url : '/ajax.php',
		type: 'POST',
		data: 
		{
			cmd: 'getVideo',
			id: id
		},
		success: function( result ){
			$('.player').html( result );
		}
	})
	
}


function getTabContent( type, id )
{
		
		
		$('a[id*="tab"]').each( function()
		{
			if( this.id == 'tab_'+type+'_'+id )
			{
				$(this).unbind( 'click' );
				$(this).addClass( 'tabactive' );
			}else{
				$(this).bind( 'click', function(){
					getTabContent( id );
				} );
				$(this).removeClass();
			}
		});
		
	
		
		$('div[id*="fiche_"]').each( function()
			{
				if( this.id == 'fiche_'+type+'_'+id ){
					$(this).show();
				}else{
					$(this).hide();
				}
			}
		)
		$('#datafiche').jScrollPane( {showArrows:true} );
		
	
}

//ouvre une popup
function openurl( url,name ) {
	var width = 900;
	var leftf =( screen.width-width )/2;
	var height = 600;
	var topf  = ( screen.height-height )/2;

	var riwindow = window.open( url, name, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width= '+width+',height='+height+',left='+leftf+',top='+topf );
	if ( riwindow ){
		riwindow.focus();
	}
}


//renvoi les frais de ports, en fonction du code postal, du poids et si c'est juste des echantillons
function calculShipping( value, weight, sample )
{
    $.ajax({
            type: 'POST',
            url: '/ajax.php',
            data:
            {
                    cmd: 'calculShipping',
                    zip: value,
					weight: weight,
                    sample: sample
            },
            success: function ( result )
            {
               $('.estimate').html( result );
            }
    });
}

function showFeatures(prd_id,state)
{
	if(state =="1")
	{
		if(document.getElementById('feature_product'+prd_id).innerHTML=="")
		{
			$.ajax({
				type : 'POST',
				url  : '/ajax.php',
				data:{
					cmd   : 'showFeatures',
					prd_id : prd_id
				},
				success: function( result )
				{
					list_feature = document.getElementsByClassName("feature_product");
					for(iKey in list_feature)
					{
						if(list_feature[iKey])
						{
							$('#'+list_feature[iKey].id).empty();
						}
					}
				
					$('#feature_product'+prd_id).append( result );
				}
			});
		}
	}
	else
	{
		list_feature = document.getElementsByClassName("feature_product");
		for(iKey in list_feature)
		{
			if(list_feature[iKey])
			{
				$('#'+list_feature[iKey].id).empty();
			}
		}
	}
}

