
/* Simulate hover on LI*/
sfHover = function() {
	if(typeof document.getElementById("menucategories") != "undefined") {
	var sfEls = document.getElementById("menucategories").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


function confirmEmptyBasket(msg)
{
var agree=confirm(msg);
if (agree)
	return true ;
else
	return false ;
}




/**
 * toggle Heading Items
 *
 * @param id 		the id of the Course item to hide or show
 * @param single	true to show only one item at a time, false the open as many as you want
 */
function toggleHeading(id, hash) {		
	if(document.getElementById('igcatitem_'+id+'_'+hash).style.display == 'none') {
		changeStyleHeading(id, true, hash);
		showHideHeading(id, true, hash);
	}
	else {
		changeStyleHeading(id, false, hash);
		showHideHeading(id, false, hash);
	}
}

function changeStyleHeading(id, status, hash) {
	var pm_id  = 'igcatitem_pm_'+id+'_'+hash;

	if(status) {
		document.getElementById(pm_id).innerHTML = '&#8211;';
	} else {
		document.getElementById(pm_id).innerHTML = '+';
	}
	
}

/**
 * shows or hides a Heading item at a time depending on the given status
 *
 * @param id 		the id of the Heading item to hide or show
 * @param status	true to show the item, false to hide it
 */
function showHideHeading(id, status, hash) {
	var heading_id = 'igcatitem_'+id+'_'+hash; 
	
	if(status) {
		document.getElementById(heading_id).style.display = 'inline';
	}
	else {
		document.getElementById(heading_id).style.display = 'none';	
	}
}




(function($) {

	/* Small plugin for AJAX loading calls on container to decide between opacity or loading animation */
	$.fn.toggleLoading = function(){
        if($.support.opacity)
			this.css('opacity',this.css('opacity') < 1 ? 1 : .4);
		else
			this.toggleClass('isLoading');
		
		return this;
    };


	$(function() {

		/* Code for control enter keypress */
		$("#formBasket input").keypress(function (e) {
			if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
				jQuery('.default_button').click();
				return false;
			} else {
				return true;
			}
		});
		
		/* AJAX global configuration */
		$.ajaxSetup({
			type : 'POST', 
			dataType: 'html',
			timeout : 6000
		});
		
		/* Address initialization */
		if($('#searchbox-form').hasClass('useajaxsearch')) {
			$.address
				.init(function(e) {
					if('sword' in e.parameters) {
						$('#searchbox-form').unserializeForm(e.queryString); // put values back into form
						$('#brand-list input:checked').click().attr('checked','checked'); // trigger the onclick for all checked checkboxes
					}
					
					$('#searchbox-form').address();
				})
				.change(function(e) {
			
					if(!('sword' in e.parameters))
						return;
					
					if(typeof e.parameters.sword == 'undefined' ||  $('#searchbox-form').hasClass('isSearching'))
						return;
					
					var $searchform = $('#searchbox-form');
					
					$searchform.addClass('isSearching'); // only one search at a time
					
					var ajaxurl = '/'+$searchform.attr('action');
					
					// decide which elements we need to load
					var loadLeftmenu = $('#zone_categories').length ? 0 : 1;
					var loadInfogauche = $('#zone_infogauche_content').length ? 0 : 1;
					var loadPubcontent = ($('#zone_blocs_pubcontent img').length || $('#bandeau_trouve img').length) ? 0 : 1;
					
					// Load the necessary visual adjustments.
					if(loadLeftmenu || loadInfogauche || loadPubcontent) {
						$.ajax({
							url : ajaxurl+'?type=323',
							data: {loadLeftmenu : loadLeftmenu, loadInfogauche : loadInfogauche, loadPubcontent : loadPubcontent},
							success : function(data, textStatus, jqXHR) { 
							
								$('#zone_blocs').css({paddingLeft: '15px',paddingRight: '10px',width: '600px','marginRight':0}); 
							
								if(!$('#zone_left').length)
									$('#zone_contenu').prepend('<div id="zone_left"></div>');
								
								if(!$('#zone_categories').length)
									$('#zone_left').prepend('<div id="zone_categories"></div>');
								
								if(!$('#zone_infogauche').length)
									$('#zone_left').append('<div id="zone_infogauche"></div>');
									
								if(!$('#zone_blocs_pubcontent').length)
									$('#zone_blocs').prepend('<div id="zone_blocs_pubcontent"></div>'); 
								
								var $data = $(data);
								
								if(loadLeftmenu)
									$('#zone_categories').html($data.filter('#zone_categories').html());
								
								if(loadInfogauche)
									$('#zone_infogauche').html($data.filter('#zone_infogauche').html());
									
								if(loadPubcontent)
									$('#zone_blocs_pubcontent').html($data.filter('#zone_blocs_pubcontent').html());
									
								// homepage
								if($('body').hasClass('home')) {
									$('#produits_vedette').insertAfter('#banniere_jour_j');
									$('#banniere_jour_j').remove();
								}
							}
						});
					}
					// ajax search
					$.ajax({
						url : ajaxurl+'?type=322',
						data : $searchform.serializeArray(),
						beforeSend : function(jqXHR, settings) {
							$('#zone_blocs_content').toggleLoading(); //.($.support.opacity ? fadeTo(100,.4) : addClass('isLoading')); 
						},
						success : function(data, textStatus, jqXHR) {
							$('#zone_blocs_title').empty(); 
							$('#zone_blocs_content')
								.html(data)
								//.($.support.opacity ? fadeTo(100,1) : removeClass('isLoading')); 
								.toggleLoading();
							// update featured products
							//if($('#produits_vedette').length > 0) {
							$('#produits_vedette')
								//.fadeTo(100,.4)
								.toggleLoading()
								.load(ajaxurl+'?type=324', {sword: $('#searchbox-form .searchbox-input').val()}, function() {
									$(this).toggleLoading(); //fadeTo(100,1);
								});
							//}
							// update language link
							$('.languagelink a').attr('href', function() {
								var $this = $(this);
								var href = $this.attr('href').indexOf('#') != -1 ? $this.attr('href').substr(0, $this.attr('href').indexOf('#')) : $this.attr('href');
								return href + '#' + ajaxurl + '?sword='+$('#searchbox-form .searchbox-input').val();
							});
						},
						error : function(jqXHR, textStatus, errorThrown) {
							var text = textStatus == 'timeout' ? 
								(L == 1 ? 'The server is not responding. Please make sure that you are connected to the internet.We apologize for this inconvenience.' : 'Le site ne r&eacute;pond plus. Cela peut être dû à une connexion défaillante. S\'il-vous-plait, assurez-vous d\'être connectés à internet. Nous sommes d&eacute;sol&eacute;s pour cet inconvénient.') :
								(L == 1 ? 'An error occurred while searching. Please <a href="#" onclick="$(\'#searchbox-form .searchbox-button\').click();return false;">try again</a> or browse by categories. We apologize for this inconvenience.' : 'Une erreur est survenue lors de la recherche. S\'il-vous-plait, <a href="#" onclick="$(\'#searchbox-form .searchbox-button\').click();return false;">r&eacute;essayez</a> ou naviguez le site par cat&eacute;gories. Nous nous excusons pour cet inconv&eacute;nient.');
								
							$('#zone_blocs_content').html('<div class="tx-ttproducts-pi1"><div id="tx-ttproducts-pi1-search"><h1><span style="color: rgb(102, 102, 102);">'+(L == 1 ? 'Search result(s)' : 'R&eacute;sultat(s) de la recherche')+' </span><img alt="" src="fileadmin/templates/images/fleches_search.jpg" /><span style="color: rgb(204, 102, 0);">"'+$('#searchbox-form .searchbox-input').val()+'"</span></h1><span class="tx-ttproducts-pi1-wrap1"><b>'+text+'</b></span></div></div></div>');
						},
						complete : function(jqXHR, textStatus) {
							$searchform.removeClass('isSearching');
						}
					});
					
					return false;
				});
		}
		
		/* Searchbox functionalities */
		// text input
		$('#searchbox-form .searchbox-input')
			.focus(function() {
				if(this.value == $(this).attr('title'))
					this.value = '';
			})
			.blur(function() {
				if(this.value == '')
					this.value = $(this).attr('title');
			})
		// submit button
		$('#searchbox-form .searchbox-button').click(function(){
			var searchinput = $('#searchbox-form .searchbox-input');
			if(searchinput.val() == searchinput.attr('title'))
				searchinput.val('');
			if($.browser.msie) { // Fix for IE. jquery.address uses .live() for submit.
				$('#searchbox-form').submit();	
				return false;
			}
		});
		// clear button
		$('#searchbox-form .brand-clear').click(function(){
			$('#searchbox-form .searchbox-input').val($('#searchbox-form .searchbox-input').attr('title'));
			$('#searchbox-form input:checked').removeAttr('checked');
			$('#searchbox-form #brandblock-over-img').html('');
			$('#searchbox-form #img-brand-popup').attr('src','fileadmin/templates/images/btn_listemarques2.jpg');
			return false;
		});
		// form submit is handled by .address()

		
		/* Basket overview */
		if($('#tx-ttproducts-pi1-overview').length == 0) {
			$.ajax({
				url : '/?type=325&L='+L,
				beforeSend : function(jqXHR, settings) {
					$('#menubandeau').prepend('<span id="basket-overview-loading">'+(L == 0 ? 'Chargement du panier...' : 'Loading basket...')+'</span>');
				},
				success : function(data, textStatus, jqXHR) {
					$('#basket-overview-loading').remove(); 
					$('#menubandeau').prepend(data); 
				},
				error : function(jqXHR, textStatus, errorThrown) {
					$('#basket-overview-loading').remove(); 
					$('#menubandeau').prepend('ElectroMike');
				}
			});
		}
		/* Testimonials */
		if($('.testimonialbox').length) {
			$('.testimonialbox-1').prepend('<span>'+(L == 0 ? 'Chargement du témoignage...' : 'Loading testimonial...')+'</span>');
			
			var loadTestimonial = function() {
				$.ajax({
					url : '/?type=326&L='+L,
					//beforeSend : function(jqXHR, settings) {},
					success : function(data, textStatus, jqXHR) {
						$data = $(data);
						
						$('.testimonialbox-1').fadeOut('slow', function() {
							$(this).html($data.filter('#testimonial-1').html())
							$(this).fadeIn('slow');
						});
					},
					error : function(jqXHR, textStatus, errorThrown) {
						$('.testimonialbox-1').html('ElectroMike');
					}
				});
			};
			loadTestimonial.call();
			var testimonialinterval = setInterval(loadTestimonial, 8500);	
		}
		
		/* Visual adjustements */
		if(!$.support.opacity) { // what we want is really box-shadow implementation but jquery does not have it so we use this instead.
			$('#pied_centre script').remove();
			$('#page').wrap('<div id="page-container-bottom"><div id="page-container-middle"></div></div>');
		}
		/*
		if($('.testimonialbox').length) {
			$('.testimonialbox-text').css('padding-top',function() {
				return (Math.floor($(this).parent().height() / 2) - 5) + 'px';
			});
			var t1h = $('.testimonialbox-1').height(), t2h = $('.testimonialbox-2').height();
			if(t1h < t2h)
				$('.testimonialbox-1').css('height',t2h+'px');				
		}*/
		//
		// Noel 2011
		/*
		if($('#produits_vedette .highlights_main td.highlights_bloc_td').length == 2) {
			$('#produits_vedette .produits_vedette_separator_1').css({left:'430px'});
			$('#produits_vedette .produits_vedette_separator_2').css({display:'none'});
		}
		if($('#produits_vedette .tx-igelectromikettproduct-pi5').width() > 275)
			$('#produits_vedette .produits_vedette_separator_1, #produits_vedette .produits_vedette_separator_2').css({top:'18px',height:$('#produits_vedette .produits_vedette_separator_1').parent().height()-18+'px'});
		*/
		
		/* Homepage slideshow */
		$('#c459 .csc-textpic-imagewrap ul').carouFredSel({
			items : 1,
			direction : "up",
			scroll : {items	: 1, duration : 500, pauseOnHover	: true},
			auto : {play : true, delay : 0, pauseDuration : 5000}
		});
		
		/* Homepage left menu categories fix */
		$('body.home #categories_contenu > ul > li:lt(3) > ul:hidden').show();
		//$('body.home #categories_contenu > div:eq(0)').hide();
		
		// Async js scripts
		// First hack into document.write
		//var documentwrite = document.write;
		document.write = (function () {
			for (var i = 0; i < arguments.length; i++) {
				$('#pied_centre').append(arguments[i]);
			}
		});
		// Geotrust 
		var s = document.createElement('script');
		s.type = 'text/javascript';
		s.async = true;
		s.src = 'https://smarticon.geotrust.com/si.js';
		document.getElementById('pied_centre').appendChild(s);
		//$('#pied_centre').load('https://smarticon.geotrust.com/si.js');
		//$('#pied_centre').html('<script language="javascript" type="text/javascript" src="https://smarticon.geotrust.com/si.js"></script>');
		//$.get('https://smarticon.geotrust.com/si.js', function(data,status,jqXhr){console.log(data);$('#pied_centre').html('<script type="text/javascript">'+data+'</script>')}, 'script');
		//$('#pied_centre').html('<script>'++'</script>');
		// 
		//document.write = documentwrite;
	})
})(jQuery);


function changeBrandListSelected(titleBrand) {
    	document.getElementById("img-brand-popup").src = 'fileadmin/templates/images/btn_listemarques_empty.jpg';
	document.getElementById("brandblock-over-img").innerHTML = titleBrand;
}

function brandCheck(idcontrol, titlebrand, origImage) {

	var currentStr = document.getElementById("brandblock-over-img").innerHTML;
	var sep = ', ';	
	// If the control is checked
	if (document.getElementById(idcontrol).checked==1)
    	{
    		document.getElementById("img-brand-popup").src = 'fileadmin/templates/images/btn_listemarques_empty.jpg';
    		if(currentStr == '') {
	    		document.getElementById("brandblock-over-img").innerHTML = titlebrand;
	    	} else {
	    		document.getElementById("brandblock-over-img").innerHTML = currentStr + sep + titlebrand;
	    	}
	} else {
		var pos=currentStr.indexOf(titlebrand);
		if(pos == 0) {
			currentStr = currentStr.replace(titlebrand + sep, '');
			currentStr = currentStr.replace(titlebrand, '');
		} else {
			currentStr = currentStr.replace(sep+titlebrand, '');
		}
    		document.getElementById("brandblock-over-img").innerHTML = currentStr;
		if(currentStr == '') {
			document.getElementById("img-brand-popup").src = 'fileadmin/templates/images/' + origImage;
		}
	}
}


function changeStyleBloc(id, status, hash, filepathparam, nbsuppitem) {
	
	if(filepathparam) {
		stylejs = 'url("' + filepathparam + '")';
	
		document.getElementById('cat-content-'+id+'_'+hash).style.backgroundImage = stylejs;
	}
	
	if(status) {
		document.getElementById('cat-list-'+id+'_'+hash).style.display = 'block';
		
		var newHeight = (nbsuppitem * 20) + 130;
		
		document.getElementById('cat-content-mask-'+id+'_'+hash).style.height = newHeight + 'px';
		document.getElementById('cat-content-mask-'+id+'_'+hash).style.backgroundColor = 'white';
		
	} else {
		document.getElementById('cat-list-'+id+'_'+hash).style.display = 'none';
		document.getElementById('cat-content-mask-'+id+'_'+hash).style.backgroundColor = 'transparent';
		
		document.getElementById('cat-content-mask-'+id+'_'+hash).style.height = '135px';
	}
	
}

function changeFormBilling(L) {
	if(L == '_en') {
		document.getElementById('msg-billing').innerHTML = 'Some informations has been modified. Taxes applied to this transaction may changed';
	} else {
		document.getElementById('msg-billing').innerHTML = 'Certaines informations ont &eacute;t&eacute; modifi&eacute;es. Les taxes appliqu&eacute;s &agrave; cette transaction pourraient avoir chang&eacute;.';
	}
	document.getElementById('personinfo_change').value = 1;
}

function changeFormShipping(L) {

	if(L == '_en') {
		document.getElementById('msg-shipping').innerHTML = 'Some informations has been modified. Shipping costs applied to this transaction may changed';
	} else {
		document.getElementById('msg-shipping').innerHTML = 'Certaines informations ont &eacute;t&eacute; modifi&eacute;es. Les frais de transport appliqu&eacute;s &agrave; cette transaction pourraient avoir chang&eacute;.';
	}
}

function updateCountryInCart(option) {
	if(typeof option != 'undefined') {
		$('select[name=locationSelector]')
			.removeAttr('disabled')
			.val(option);
	}

	$('body').toggleLoading();
	$('#formBasket')
		.append('<input type="hidden" name="step1_updatecountry" value="1" />')
		.submit();
}


