// common
var w = window;
var d = w.document;
$(document).ready(function () {
	
	$('.slideshow .insert .menu li a').click(function () {
		var elmt = $(this);
		if(!$('.slideshow').hasClass('move')) {
			var index = elmt.parent().index();
			manageFavoriteIsert.pause();
			manageFavoriteIsert.select(index); // 0, 1, 2 ou 3
		}
		return false;
	});
	//manageFavoriteIsert.play();
	
	// manage custom select
	$('#goodPlans select').ySelect();
	
	// manage add to favorites
	$('#header .addFavorites').hover(function () {
		// enter
		var elmt = $(this);
		
		if(isIE == 6) {
			var path = $('img', elmt)[0].style.filter;
			path = path.replace('.png', '-hover.png');
			$('img', elmt)[0].style.filter = path;
			
		} else {
			var path = $('img', elmt).attr('src');
			path = path.replace('.png', '-hover.png');
			$('img', elmt).attr('src', path);
		}
		
	}, function () {
		// leave
		var elmt = $(this);
		
		if(isIE == 6) {
			var path = $('img', elmt)[0].style.filter;
			path = path.replace('-hover.png', '.png');
			$('img', elmt)[0].style.filter = path;
			
		} else {
			var path = $('img', elmt).attr('src');
			path = path.replace('-hover.png', '.png');
			$('img', elmt).attr('src', path);
		}
	});
	
	// hover png
	$('img[src*=add-event.png], img[src*=clock.png]').hover(function () {
		// enter
		var elmt = $(this);
		
		if(isIE == 6) {
			var path = $(elmt)[0].style.filter;
			path = path.replace('.png', '-hover.png');
			$(elmt)[0].style.filter = path;
			
		} else {
			var path = $(elmt).attr('src');
			path = path.replace('.png', '-hover.png');
			$(elmt).attr('src', path);
		}
		
	}, function () {
		// leave
		var elmt = $(this);
		
		if(isIE == 6) {
			var path = $(elmt)[0].style.filter;
			path = path.replace('-hover.png', '.png');
			$(elmt)[0].style.filter = path;
			
		} else {
			var path = $(elmt).attr('src');
			path = path.replace('-hover.png', '.png');
			$(elmt).attr('src', path);
		}
	});
	
	// ajouter MWEAL aux favoris
	$('#header .addFavorites').click(function (e) {
		e.preventDefault();
		var url = w.location.href;
        var title = d.title;
        if (w.sidebar) { // Mozilla Firefox Bookmark
            w.sidebar.addPanel(title, url, "");
        } else { // IE Favorite
            w.external.AddFavorite( url, title);
        }
		return false;
	});
	
	manageCarousel.init($('.carousel'));
	
	$('.off .scroll').show();
	$('.scroll .container').jScrollPane({
		dragMinHeight : 27,
		dragMaxHeight : 27
	});
	$('.off .scroll').removeAttr('style');
	
	$('#nav > div > .menu > li').hover(function () {
		// enter
		var elmt = $(this);
		elmt.addClass('hover');
	}, function () {
		// leave
		var elmt = $(this);
		elmt.removeClass('hover');
	});
	
	manageForm.emptyField('#newsletter .textField input', 'Votre adresse email');
	
	// manage stars
	stars.init();
	
	manageEventList.init();
	manageDateList.init();
	
	// manage login & date list navigation
	$('#login, #eventList').live('mouseenter', function () {
		// enter
		var elmt = $(this);
		elmt.css('z-index', 2);
		elmt.addClass('select');
		$('> .contentTab', elmt).stop().fadeTo(200, 1, function () {
			$(this).removeAttr('style').show();
		});
		manageDateList.update();
		manageEventList.update();
	});
        $('#login, #eventList').live('mouseleave', function () {
		// leave
		var elmt = $(this);
		elmt.css('z-index', 1);
		elmt.removeClass('select');
		$('> .contentTab', elmt).stop().fadeTo(200, 0, function () {
			$(this).removeAttr('style');
		});
	});
	
	// manage tab navigation
	$('#goodPlans .tabs .menu li').click(function () {
		var elmt = $(this);
		if(!elmt.hasClass('select')) {
			$('#goodPlans .tabs .menu li.select').removeClass('select');
			elmt.addClass('select');
			manageTabs2.show(elmt);
		}
		return false;
	});
	
	// show territory hover "pres de chez moi" links
	$('#tabs1Item1 ul a').hover(function() {
		enterMap($(this).parent());
	},function() {
		leaveMap($(this).parent());
	});
	$('#tabs1Item1 #mapLyon area').hover(function(e) {
		enterMap(e.currentTarget);
	},function(e) {
		leaveMap(e.currentTarget);
	});
	
	var enterMap = function(el) {
		var elmt = $(el);
		var index = elmt.index();
		$('#tabs1Item1 .visual img').eq(index).show();
	};
	
	var leaveMap = function(el) {
		var elmt = $(el);
		var index = elmt.index();
		$('#tabs1Item1 .visual img').eq(index).hide();
	};
	
	
});

