var elementInSearchDefault = 'RECHERCHER UNE RÉFÉRENCE OU UN ARTICLE'; 
var elementInNlDefault = 'Entrez votre adresse e-mail';
var elementInNlPwd = 'Mot de passe';
var winCatalog;
var stopRotateNews = false;

/*Permet d'afficher une vignette en grand*/
function showImg(id) {
	lnks = $('#fiche-prd .media img');
	lnk = lnks[id];
	if(!$(lnk).is(':visible')) {
		lnks.fadeOut();
		$(lnk).fadeIn();
	}
}
/*
 *	Cette fonction permet de rechercher la position du scroll
*/
function getScrollPositionPopup()
{
		return Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop);
}

/*
 * Cette fonction permet de mettre à jour la position de la popup ainsi que de la redimensionner à la taille de la fenetre de l'utilisateur
*/
function positionPopup()
{
	if( $('#avis-usr-crt-should').css('display')  )
	{
		currentScroll = getScrollPositionPopup();
		$('#avis-usr-crt-should').css('top', ( $(window).height()/2 - $('#avis-usr-crt-should').height()/2 + currentScroll[1] )+ 'px');
		$('#avis-usr-crt-should').css('left', ( $(window).width()/2 - $('#avis-usr-crt-should').width()/2 )+ 'px');
	}
	if( $('#your-avis').css('display')  )
	{
		currentScroll = getScrollPositionPopup();
		$('#your-avis').css('top', ( $(window).height()/2 - $('#your-avis').height()/2 + currentScroll[1] )+ 'px');
		$('#your-avis').css('left', ( $(window).width()/2 - $('#your-avis').width()/2 )+ 'px');
	}
}

