//Nomensa jquery plugins
//author: nomensa
//website: http://www.nomensa.com

//SITEMAP
(function($){jQuery.fn.nomSitemap=function(options){var opts=$.extend({},$.fn.nomSitemap.defaults,options);return this.each(function(){var o=options;var $sitetree=$(this);var $liElements=$sitetree.find("li");var $collapseText="Collapse children";var $expandText="Expand children";$liElements.each(function(){if($(this).children('ul').length>0){$(this).prepend('<a href="#" class="toggle-li-element collapse">Collapse children</a>')}});$sitetree.find('ul ul').hide();$sitetree.find('ul .toggle-li-element').removeClass('collapse').text($expandText);$('a.toggle-li-element').click(function(){if($(this).hasClass("collapse")){$(this).text($expandText).removeClass('collapse').nextAll('ul')[o.animate?'slideToggle':'toggle'](o.speed)}else{$(this).text($collapseText).addClass('collapse').nextAll('ul')[o.animate?'slideToggle':'toggle'](o.speed)}})})};var defaults={animate:false}})(jQuery);

//INPUT TEXT REMOVAL
(function(a){jQuery.fn.nomFormFocus=function(){return this.each(function(){var c="Search BDO...";a(this).val(c);var b=a(this).attr("value");a(this).focus(function(){var d=a(this).val();if(b==d){a(this).removeClass("default-value");a(this).attr("value","")}});a(this).blur(function(){if(a(this).val()==""){a(this).addClass("default-value");a(this).val(b)}})})}})(jQuery);

// LAST CLASS ADDITION - adds a class of last to the last li in a list
(function(b){jQuery.fn.nomLast=function(){return this.each(function(){b("li:last-child").addClass("last")})}})(jQuery);

// STAR RATINGS 
(function(a){a.fn.nomRating=function(b){var d={type:"stars"};var b=a.extend(d,b);var c=a("<div />");function e(){var f=a(this).val();a(this).parents("div:eq(0)").attr({className:"star-"+f})}return this.each(function(){var f=a(this);a(f).wrapInner(c);a("input[type=radio]").click(e)})}})(jQuery);

// Force Submit
(function($){$.fn.force_submit=function(options){var defaults={field:'#example'};var config=$.extend(defaults,options);return this.each(function(){$form=$(this);$action=$(this).attr('action');$(config.field,$(this)).change(function(){if($(this).val()!=''){window.location=$action+'?'+$(config.field).attr('name')+'='+$(config.field).val();}});});};})(jQuery);

//STYLISH SELECT
/*
Stylish Select 0.3 - $ plugin to replace a select drop down box with a stylable unordered list
http://scottdarby.com/
Copyright (c) 2009 Scott Darby
Requires: $ 1.3

Licensed under the GPL license:
http://www.gnu.org/licenses/gpl.html
*/
(function(a){Array.prototype.indexOf=function(c,d){for(var b=(d||0);b<this.length;b++){if(this[b]==c){return b}}};a.fn.extend({getSetSSValue:function(b){if(b){a(this).val(b).change();return this}else{return selText=a(this).find(":selected").text()}},resetSS:function(){$this=a(this);$this.next().remove();$this.unbind().sSelect()}});a.fn.sSelect=function(b){return this.each(function(){var j={defaultText:"Please select",animationSpeed:0,ddMaxHeight:""};var m=a.extend(j,b),e=a(this),k=a('<div class="selectedTxt"></div>'),s=a('<div class="newListSelected" tabindex="0"></div>'),A=a('<ul class="newList"></ul>'),u=-1,d=-1,n=[],x=false,h="",w=false;s.insertAfter(e);k.prependTo(s);A.appendTo(s);e.hide();if(e.children("optgroup").length==0){e.children().each(function(B){var C=a(this).text();n.push(C.charAt(0).toLowerCase());if(a(this).attr("selected")==true){m.defaultText=C;d=B}h+="<li>"+C+"</li>"});A.html(h);h="";var y=A.children()}else{e.children("optgroup").each(function(D){var B=a(this).attr("label"),E=a('<li class="newListOptionTitle">'+B+"</li>");E.appendTo(A);var C=a("<ul></ul>");C.appendTo(E);a(this).children().each(function(){++u;var F=a(this).text();n.push(F.charAt(0).toLowerCase());if(a(this).attr("selected")==true){m.defaultText=F;d=u}h+="<li>"+F+"</li>"});C.html(h);h=""});var y=A.find("ul li")}var p=A.height()+3,o=s.height()+3,z=y.length;if(d!=-1){i(d,true)}else{k.text(m.defaultText)}function q(){var C=s.offset().top,B=jQuery(window).height(),D=jQuery(window).scrollTop();if(p>parseInt(m.ddMaxHeight)){p=parseInt(m.ddMaxHeight)}C=C-D;if(C+p>=B){A.css({top:"-"+p+"px",height:p});e.onTop=true}else{A.css({top:o+"px",height:p});e.onTop=false}}q();a(window).resize(function(){q()});a(window).scroll(function(){q()});function t(){s.css("position","relative")}function c(){s.css("position","static")}k.click(function(){if(A.is(":visible")){A.hide();c();return false}s.focus();A.slideDown(m.animationSpeed);t();A.scrollTop(e.liOffsetTop)});y.hover(function(C){var B=a(C.target);B.addClass("newListHover")},function(C){var B=a(C.target);B.removeClass("newListHover")});y.click(function(C){var B=a(C.target);d=y.index(B);w=true;i(d);A.hide();s.css("position","static")});function i(D,F){var B=s.offset().top,G=y.eq(D).offset().top,C=A.scrollTop();if(e.onTop==true){e.liOffsetTop=(((G-B)-o)+C)+parseInt(m.ddMaxHeight)}else{e.liOffsetTop=((G-B)-o)+C}A.scrollTop(e.liOffsetTop);y.removeClass("hiLite").eq(D).addClass("hiLite");var E=y.eq(D).text();if(F==true){e.val(E);k.text(E);return false}e.val(E).change();k.text(E)}e.change(function(B){$targetInput=a(B.target);if(w==true){w=false;return false}$currentOpt=$targetInput.find(":selected");d=$targetInput.find("option").index($currentOpt);i(d,true)});function r(B){B.onkeydown=function(E){if(E==null){var D=event.keyCode}else{var D=E.which}w=true;switch(D){case 40:case 39:v();return false;break;case 38:case 37:l();return false;break;case 33:case 36:g();return false;break;case 34:case 35:f();return false;break;case 13:case 27:A.hide();c();return false;break}keyPressed=String.fromCharCode(D).toLowerCase();var C=n.indexOf(keyPressed);if(typeof C!="undefined"){++d;d=n.indexOf(keyPressed,d);if(d==-1||d==null||x!=keyPressed){d=n.indexOf(keyPressed)}i(d);x=keyPressed;return false}}}function v(){if(d<(z-1)){++d;i(d)}}function l(){if(d>0){--d;i(d)}}function g(){d=0;i(d)}function f(){d=z-1;i(d)}s.click(function(){r(this)});s.focus(function(){a(this).addClass("newListSelFocus");r(this)});s.blur(function(){a(this).removeClass("newListSelFocus");A.hide();c()});k.hover(function(C){var B=a(C.target);B.parent().addClass("newListSelHover")},function(C){var B=a(C.target);B.parent().removeClass("newListSelHover")});A.css("left","0").hide()})}})(jQuery);