$(window).load(function () {
	manageColumnHeight.init();
});

//Cufon.replace('#nav .menu a .rightBorder, .link1 .rightBorderDeco, .slideshow .insert .menu li a, .carousel .daysBefore, #goodPlans h2, #newsletter h2, #newsletter p, #share h2 a, #share .content h2, #mainContent > div:not(#movies) .title h2, #mainContent .title h1');
function updateCufon (elmt) {
    var path = '#nav .menu a .rightBorder, .link1 .rightBorderDeco, .slideshow .insert .menu li a, .carousel .daysBefore, #goodPlans h2, #newsletter h2, #newsletter p, #share h2 a, #share .content h2, #mainContent > div:not(#movies) .title h2, #mainContent .title h1';
    if(elmt) {
        path = elmt;
    }
    Cufon.replace(path);
}
updateCufon();

var manageColumnHeight = {
	init: function () {
		// contextual & mainContent equalise height
		var mainContentH = $('#mainContent').height();
		var contextualH = $('#contextual').height();
		var max = mainContentH;
		if (contextualH > mainContentH) {
			max = contextualH;
		}
		if(isIE == 6) {
			$('#mainContent, #contextual').css('height', max+'px');
		} else {
			$('#mainContent, #contextual').css('min-height', max+'px');
		}
	},
	update: function () {
		$('#mainContent, #contextual').removeAttr('style');
		manageColumnHeight.init();
	}
};

// WARNING : la mécanique de "manageDateList" et "manageEventList" est sensiblement la même
// TODO : factoriser la mécanique...
var manageDateList = {
	init: function () {
		$('#dateList .showMore').click(function () {
			var elmt = $(this);
			var container = $('#dateList .eventList');
			var hidden = false;
			if(elmt.text().indexOf('afficher') > -1) {
				hidden = true;
			}
			if(hidden) {
				if($('> li', container).length > 3) {
					elmt.html('Réduire');
				}
				manageDateList.show();
				manageDateList.config.hidden = false;
			} else {
				elmt.html('Tout afficher');
				manageDateList.hide();
				manageDateList.config.hidden = true;
			}
			return false;
		});
		
		var wrapper = $('#dateList .eventList').parent();
		var minHeight = manageDateList.minHeight();
		wrapper.css('height', minHeight+'px');
		
		$('#dateList .eventList .tools a').each(function () {
         			var elmt = $(this);
			$('a:first', elmt).live('click', function () {
				var elmt = $(this);
				var duration = manageDateList.config.duration;
				var container = elmt.parent().parent().parent();
				container.slideUp(duration, function () {
					container.remove();
					manageDateList.update();
				});
				return false;     
			});
		});
	},
	config: {
		duration : 350,
		hidden : true
	},
	update: function () {
		var hidden = manageDateList.config.hidden;
		if(hidden) {
			manageDateList.hide();
		} else {
			manageDateList.show();
		}
		
	},
	maxHeight: function () {
		var container = $('#dateList .eventList');
		var visible = false;
		if($('#dateList.select .contentTab:visible').length > 0) {
			visible = true;
		}
		if(!visible) {$('#dateList.select .contentTab').show();}
		
		var maxHeight = container.height();
		if($(container).css('border-top-width')) {
			maxHeight = maxHeight + parseInt($(container).css('border-top-width'));
		}
		
		if(!visible) {$('#dateList.select .contentTab').hide();}
		return maxHeight;
	},
	minHeight: function () {
		var container = $('#dateList .eventList');
		var minHeight = 0;
		var count = 0;
		if($(container).css('border-top-width')) {
			minHeight = minHeight + parseInt($(container).css('border-top-width'));
		}
		
		while((count < $('> li', container).length) && (count < 3)) {
			minHeight = minHeight + $('> li', container).eq(count).height();
			
			if($('> li', container).eq(count).css('border-bottom-width')) {
				minHeight = minHeight + parseInt($('> li', container).eq(count).css('border-bottom-width'));
			}
			count++;
		}
		return minHeight;
	},
	show: function () {
		var maxHeight = manageDateList.maxHeight();
		var wrapper = $('#dateList .eventList').parent();
		var duration = manageDateList.config.duration;
		
		wrapper.animate({
				'height': maxHeight+'px'
			},
			duration
		);
	},
	hide: function () {
		var minHeight = manageDateList.minHeight();
		var wrapper = $('#dateList .eventList').parent();
		var duration = manageDateList.config.duration;
		
		wrapper.animate({
				'height': minHeight+'px'
			},
			duration
		);
	}
};

