/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

window.addEvent('domready',function() {
									
	$$('.int-countries ul').each(function(section) {
		if ( !Modernizr.csscolumns )
		{

			var objSize = section.getSize(), items = section.getElements('li'), length = section.getElements('li').length,
			colCount = section.get('data-col-count'), colGap = section.get('data-col-gap'), counter = 0, column = Array();
			
			var adjTop = items[Math.ceil(length / colCount)].getPosition(section);
			var colWidth = Math.floor((objSize.x - (colGap * (colCount-1))) / colCount);
			var temp_container = new Element('div', { 'class' : 'temp_container', 'styles' : { 'height': adjTop.y, 'overflow' : 'hidden', 'position': 'relative' } }).inject(section, 'before');
	
			for (var i=0; i < colCount; i++) {
				column[i] = section.clone().inject(temp_container);
				
				column[i].setStyles({
					'float': 'left',
					'margin-left': -colGap * -1,
					'position': 'relative', 
					'top': -(adjTop.y * i),
					'width': colWidth
				});
							
				if ( !i )
				{
					column[i].setStyles({
						'left': '0',
						'margin-left': '0',
						'top': '0' 	
					});
				}
			} 
			
			section.setStyle('display', 'none');
		}
	});
									
	$$('.expandable').each(function(info) {
		
		var hidden = info.getElement('.more-details');	
		
		var expand_link = new Element('a', {
			'class': 'expand-link',
			'href': 'javascript:void(0);',
			'html': '[...]',
			'events': {
				'click': function() {
					expand_link.setStyle('display', 'none');
					collapse_link.setStyle('display', 'inline');
					hidden.setStyle('display', 'inline');
				},
			}
		}).inject(hidden, 'before');
		
		var collapse_link = new Element('a', {
			'class': 'collapse-link',
			'href': 'javascript:void(0);',
			'html': '[^]',
			'events': {
				'click': function() {
					expand_link.setStyle('display', 'inline');
					collapse_link.setStyle('display', 'none');
					hidden.setStyle('display', 'none');
				},
			}
		}).inject(hidden, 'after');
		
		
	});
	
	new Fx.SmoothScroll({
				duration: 200
			},window);

//	var contactList = $('contactList').getElements('li'), showingParent;

/*	var contactList = document.id('contactList').getElements('li'), showingParent;
	
	contactList.setStyle('opacity', 0);

	var showInterval = 5000;
	var index = 0;
	(function() {contactList[index].tween('opacity',1); }).delay(1000);
	var start = function() {
		(function() {
			contactList[index].fade(0);
			++index;
			index = (contactList[index] ? index : 0);
			contactList[index].fade(1);
		}).periodical(showInterval);
	};

	/* start the show 
	start();*/
/*	
	$$('.dropdowns').each(function(e){
		e.set('opacity','0');
	})
	
	$$('.igniter').addEvent('mouseenter',function(e){
		$$('.dropdowns').setStyle('display','none');
		var r = this.get('rel');
		var pos = this.getPosition();
//		alert(pos.x);

//		parent = e.getParent('div');
		
		if(document.id(r).getStyle('display') == 'none'){
			showingParent = r;
			if ( document.id(r).hasClass('menuList') ) {
				document.id(r).setStyles({
					'opacity':'0',
					'position':'absolute',
					'top':'144px',
					'left':pos.x,
					'display':'block',
					'z-index': '999'
				});
			} else {
				document.id(r).setStyles({
					'opacity':'0',
					'display':'block'
				});
			}
		
			var myFx = new Fx.Tween(document.id(r));
			document.id(r).fade(1);
		}
	})
/*
	$$('.dropdowns').addEvent('mouseleave',function(e){
		var myEffects = new Fx.Morph(this, {duration: 300, transition: Fx.Transitions.Sine.easeOut, 'link':'cancel'});
		 
		myEffects.start({
			'opacity': [1,0]
		}).chain(function(e){
			$$('.dropdowns').setStyle('display','none');
		})
	})
	
	document.id(document.body).addEvent('mouseenter',function(e) {
		if ( document.id(showingParent) != null && document.id(showingParent).getStyle('display') == 'block' ) {
			if(showingParent && !e.target || !document.id(e.target).getParents().contains(showingParent)) { 
				var myEffects = new Fx.Morph(document.id(showingParent), {duration: 300, transition: Fx.Transitions.Sine.easeOut, 'link':'cancel'});
			 
				myEffects.start({
					'opacity': [1,0]
				}).chain(function(e){
					$$('.dropdowns').setStyle('display','none');
				})
			}
		}
	}); */
	
	if ( document.id('smallPrint') == null ) {
	$$('.bodyContent ol').each(function(z) {
		i = 0;
		$$('ol > li').each(function(a) {
			i++;
			var changeImage = new Element('div', {
				'text': i,										  
				'styles': {
					'display': 'inline-block',
					'width': 17,
					'height': 17,
					'line-height':17,
					'text-align':'center',
					'font-size':10,
					'margin-bottom': 5,
					'color':'#FFFFFF',
					'opacity':1,
					'background': 'url(images/background/list-background.jpg) no-repeat left center',
					'padding': '0 5px 0 0'
				}
			}).inject(a, 'top');
										  
		});
	});
	}
	
	$$('.email').each(function(el) {
		if ( el.get('html').length == 0 ) {
			var anchor = new Element('a', {
				href: 'mailto:' + el.get('rel').replace('|','@'),
				'class': el.get('class'),
				'text': el.get('rel').replace('|','@')
			}).replaces(el);
		} else {
			var anchor = new Element('a', {
				href: 'mailto:' + el.get('rel').replace('|','@'),
				'class': el.get('class'),
				'text': el.get('text')
			}).replaces(el);
		}
	});
	
	$$('.contact_email').each(function(ewl) {
		var anchor2 = new Element('a', {
			href: 'mailto:' + ewl.get('rel').replace('|','@'),
			'class': ewl.get('class'),
			'text': ewl.get('rel').replace('|','@')
		}).replaces(ewl);
	});
	
	if ( document.id("aJoinNow") != null ) {
		document.id("aJoinNow").setStyles({"color": "red", "font-weight": "bold"});
	}
	
/*	if ( document.id("tblJobItems") != null ) {
		
		var dim = document.id("tblJobItems").getElements(".dimensions");
		
		dim.each(function(d,i) {
						  			
///			d.set("html", d.get('html').replace('x','cm x'));
//			d.set("html", "asd");
//			alert(d.get("text"));
						  
		});

	}*/
	
//	window.onload = initialize; 
//	window.onunload = google.maps.Unload();

});

