// JavaScript Document
var slideFadeIN = 1000;
var slideFadeOUT = 2000;
var interval = 14000;
var ieCompatVersion = 9;
var isIE = false;


$(document).ready
(
	
	function()
	{
		$('a[href^="http"]:not([href^="http://www.yellowcat"]):not([href^="http://yellowcat"]) ').attr("target", "_blank");
		$('a[href$=".pdf"]').attr("target", "_blank");
		
		if ( $.browser.msie && $.browser.version < ieCompatVersion) 
		{
			isIE = true;
			
			if($.browser.version == 7 )
			{
				$('div#wrappercontent ul.ewi2_styles_regdetails_cvupload input[type=file]').removeAttr('style');
				$('div#wrappercontent div#searchbar input').css('margin-top','0');
				$('div#wrappercontent div#searchbar select').css('margin-top','0');
				
				
			}
		}
		
		if($('div.vacancyticker').size() ==1 )
		{
			/*
			crossfader script does not use display:none/display:block so positioning li's relative (even over-riding crosfader pos:absolute) will not work
			This JS therefore calculates height of tallest li (positioned absolute by crossfader) and sets container to this height
			*/
			var maxHeight = 0;
			$('div.vacancyticker li').each
			(
				function(i)
				{
					var h = $(this).height();
					maxHeight = Math.max(h,maxHeight);
					//alert('li height is: '+h+' max height is now: '+maxHeight);					
				}
			);
			$('div.vacancyticker').height(maxHeight);
		}
		
		if($('div#headercontent_right_buttons div#ewi2_styles_login_details').size() ==1 )
		{
			$('div#headercontent_right_buttons').prepend($('div#ewi2_styles_login_details a'));
			$('div#headercontent_right').prepend($('div#ewi2_styles_login_details'));
			$('div#headercontent_right').addClass('header-login-name');
		}
		
		
		if($('div.trigger-wrapper-jump').size()==1)
		{
			$('div#wrappercontent').addClass('wrapper-jump');
		}
		else if($('div.trigger-group-hug').size()==1)
		{
			$('div#wrappercontent').addClass('group-hug');
		}
		
		$('div#ewi2_styles_joblinks_industries li').each(
		function(i)
		{
			/* see if jobs by locationb link is active */
			var href = $(this).find('a').attr('href');
			var txt = $(this).find('a').text();
			var newtxt = txt.replace(/,$/,'');//get rid of last comma
			
			$(this).find('a').text(newtxt);
			var current_loc = window.location.href;
			//alert('Link a href: '+href+' Current loc: '+current_loc);
			if(current_loc.indexOf(href) > 0 )
			{
				$(this).addClass('selected');
				//return false;
			}
			
		}
		);
		
				
		$("body div#wrappercontent table.ewi2_skin_gridview th div").each(function() {
			 /*Replace spurious breaks in backend template*/
			var text = $(this).html();
			var newText = text.replace(/&nbsp;/g, "");
			$(this).html(newText);
							   

		});
		
		//$("body div#wrappercontent table.ewi2_skin_gridview td").text().replace(/&nbsp;/g, '');
		
		if($('div#maincontent div#searchbar').size()==1)$('body').addClass('search_bar');
		
		$('div.testimonials .slide').hide();
		$('div.testimonials').onImagesLoaded(function(_this){
			//alert('testimonials images loaded');
			$('div.testimonials').shuffle();
			testimonials();
		  });

		
		
		addHtml();
		
		
	}
);


function testimonials()
{
	
	$('div.testimonials .slide').eq(0).show();
	
	
	if(isIE)
	{
		window.setInterval(function()
		{
			$('div.testimonials .slide').filter(':visible').hide(0,function()
			{
				if($(this).next('div.slide').size())
				{
					$(this).next().show();
				}
				else
				{
					$('div.testimonials .slide').eq(0).show();
				}
			});
		},interval);	
		
		
	}
	else
	{
		window.setInterval(function()
		{
			$('div.testimonials .slide').filter(':visible').fadeOut(slideFadeOUT,function()
			{
				if($(this).next('div.slide').size())
				{
					$(this).next().fadeIn(slideFadeIN  );
				}
				else
				{
					$('div.testimonials .slide').eq(0).fadeIn(slideFadeIN );
				}
			});
		},interval);	
	}
}//function



