$(document).ready(function() {
	// PRODUCT MENU
	initProductMenu();

	// PRODUCT SEARCH
	initProductSearch();

	// PROMOTIONS
	initPromotions();

	// HEADER
	initHeader();

	// BLOCK PRODUCT 
	initBlockProduct();

	//BLOCK PHONE 
	initPhone();
});

/**
* Function initBlockProduct 
* @description : init the product part
* @date : 2011-05-24
* @author : antoine <a.friteau@hegyd.com>
**/
function initBlockProduct() {
	var products = $('.blockItemProduct');

	products.each(function(index, item) {
		var link = $(item).find('.visual a').attr('href');
		var regex = new RegExp('^javascript');

		//TEST référencement sur bloc objet publicitaire
		if(regex.test(link)){
			var pos = link.lastIndexOf('/');	
			link = link.substring(pos,link.length - 1);
		}
		$(item).find('.infoPrice .price').click(function(event) {
			$(this).css({cursor: 'pointer'});
			window.location.href = link;
		});
		$(item).find('.actions .btnDetail').click(function(event) {
			$(this).css({cursor: 'pointer'});
			window.location.href = link;
		});
		$(item).find('.actions .btnVideo').click(function(event) {
			$(this).css({cursor: 'pointer'});
			window.location.href = link;
		});
	});
}

var selectedCountry = null;
/**
* Function initHeader 
* @description : init the header part
* @date : 2011-05-24
* @author : antoine <a.friteau@hegyd.com>
**/
function initHeader() {
	var countriesPart = $('#header .hotlineTop .listCountries');
	var chooseCountry = countriesPart.find('.chooseCountry');
	var countryMenu = countriesPart.find('.countryMenu');
	var phones = $('#header .hotlineTop .hotlineNum .phone');
	var newCountry = '';

	chooseCountry.click(function(event) {
		if(countryMenu.hasClass('activ')) {
			countryMenu.removeClass('activ');
			countryMenu.slideUp('fast');
			countryMenu.append(selectedCountry);
			countryMenu.before($(this));
			if($(this).hasClass('france') == true) {
				newCountry = 'france';
			}
			else if($(this).hasClass('luxembourg') == true) {
				newCountry = 'luxembourg';
			}
			else if($(this).hasClass('belgique') == true) {
				newCountry = 'belgique';
			}
			else if($(this).hasClass('suisse') == true) {
				newCountry = 'suisse';
			}
			phones.each(function(index, item) {
				if($(item).hasClass(newCountry) == true) {
					$(item).fadeIn('slow');
				}
				else {
					$(item).hide();
				}
			});
		}
		else {
			countryMenu.addClass('activ');
			countryMenu.slideDown('fast');
			selectedCountry = $(this);
		}
	});
}

var selectedCountry = null;
/**
* Function initPhone 
* @description : init the phone part
* @date : 2011-05-25
* @author : antoine <a.friteau@hegyd.com>
**/
function initPhone() {
	var countriesPart = $('.infoHotline .flags');
	var chooseCountry = countriesPart.find('.chooseCountry');
	var phones = $('.infoHotline .phone');
	var newCountry = '';

	chooseCountry.click(function(event) {
		if($(this).hasClass('france') == true) {
			newCountry = 'france';
		}
		else if($(this).hasClass('luxembourg') == true) {
			newCountry = 'luxembourg';
		}
		else if($(this).hasClass('belgique') == true) {
			newCountry = 'belgique';
		}
		else if($(this).hasClass('suisse') == true) {
			newCountry = 'suisse';
		}
		phones.each(function(index, item) {
			if($(item).hasClass(newCountry) == true) {
				$(item).fadeIn('slow');
			}
			else {
				$(item).hide();
			}
		});
	});
}

