// JavaScript Document
// Script pour afficher le menu principal de haut de page
jQuery(document).ready(function() {

	// Chargement des sous-menus déroulant
	jQuery('ul.sf-menu').supersubs({
		minWidth:    12,   		// minimum width of sub-menus in em units
		maxWidth:    27,   		// maximum width of sub-menus in em units
		extraWidth:  1     		// extra width can ensure lines don't sometimes turn over
								// due to slight rounding differences and font-family
	}).superfish({ 
		delay:       500, 
		speed:       'fast',
		autoArrows:  false,     // disable generation of arrow mark-up
		dropShadows: false      // disable drop shadows
	});
	
	// Chargement de prettyPhoto
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'facebook', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
	});
 
	// Récupérer la langue
	var user_lang = jQuery('html').attr('lang');

	// Chargement du menu Inscription-Mon compte
	var current_url = window.location.href;
	var blog = jQuery('body').attr('id');
	if (user_lang == 'en-US')
		url = '/en/wp-content/themes/alterheros-bp/ajax_menu.php'
	else
		url = '/wp-content/themes/alterheros-bp/ajax_menu.php'
	//console.log(current_url);
	jQuery.get(url, { url : current_url }, function(data) {
		jQuery('#ajax_menu').append(data);
		initMenu();
		if (user_lang != 'en-US') {
			jQuery('a#bp-admin-links').replaceText( /Link/gi, "Lien" );
			jQuery('a#bp-admin-links-my-links').replaceText( /My Link/gi, "Mes lien" );
		}
		jQuery('a#bp-admin-blogs').parent().hide();
	});
	
	// custom easing called "custom"
	jQuery.easing.custom = function (x, t, b, c, d) {
		var s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	}
	
	jQuery(function() {
		jQuery("#tabs").tabs({ cookie: { expires: 1 } });
	});
	
	// Corriger les libéllés en anglais sur la page d'abonnement
	if (jQuery('#signup_form').length && (user_lang == 'en-US')) {
		jQuery('#field_70').prev().text('About you');
		jQuery('#field_3_day').prev().text('Birthday (required)');
		jQuery('#field_25').prev().text('Country');
		jQuery('#field_30').prev().text('Province-State');
		jQuery('#field_34').prev().text('Region');
		jQuery('#field_71').prev().text('City');
		jQuery('#field_4').prev().text('Sexual orientation');
		jQuery('#field_4').html('<option value="">--------</option><option value="Bisexuel(le)">Bisexual</option><option value="Homosexuel(le)">Homosexual</option><option value="Heterosexuel(le)">Heterosexual</option><option value="En questionnement">Questioning</option>');
		jQuery('#field_8').prev().text('Gender identity');		
		jQuery('#option_65').parent().html('<input type="radio" value="Homme" id="option_65" name="field_8">Man</input>');
		jQuery('#option_66').parent().html('<input type="radio" value="Femme" id="option_66" name="field_8">Woman</input>');
		jQuery('#option_67').parent().html('<input type="radio" value="Homme transsexuel" id="option_67" name="field_8">Transsexual Man</input>');
		jQuery('#option_68').parent().html('<input type="radio" value="Femme transsexuelle" id="option_68" name="field_8">Transsexual Woman</input>');
		jQuery('#option_69').parent().html('<input type="radio" value="Transgenre" id="option_69" name="field_8">Transgender</input>');
	}
	
	// Corriger les libéllés en anglais sur la page de profile
	if (jQuery('.profile-fields').length && (user_lang == 'en-US')) {
		jQuery('.field_70').find('td.label').text('About you');
		jQuery('.field_3').find('td.label').text('Birthday');
		jQuery('.field_25').find('td.label').text('Country');
		jQuery('.field_30').find('td.label').text('Province-State');
		jQuery('.field_34').find('td.label').text('Region');
		jQuery('.field_71').find('td.label').text('City');
		jQuery('.field_4').find('td.label').text('Sexual orientation');
		jQuery('.field_8').find('td.label').text('Gender identity');		
	}	
	
	// Chargement de la location, si le champ pays existe sur la page
	if (jQuery('#field_25').length ) {
		var type_id = jQuery('#field_25').attr('class');
		p_url = '/wp-content/plugins/ask-a-question/ajax_pays.php'
		pr_url = '/wp-content/plugins/ask-a-question/ajax_provinces.php'
		r_url = '/wp-content/plugins/ask-a-question/ajax_regions.php'
	
		// Changer le type si c'est un input
		var tag = jQuery('#field_25');
		var tag_type = tag[0].nodeName;

		if (tag_type == 'INPUT') {
			jQuery('#field_25').replaceWith(jQuery('<select name="field_25" id="field_25"></select>'));
			jQuery('#field_30').replaceWith(jQuery('<select name="field_30" id="field_30"></select>'));
			jQuery('#field_34').replaceWith(jQuery('<select name="field_34" id="field_34"></select>'));
		}
		// Chargement des pays dans le drop-down du profil
		jQuery.get(p_url, { lang : user_lang, value : type_id }, function(data) {
			if(data) {
				jQuery('#field_25').html(data);
			}
		});
		
		// Chargement des province dans le drop-down du profil
		jQuery.get(pr_url, { lang : user_lang }, function(data) {
			if(data) {
				jQuery('#field_30').html(data);
			}
		});
		
		// Chargement des régions dans le drop-down du profil
		jQuery.get(r_url, { lang : user_lang }, function(data) {
			if(data) {
				jQuery('#field_34').html(data);
			}
		});	
			
		// Chargement des provinces dans le drop-down du profil
		jQuery('#field_25').live('change', function(){  
			var pays_id = jQuery('#field_25').val();
			//console.log(current_url);
			// Réinitialiser les autres champs province et région
			jQuery('#field_30').html('<option></option>');
			jQuery('#province').hide();
			jQuery('#field_34').html('<option></option>');
			jQuery('#region').hide();
			jQuery.get(pr_url, { id : pays_id, lang : user_lang, site : blog }, function(data) {
				if(data) {
					jQuery('#field_30').html(data);
					jQuery('#province').show();
				}
			});
		});
		// Chargement des regions dans le drop-down du profil	
		jQuery('#field_30').live('change', function(){  
			var province_id = jQuery('#field_30').val();
			//console.log(current_url);
			jQuery('#field_34').html('<option></option>');
			jQuery('#region').hide();
			jQuery.get(r_url, { id : province_id, lang : user_lang, site : blog }, function(data) {
				if(data) {
					jQuery('#field_34').html(data);
					jQuery('#region').show();				
				}
			});
		});
	}
	
	// Changer le URL du formulaire de création de lien sur Altercité
	if (jQuery('#link-details-form').length ) {
		var create_url = jQuery('#link-details-form').attr('action');
		create_url = create_url.replace('cite/', '');
		jQuery('#link-details-form').attr('action', create_url);
	}
	
	// Traduire en JS les strings qui ne veulent pas se traduire en PHP (bogue)
	fixFrench(user_lang);
	var liens_li = jQuery('li#links-personal-li').detach();
	var end_li = jQuery('li#groups-personal-li');
	liens_li.insertAfter(end_li);
	
	//Get our elements for faster access and set overlay width
    var div = jQuery('div.item-list-tabs');
	var ul = jQuery('div.item-list-tabs ul');
    // unordered list's left margin
    var ulPadding = 15;

    //Get menu width
    var divWidth = div.width();

    //Remove scrollbars
    div.css({overflow: 'hidden'});

    //Find last image container
    var lastLi = ul.find('li:last-child');

    //When user move mouse over menu
    div.mousemove(function(e){

      //As images are loaded ul width increases,
      //so we recalculate it each time
      var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

      var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
      div.scrollLeft(left);
    });
	
});

