/*******************
EGBX WWW SCRIPTS
*******************/

$(document).ready(function(){
	initMenu();
	gestionSideBar();
	toggleDiv();
	displayVignettage();
	recoveryMail();
	displayBrand();
	displayDeliveryAdress();
	sizeGuide();
	insertNewsletter();
	// --------
	
	setColor();
	
	getHeaderAccount();
});

function getHeaderAccount()
{
	$.ajax({
		type:'POST',
		url:'/www/tools/tool.www.header.account.php',
		success:function(_html)
		{
			$('#headerAccountAjax').html(_html);
		}
	});
}

function setColor()
{
	$(document).ready(function(){
		$('.push a').live('click',function(){
			var _push = $(this).parents('.push');
			if(_push.hasClass('setColor'))
			{
				var _url = $(this).attr('href');
				var _id = _push.attr('id').split('_')[1];
				$.ajax({
					type:'POST',
					url:'/www/ajax/setColor.php',
					data:{id:_id},
					success:function(_html)
					{
						document.location.href = _url;
					}
				});
				return false;
			}
		});
	});
}

function slideToggle(id,speed)
{
	$('#'+id).slideToggle(speed);
}

function initMenu()
{
	var NomNav = navigator.appName; 
	var VersNav = parseFloat(navigator.appVersion);
	if (NomNav == "Microsoft Internet Explorer" && VersNav<=6) {
		//
		
	}
	else
	{
		$('#mainMenu a').hover(
			function(){
				var w = $(this).width();
				$(this).width(w);
				$(this).children('h4.normal').css('display','none');
				$(this).children('h4.hover').css('display','block');
			},
			function(){
				$(this).children('h4.normal').css('display','block');
				$(this).children('h4.hover').css('display','none');
			}
		);
	}
}

function gestionSideBar()
{
	$('.flowMenu').click(function(){
		//
		var idS = $(this).attr('id');
		var id = idS.split('_')[1];
		var idAttr = 'line_'+id;
		
		$('#line_'+id).slideToggle(300);
		
		
		$('.smenu').each(function(i){
			var currentId = $(this).attr('id');
			//alert(currentId+' / '+idAttr);
			if(currentId != idAttr) $(this).slideUp(300);					  
		});
		
		return false;
	});
}

function toggleDiv()
{
	$('.toggler').click(function(){
		$('.toggler').removeClass('selected');
		$(this).addClass('selected');
		var div = $(this).attr('id').split('OPEN_')[1];
		if($('#'+div).css('display') == 'none')
		{
			$('#toggleContent > div').css('display','none');
			$('#'+div).css('display','block');
			//$('#'+div).slideDown(100);
			//$('#toggleContent').slideToggle(100);
		}
		return false;
	});
}

function recoveryMail()
{
	$('.recoveryMail .hideRecoberyForm a').click(function(){
		$(this).css('display','none');
		$('.displayRecoveryForm').css('display','block');
	});
}

function popup(_content,_width)
{
	// Modification du css
	//$('body').css({overflow:'hidden'});
	$('#popupBackground').css({display:'block',opacity:0});
	
	// animation
	$('#popupBackground').animate({
		opacity:0.5
	},200,function(){
		$.ajax({
			type:'POST',
			url:_content,
			data:{},
			success:function(dat){
				// REMPLIR LE CONTENU DE LA POPUP
				$('#popup').html(dat);
				// MODIF CSS
				var new_width = (parseInt(_width)+26+32)+"px";
				$('#popup').css({display:'block',opacity:0,width:new_width});
				// CALCUL W+H
				var width_margin = ($('#popup').width())/2;
				var height_margin = ($('#popup').height())/2;
				
				var heightWindow = $(window).height();
				var _top = 50;
				if($('#popup').height() > heightWindow)
				{
					height_margin = 0;
					_top = 0;
				}
				
				$('#popup').css({marginLeft:"-"+width_margin+"px",marginTop:"-"+height_margin+"px",top:_top+'%'});
				
				$('#popup').animate({
					opacity:1
				},200);
			}
		});
	});
}

function closePopup()
{
	$('#popup').animate({
		opacity:0
	},200,function(){
		$('#popupBackground').animate({
			opacity:0
		},200,function(){
			// EFFACER LE CONTENU DE LA POPUP
			$('#popup_content').html('');
			// PASSER EN DISPLAY NONE
			$('#popup').css({display:'none'});
			$('#popupBackground').css({display:'none'});
			$('body').css({overflow:''});
		});	
	});
}

