/**
 * @author rocky2000
 */

 $(document).ready(function(){

	// hide informational banners
	if ($('#status_msg').length > 0)
	{
		setTimeout(function () { 
		$('#status_msg').fadeOut('slow');
		},3000);
	}
	
	$(".input_login, .query").focus(function() {
		$(this).val("").removeClass("blur");
	});
	
	$('input[title!=""], textarea[title!=""]').hint();
	
	$(".job_block").mouseover(highlightJobOffer);
	$(".job_block").mouseleave(unHighlightJobOffer);
	$(".job_block").click(goToJobOffer);
	
	$("#logo, #primary-content").pngFix();
	
	$("#personal_message, #personal_intro").change(function() {
		if($("#personal_message").hasClass("blur"))
			var personal_message = "";
		else
			var personal_message = $("#personal_message").val();
		
		if($("#personal_intro").hasClass("blur"))
			var personal_intro = "";
		else
			var personal_intro = $("#personal_intro").val();
		
		var destination_url = "/profile/setPersonalMessage"
		$.post(destination_url, {personal_message: personal_message, personal_intro: personal_intro}, {} , "json");
	});
	
	if($("#feedback_btn").length > 0)
	{
		$("#feedback_btn a").facebox();
		
		$(document).bind('reveal.facebox', function() {
			$(".send_feedback_btn").click(sendFeedback);
			$(".template").hide();
		});
	}
});


function goToJobOffer()
{
	window.location.href = $("h2 > a", this).attr("href");
	
}

function highlightJobOffer()
{
	$(this).addClass("highlighted");
}

function unHighlightJobOffer()
{
	$(this).removeClass("highlighted");
}


/* function for ghost texts */
function ghost_text(){
	$(".ghost_text").each(function(){
		var org_value = $(this).attr("value");
		$(this).bind("focus", function(){
			if($(this).attr("value") == org_value){
				$(this).attr("value", "");
			}
			$(this).removeClass("ghost_text");
		});
		$(this).bind("click", function(){
			if($(this).attr("value") == org_value){
				$(this).attr("value", "");
			}
			$(this).removeClass("ghost_text");
		});
		$(this).bind("blur", function(){
			if($(this).attr("value") == ""){
				$(this).addClass("ghost_text");
				$(this).attr("value", org_value);
			}
		});
	})
}


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function r2(n) {

  ans = n * 1000
  ans = Math.round(ans /10) + ""
  while (ans.length < 3) {ans = "0" + ans}
  len = ans.length
  ans = ans.substring(0,len-2) + "." + ans.substring(len-2,len)
  return ans
} 

function str_replace(search, replace, subject) {
    var temp = subject.split(search);
    return temp.join(replace);
}

function cachableTimeStr()
{
	var dateObj	  =	new Date();
	dateObj = new Date();
	var timeStr = dateObj.getTime();
	dateObj = null;
	return timeStr;
}


/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }
    
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),
    
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
        else if($input.val() === title)
        	$input.val('').addClass(blurClass);
      }).focus(remove).blur(); // now change all inputs to title
      
      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);



function sendFeedback()
{
	var feedback = $("#feedback", $(this).parent("p").parent("div")).val();
	
	var destination_url = "/welcome/sendFeedback/"+cachableTimeStr();
	$.post(destination_url, {feedback: feedback}, function(result)
	{
		$(document).trigger('close.facebox');
	});

}
