/**
 * This file contains a small number of very useful functions which
 * are available on all pages.
 */

function isIE()
{
  return jQuery.browser.msie;
}

/**
* Mantis ID: 0005867 check safari browser type and to use firebug console
*/
function isSafari()
{
	return jQuery.browser.safari;
}

if (jQuery.browser.msie && jQuery.browser.version < 7)
{
	Array.prototype.indexOf = function(item)
	{
		var item = item ? item : 0, n;
		for (var idx = 0; n = this[idx]; idx++)
		{
			if (n == item)
			  return idx;
		}
		return -1;
	};
}

if (jQuery.browser.msie && typeof(console) == "undefined")
{
	// To get around no Firebug in IE causing stray
	// logging statements to potentially crash
	// our running Javascript.
	console = {
		log : function() { },
		dir : function() { },
		group : function() { },
		groupEnd : function() { },
		debug : function() { },
		trace : function() { }
	}
}


/**
 * Try and log output using the Firebug console, otherwise fall back to the
 * standard Firefox console (requires starting with -console to see).
 */
function log(text)
{
	
	if (isIE()) 
	   return;	
	if (isSafari()) 
	   return;

  try
  {
    console.log.apply(text, arguments);
  }
  catch (exception)
  {
    dump(text);
  }
}

/**
 * Try and inspect an object in the Firebug console, otherwise fall back to the
 * standard Firefox console (requires starting with -console to see) and
 * just print the object.
 */
function dir(object)
{
  if (isIE())
     return;
  if (isSafari()) 
   return;

  try
  {
      console.dir(object);
  }
  catch (exception)
  {
      dump(object);
  }
}
/**
 * Log an error to the Firebug console, or display an alert box if
 * it isn't available.
 */
function logError(text, iException)
{
	if (isIE())
        return;
    if (isSafari()) 
	   return;

    try
    {
        console.group(text);

        // These are set for exceptions thrown from Java.
        if (iException.message)
        {
            console.log(iException.message);
        }
        if (iException.javaStack)
        {
            console.log(iException.javaStack);
        }

        console.dir(iException);
        console.trace();
        console.groupEnd();
    }
    catch (exception)
    {
        var msg = text + " " + iException.description +
              "\n filename=" + iException.filename +
              "\n lineNumber=" + iException.lineNumber +
              "\n Message=" + iException.message +
              "\n name=" + iException.name +
              "\n number=" + iException.number;

        if (iException.javaStack)
        {
            msg += "\n javaStack = " + iException.javaStack;
        }

        window.alert(msg);
    }
}

/**
 * Simple little func to insert lots
 * of strings into another string without
 * having to keeping adding sections.
 */
function fmt(text)
{
	for (var i = 1; i < arguments.length; i++)
	{
		text = text.replace(/%s/, arguments[i]);
	}
	return text;
}

/**
 * Redirect to javascript version of the register page
 */
function RegisterRedirc(lastPage)
{
window.location.href = "/careers/register.html?lastPage="+lastPage;
return false;
}

/**
* Redirect to Javascript version of save search page
*/
function saveSearch(){
	var url = "/careers/user/save-search.html";
	window.location.href=url;
	return false;
}

/**
* Redirect to Javascript version of save search page
*/
function saveSearch(type){
	var url = "/careers/user/save-search.html?searchType="+type;
	window.location.href=url;
	return false;
}

/**
*	Redirect to Javascript version of save search page
*/
function editSearch(id){
	var url = "/careers/user/edit-search.html?searchId="+id;	
	window.location.href=url;
	return false;
}

/**
*	Redirect to Javascript version of advance search
*/
function advanceSearch(id, searchType){
	var url = "/careers/submit-advanced-search.html?searchId="+id+"&searchType="+searchType;	
	window.location.href=url;
	return false;
}
/**
 * Additional common jQuery functions we might use.
 */