/* Masque le label de recherche lorsque le champ reçoit le focus */
$(document).ready(function() {
	positionPopup();
	window.onresize = positionPopup;
	window.onscroll = positionPopup;
	window.onresize = positionPopup;
	$('#shadow').height( $('body').height() );

	$('a.lnk-consulter').click(function(){
		if( winCatalog && !winCatalog.closed  ){
			winCatalog.document.location = $(this).attr('href');
			winCatalog.focus();
		}else{
			var left = (screen.width - 1000) / 2;
			var top = (screen.height - 700) / 2;
			winCatalog = window.open( $(this).attr('href'), 'Catalogue Pierre Oteiza', 'width=1000,height=700,left=' + left + ',top=' + top );
		}
		return false;
	});

	//Champ de recherche
	elementInSearch = $('#searchinput');
	elementInSearch.focus(function() {
		if(elementInSearch.val()== elementInSearchDefault) {
			elementInSearch.val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			elementInSearch.val(elementInSearchDefault);
		}
	}); 
	
	elementInSearch2 = $('#searchinput-error');
	elementInSearch2.focus(function() {
		if(elementInSearch2.val()== elementInSearchDefault) {
			elementInSearch2.val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			elementInSearch2.val(elementInSearchDefault);
		}
	}); 
	
	elementInNl = $('#email');
	elementInNl.focus(function() {
		if(elementInNl.val()== elementInNlDefault) {
			elementInNl.val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			elementInNl.val(elementInNlDefault);
		}
	}); 
	
	elementInPwd = $('#password');
	elementInPwd.focus(function() {
		if(elementInPwd.val()== elementInNlPwd) {
			elementInPwd.val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			elementInPwd.val(elementInNlPwd);
		}
	}); 
	
	$('#menu-h > li ').hover(
		function() {
			$(this).addClass('selected');
			$('.box-menu-h-smenu,.halo',this).css('display','block');
			
		},
		function() {
			$(this).removeClass('selected');
			$('.box-menu-h-smenu,.halo',this).css('display','none');
			
		});
	
	// Gestion visionneuse accueil	
	nbItems = $("#publicites-id ul li").length;
	
	title = href = '';
	title = $('#publicites-id ul li:first img').attr('title');
	href = $('#publicites-id ul li:first a').attr('href');
	$('#hc-title a').attr('href',href);
	$('#hc-title a').text(title);
	/*if(title.length==0)
		$('#hc-title').hide();
	else
		$('#hc-title').show();	*/
	
	title = $('#hc-visus img:first').attr('title');
	$('#hc-title a').text(title);
	if($('#hc-visus li:first a').length) {
		$('#hc-title span').remove();
		href = $('#hc-visus li:first a').attr('href');
		$('#hc-title').append('<a href="'+href+'">'+title+'</a>');
	} else {
		$('#hc-title a').remove();
		$('#hc-title').append('<span>'+title+'</span>');
	}
	
	if(nbItems>1) {
		i = 0;
		$("#hc-buttons").append('<button class="hc-but-nav" id="hc-but-prev"></button>&nbsp;<button class="hc-but-nav" id="hc-but-next"></button>');
		
		$('#publicites-id ul li').each(function(){
			$(this).attr('rel',$(this).attr('title'));
			$(this).removeAttr('title');
		});
		
		$("#publicites-id").jCarouselLite({
			auto: 8000,
			speed: 500,
			visible: 1,
			circular:true,
			pauseOnHover:"#hc-visus",
			btnPrev: "#hc-but-prev",
			btnNext: "#hc-but-next",
			beforeStart:function(a,b) {
			},
			afterEnd: function(a) {
				title = $('img',a).attr('title');
				$('#hc-title a').text(title);
				if($('a',a).length) {
					$('#hc-title span').remove();
					href = $('a',a).attr('href');
					$('#hc-title').html('<a href="'+href+'">'+title+'</a>');
				} else {
					$('#hc-title a').remove();
					$('#hc-title').html('<span>'+title+'</span>');
				}
				if(title.length==0 || $('img',a).parent().parent().attr('id')=='pub95')
					$('#hc-title').hide();
				else
					$('#hc-title').show();
			}
		});
	}
	
	$('#menu-h .menu-h-item .menu-h-item-lnk').each(function(){
		// defaultH = $(this).height();
		// $(this).css('height','auto');
		// if($(this).height()>20)
			// $(this).css('padding-top','10px').height(defaultH-10);
		// else
			// $(this).css('padding-top','18px').height(defaultH-18);
		// var padding = ''; var defaultH = 0;
		// var obj = $(this);
		// var heihgt = obj.height();
		
		// if(obj.html().length>30)
			// padding = '3px';
		// else if(obj.html().length>18)
			// padding = '10px';
		// else
			// padding = '18px';
		// obj.css('padding-top',padding);
	});

	$('#list-tabs a,.scroll').click(function(){
	
		hash = this.hash;
		$.scrollTo(hash, 500,{
			onAfter:function(){
				window.location.hash = hash;
				$(hash).focus();
			}
				
			
		});
		return false;
	});
	
	$('.rsopenfancybox').fancybox({centerOnScroll:'true'});
	
	$('#fiche-prd .oviews a').each(function(i){
		$(this).click(function(){
			showImg(i);
			
			return false;
		});
	});
	
	
	
	$('#fiche-prd .qte').spinner();
	if (stopRotateNews == false){
		setTimeout('rotateNews()',5000);
	}
	$("#box-news").hover(
	  function () {
		stopRotateNews = true;
	  },
	  function () {
		stopRotateNews = false;
		setTimeout('rotateNews()',5000);
	  }
	);
	// $('#lnk-connaitre-oteiza2').click(function(){
		// $('a.connaitre-oteiza', this).click();
	// });
});

function rotateNews() {
	if (stopRotateNews == false){
		visible = $('#box-news .news:visible');
		var next = visible.next();
		if(!next.length ) {
			next = $('#box-news .news:first');
		}
		visible.hide();
		next.show();
		setTimeout('rotateNews()',5000);
	}
}

function printPage(){
	if( window.print ){
		window.print();
	}
	return false;
}