// Detect if the devices supports touch events.
function touchDeviceTest() {
	var el = document.createElement('div');
	el.setAttribute('ontouchmove', 'return;');
	if(typeof el.ontouchmove == "function"){
		return true;
	}else {
		return false
	}
}

//Nomensa jquery init functions
//author: nomensa
//website: http://www.nomensa.com


$(function(){
    $('body').addClass('js');
        
    //if you want it to animate (slide) enter {animate: true} speed can be "slow" "normal" or "fast"
    $('#searchBlock .formText input').nomFormFocus(); 
    
    $('li').nomLast();

	if (!touchDeviceTest()) {
		$('.selectbox').sSelect();
	} 
	
	
	$('.formRating').nomRating(); 
	
	$('#sector_form').force_submit({field : '#sector'});
	$('#service_form').force_submit({field : '#service'});
	$('#office_form').force_submit({field : '#office-location'});
	
	$("a.external").click(function() { window.open(this.href); return false; });

});

// Add the international drop-down.
$(function (){
	var allWebsites=new Array();
	
	try{
	  	allWebsites=bdoWebsites; //uses remote DNSs on BDOInternational.com
	}
	catch(err){
		allWebsites=bdoWebsitesLocal; //uses Local DNSs found in the BDOInternationalWebsiteAddressesLocal.js
	}
	
	if (allWebsites) {
		
		$('#international-sites').append('<label class="rm" for="country">Select a country</label>');
		$('#international-sites').append('<select id="country"><option value="">BDO locations</option></select>');
		
		var theDNS=location.host;
	
		$.each(allWebsites, function(key, data) {
			var thisDataDNS=data.dns;
			var thisDataDNStrimmed=thisDataDNS.slice((thisDataDNS.indexOf('//')+2),thisDataDNS.length);
			if(thisDataDNStrimmed!=theDNS){
				$('<option value="'+data.dns+'">'+data.country+'</option>').appendTo('#country');
			};
		});
		
		if (!touchDeviceTest()) {
			$('#country').sSelect();
		} 
		
		$('#country').change(function() { //onChange then open a new window
			if($(this).val()!=""){
				window.open($(this).val());
			}
		});
	}
})

// small tabs on the homepage
$(function(){
	$(".tabsSmall li img:gt(0)").hide();
	$(".tabsSmall li:eq(0) div").addClass('highlight');
	
	$('.tabsSmall li a').bind('mouseover focus', function(){
		$('.tabsSmall img').hide();	
		$('.tabsSmall div').removeClass('highlight');		
		$(this).parent().addClass('highlight');
		$(this).children('img').show();
	});	
});

//  for ie browsers - hover on form labels
$(function(){
  $('.formRating label').hover(function() {
    $(this).addClass('ratingHover');
  }, function() {
    $(this).removeClass('ratingHover');
  });
});


// Adds additional classes to the homepage tabs to fake equal height tabs
$(function(){
	$('#tabs .tabNav').addClass('tabs-1');
	$("#tabs .tabNav li a").each(function(index) {
		$(this).click(function() {
			var tab = $(this).attr('href').slice(1);
			$('#tabs .tabNav').removeClass('tabs-1 tabs-2 tabs-3 tabs-4');	
			$('#tabs .tabNav').addClass(tab);
			// Stop the player in IE
			if (PlayerList ) {
				for (i = 0; i < PlayerList.length; i++) {
					PlayerDaemon().stop(PlayerList[i]);
				}	
			}

		//	if (PlayerList[0] && PlayerDaemon().getCurrentTime(PlayerList[0]) > 0) {
		//		PlayerDaemon().stop(PlayerList[0])
		//	}
		});
	});
});
function replaceMainContent(url){
	window.location.href=url;
}
