$(document).ready(function() {
      // put all your jQuery goodness in here.

  stretch_portal_content();
  $(window).resize( stretch_portal_content );

  // onload
  // check stylesheet cookie
  if(readCookie("wltContrast")=='high')
  {
    $("#setHighContrast").hide();
    setStylesheet('high');
  }
  else if(readCookie("wltContrast")=='normal')
  {
    $("#setHighContrast").hide();
    setStylesheet('normal');
  }
  else
  {
    $("#setNormalContrast").hide();
    setStylesheet('normal');
  }


  // click link to switch stylesheet
  $("a.switchStylesheet").click(function () {
    if($(this).attr('id')=="setHighContrast")
    {
      setStylesheet('high');
    }
    else
    {
      setStylesheet('normal');
    }
    return false;
  });

  // click link to switch stylesheet
  $("a.recruiterDelete").click(function () {
    if(confirm($(this).attr('rel')))
    {
      return true;
    }else{
      return false;
    }
  });
  // click link to switch stylesheet
  $("a.itemDelete").click(function () {
    if(confirm($(this).attr('title')))
    {
      return true;
    }else{
      return false;
    }
  });

  $('a.delCurrentLanguage').click(function() {
	  $(this).parent().addClass('hideContainer');
	  $(this).parent().find('select').each(function(){
		  $(this).val('');
	  });
	  $('#jobprofile_lang_displayed_fields').attr('value', $('#jobprofile_lang_displayed_fields').attr('value') -1);
	  if (parseInt($('#jobprofile_lang_displayed_fields').attr('value')) < parseInt($('#jobprofile_lang_total_fields').attr('value'))) {
		  $('a#addOtherLanguage').show();
	  }
	  return false;
  });
  
  $('a#addOtherLanguage').click(function() {
	  if (parseInt($('#jobprofile_lang_displayed_fields').attr('value')) < parseInt($('#jobprofile_lang_total_fields').attr('value'))) {
		  $('#jobprofile_lang_displayed_fields').attr('value', parseInt($('#jobprofile_lang_displayed_fields').attr('value')) + 1);
		  var found = false;
		  $('#allLangContainer').children().each(function() {
			  if ($(this).hasClass('hideContainer')) {
				  if (!found) {
					  $(this).removeClass('hideContainer');
					  found = true;
				  }
			  }
		  });
		  
		  $('#grp_lang_' + $('#jobprofile_lang_displayed_fields').attr('value')).parent().removeClass('hideContainer');
	  }
	  
	  if (parseInt($('#jobprofile_lang_displayed_fields').attr('value')) == parseInt($('#jobprofile_lang_total_fields').attr('value'))) {
		  $('a#addOtherLanguage').hide();
	  }
	  return false;
  });  
  if (parseInt($('#jobprofile_lang_displayed_fields').attr('value')) != 0) {
	  for (i= parseInt($('#jobprofile_lang_displayed_fields').attr('value')); i<parseInt($('#jobprofile_lang_total_fields').attr('value')); i++) {
		  $('#grp_lang_' + (i+1)).parent().addClass('hideContainer');
	  }
  }
  


  // forms
  // Form Highlight :
	// http://www.keyframesandcode.com/resources/javascript/jQuery/demos/highlight-demo.html
  // $('form').highlight('.formSection', 'formSectionFocus');
  // Form Help Tooltip


  // add a cv to favorites
  $(".linkToFav a").click(function () {
    var favId = $(this).attr('id');
    $.getJSON($("#"+favId).attr('href'),function(data){
      $("#"+favId).attr('href', data['href']);
      $("#"+favId).text(data['label']);
      if(data['status']=='added')
      {
        $("#"+favId).removeClass("addFavoriteCV");
        $("#"+favId).addClass("removeFavoriteCV");
      }else{
        $("#"+favId).removeClass("removeFavoriteCV");
        $("#"+favId).addClass("addFavoriteCV");
      }
      /*
		 * $.prompt(data['status'],{ //submit: mysubmitfunc, buttons: { Ok:true }
		 * });
		 */
    });
    return false;
  });

  /*
	 * // add a cv to favorites $("#saveSearch").click(function () { // var hmtl =
	 * $.get($(this).attr('href')) var hmtl = ""; var searchQuery = "blabla"
	 * $.get($(this).attr('href'), function(data){ hmtl=hmtl+data; });
	 *  // ; hmtl=hmtl+'<input type="text" id="searchTitle" name="searchTitle"
	 * value="" />'; $.prompt(hmtl,{ submit: mysubmitfunc, callback:
	 * mycallbackform, buttons: { Ok: true, Cancel: false }, top: '15%' });
	 * 
	 * return false; });
	 */

  function mycallbackform(v,m,f){
    alert('search title => ' + f.searchTitle + " / query => " + $("#saveSearch").attr('rel'));
    alert('post to action to save search params');
    $.prompt('Search Saved!')
  }

  function mysubmitfunc(v,m,f){
    an = m.children('#searchTitle');
    if(f.searchTitle == ""){
      an.css("border","solid #ff0000 1px");
      return false;
    }
    return true;
  }


  // overlay / modal
  // video
  // $(".videoFrame").colorbox({iframe:true, innerWidth:425,
	// innerHeight:344});
  // save search
  // $("#saveSearch").colorbox();


});