/**
* Function initProductMenu 
* @description : init the product menu
* @date : 2011-05-19
* @author : antoine <a.friteau@hegyd.com>
**/
function initProductMenu() {
	// BUTTON PART
	var menuButton = $('#mainMenu .menu ul li.item-2 a');
	menuButton.click(function(event) {
		if(menuButton.hasClass('activ')) {
			menuButton.removeClass('activ');
			$('.blockMenuHover').slideUp('fast');
		}
		else {
			menuButton.addClass('activ');
			$('.blockMenuHover').slideDown('fast');
		}

		return false;
	});

	// MENU PART
	var menu = $('.blockMenuHover .menuTop .blockCategories ul');
	var subMenusList = menu.find('li ul').parent();
	subMenusList.each(function(index, item) {
		$(item).find('>a').click(function(event) {
			if($(item).find('>ul').hasClass('activ') == true) {
				$(item).find('>ul').slideUp('fast');
				$(item).find('>ul').removeClass('activ');
			}
			else {
				$(item).find('>ul').slideDown('fast');
				$(item).find('>ul').addClass('activ');
			}
			return false;
		});
	});

	// SEARCH PART
	var searchForm = $('.blockMenuHover .menuBottom .formSearch form');
	var searchInput = searchForm.find('.searchField');
	searchInput.focus(function(event) {
		if(searchInput.val() == 'Votre recherche...') {
			searchInput.val('');
		}
	});
	searchInput.blur(function(event) {
		if(searchInput.val() == '') {
			searchInput.val('Votre recherche...');
		}
	});
	var categorySelect = searchForm.find('.categoryField');
	var subcategorySelect = searchForm.find('.subcategoryField');
	categorySelect.change(function(event) {
		categoryId = categorySelect.val();
		$.ajax({
			url: '/get-sub-categories.html?ajax',
			data: {categoryId: categoryId},
			method: 'POST',
			success: function(data) {
				// remplacement des sous-categories
				subcategorySelect.empty().append(data);
			}
		});
	});
	searchInput.parent().parent().submit(function(event) {
		if(searchInput.val() == 'Votre recherche...') {
			searchInput.val('');
		}
	});

	// NEWSLETTER PART
	var newsletterInput = $('.blockMenuHover .menuBottom .formNewsletter form .emailField');
	newsletterInput.focus(function(event) {
		if(newsletterInput.val() == 'Votre email...') {
			newsletterInput.val('');
		}
	});
	newsletterInput.blur(function(event) {
		if(newsletterInput.val() == '') {
			newsletterInput.val('Votre email...');
		}
	});
}

/**
* Function initProductMenu
* @description : init the product menu
* @date : 2011-05-19
* @author : antoine <a.friteau@hegyd.com>
**/
function initProductSearch() {
	var searchProduct = $('#column-2 .searchProduct form input.searchField');
	searchProduct.focus(function(event) {
		var searchValue = searchProduct.val();
		if(searchValue  == 'Votre recherche...') {
			searchProduct.val('');
		}
	});
	searchProduct.blur(function(event) {
		var searchValue = searchProduct.val();
		if(searchValue  == '') {
			searchProduct.val('Votre recherche...');
		}
	});
	searchProduct.parent().parent().submit(function(event) {
		var searchValue = searchProduct.val();
		if(searchValue  == 'Votre recherche...') {
			searchProduct.val('');
		}
	});
}

/**
* Function initProductMenu
* @description : init the product menu
* @date : 2011-05-19
* @author : antoine <a.friteau@hegyd.com>
**/
function initPromotions() {
	var promotionsForm = $('#column-2 .blockPromo .content form');
	var promotionsEmail = promotionsForm.find('.emailField');
	var promotionsCompany = promotionsForm.find('.companyField');
	promotionsEmail.focus(function(event) {
		if(promotionsEmail.val() == 'Votre e-mail...') {
			promotionsEmail.val('');
		}
	});
	promotionsEmail.blur(function(event) {
		if(promotionsEmail.val() == '') {
			promotionsEmail.val('Votre e-mail...');
		}
	});
	promotionsCompany.focus(function(event) {
		if(promotionsCompany.val() == 'Société') {
			promotionsCompany.val('');
		}
	});
	promotionsCompany.blur(function(event) {
		if(promotionsCompany.val() == '') {
			promotionsCompany.val('Société');
		}
	});
}

