
function createOspCookies(results,uri){
	createCookie('letudiant_osp_search_results',results);
	createCookie('letudiant_osp_uri',uri);
}


function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License


function getBackOspLink(domain){

	var value = readCookie('letudiant_osp_uri');
	if(value != null && value != undefined){
		url = domain+value;
		$('.back_osp_link').attr('href',decodeURIComponent(url.replace(/\+/g, '%20')));
		$('.back_osp_link').show();
	}

}

function getSimilar(search_engine_id,fiche_id,limit,domain,proxy_domain){
	 jQuery(document).ready(function(){

	$.post('www.letudiant.fr/ajax/recherche_osp_similar.html',
		    {
				'search_engine_id': search_engine_id,
				'fiche_id': fiche_id,
				'url' : location.href,
				'limit' : limit,
				'proxy_domain' : 'http://www.letudiant.fr/static'
		    },
		    function(data){
		        if ("" != data)
		        {
		        	$('#osp_similar').html(data);
		        	$('#osp_similar').show();
		        	$('#osp_similar_title').show();
		        }
		    });});
}

function getPreviousNextLinks(current_id,domain){
	 jQuery(document).ready(function(){

	$.post('www.letudiant.fr/ajax/recherche_osp_link.html',
    {
        'current_id': current_id,
        'domain' : domain
    },
    function(data){
    	if ("" != data)
        {
        	var links = data.split('|');
        	if(links[1] != ''){
        		$('.next_link').html("<a class='next_link' href='"+links[1]+"'><em>Suivant &gt;&gt;</em></a>");
        	}
        	if(links[0] != ''){
        		$('.prev_link').html("<a class='prev_link' href='"+links[0]+"'><em>&lt;&lt; Précédent</em></a>");
        	}
        }
    });
	});
}

function getStats(id_fiche,id_search_engine,domain){
	$.post('www.letudiant.fr/ajax/recherche_osp_stats.html',
		    {
		        'id_fiche': id_fiche,
		        'id_search_engine' : id_search_engine
		    },
		    function(data){
		    	if ("" != data)
		        {
		    		$('#osp_stats').html(data);
		        	$('#osp_stats').show();
		        }
		    });
}

function getArrayStats(ids_fiche,id_search_engine,domain){
	$.post('www.letudiant.fr/ajax/recherche_osp_stats.html',
		    {
		        'ids_fiche': ids_fiche,
		        'id_search_engine' : id_search_engine
		    },
		    function(data){
		    	if ("" != data)
		        {
		    		$('#osp_stats').html(data);
		        	$('#osp_stats').show();
		        }
		    });
}

function getLogoStatsScript(id_fiche,id_search_engine,domain){
	 jQuery(document).ready(function(){
	$.post('www.letudiant.fr/ajax/recherche_osp_stats.html',
		    {
		        'id_fiche': id_fiche,
		        'id_search_engine' : id_search_engine,
		        'for_logo' : true
		    },
		    function(data){
		    	if ("" != data)
		        {
		    		$('#osp_stats').html(data);
		        	$('#osp_stats').show();
		        }
		    });
				});
}



function showVideoIframe(video_id, title) {
  var c = jQuery('div#video_' + video_id);

  if(!title) {
    title = 'Présentation de l\'école'
  }

  c.dialog({
    autoOpen: false,
    closeText: '',
    title: title,
    draggable: false,
    width: 481,
    height: 340,
    modal: true,
    resizable: false,
    zindex: 9999,
    dialogClass: 'video-iframe'
  });

  jQuery('div#video_' + video_id + ' .ui-dialog-container .ui-dialog-titlebar').hide();

  c.show();
  c.dialog('open');

  jQuery('#header').css('z-index', '100');
}

function closeVideoIframe(video_id){
	var c = jQuery('div#video_' + video_id);
	c.dialog('close');
	jQuery('#header').css('z-index', '9999');
}