// WARNING : la mécanique de "manageDateList" et "manageEventList" est sensiblement la même
// TODO : factoriser la mécanique...
var manageEventList = {
	init: function () {
		$('#eventList .textContainer .showMore').click(function () {
			var elmt = $(this);
			var container = $('#eventList .eventList');
			var hidden = false;
			if(elmt.text().indexOf('afficher') > -1) {
				hidden = true;
			}
			if(hidden) {
				if($('> li', container).length > 3) {
					elmt.html('Réduire');
				}
				manageEventList.show();
				manageEventList.config.hidden = false;
			} else {
				elmt.html('Tout afficher');
				manageEventList.hide();
				manageEventList.config.hidden = true;
			}
			return false;
		});
		
		var wrapper = $('#eventList .eventList').parent();
		var minHeight = manageEventList.minHeight();
		wrapper.css('height', minHeight+'px');
		
		$('#eventList .eventList .tools a').each(function () {
					var elmt = $(this);
			$('a:first', elmt).bind('click', function () {
				var elmt = $(this);
				var duration = manageEventList.config.duration;
				var container = elmt.parent().parent().parent();
				container.slideUp(duration, function () {
					container.remove();
					manageEventList.update();
				});
				return false;
						});
		});
	},
	config: {
		duration : 350,
		hidden : true
	},
	update: function () {
		var hidden = manageEventList.config.hidden;
		if(hidden) {
			manageEventList.hide();
		} else {
			manageEventList.show();
		}
		
	},
	maxHeight: function () {
		var container = $('#eventList .eventList');
		var visible = false;
		if($('#eventList.select .contentTab:visible').length > 0) {
			visible = true;
		}
		if(!visible) {$('#eventList.select .contentTab').show();}
		
		var maxHeight = container.height();
		if($(container).css('border-top-width')) {
			maxHeight = maxHeight + parseInt($(container).css('border-top-width'));
		}
		
		if(!visible) {$('#eventList.select .contentTab').hide();}
		return maxHeight;
	},
	minHeight: function () {
		var container = $('#eventList .eventList');
		var minHeight = 0;
		var count = 0;
		if($(container).css('border-top-width')) {
			minHeight = minHeight + parseInt($(container).css('border-top-width'));
		}
		
		while((count < $('> li', container).length) && (count < 3)) {
			minHeight = minHeight + $('> li', container).eq(count).height();
			
			if($('> li', container).eq(count).css('border-bottom-width')) {
				minHeight = minHeight + parseInt($('> li', container).eq(count).css('border-bottom-width'));
			}
			count++;
		}
		return minHeight;
	},
	show: function () {
		var maxHeight = manageEventList.maxHeight();
		var wrapper = $('#eventList .eventList').parent();
		var duration = manageEventList.config.duration;
		
		wrapper.animate({
				'height': maxHeight+'px'
			},
			duration
		);
	},
	hide: function () {
		var minHeight = manageEventList.minHeight();
		var wrapper = $('#eventList .eventList').parent();
		var duration = manageEventList.config.duration;
		
		wrapper.animate({
				'height': minHeight+'px'
			},
			duration
		);
	}
};