if ( window.location.pathname.toLowerCase().contains('int') == true && ( window.location.pathname.length == 10 || window.location.pathname.length == 11 ) ) {
	if ( document.id("map") != null ) {
	function initialize() {
		if (google.maps.BrowserIsCompatible()) {
			var map = new google.maps.Map2(document.getElementById("map"));
			var latitude = document.id('latitude').get('html');
			var longitude = document.id('longitude').get('html');

			var center = new google.maps.LatLng(latitude,longitude);
			map.setMapType(G_NORMAL_MAP);
			map.setCenter(new google.maps.LatLng(latitude,longitude), 15);
			
			var mapControl = new google.maps.MapTypeControl();
			map.addControl(mapControl);
			map.addControl(new google.maps.LargeMapControl());
			
			var tinyIcon = new google.maps.Icon();
			tinyIcon.image = "images/background/pointer2.png";
			tinyIcon.iconSize = new google.maps.Size(50, 60);
			tinyIcon.iconAnchor = new google.maps.Point(25, 60);
			
			markerOptions = { icon:tinyIcon };
			map.addOverlay(new google.maps.Marker(center, markerOptions));
		}
	}

	}
}

window.addEvent('load',function() {
								
/*	var REQUEST_URI = rtrim(window.location.pathname.toLowerCase(), "/");
	
	loadLogo = function(url) {
		
		var jpgLogo = new Element('img', {
			//here you set all the element parameters
			'src': url,
			'styles': {
				//here you set all the style parameters
				'opacity': 0
			}
		});

		var bodyWrapVar = document.id('logo');
		jpgLogo.inject(bodyWrapVar , 'top');
		
		jpgLogo.set('tween', {duration: 1});
		jpgLogo.tween('opacity',1);
		
	};

								
	if ( REQUEST_URI == "/" || REQUEST_URI == "/index.php" ) {
		
		var crash = function() {
			var obj = new Swiff('../images/flash/header-home.swf', {
				id: 'flash_logo',
				width:  1070,
				height: 114,
				container: document.id('logo')
			});
		};

		setTimeout(crash,0);
	
	} else if ( REQUEST_URI == "/courier/obtain-quote" || REQUEST_URI == "/courier/gateway" || REQUEST_URI == "/courier/courier/sign-up" ) {
		loadLogo('images/transways-courier-service-logo.jpg');
	} else {
		loadLogo('images/transways-logo.jpg');
	}	
	*/
	
	if ( document.id("map") != null ) initialize();

});

if ( document.getElementById("map") != null ) {
	window.addEvent('onunload', google.maps.Unload);
}
window.addEvent('domready',function() {
	if ( document.id("marqueeTest") != null ) {
		
		fxmq = new Fx.Marquee(document.id("marqueeTest"), {
			duration: 500,
			showEffect: {
				top: [0,0], //reset the top every time to zero
				left: [-100, 0],
				opacity: [0,1]
			},
			hideEffect: {
				top: 20
			},
			revertEffect: {
				top: [-30, 0],
				left: [0,0] //reset the left back to zero on revert
			}
		});
		
		var k = function() {
			fxmq.announce({
				message: document.id("marqueeTest").getParent().getElements("strong")[1].get("html"),
				delay: 1200, revert: true
			}).chain(function(){
				fxmq.announce({
					message: document.id("marqueeTest").getParent().getElements("strong")[2].get("html"),
					delay: 1500, revert: false,
					showEffect: {
						top: [0,0],
						left: [500, 0],
						opacity: [0,1]
					},
					hideEffect: {
						top: [0,0],
						left: [0, 500]
					}
				});
			});
		};
		k();
		k.periodical(10000);
	}
	
});