(function($) {
	$.fn.extend({
	  /**
	   * Set visibility of current selection based on
	   * the value of mode we pass in.
	   */
	  visible : function(mode)
	  {
	    if (mode)
	      $(this).show();
	    else
	      $(this).hide();
	    return this;
	  },

	  /**
	   * Appends a new child element/elements, and then selects
	   * inwards to the innermost new element.
	   */
	  appendAndSelect : function(html)
	  {
	    var r = [];

	    this.each(function()
	    {
	      $(this).append(html);

	      var innermost = this.lastChild;
	      while (innermost.lastChild)
	      {
	        innermost = innermost.lastChild;
	      }
	      r.push(innermost);
	    });

	    return $(r);
	  },

	  checked : function(value)
	  {
	  	if (typeof(value) == "undefined")
	  	{
	  		return this.length > 0 ? this.get(0).checked : null;
	  	}
	  	else
	  	{
	  		return this.attr("checked", !!value);
	  	}
	  },

	  /**
	   * Makes an element take up the whole screen.
	   */
	  fullScreen : function()
	  {
	  	var pageSizes = $.getPageSize();

	  	$(this).css({
	  		position: "fixed",
	  		left: 0,
	  		top: 0,
	  		width: pageSizes.pageWidth,
	  		height: pageSizes.pageHeight
	    });

	    return this;
	  },

	  /**
	   * Centres the popup by changing it to position: fixed and setting
	   * its left and top CSS elements.
	   */
	  centre : function()
	  {
	    var arrPageSizes = $.getPageSize();
	    var arrPageScroll = $.getPageScroll();

	    var setX = (arrPageSizes.windowWidth - $(this).width()) / 2;
	    setX = (setX < 0) ? 0 : setX;

      if ($.browser.msie && $.browser.version < 7)
      {
        $(this).css({
            "position"    : "absolute",
            "left"        : setX + "px",
            "top"         : arrPageScroll.top + (arrPageSizes.windowHeight / 10)
        });
      }
	    else
      {
		    $(this).css({
		    	  "position"    : "fixed",
		        "left"        : setX + "px",
		        "top"         : arrPageSizes.windowHeight / 10
		    });
	   }
	  }
  });

  /**
   * Extra selectors for positioning
   */
  $.extend($.expr[":"], {
    // Selects objects with CSS "position: absolute"
    "absolute" : "jQuery.css(a,'position')=='absolute'",

    // Selects objects with CSS "position: relative"
    "relative" : "jQuery.css(a,'position')=='relative'"
  });

	/**
	 * Very handy functions for getting page sizes and offsets.
	 */
	$.extend($, {
	  /**
	   * getPageSize() by quirksmode.com
	   *
	   * @return Return an object with page width, height and window width, height
	   */
	  getPageSize : function()
	  {
	    var xScroll, yScroll;
	    if (window.innerHeight && window.scrollMaxY) {
	      xScroll = window.innerWidth + window.scrollMaxX;
	      yScroll = window.innerHeight + window.scrollMaxY;
	    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
	      xScroll = document.body.scrollWidth;
	      yScroll = document.body.scrollHeight;
	    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	      xScroll = document.body.offsetWidth;
	      yScroll = document.body.offsetHeight;
	    }
	    var windowWidth, windowHeight;
	    if (self.innerHeight) { // all except Explorer
	      if(document.documentElement.clientWidth){
	        windowWidth = document.documentElement.clientWidth;
	      } else {
	        windowWidth = self.innerWidth;
	      }
	      windowHeight = self.innerHeight;
	    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	      windowWidth = document.documentElement.clientWidth;
	      windowHeight = document.documentElement.clientHeight;
	    } else if (document.body) { // other Explorers
	      windowWidth = document.body.clientWidth;
	      windowHeight = document.body.clientHeight;
	    }
	    // for small pages with total height less then height of the viewport
	    if(yScroll < windowHeight){
	      pageHeight = windowHeight;
	    } else {
	      pageHeight = yScroll;
	    }
	    // for small pages with total width less then width of the viewport
	    if(xScroll < windowWidth){
	      pageWidth = xScroll;
	    } else {
	      pageWidth = windowWidth;
	    }
	    return {
	      pageWidth: pageWidth,
	      pageHeight: pageHeight,
	      windowWidth: windowWidth,
	      windowHeight: windowHeight
	    };
	  },

	  /**
	   * getPageScroll() by quirksmode.com
	   *
	   * @return Return an object with x,y page scroll values.
	   */
	  getPageScroll : function()
	  {
	    if (self.pageYOffset)
	    {
	      return { left: self.pageXOffset, top: self.pageYOffset };
	    }
	    else if (document.documentElement && document.documentElement.scrollTop)
	    {  // Explorer 6 Strict
	      return { left: document.documentElement.scrollLeft, top: document.documentElement.scrollTop };
	    }
	    else if (document.body)
	    {// all other Explorers
	      return { left: document.body.scrollLeft, top: document.body.scrollTop };
	    }
	    return { left: 0, top: 0 };
	  }
	});
})(jQuery);