function showIframe(id){
alert('dfdsf');
	var c = jQuery('div#jqueryuip_'+id+'.jobapply.disown.window');
    c.dialog({
      autoOpen: false,
      width: 1000,
      height: 1000,
      modal: true,
      resizable: false
    });
      var c = jQuery('div#jqueryuip_'+id+'.jobapply.disown.window');
	  c.show();

      c.dialog('open');

      jQuery('.ui-dialog').css('border', 'none');
      jQuery('.ui-dialog').css('background', 'none');

	  if (jQuery.browser.msie) {
		var e = jQuery("#myiframepostuler").contents().find("#jqueryui.jobapply.postuler"), wp = window.parent.jQuery('div#jqueryuip_'+id+'.jobapply.disown.window');
		wp.data('height.dialog', e.outerHeight(true));
		wp.data('width.dialog', e.outerWidth(true));
		wp.data('position.dialog', 'center');
	  }



	setDefaultsValuesPostulation();
	var e = jQuery("#jqueryui_'+id+'.jobapply.postuler");
	e.find("a.close:first").click(function()
	{
	  if (window.parent)
	  {
	    window.parent.jQuery('div#jqueryuip_'+id+'.jobapply.disown.window').dialog('close');
    }
	  return false;
	});

	e.find("button#bt_reset").click(function(event)
	{
	  event.stopPropagation();
	  setDefaultsValuesPostulation();
          return false;
	});

	e.find("input#postuler_email_apply-name").blur(function()
	{
		createCookie('postuler_email_apply-name', jQuery(this).val(), 3);
	});
	e.find("input#postuler_email_apply-firstname").blur(function()
	{
		createCookie('postuler_email_apply-firstname', jQuery(this).val(), 3);
	});
	e.find("input#postuler_email_apply-email").blur(function()
	{
		createCookie('postuler_email_apply-email', jQuery(this).val(), 3);
	});
	e.find("select#postuler_email_niveau_etude").blur(function()
	{
		createCookie('postuler_email_niveau_etude', jQuery(this).val(), 3);
	});


	var h_resize = e.outerHeight(), w_resize = e.outerWidth(), wp = window.parent.jQuery('div#jqueryuip.jobapply.disown.window');
	wp.data('height.dialog', h_resize);
	wp.data('width.dialog', w_resize);
	wp.data('position.dialog', 'center');

}

/**
 * @author BEGUIER_Gilles
 *
 * @param elements
 *          les elements auxquels appliquer le toolTip
 * @param baseIdMouseFollower
 *          la racine de l'id du tooltip auquel sera ajouté l'id de la fiche
 *
 *@param borderColorToolTip
 *          code hexadecimal de la couleur de la bordure du tooltip
 *
 */
function initOSPFicheWToolTips(elements,baseIdMouseFollower, borderColorToolTip)
{
    $(elements).wTooltip({id:"none",
                            callBefore: function(tooltip, node)
                                         {
                                           $(tooltip).attr( "id",baseIdMouseFollower+ $(node).children(".idFiche").html() );
                                           //$(tooltip).html($(tooltip).attr("id"))  ;
                                         },
                            style:{padding:"7px",
                                    border:"2px solid "+borderColorToolTip,
                                    borderTopWidth:"4px",
                                    zIndex:1000,
                                    background:"#FFFFFF",
                                    width:"300px",
                                    minHeight:"50px",
                                    fontSize:"12px"
                                   }
                         });
}







