(function($) {

jQuery.fn.pngFix = function(settings) {

	// Settings
	settings = jQuery.extend({
		blankgif: 'blank.gif'
	}, settings);

	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if (jQuery.browser.msie && (ie55 || ie6)) {

		//fix images with png-source
		jQuery(this).find("img[src$=.png]").each(function() {

			jQuery(this).attr('width',jQuery(this).width());
			jQuery(this).attr('height',jQuery(this).height());

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
			var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
			var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
			var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
			var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
			var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;'+'">' + strNewHTML + '</span>';
			}

			jQuery(this).hide();
			jQuery(this).after(strNewHTML);

		});

		// fix css background pngs
		jQuery(this).find("*").each(function(){
			var bgIMG = jQuery(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				jQuery(this).css('background-image', 'none');
				jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
			}
		});
		
		//fix input with png-source
		jQuery(this).find("input[src$=.png]").each(function() {
			var bgIMG = jQuery(this).attr('src');
			jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
   		jQuery(this).attr('src', settings.blankgif)
		});
	
	}
	
	return jQuery;
};

})(jQuery);

(function($){
	/* hoverIntent by Brian Cherne */
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);

(function($){
	$.fn.superfish = function(op){

		var sf = $.fn.superfish,
			c = sf.c,
			$arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')),
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$), o = sf.op;
				clearTimeout(menu.sfTimer);
				menu.sfTimer=setTimeout(function(){
					o.retainPath=($.inArray($$[0],o.$path)>-1);
					$$.hideSuperfishUl();
					if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
				},o.delay);	
			},
			getMenu = function($menu){
				var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
				sf.op = sf.o[menu.serial];
				return menu;
			},
			addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };
			
		return this.each(function() {
			var s = this.serial = sf.o.length;
			var o = $.extend({},sf.defaults,op);
			o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
				$(this).addClass([o.hoverClass,c.bcClass].join(' '))
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			sf.o[s] = sf.op = o;
			
			$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {
				if (o.autoArrows) addArrow( $('>a:first-child',this) );
			})
			.not('.'+c.bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			o.onInit.call(this);
			
		}).each(function() {
			var menuClasses = [c.menuClass];
			if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
			$(this).addClass(menuClasses.join(' '));
		});
	};

	var sf = $.fn.superfish;
	sf.o = [];
	sf.op = {};
	sf.IE7fix = function(){
		var o = sf.op;
		if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity!=undefined)
			this.toggleClass(sf.c.shadowClass+'-off');
		};
	sf.c = {
		bcClass     : 'sf-breadcrumb',
		menuClass   : 'sf-js-enabled',
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		shadowClass : 'sf-shadow'
	};
	sf.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'overideThisToUse',
		pathLevels	: 1,
		delay		: 800,
		animation	: {opacity:'show'},
		speed		: 'normal',
		autoArrows	: true,
		dropShadows : true,
		disableHI	: false,		// true disables hoverIntent detection
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = sf.op,
				not = (o.retainPath===true) ? o.$path : '';
			o.retainPath = false;
			var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = sf.op,
				sh = sf.c.shadowClass+'-off',
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			sf.IE7fix.call($ul);
			o.onBeforeShow.call($ul);
			$ul.animate(o.animation,o.speed,function(){ sf.IE7fix.call($ul); o.onShow.call($ul); });
			return this;
		}
	});

})(jQuery);

var default_search = "Rechercher ...";
var currentfontselector = 0;

function autoRefresh()
{	$('#auto-refresh').load('auto-refresh.php');
	setTimeout("autoRefresh()",300000);
}