function openClose(_id,_item,_menu)
{
	$('.'+_item).each(function(i){
		$(this).removeClass('selected');
	})
	$('#'+_item+'_'+_id).addClass('selected');
	
	$('.'+_menu).each(function(i){
		$(this).css({display:'none'});
	});
	
	$('#'+_menu+'_'+_id).css({display:'block'});
}
function displayVignettage()
{
	$('#vignettage').fadeIn(1000);	
}

function displayDeliveryAdress()
{
	$('.is_Delivery').click(function(){
		if($(this).val() == '1')
		{
			$('#delivery_address_display').slideDown(500);
		}
		else
		{
			$('#delivery_address_display').slideUp(500);
		}
	});
}
function displayBrand()
{
	var _brand_id = 'all';
	var _family_id = 'all';
	
	$('#brand_selector').change(function(){
		//$('#family_selector').val('all');
		// CLICK
		_brand_id = $(this).val();
		_family_id = $('#family_selector').val();
		animatePush(_brand_id,_family_id);
		/*
		$.ajax({
			type:'POST',
			url:'/www/ajax/profiler.ajax.php',
			data:{brand_selector:_brand_id,family_selector:_family_id},
			success:function(_html){
			}
		});
		*/
		// Desactiver lien
		return false;
	});
	$('#family_selector').change(function(){
		$('#brand_selector').val('all');
		// CLICK
		_brand_id = $('#brand_selector').val();
		_family_id = $(this).val();
		animatePush(_brand_id,_family_id);
		
		$.ajax({
			type:'POST',
			url:'/www/ajax/profiler.ajax.php',
			data:{brand_selector:_brand_id,family_selector:_family_id}
		});
		
		// Desactiver lien
		return false;
	});
}
function animatePush(_brand_id,_family_id)
{
	// Animation
	if(_brand_id == 'all' && _family_id == 'all')
	{
		$('.content .push').each(function(i){
			$(this).fadeIn(500);
		});				   
	}
	else
	{
		$('.content .push').each(function(i){
			if( ($(this).hasClass('theBrand_'+_brand_id) || _brand_id == 'all') && ($(this).hasClass('theFamily_'+_family_id) || _family_id == 'all') )
			{
				$(this).fadeIn(500);
			}
			else
			{
				$(this).fadeOut(500);
			}
		});
	}
}
function sizeGuide()
{
	$('.read_size_guide').live('click',function(){
		
		$('body').css({overflow:'hidden'});		
		// Modification du css
		$('#popupBackground').css({display:'block',opacity:0});
		// animation
		$('#popupBackground').animate({
			opacity:0.5
		},200,function(){
			$.ajax({
				type:'POST',
				url:'/www/ajax/sizes_guide.php',
				data:{},
				success:function(dat){
					$('#popup').html(dat);
					$('#popup').css({display:'block',opacity:0});
					var width_margin = ($('#popup').width())/2;
					var height_margin = ($('#popup').height())/2;
					$('#popup').css({marginLeft:"-"+width_margin+"px",marginTop:'-100px'});
					
					
					
					$('#popup').animate({
						opacity:1,
						marginTop:"-"+height_margin+"px",
						marginLeft:"-"+width_margin+"px"
					},200);
					
					// ----
				}
			});
		});
		//
		return false;
	});
}

function recoveryMail()
{
	$('.recoveryMail .hideRecoberyForm a').click(function(){
		$(this).css('display','none');
		$('.displayRecoveryForm').css('display','block');
	});
}


function insertNewsletter()
{
	$('.PPinsertNewsletter').live('click',function(){
		
		$('body').css({overflow:'hidden'});		
		// Modification du css
		$('#popupBackground').css({display:'block',opacity:0});
		// animation
		$('#popupBackground').animate({
			opacity:0.5
		},200,function(){
			$.ajax({
				type:'POST',
				url:'/www/ajax/insertNewsletter.php',
				data:{},
				success:function(dat){
					$('#popup').html(dat);
					$('#popup').css({display:'block',opacity:0});
					var width_margin = ($('#popup').width())/2;
					var height_margin = ($('#popup').height())/2;
					$('#popup').css({marginLeft:"-"+width_margin+"px",marginTop:'-100px'});
					
					
					
					$('#popup').animate({
						opacity:1,
						marginTop:"-"+height_margin+"px",
						marginLeft:"-"+width_margin+"px"
					},200);
					
					// ----
				}
			});
		});
		//
		return false;
	});
}