function saveCookiesLinks() {
    createCookie('jsa_search_url_page', $('#_search_url_page').val(), 0);
    createCookie('jsa_search_types', $('#_search_types').val(), 0);
}
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function setDefaultsValuesPostulation() {
    var genre = readCookie('postuler_email_genre');
    var nom = readCookie('postuler_email_apply-name');
    var prenom = readCookie('postuler_email_apply-firstname');
    var email = readCookie('postuler_email_apply-email');
    var date_naissance_day = readCookie('postuler_email_date_naissance_day');
    var date_naissance_month = readCookie('postuler_email_date_naissance_month');
    var date_naissance_year = readCookie('postuler_email_date_naissance_year');
    var departement = readCookie('postuler_email_departement');
    var id_statut_etude = readCookie('postuler_email_id_statut_etude');
    var niveau_etude = readCookie('postuler_email_niveau_etude');
    var domaine = readCookie('postuler_email_domaine');
    var filiere = readCookie('postuler_email_filiere');
    var message = readCookie('postuler_email_message');
    var cv = readCookie('postuler_email_cv');
    var lm = readCookie('postuler_email_lm');

    init_input_default_value('$("input#postuler_email_apply-name")', 'Votre nom*');
    init_input_default_value('$("input#postuler_email_apply-firstname")', 'Votre prénom*');
    init_input_default_value('$("input#postuler_email_apply-email")', 'Votre e-mail*');
    init_input_default_value('$("textarea#postuler_email_message")', 'Votre message d\'accompagnement (5000 caractères maximum)');

    if (null != genre && '' != $.trim(genre)) {
      for(cpt = 1; cpt <= 2; cpt++)
      {
        if($("input#postuler_email_genre_" + cpt).val() == genre)
        {
          $("input#postuler_email_genre_" + cpt).attr('checked', true);
          break;
        }
      }
    }
    if (null != nom && '' != $.trim(nom)) $("input#postuler_email_apply-name").val($.trim(nom));
    if (null != prenom && '' != $.trim(prenom)) $("input#postuler_email_apply-firstname").val($.trim(prenom));
    if (null != email && '' != $.trim(email)) $("input#postuler_email_apply-email").val($.trim(email));
    if (null != date_naissance_day && '' != $.trim(date_naissance_day)) $("select#postuler_email_date_naissance_day option[@value=" + date_naissance_day + "]").attr("selected", "selected");
    if (null != date_naissance_month && '' != $.trim(date_naissance_month)) $("select#postuler_email_date_naissance_month option[@value=" + date_naissance_month + "]").attr("selected", "selected");
    if (null != date_naissance_year && '' != $.trim(date_naissance_year)) $("select#postuler_email_date_naissance_year option[@value=" + date_naissance_year + "]").attr("selected", "selected");
    if (null != departement && '' != $.trim(departement)) $("select#postuler_email_departement option[@value=" + departement + "]").attr("selected", "selected");
    if (null != id_statut_etude && '' != $.trim(id_statut_etude)) $("select#postuler_email_id_statut_etude option[@value=" + id_statut_etude + "]").attr("selected", "selected");
    if (null != niveau_etude && '' != $.trim(niveau_etude)) $("select#postuler_email_niveau_etude option[@value=" + niveau_etude + "]").attr("selected", "selected");
    if (null != domaine && '' != $.trim(domaine))
    {
      domaine = domaine.split(',');
      nb_domaines = domaine.length;
      for(cpt = 0; cpt < nb_domaines; cpt++)
      {
        $("select#postuler_email_domaine option[@value=" + domaine[cpt] + "]").attr("selected", "selected");
      }
    }
    if (null != filiere && '' != $.trim(filiere))
    {
      filiere = filiere.split(',');
      nb_filieres = filiere.length;
      for(cpt = 0; cpt < nb_filieres; cpt++)
      {
        $("select#postuler_email_filiere option[@value=" + filiere[cpt] + "]").attr("selected", "selected");
      }
    }
    if (null != message && '' != $.trim(message)) $("textarea#postuler_email_message").val($.trim(message));
    //Si on trouve des infrmations dans les cookies, on les affiche
    if (null != cv && '' != $.trim(cv))
    {
        $("span#postuler_cv_cookie_name").html(cv);
        $("span#postuler_cv_cookie").css('display', 'inline');
        $("span#postuler_cv_file").hide();
        $("input#postuler_cv_cookie_hidden").attr('value', cv);
    }
    if (null != lm && '' != $.trim(lm))
    {
        $("span#postuler_lm_cookie_name").html(lm);
        $("span#postuler_lm_cookie").css('display', 'inline');
        $("span#postuler_lm_file").hide();
        $("input#postuler_lm_cookie_hidden").attr('value', lm);
    }
}