$(document).ready(function()
{	var intRegex = /^\d+$/;
	var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;

	$('ul.sf-menu').superfish();
	$('#qtt-par-page').change(function(){ document.location.href = $(this).val(); });

	$('#infobox').hover(
		function(){$(this).find('div.divinfobox:hidden').fadeIn(500);},
		function(){$(this).find('div.divinfobox:visible').fadeOut(500);}
	);

	$(document).pngFix();

	$('.info').qtip({
	  content: $(this).attr('alt'), show: 'mouseover', hide: 'mouseout',
	  style: { tip: { corner: 'topLeft', color: '#6699CC' } },
	  show: { effect: { length: 430, type: 'fade' } }, hide: { effect: { length: 430, type: 'fade' } }
	});

	$('.zoom').click(function()
	{	var info = $(this).attr('id').substring(5).split('|');
		var url = "affiche-faire-part-mariage.php?id="+encodeURIComponent(info[0])+'&f='+encodeURIComponent(info[1])+'&c='+encodeURIComponent(info[2]);
		$.fn.colorbox({href:url, iframe:true, innerWidth:700, innerHeight:700, scrolling:false, opacity:0.75});
	});

	$('.slideshow').cycle({ fx:'fade', timeout:3000, speed:1700, delay:-1500 });

	$('#app-qtt').keyup(function()
	{	var punit = $(this).attr('rel');
 		var qtt	  = $(this).val();
		var out   = '0';
		if(!intRegex.test(qtt)) qtt = 0;
		if(qtt > 9999) qtt = 9999; else if(qtt < 0) qtt = 0;
		out = Math.round(punit*qtt*100)/100;
		$('#app-qtt').val(qtt*1);
		$('#app-prix').html(out);
	});

	$('#launch-search').click(function(event)
	{	event.preventDefault();
		var url = ""; var urlend = $(this).attr('rel');
		$("input:checked").each(function(index,elem)
		{	var filter = $(elem).attr('id').split('_');
			if(filter[1] != 'all')
			{	if(index) url += "!";
				url += filter[0]+"~"+filter[1];
			}
		});
		url = "faire-part-mariage-search-"+url+urlend+".php";
		$(location).attr('href',url);
	});

	$('input:checkbox').click(function()
	{	if($(this).attr('checked'))
		{	//alert('toto!');
			var filter = $(this).attr('id').split('_');
			if(filter[1] == 'all')
			{	$('[rel='+filter[0]+']').attr('checked',false);
				$(this).attr('checked',true);
			} else {
				var zclass = $(this).attr('rel');
				$('#'+zclass+'_all').attr('checked',false);
			}
		}
	});

	$('#search-valid').submit(function(event)
	{	event.preventDefault();
		var value = $('#search-box').val();
		if(value == default_search || value == '') return false;
		var url = "faire-part-mariage-search-recherche~"+value+"-page-12-1-show-asc-ref.php";
		$(location).attr('href',url);
		return false;
	});

	$('#search-box').click(function()
	{	var value = $(this).val();
		if(value == default_search) $(this).val('');
	});

	$('[panier]').click(function(e)
	{ e.preventDefault();
	  prix = encodeURIComponent($(this).attr("prix"));
	  obj  = encodeURIComponent($(this).attr("panier"));
	  ref  = encodeURIComponent($(this).attr("ref"));
	  img  = encodeURIComponent($(this).attr("img"));
	  qtt  = $("#app-qtt").val();
	  $.fn.colorbox({href:"mon-panier.php?add="+ref+"&desc="+img+"&ref="+obj+"&prix="+prix+"&qtt="+qtt,iframe:"true",innerWidth:"950px",innerHeight:"600px"});
	});
	
	$('#aff-panier').click(function(e)
	{	e.preventDefault();
		$.fn.colorbox({href:"mon-panier.php",iframe:"true",innerWidth:"950px",innerHeight:"600px"});
	});

	$('.selectfont').click(function()
	{	var nfo = $(this).attr("rel").split('|');
		currentfontselector = nfo;
		//$.fn.colorbox({href:"affiche_police.php?style="+encodeURIComponent(stl)+"&id="+encodeURIComponent(zid),iframe:"true",innerWidth:"650px",innerHeight:"500px"});
		$.fn.colorbox({innerWidth:"650px", innerHeight:"400px", inline:true, href:"#font-selector"});
	});

	$('#tab').tabs({ fx: { opacity: 'toggle' } });

	// hover sur les polices de la popup
	$('.fntPrv').hover(function() { $(this).animate({paddingLeft: '16px' }, 200); },	function() { $(this).animate({paddingLeft: '1px' }, 200);	});
	$('.fntTd').css({border:'1px solid white'}).hover(function()  { $(this).css({border:'1px solid rgb(200,200,200)'}); },	function() { $(this).css({border:'1px solid white'}); });
	$('.fntPrv').click(function()
	{	var stl = currentfontselector[0];
		var zid = currentfontselector[1];
		var imgid = 'img'+stl+'font'+zid;
		var font = $(this).attr('font');
		$("#"+imgid).html('<img src="fnt_thumb.php?font='+encodeURIComponent(font)+'">').next().val(font);
		$("#auto-refresh").load("auto-refresh.php?style="+stl+"&id="+zid+"&font="+encodeURIComponent(font));		
		$.fn.colorbox.close();
	});

// 	var colorarray = new Array("#FFFFFF", "#EEEEEE", "#FFFF88", "#FF7400", "#CDEB8B", "#6BBA70", "#006E2E", "#C3D9FF", "#4096EE", "#356AA0", "#FF0096", "#B02B2C", "#000000");

	$('.selectcolor').each(function()
	{	var zcolor = $(this).attr('color');
		var obj = this;
		$(this).colorPicker({
			defaultColor: parseInt(zcolor,10),
			click: function(color)
			{	/*$("#auto-refresh").load("auto-refresh.php?style="+$(obj).attr("stl")+"&id="+$(obj).attr("zid")+"&color="+encodeURIComponent(color));*/
				
				$('input[name=color_'+$(obj).attr("stl")+$(obj).attr("zid")+']').val(color);
				//alert(color);
			}
		});
	});

	autoRefresh();
});