function addHtml()
{
	/*
	Note 	- do not use JS to remove or move the last element #main_gutter, into the newly created container appended to body here
			- also do not set last element to display none when appending to body
			- both cases cause  ie6 to bork - see css - #main_gutter is simply moved off the screen but ie6 is happy
	
	*/
	$('body').addClass('yc-js');
	$('div.mainnavigation div.callus').prepend('<div class="social"><a href="http://www.facebook.com/pages/Yellow-Cat-Recruitment/103796448126" title="Follow the Yellow Cat on Facebook" class="fb"  target="_blank"><span>&nbsp;</span></a><a href="http://twitter.com/yellowcatjobs" title="Follow the Yellow Cat on Twitter" class="twitter"  target="_blank"><span>&nbsp;</span></a><a href="http://www.linkedin.com/groups/Yellow-Cat-Recruitment-2947966?mostPopular=&gid=2947966" title="Follow the Yellow Cat on LinkedIn" class="linkedin"  target="_blank"><span>&nbsp;</span></a><a href="/eclipse-jobs.ashx" title="Subscribe to the Yellow Cat jobs List" class="feed" target="_blank"><span>&nbsp;</span></a> </div>');
	$('body').append('<div class="js-bottom-wrapper"><div class="yc-site-map-container"><div class="yc-site-map"></div></div><div class="js-gutter"></div></div>');
	
		
		var wwwdom = document.domain;
		var smap = '';
		wwwdom.indexOf('.dev') !=-1?smap = '/template/sitemap.htm':smap = '/Sitemap.aspx';
		$('div.yc-site-map-container div.yc-site-map').load(smap+' div.sitemap-list-style', function(response, status, xhr) 
		{
		  if (status!= "error") 
		  {
			$('div.yc-site-map-container div.yc-site-map').prepend('<div class="rec-link"><a href="http://www.rec.uk.com/home" target="_blank"></a></div>');
			resortSitemap();
		  }
		  else if (status == "error") 
		  {
			var msg = "Sorry but there was an error: ";
			alert(msg + xhr.status + " " + xhr.statusText);
		  }
		});
		
		
		
		if($("[id$=pnlFooter]").size()==1 )
		{
			var footer = $('[id$=pnlFooter]');		//footer panels can have ids #ctl00_ctl00_pnlFooter or #ctl00_ctl00_ctl00_ctl00_pnlFooter
			$('div.js-bottom-wrapper div.js-gutter').append(footer );
			
			var el = $('div.js-bottom-wrapper div.js-gutter div#maincontent_copyright p');
			$('div.js-bottom-wrapper div.js-gutter div#maincontent_copyright a[href="/eclipse-jobs.ashx"]').replaceWith('');
			el.html(el.html().replace(/\|/ig, ""));
			el.html(el.html().replace(/ All rights reserved/ig, '<span class="pad-footer-els">|</span><span>Unit 12.3.2 The Leathermarket 11-13 Weston Street London SE1 3ER </span><span class="pad-footer-els">|</span><span>Tel: 020 7580 7333</span><span class="pad-footer-els">|</span><span><a href="mailto:info@yellowcatrecruitment.co.uk" class="email-us" title="Email us">info@yellowcatrecruitment.co.uk</a></span>'));
			$('div.js-bottom-wrapper div.js-gutter div#maincontent_copyright p').wrapInner('<span class="left-block"></span>');
			$('div.js-bottom-wrapper div.js-gutter div#maincontent_copyright p a:not(a.email-us)').wrapAll('<span class="right-block"></span>');
			$('div.js-bottom-wrapper div.js-gutter div#maincontent_copyright p').append($('span.right-block') );
			
			$('div.js-bottom-wrapper div.js-gutter div#maincontent_copyright p span.right-block a:not(:last)').after('<span class="pad-footer-els">|</span>');
			$('div.js-bottom-wrapper div.js-gutter div#maincontent_copyright').prepend($('a.footer-power-by') );/* prepend abs positioned block - ie6*/
		}
		$('body').prepend($('div#ewi2_styles_loggedout_window'));
		$('body').prepend($('div#ewi2_styles_loggedout_window_inner'));
		
	
}//function

function resortSitemap()
{
	var home_list = null;
	var about_list = null;
	var contact_list = null;
	var test=true;
	//alert($('div.yc-site-map-container div.yc-site-map ul:first > li').length)
	
	
	$('div.yc-site-map-container div.yc-site-map ul:first > li').each(
		function(i)
		{
			if($(this).find('a:first').text()=='Home')
			{
				home_list = $(this);
				//alert('woot home');
			}
			else if($(this).find('a:first').text()=='About Us')
			{
				about_list = $(this);
				//alert('woot home');
			}
			else if($(this).find('a:first').text()=='Contact Us')
			{
				contact_list = $(this);
			}
		}
		);
	
	if(home_list != null)
	{
		$(home_list).replaceWith('');
		//$(about_list).replaceWith('');
	}
	$('div.yc-site-map-container div.yc-site-map ul#utilityNav li').each(
		function(i)
		{
			var li_text = $(this).find('a:first').text();
			if(li_text!='Sitemap' && li_text!='Contact Us')
			{
				//$(about_list).find('ul').append($(this));
				$(contact_list).find('ul').append($(this));
				
			}
			
		}
		);
	$('div.yc-site-map-container div.yc-site-map ul#utilityNav').replaceWith('');
}//function


(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);


if(!Array.indexOf){//index of breaks ie
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

try {//ie6 image swap cache problem
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


function isArray(testObject) 
{   
    return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}

function isset( variable )
{

	return( typeof( variable ) != 'undefined' );

}


(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


jQuery.fn.onImagesLoaded = function(_cb) { 
  return this.each(function() {
 
    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
        _cont = this,
            i = 0,
    _done=function() {
      if( typeof _cb === 'function' ) _cb(_cont);
    };
 
    if( $imgs.length ) {
      $imgs.each(function() {
        var _img = this,
        _checki=function(e) {
          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
          {
            if( ++i===$imgs.length ) _done();
          }
          else if( _img.readyState === undefined ) // dont for IE
          {
            $(_img).attr('src',$(_img).attr('src')); // re-fire load event
          }
        }; // _checki \\
 
        $(_img).bind('load readystatechange', function(e){_checki(e);});
        _checki({type:'readystatechange'}); // bind to 'load' event...
      });
    } else _done();
  });
};


(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);