var stars = {
	hover: function (obj) {
		var current = $(obj);
		
		if(isIE == 6) {
			var path = $('img', current)[0].style.filter;
			path = path.replace('star-empty.png', 'star-hover.png');
			path = path.replace('star-full.png', 'star-hover.png');
			$('img', current)[0].style.filter = path;
			
		} else {
			var path = $('img', current).attr('src');
			path = path.replace('star-empty.png', 'star-hover.png');
			path = path.replace('star-full.png', 'star-hover.png');
			$('img', current).attr('src', path);
		}
	},
	empty: function (obj) {
		var elmt = $(obj);
		
		if(isIE == 6) {
			var path = $('img', elmt)[0].style.filter;
			path = path.replace('star-hover.png', 'star-empty.png');
			path = path.replace('star-full.png', 'star-empty.png');
			$('img', elmt)[0].style.filter = path;
		} else {
			var path = $('img', elmt).attr('src');
			path = path.replace('star-hover.png', 'star-empty.png');
			path = path.replace('star-full.png', 'star-empty.png');
			$('img', elmt).attr('src', path);
		}
	},
	full: function (obj) {
		var elmt = $(obj);
		
		if(isIE == 6) {
			var path = elmt[0].style.filter;
			path = path.replace('star-empty.png', 'star-full.png');
			elmt[0].style.filter = path;
		} else {
			var path = elmt.attr('src');
			path = path.replace('star-empty.png', 'star-full.png');
			elmt.attr('src', path);
		}
	},
	init: function () {
		
		$('.fixedStars .full').each(function () {
			stars.hover(this);
		});
		
		$('.stars li a').live('click', function () {
			var elmt = $(this);
			var container = elmt.parent().parent();
			var index = elmt.parent().index();
			container.addClass('fixedStars');
			
			$('li', container).removeClass('empty').removeClass('full').addClass('empty').slice(0, (index+1)).addClass('full').removeClass('empty');

            vote(container.attr('id'),index+1);

			return false;
		});
		
		
		$('.stars li').live('mouseenter', function () {
			// enter
			var current = $(this);
			var container = current.parent();
			var link = $('a', current);
			var status = 'on';
			if(container.hasClass('fixedStars')) {
				status = 'off';
				link.unbind('click');
				link.bind('click', function () {
					$(this).blur();
					return false;
				});
				return;
			}
			
			stars.hover(current);
			
			current.nextAll().each(function () {
				var elmt = $(this);
				stars.empty(elmt);
			});
			
			current.prevAll().each(function () {
				var elmt = $(this);
				stars.hover(elmt);
			});
			
		});
		
		$('.stars').live('mouseleave', function () {
			var container = $(this);
			
			if(container.hasClass('fixedStars')) {
				return;
			}
			
			$('li', container).each(function () {
				var elmt = $(this);
				stars.empty(elmt);
			});
			
			$('li.full img', container).each(function () {
				var elmt = $(this);
				stars.full(elmt);
			});
			
			if(isIE == 6) {
				fixPng($('.stars li'));
			}
		});
	}
};

var manageForm = {
	emptyField: function (selector, text) {
		var elmt = $(selector);
		elmt.focus(function () {
			if(elmt.val() == text) {
				elmt.val('');
			}
		});
		
		elmt.blur(function () {
			if(elmt.val() == '') {
				elmt.val(text);
			}
		});
	}
};

var manageFavoriteIsert = {
	config: {
		duration : 500,
		pauseDuration: 35000,
		cycleDuration: 20000
	},
	vars: function () {
		slideshowCycle : {}
	},
	play: function () {
		if(typeof(manageFavoriteIsert.vars.slideshowCycle) != 'undefined') {
			window.clearInterval(manageFavoriteIsert.vars.slideshowCycle);
		}
		manageFavoriteIsert.vars.slideshowCycle = window.setInterval(function () {
			manageFavoriteIsert.next();
		}, manageFavoriteIsert.config.cycleDuration);
	},
	pause: function () {
		window.clearInterval(manageFavoriteIsert.vars.slideshowCycle);
		window.setTimeout(function () {
			manageFavoriteIsert.play();
		}, manageFavoriteIsert.config.pauseDuration);
	},
	next: function () {
		var index = manageFavoriteIsert.nextIndex();
		manageFavoriteIsert.select(index);
	},
	nextIndex: function () {
		// retourne l'index de l'élement suivant
		var nextIndex = 0;
		
		var curentLi = $('.slideshow .insert .menu li.select');
		if(curentLi.next().length > 0) {
			nextIndex = curentLi.text();
		}
		
		return nextIndex;
	},
	select: function (nbItem) {
		
		var selectedItemNb = ($('.slideshow .insert .menu li.select').text()-1); // 0, 1, 2 ou 3
		
		if(selectedItemNb != nbItem) {
		
			if(!$('.slideshow').hasClass('move')) {
				$('.slideshow').addClass('move');
				
				var duration = manageFavoriteIsert.config.duration;
				
				
			
				var nextItem = $('.slideshow .wrapper ul li')[nbItem];
				
				$('.slideshow .insert .menu li').removeClass('select');
				$($('.slideshow .insert .menu li')[nbItem]).addClass('select');
				
				$(nextItem).show().css('z-index', '1');
				$('.slideshow .wrapper ul li.select').css('z-index', '2').stop().fadeTo(duration, 0, function () {
					$('.slideshow .wrapper ul li').removeClass('select').removeAttr('style');
					$(nextItem).addClass('select');
					
					$('#mainContent .slideshow .insert .selectedItem').removeClass('selectedItem');
					$($('#mainContent .slideshow .insert .item')[nbItem]).addClass('selectedItem');
				});
					
				
				
				var timer = window.setTimeout(function() {
					$('.slideshow').removeClass('move');
					window.clearTimeout(timer);
				}, duration);
			}
		}
	}
};

var manageTabs2 = {
	show: function (elmt) {
		
		var item = $('a', elmt).attr('href');
		
		$('#goodPlans .tabContent').hide();
		$(item).show();
		
	}
};