function stretch_portal_content() {
  if( $(window).height() > $('body').height() ) {
    $('.wContent').height('auto')
    $( '#mainContent' ).height(
    $(window).height() - ( $('body').height() - ($('#mainContent').height()) )
    );
  }
}

function mysubmitfunc(v,m,f){
  an = m.children('#alertName');
  if(f.alertName == ""){
    an.css("border","solid #ff0000 1px");
    return false;
  }
  return true;
}


// switch css + set cookie
function setStylesheet(stylesheet)
{
  if(stylesheet=="high")
  {
    $('#mainCss').attr("href", "/assets/css/screenHigh.css");
    $("#setHighContrast").hide();
    $("#setNormalContrast").show();
    createCookie("wltContrast", "high", 7);

  }
  else
  {
    $('#mainCss').attr("href", "/assets/css/screen.css");
    $("#setNormalContrast").hide();
    $("#setHighContrast").show();
    createCookie("wltContrast", "normal", 7);
  }
}

// http://www.quirksmode.org/js/cookies.html
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 eraseCookie(name) {
  createCookie(name,"",-1);
}



/* temp */
function parseQuery ( query ) {
  var Params = new Object ();
  if ( ! query ) return Params; // return empty object
  var Pairs = query.split(/[;&]/);
  for ( var i = 0; i < Pairs.length; i++ ) {
    var KeyVal = Pairs[i].split('=');
    if ( ! KeyVal || KeyVal.length != 2 ) continue;
    var key = unescape( KeyVal[0] );
    var val = unescape( KeyVal[1] );
    val = val.replace(/\+/g, ' ');
    Params[key] = val;
  }
  return Params;
}

/**
* Unobtrusively set up placeholder behaviors on all text inputs for which
* the title attribute has been set. The title text will be used as the
* placeholder text.
* @author Travis Miller
* @link http://www.electrumdigital.com/

$( function() {

  $("input[type=text][title],input[type=password][title],textarea[title]")
  .each( showPlaceholder ) // initialize each control on page load
  .blur( showPlaceholder )
  .focus( hidePlaceholder );

  $("form").submit( function() {
    $( "input[type=text][title],input[type=password][title],textarea[title]", this ).each( hidePlaceholder );
  } );

  function showPlaceholder() {
    var $control = $(this);
    var placeholderText = $control.attr("title");
    if ( $control.val() === "" || $control.val() === placeholderText ) {
      $control.addClass("placeholder");
      $control.val(placeholderText);
    }
  };

  function hidePlaceholder() {
    var $control = $(this);
    if ( $control.val() === $control.attr("title") ) {
      $control.removeClass("placeholder");
      $control.val("");
    }
  }

} );
*/
