/**
 * Applicat-Prazan
 * navigation & animations
 */
(function($){
$(document).ready(function() {

    $('#menu li').hover(
	function() {
	    var a = $(this); a.data('hover', true); a.find('ul.sub-menu').slideDown(600);
	},
	function() {
	    var a = $(this); a.data('hover', false);
	    setTimeout(function() { if (!a.data('hover')) a.find('ul.sub-menu').slideUp(300); }, 1000);
	}
    );
    $('#menu li.current-menu-ancestor').mouseover().find('ul.sub-menu').removeClass('sub-menu');
    $('#menu li.current-menu-item').mouseover().find('ul.sub-menu').removeClass('sub-menu');
    $('#footer a').each(function() { $(this).attr('target', 'ap-contact'); });

    $('.menu-item-644 a').click(function() {
	var l = document.location.href;
	if (l.match('/en/')) return false;
	l = l.replace(document.location.host, document.location.host + '/en');
	l = l.replace('/artistes/', '/en-artistes/');
	l = l.replace('/attachment/', '/attachment/en-');
	l = l.replace(/([0-9]{4})\//, '$1/en-');
	document.location.href = l;
	return false;
    });
    $('.menu-item-645 a').click(function() {
	var l = document.location.href;
	l = l.replace('/en/', '/').replace(/\/en\-/g, '/');
	document.location.href = l;
	return false;
    });

    $('.visits .picture a').hover(
	function() { $(this).find('.play').fadeIn(600); },
	function() { $(this).find('.play').fadeOut(300); }
    );

    $('div.artists a.artist').hover(
	function() {
	    var tid = '#' + $(this).attr('id').replace('link-', 'picture-');
	    $('div.pictures div.picture').fadeOut();
	    $(tid).fadeIn();
	},
	function() { }
    );

    var sc = { 'on': false };
    var sl = $('#slider');
    var c = $('#content');
    var ch = c.height();
    if (ch < 550) sl.hide();
    var sh = 550;
    function updateScroll() {
	sh = $(window).height() - 261;
	$('.artist .story').height(sh + 41);
    }
    function startScroll(e) {
	if (sc.on) return;
	ch = c.height();
	sc = { 'on': true,
	       'y': e.pageY };
	e.preventDefault();
    };
    function doScroll(e) {
	if (!sc.on) return;
	var mt = parseInt(sl.css('top'));
	mt += e.pageY - sc.y;
	mt = Math.max(41, mt);
	mt = Math.min(sh, mt);
	var p = (mt - 41) / (sh - 41);
	var q = ch - sh + 41 + 60;
	c.css('top', Math.min(0, -Math.round(p * q)) + 'px');
	sl.css('top', mt + 'px');
	sc.y = e.pageY;
	e.preventDefault();
    };
    function stopScroll(e) {
	if (!sc.on) return;
	sc.on = false;
    };

    sl.mousedown(startScroll);
    $(document).mousemove(doScroll);
    $(document).mouseup(stopScroll);
    if ($('.artist .story .zone').length) {
	c = $('.artist .story .zone');
	if ($('#body').height() > 610) {
	    updateScroll();
	    $(window).resize(function() { updateScroll(); });
	}
    }

    if ($('#anim').length) {

	var rg = $('#rivegauche').attr('src').replace('2.png', '1.png');
	var rd = $('#rivedroite').attr('src').replace('2.png', '1.png');
	(new Image()).src = rg;
	(new Image()).src = rd;
	setTimeout(function() { $('#rivegauche').attr('src', rg); }, 3500);
	setTimeout(function() { $('#rivedroite').attr('src', rd); }, 3500);
	$('#paris').delay(2000).fadeOut(2000);
	$('#rivegauche0').delay(2000).fadeOut(100);
	$('#rivegauche').delay(2000).animate({ 'left': 0, 'top': 30,
					       'width': 370, 'height': 366 }, 2000);
	$('#rivedroite0').delay(2000).fadeOut(100);
	$('#rivedroite').delay(2000).animate({ 'left': 520, 'top': 30,
					       'width': 374, 'height': 366 }, 2000);

    }

    if ($('.scroller').length) {

	var sx = $('.slidebox');
	var bw = sx.width();
	var sb = $('.supersize');
	var s = $('.slider');
	var w = s.find('ul').width() + 300;
	var wp = { 'on': false, 'kn': false, 'fc': false };
	var o = $('#overlay');
	var t = new Date();
	s.width(w);

	var ca = document.cookie.split(';');
	for (var i=0; i < ca.length; i++) {
	    var c = ca[i];
	    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
	    if (c.indexOf('scroll=') == 0)
		sb.scrollLeft(c.substring(7, c.length));
	}

	function startSlide(e) {
	    if (wp.on || wp.fc) return;
	    t = new Date();
	    wp = { 'on': true, 'kn': false,
		   'fc': false,
		   'target': $(e.target),
		   'x': e.pageX, 'x0': e.pageX,
		   'vx': 0, 't': t.getTime() };
	    e.preventDefault();
	};
	function moveSlide(e) {
	    if (!wp.on || wp.fc) return;
	    if (Math.abs(e.pageX - wp.x0) > 10) {
		t = new Date();
		wp.vx = .5 * wp.vx + .5 * (e.pageX - wp.x) / (t.getTime() - wp.t);
		if (wp.vx > 2) wp.vx = 2;
		if (wp.vx < -2) wp.vx = -2;
		wp.sx = sb.scrollLeft();
		wp.sx -= (e.pageX - wp.x);
		sb.scrollLeft(wp.sx);
		wp.x = e.pageX;
		wp.t = t.getTime();
	    }
	    e.preventDefault();
	};
	function scrollSlide(e) {
	    if (!wp.kn) return false;
	    wp.sx -= 50 * wp.vx;
	    sb.scrollLeft(wp.sx);
	    wp.vx = .8 * wp.vx;
	    if (Math.abs(wp.vx) > .01)
		setTimeout(scrollSlide, 50);
	    else wp.kn = false;
	};
	function stopSlide(e) {
	    if (!wp.on) return;
	    wp.on = false;
	    if (Math.abs(e.pageX - wp.x0) > 10) {
		wp.sx = sb.scrollLeft();
		wp.kn = true;
		scrollSlide();
	    } else {
		if (wp.target.hasClass('medium'))
		    focusPic();
	    }	    
	};

	function focusPic() {
	    wp.fc = true;
	    wp.target = wp.target.parent().find('.large');
	    wp.target.show().css('z-index', 93);
	    wp.target.animate({ // 'margin-top': - .2 * wp.target.data('height'),
				'margin-left': - .2 * wp.target.data('width'),
				'width': 1.4 * wp.target.data('width'),
				'height': 1.4 * wp.target.data('height') }, 500);
	    o.show();
	    // o.fadeIn(300);
	    var x = wp.target.offset().left + sb.scrollLeft();
	    x += (wp.target.width() - bw) / 2;
	    sb.animate({ 'scrollLeft': x }, 1000);
	    setTimeout(function() { o.mouseover(blurPic); }, 1000);
	    document.cookie = 'scroll=' + sb.scrollLeft();
	};
	function blurPic() {
	    wp.fc = false;
	    if (!wp.target) return;
	    o.hide();
	    // o.fadeOut(100);
	    o.unbind('mouseover');
	    wp.target.animate({ // 'margin-top': 0,
				'margin-left': 0,
				'width': wp.target.data('width'),
				'height': wp.target.data('height') }, 300,
			      function() { wp.target.hide(); });
	};

	s.bind('touchmove', function(e) {
	    e.preventDefault();
	    var h = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
	    e.pageX = h.pageX;
	    e.pageY = h.pageY;
	    if (!wp.on) startSlide(e);
	    moveSlide(e);
	});
	$(document).bind('touchend', stopSlide);
	s.mousedown(startSlide);
	$(document).mousemove(moveSlide);
	$(document).mouseup(stopSlide);
	o.click(blurPic);

	$('.scroller .next').click(function() { blurPic(); sb.animate({ 'scrollLeft': sb.scrollLeft() + 600 }, 1000); });
	$('.scroller .prev').click(function() { blurPic(); sb.animate({ 'scrollLeft': sb.scrollLeft() - 600 }, 1000); });

	s.find('.large').each(function() {
	    $(this).hide();
	    $(this).data({ 'width': $(this).width(),
			   'height': $(this).height() });
	});
	s.find('.medium').each(function() {
	    $(this).mouseover(function() {
		var over = this;
		wp.over = this;
		setTimeout(function() {
		    if (wp.over != over ||
			wp.on || wp.fc || wp.kn)
			return;
		    wp.target = $(over);
		    if (!wp.target.hasClass('medium'))
			alert(wp.target.attr('class'));
		    else focusPic();
		}, 500);
	    });
	});

    }
});
})(jQuery);