$(document).ready(function()
{
    
  /* postuler */
  if ($('div#jqueryuip.jobapply.disown.window').size())
  {
    var c = $('div#jqueryuip.jobapply.disown.window');
    c.dialog({
      autoOpen: false,
      width: 1000,
      height: 1000,
      modal: true,
      resizable: false,
      open: function() {$('body').css('overflow','visible'); }
    });

    jQuery('#header').css('z-index', '100');
    $('#myiframepostuler').attr("scrolling", "no");



    //Lors du click sur le bouton Postuler
    $('a.disownp.jobapply').click(function()
    {
        var c = $('div#jqueryuip.jobapply.disown.window');
        c.show();
        c.dialog( "option", "zIndex", 10000 );
        c.dialog('open');

        $('.ui-dialog').css('border', '1px solid black');
        $('.ui-dialog-titlebar').hide();
        if ($.browser.msie) {
		var e = $("#myiframepostuler").contents().find("#jqueryui.jobapply.postuler"), wp = window.parent.$('div#jqueryuip.jobapply.disown.window');
		wp.data('height.dialog', e.outerHeight(true));
		wp.data('width.dialog', e.outerWidth(true));
		wp.data('position.dialog', 'center');


        }

        return false;
    });
  }

  //Si la fenetre Postuler est ouverte
  if ($('#jqueryui.jobapply.postuler').size() && window.parent)
  {
      setDefaultsValuesPostulation();

      var e = $("#jqueryui.jobapply.postuler");
      //Fermeture de la fenetre
      e.find("a.close").click(function()
      {
          jQuery('#header').css('z-index', '9999');
		  if (window.parent)
	  {
              window.parent.$('div#jqueryuip.jobapply.disown.window').dialog('close');
          }
          return false;
      });

      //Réinitialisation du formulaire
      e.find("button#bt_reset").click(function(event)
      {
      });

      /*
       * Sur le click des boutons "Supprimer / resaisir CV(LM)"
       * Cachage des PJ en cookies
       * Affichage des champs file
       * Mise à nulle de la valeur du champ caché
       */
      e.find("button#bt_reset_cv_cookie").click(function(event)
      {
          $("span#postuler_cv_cookie").hide();
          $("span#postuler_cv_file").css('display', 'inline');
          $("input#postuler_cv_cookie_hidden").attr('value', '');

      });
      e.find("button#bt_reset_lm_cookie").click(function(event)
      {
          $("span#postuler_lm_cookie").hide();
          $("span#postuler_lm_file").css('display', 'inline');
          $("input#postuler_lm_cookie_hidden").attr('value', '');
      });

      //Enregistrement des valeurs des champs dans les cookies à chaque perte de focus
      e.find("input#postuler_email_genre_1").blur(function()
      {
          createCookie('postuler_email_genre', $(this).val(), 3);
      });
      e.find("input#postuler_email_genre_2").blur(function()
      {
          createCookie('postuler_email_genre', $(this).val(), 3);
      });
      e.find("input#postuler_email_apply-name").blur(function()
      {
          createCookie('postuler_email_apply-name', $(this).val(), 3);
      });
      e.find("input#postuler_email_apply-firstname").blur(function()
      {
          createCookie('postuler_email_apply-firstname', $(this).val(), 3);
      });
      e.find("input#postuler_email_apply-email").blur(function()
      {
          createCookie('postuler_email_apply-email', $(this).val(), 3);
      });
      e.find("select#postuler_email_date_naissance_day").blur(function()
      {
          createCookie('postuler_email_date_naissance_day', $(this).val(), 3);
      });
      e.find("select#postuler_email_date_naissance_month").blur(function()
      {
          createCookie('postuler_email_date_naissance_month', $(this).val(), 3);
      });
      e.find("select#postuler_email_date_naissance_year").blur(function()
      {
          createCookie('postuler_email_date_naissance_year', $(this).val(), 3);
      });
      e.find("select#postuler_email_departement").blur(function()
      {
          createCookie('postuler_email_departement', $(this).val(), 3);
      });
      e.find("select#postuler_email_id_statut_etude").blur(function()
      {
          createCookie('postuler_email_id_statut_etude', $(this).val(), 3);
      });
      e.find("select#postuler_email_niveau_etude").blur(function()
      {
          createCookie('postuler_email_niveau_etude', $(this).val(), 3);
      });
      e.find("select#postuler_email_domaine").blur(function()
      {
          createCookie('postuler_email_domaine', $(this).val(), 3);
      });
      e.find("select#postuler_email_filiere").blur(function()
      {
          createCookie('postuler_email_filiere', $(this).val(), 3);
      });
      e.find("textarea#postuler_email_message").blur(function()
      {
          createCookie('postuler_email_message', $(this).val(), 3);
      });

      var h_resize = e.outerHeight(), w_resize = e.outerWidth(), wp = window.parent.$('div#jqueryuip.jobapply.disown.window');
      wp.data('height.dialog', h_resize);
      wp.data('width.dialog', w_resize);
      wp.data('position.dialog', 'center');

  }







  // Vide les champs ayant toujours leurs valeur par défaut lors de la soumission d'un formulaire
  $('form').submit(function()
  {
	$('input[type=text]').each(function()
    {
      if ($(this).val() == $(this).attr('title'))
      {
        $(this).val('');
      }
    });
	$('textarea').each(function()
    {
      if ($(this).val() == $(this).attr('title'))
      {
        $(this).val('');
      }
    });
  });
});


// Met une valeur dans un champ input qui sera effacé lors du focus et remise lors de la perte du focus si il est vide
//function init_input_default_value(selecteur, valeur)
function init_input_default_value(selecteur, valeur)
{
  if (!eval(selecteur+".size()"))
    return ;

  var objet = eval(selecteur);

  objet.attr('title', valeur)
  if (objet.val() == '')
  {
    objet.val(valeur);
  }
  objet.focus(function(srcc)
  {
    if ($(this).val() == $(this).attr('title'))
    {
	  $(this).val('');
    }
  });
  objet.blur(function()
  {
    if ($(this).val() == '')
    {
	  $(this).val($(this).attr('title'))
    }
  });
  objet.blur();
}


/* Elément chargées une fois que tous les HTML est chargé ainsi que tous les éléments de la page */
$(window).load(function(){

  // Affichage du bouton postuler une fois la page chargée
  if($('.jsa.showjob span.apply'))
  {
    $('.jsa.showjob span.apply').css('display', 'block');
  }

        if($('h1.color_custom').length > 0){
            var borderTooltipColor = $('h1.color_custom').css('background-color');
            initOSPFicheWToolTips(".displayToolTip", "osp_mouse_follower_", borderTooltipColor);
        }



});
