var objGeoNumbers=null;
var objInteractions=null;

var GeoNumbers = Class.create({

	initialize: function(){
	
		this.strDefaultCountry='uk';

		//Define and fill the array of contact numbers for the system
		this.arrNumbers = new Array();
		this.arrNumbers['uk'] = "+44 (0)203 283 8828";
		this.arrNumbers['ie'] = "+353 (1)800 812 799";
		this.arrNumbers['us'] = "+1 866 736 0022";
		this.arrNumbers['fr'] = "+33 (0)1 7070 0499";
		this.arrNumbers['de'] = "+49 (0)69 2222 7307";
		this.arrNumbers['dk'] = "+45 (0)808 895 26";
		this.arrNumbers['ca'] = "+1 866 543 0618";
		this.arrNumbers['be'] = "+32 (0)800 754 69";
		this.arrNumbers['at'] = "+43 (0)800 005 891";
		this.arrNumbers['se'] = "+46 (0)200 895 462";
		this.arrNumbers['dk'] = "+45 (0)808 895 26";
		this.arrNumbers['no'] = "+47 (0)800 199 93";
		this.arrNumbers['fi'] = "+358 (0)800 914 891";
		this.arrNumbers['za'] = "+27 (0)800 981 275";
		this.arrNumbers['nl'] = "+31 (0)800 020 4662";
		
		var intCount=0;
		
		this.arrImages=new Array();
		
	},
	build: function(){

		//Locate the user
		this.locateUser();
	},
	
	locateUser: function(){

		var strUrl = window.location.protocol+'//'+window.location.host+'/bar/geoNumbers.php';

		//Run the ajax function to get the users country from IP address
		new Ajax.Request(strUrl,{
		
			method: 'post',
			
			onSuccess: this.updateNumberDisplay.bind(this),
			
			onFailure: this.updateNumberDisplay.bind(this)
		});
	},
	
	updateNumberDisplay: function(transport){

		//Check the result of the AJAX query if true use the result otherwise default to UK (lowercase the country code result)
		strCountryCode = (typeof transport != 'undefined' && transport.responseJSON.status == true) ? transport.responseJSON.country_code.toLowerCase() : this.strDefaultCountry;
		
		//Check the country code is valid
		strCountryCode = (typeof this.arrNumbers[strCountryCode] == 'undefined') ? this.strDefaultCountry : strCountryCode;
		
		//Get the number from the array
		Element.update('bar-geoContainer-number',this.arrNumbers[strCountryCode]);
		
		var strFlagImage=new Image();

		strFlagImage.src='/bar/images/flags/'+strCountryCode+'.gif';

		//Update the relevant boxes on the number viewer
		
		Element.update('bar-geoContainer-image','<img src="/bar/images/flags/'+strCountryCode+'.gif" alt="'+strCountryCode+'" />');
		
		if(typeof $('geo-number-callback-update') != 'undefined'){
		
			Element.update('geo-number-callback-update',this.arrNumbers[strCountryCode]);
		}
	}
});

var txtNationBar = Class.create({
	
	initialize:function(){

		new Ajax.Request(window.location.protocol+'//'+window.location.host+'/bar/template.html', {
		
			evalScripts:false,
			
			onSuccess: function(objTransport){

				Element.insert(document.body, {'top':objTransport.responseText});
				
				/* Add the linkedin button */
				
				$('bar-linkedin-button').innerHTML='<script type="in/share"></script>';
				
				var objScript=document.createElement('script');
				
				objScript.setAttribute('type','text/javascript');
				
				objScript.setAttribute('src','https://platform.linkedin.com/in.js');
				
				document.getElementsByTagName('head')[0].appendChild(objScript);

				objGeoNumbers=new GeoNumbers();

				objGeoNumbers.build();
				
				objInteractions=new BarInteractions();
				
				objInteractions.setListeners();

			}.bind(this)
		});
	}
});

var BarInteractions=Class.create({

	initialize:function(){
	},
	
	setListeners:function(){
	
		Element.observe('bar-liveChatButton','click',function(){this.loadLiveChat();}.bind(this));
		
		var agent=navigator.userAgent.toLowerCase();
		
		var is_iphone = (((agent.indexOf('iphone')!=-1))||((agent.indexOf('ipod')!=-1))||((agent.indexOf('ipad')!=-1)));
		
		if(is_iphone){
		
			// if this is an iPod/iPad/iPhone browser, we need to setup listeners for the rollovers
			Element.observe('bar-billing','click',function(){$('bar-billing-menu').toggle();}.bind(this));
			Element.observe('bar-messaging','click',function(){$('bar-messaging-menu').toggle();}.bind(this));
			Element.observe('bar-campaigns','click',function(){$('bar-campaigns-menu').toggle();}.bind(this));
			Element.observe('bar-live-chat','click',function(){$('bar-live-chat-menu').toggle();}.bind(this));
		}
	},
	
	loadLiveChat:function(){

		window.open('http://zebra.livechatnow.com/js/enter.php?cid=8908&skin=&group=General','LiveChat','width=500,height=350,titlebar=1,resizable=1,scrollbars=0'); 
	},
	
	showMenu:function(strMenuId){
	
		$(strMenuId).style.display='block';
	}
});

//Start the listener to deploy the nubers box
document.observe("dom:loaded",function(){

	// Add the stylesheet
	var objHead = document.getElementsByTagName('head')[0];
	var objStyle = document.createElement('link');
	objStyle.type = 'text/css';
	objStyle.rel = 'stylesheet';
	
	// cache the stylesheet for up to 1 hour by appending a param on the end
	objStyle.href = '/bar/styles.css?d='+new Date().getHours();
	
	objStyle.media = 'screen';
	objHead.appendChild(objStyle);
	
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
	
		var intIeVersion=new Number(RegExp.$1);
		
		if(intIeVersion==6||intIeVersion==7){
		
			var objIeStyle=document.createElement('link');
			objStyle.type = 'text/css';
			objStyle.rel = 'stylesheet';
			
			// cache the stylesheet for up to 1 hour by appending a param on the end
			objStyle.href = '/bar/ie7_styles.css?d='+new Date().getHours();
			
			objStyle.media = 'screen';
			objHead.appendChild(objStyle);
		}
	}

	// Make the bar
	var objBar=new txtNationBar();
	
	if(typeof blP !== 'undefined'){
	
		document.body.addClassName('bgOffset');
	}
	
	window.___gcfg = {lang: 'en-GB'};
	
	(function() {
		var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
		po.src = 'https://apis.google.com/js/plusone.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
	})();
});