function fixFrench(user_lang) {
	if (user_lang != 'en-US') {
		jQuery('a#user-links').replaceText( /Link/gi, "Lien" );
		jQuery('a#links-my-links').replaceText( /My Link/gi, "Mes lien" );
	}
}

function initMenu() {
	jQuery('ul.menu ul').hide();
	jQuery('ul.menu ul ul').hide();
	jQuery('ul.menu ul').before("<a href='#' class='lien_bg'></a>");
	//jQuery('ul.menu ul ul').before("<a href='#' class='lien_bg png_bg'></a>");
	jQuery('ul.menu ul ul').hide();
	
	jQuery('li.bp-login a').click(
		function() {
			jQuery('.login_window').slideToggle();
			return false;
		}
	);
	
	jQuery('li#bp-adminbar-notifications-menu a.lien_bg').click(
		function() {
			jQuery('li#bp-adminbar-notifications-menu ul').slideToggle();
			return false;
		}
	);

	jQuery('li#bp-adminbar-notifications-menu a:first').click(
		function() {
			jQuery('li#bp-adminbar-notifications-menu ul').slideToggle();
			return false;
		}
	);
	
	jQuery('li#bp-adminbar-account-menu a:first').click(
		function() {
			var checkElement = jQuery(this).next().next();
			checkElement.slideToggle();
			return false;
		}
	);		
	
	jQuery('li#bp-adminbar-account-menu a.lien_bg').click(
		function() {
			var checkElement = jQuery(this).next();
			var parent = "";
			var parent_depth = jQuery(this).parents().map(function () { 
                                return this.tagName; 
                              })
                          .get().length;
						
			if (parent_depth == 7) {
				parent = 'menu';
				jQuery('.menu ul:visible').addClass('collapsible');
			}
			if (parent_depth == 9) {
				parent = "menu li ul";
				jQuery('.' + parent + ' ul:visible').addClass('collapsible');
			}
			if (parent_depth == 11) {
				parent = "menu li ul li ul";
				jQuery('.' + parent + ' ul:visible').addClass('collapsible');
			}		
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if(jQuery('.' + parent).hasClass('collapsible')) {
					jQuery('.' + parent + ' ul:visible').slideUp('normal');
				}
				jQuery('.' + parent + ' ul:visible').slideUp('normal');
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				jQuery('.' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);	
}
