(function($) {

	$(function() {

		var IE6 = $('html').hasClass('ie6');

		//homepage blocks fix
        //$('ul#adverts li:last').addClass('last-in-row');
		
		if ($(window).width() > 960){
			$('img#fifty').show();
		}

		$(window).resize(function(){
			if ($(window).width() < 960){
				$('a#fifty').fadeOut(1000);
			} else {
				$('a#fifty').fadeIn(1000);
			}
		});

		//Menu animation
		var menuHoverDiff = 5;
		$('#main-menu li a').not('#main-menu li.search a').each(function() {
			$this = $(this);
			$this.data('padding-top', parseInt($this.css('padding-top')));
			$this.data('padding-bottom', parseInt($this.css('padding-bottom')));
		}).hover(function() {
			$this = $(this);
			$this.stop().animate({'padding-top': $this.data('padding-top') - menuHoverDiff, 'padding-bottom': $this.data('padding-bottom') + menuHoverDiff}, 'fast');
		}, function() {
			$this = $(this);
			$this.stop().animate({'padding-top': $this.data('padding-top'), 'padding-bottom': $this.data('padding-bottom')}, 'fast');
		});

		$('#main-menu li.search a').click(function() {
			$('#search').fadeIn('fast', function(){
				$(this).find('input').focus();
			});
			return false;
		});

		//Banner slides
		(function() {

			var wrapper = $('#top-slides');
			var slidesWrapper = wrapper.find('.slides');
			var slides = slidesWrapper.find('li');
			var current = 0;
			var animating = false;
			var changeInterval = false;
			var shortcuts = $('<ul class="shortcuts">').appendTo(wrapper);
			var shortcutsLis = false;

			var changeSlide = function(id) {

				if( current != id ) {
					shortcutsLis.removeClass('current');
					$(shortcutsLis.get(id)).addClass('current');

					$(slides.get(id)).css({'z-index': 2}).fadeIn('slow', function(){
						animating = false;
						$(slides.get(current)).hide();
						current = id;
						$(this).css({'z-index': 1});
					});
				} else {
					animating = false;
				}

			}

			var nextSlide = function() {
				changeSlide( current + 1 >= slides.length ? 0 : current + 1 );
			}

			changeInterval = setInterval(function() {
				var nextID = current + 1;
				if( nextID >= slides.length ) {
					nextID = 0;
				}
				if( !animating ) {
					animating = true;
					changeSlide(nextID);
				}
			}, 4000);

			if( slides.length > 1 ) {
				slides.each(function(id) {
					var li = $(this);
					if( id != current ) {
						li.hide();
					}

					li.click(function(){
						if( !animating ) {
							clearInterval(changeInterval);
							animating = true;
							nextSlide();
						}
					})

					shortcuts.append(
						$('<li>').data('id', id).click(function() {
							if( !animating ) {
								clearInterval(changeInterval);
								animating = true;
								changeSlide(id);
							}
							return false;
						})
					);
				});

				shortcutsLis = shortcuts.find('li');
				$(shortcutsLis.get(current)).addClass('current');
			}

		})();


		//Adverts
		$('#adverts li, #meet-people.widget, #nlp-offices.widget').each(function() {
			var li = $(this);
			li.data('a', li.find('a')).css('cursor', 'pointer');
		}).click(function() {
			window.location = $(this).data('a').attr('href');
			return false;
		}).hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});


		//News
		(function(){
			var wrapper = $('#news ul');

			if( !wrapper.length ) {
				return false;
			}

			var lis = wrapper.find('li');
			var current = 0;
			var animating = false;
			var changeInterval = false;
			var speed = 2000;
			var scrollSpeed = 0;
			var wrapperScrollHeight = lis.filter(':last').position().top + lis.filter(':last').height();
			wrapper.get(0).scrollTop = 0;

			/*
			if( lis.length % 3 != 0 ) {
				for(var i = 0; i <= lis.length % 3; ++i) {
					wrapper.append( $(lis.get(0)).clone(true).addClass('place-holder').css('visibility', 'hidden') );
				}
			}
			*/

			var change = function(newID) {

				if( newID != current ) {
					if( newID > current ) {
						wrapper.animate({'scrollTop': lis.get(newID).offsetTop}, speed, function() {
							current = newID;
							animating = false;
						});
					} else {
						wrapper.animate({'opacity': 0}, 'slow', function() {
							wrapper.get(0).scrollTop = 0;
							wrapper.animate({'opacity': 1}, 'slow', function() {
								current = newID;
								animating = false;
							});
						});
					}
				} else {
					animating = false;
				}
			}

			changeInterval = setInterval(function() {
				if( !animating ) {
					animating = true;
					var newID = current + 3;
					if( newID > lis.length ) {
						newID = 0;
					}
					change(newID);
				}
			}, 4000 + speed);


			wrapper.hover(function(){
				clearInterval(changeInterval);
				wrapper.stop();
				animating = false;
 				scrollWorkerInterval = setInterval(scrollWorker, 20);

			}, function(){

				clearInterval(scrollWorkerInterval);

			});

			var ul = wrapper.get(0);
			var scrollWorkerInterval = false;
			var maxSpeed = 5;
			var scrollWorker = function() {

				if( scrollSpeed > 0 ) {

					if( ul.scrollTop < ul.scrollHeight ) {
						ul.scrollTop += parseInt( scrollSpeed / 100 * maxSpeed);
					} else {
						ul.scrollTop = ul.scrollHeight;
					}
				} else {
					if( ul.scrollTop > 0 ) {
						ul.scrollTop -= parseInt( -scrollSpeed / 100 * maxSpeed);
					} else {
						ul.scrollTop = 0;
					}
				}

			}

			var wrapperHeight = wrapper.height();
			wrapper.mousemove(function(ev){

				var p = (ev.pageY - wrapper.offset().top) * 100 / wrapperHeight;

				if( p > 50 ) {
					scrollSpeed = (p - 50) * 100 / 50;
				} else {
					scrollSpeed = - (50 - p) * 100 / 50;
				}

			})

		});

		$('#news ul').each(function(){

			var ul = this;
			ul.scrollTop = 0;

			var wrapper = $(this).addClass('active-scroll');
			var up = $('<a class="up" href=""></a>').insertAfter(ul).css('top', wrapper.position().top);
			var down = $('<a class="down" href=""></a>').insertAfter(ul);
			down.css('top', wrapper.position().top + wrapper.height() - down.height());


			var scrollInterval = false;
			var scrolling = false;
			var scrollAmount = 2;
			var startScroll = function(dir) {
				scrollInterval = setInterval(function(){
					ul.scrollTop = ul.scrollTop + (dir == 'up' ? -scrollAmount : scrollAmount);
					if( ul.scrollTop == ul.scrollHeight ) {
						cancelScroll();
					}
				}, 10);
			}

			var cancelScroll = function() {

				clearInterval(scrollInterval);
				scrolling = false;

			}


			var increseSpeed = function() {
				scrollAmount = 4;
			}

			var decreaseSpeed = function() {
				scrollAmount = 2;
			}


			up.hover(function() {
				if( !scrolling ) {
					scrolling = true;
					startScroll('up');
				}
			}, function() {
				cancelScroll();
			}).click(function(){ return false; }).mousedown(increseSpeed).mouseup(decreaseSpeed);

			down.hover(function() {
				if( !scrolling ) {
					scrolling = true;
					startScroll('down');
				}
			}, function() {
				cancelScroll();
			}).click(function(){ return false; }).mousedown(increseSpeed).mouseup(decreaseSpeed);


		});
		
		
		$('#news.home-widget li').click(function() {
			window.location = $(this).find('a').attr('href');
			return false;
		});
		

		
		var newsLightBox = new boxMe({
			'url': '',
			'width': 660,
			'onload': function() {
				this.center();
			}
		});
		$('a.news-lightbox').click(function() {
			newsLightBox.options.url = this.href;
			newsLightBox.show().center();
			return false;
		});
		$('div#news li').css('cursor', 'pointer');
		$('ul#news li:nth-child(3n)').addClass('last-in-row');


		//Offices
		var lastOfficeLocation = "";
		var officesLightBox = new boxMe({
			'url': '',
			'width': 660,
			'onload': function() {
				this.center();

				
				this.wrappers.box.find('a.print').show().click(function(){
					var newWindow = window.open(lastOfficeLocation, 'printcontact', 'width='+ officesLightBox.wrappers.box.width() +',height='+ (officesLightBox.wrappers.box.height()+30) +'');
					newWindow.onload = function(){
						$('.print', newWindow.document).remove();
						$('.map-wrapper small a', newWindow.document).remove();
						if( !$(window).hasClass('id') ) {
							newWindow.print();
							setTimeout(function(){
								newWindow.close();
							}, 400);
						}
					}
					return false;
				});
			}
		});
		$('a.office-details-lightbox').click(function() {
			officesLightBox.options.url = this.href;
			lastOfficeLocation = this.href;
			officesLightBox.show().center();
			return false;
		});



		//projectDetailsLightBox
		var projectDetailsLightBox = new boxMe({
			'url': '',
			'width': 660,
			'onload': function() {
				this.center();

			}
		});
		$('a.project-lightbox').click(function() {
			projectDetailsLightBox.options.url = this.href;
			projectDetailsLightBox.show().center();
			return false;
		});


		//Main Banner hover
		(function(){

			var wrapper = $('#top-slides');
			var tabs = $('#top-slides .tab-wrappers >li');
			var tabsButtons = $('#top-slides .tab-buttons > li');
			var cover = $('<div>').css({
				'position': 'absolute',
				'top': 0,
				'left': 0,
				'width': '100%',
				'height': $(document).height(),
				'opacity': 0.5,
				'background': '#000',
				'z-index': 1000
			}).hide().appendTo('body');
			var coverWrapper = $('<div>').css({
				'position': 'absolute',
				'z-index': 10001
			}).hide().appendTo('body');
			var placeHolder = $('<div>');
			var hasCover = false;
			var current = false;
			var working = false;

			var hideCover = function() {
				
				if( hasCover ) {

					wrapper.insertAfter(placeHolder);
					placeHolder.remove();
					cover.fadeOut('fast', function() {
						working = false;
						hasCover = false;
					});


				} else {
					working = false;
				}
			}


			var showCover = function() {

				if( !hasCover ) {
					hasCover = true;
					
					
					cover.fadeIn('fast', function() {
						working = false;
					});

					placeHolder
						.css('height', wrapper.outerHeight() + parseInt(wrapper.css('margin-bottom')))
						.insertAfter(wrapper)
					;

					coverWrapper
						.css({
							'top': wrapper.offset().top,
							'left': wrapper.offset().left
						})
						.append(wrapper)
						.show()
					;
				} else {
					working = false;
				}

			}

			var reset = function() {

				if( !working ) {
					working = true;
					tabs.hide();
					tabsButtons.removeClass('selected');
					hideCover();
					current = false;
				}
			}


			cover.mousemove(reset);
			wrapper.mouseleave(reset);

			tabsButtons.mousemove(function(){
				var li = $(this);
				var className = li.find('a').attr('rel');
				if( !working && className != current ) {
					working = true;
					current = className;
					tabsButtons.removeClass('selected');
					tabs.hide();
					tabs.filter('.' + className).show();
					li.addClass('selected');
					showCover();
				}
			}).click(function() { return false });
			
			
		})();
		
		
		$('.tab-wrappers li.sectors, .tab-wrappers li.services').each(function(){
			
			
			var wrapper = $(this);
			var sectorImage = wrapper.find('.main-image');
			//var sectorText = $('.tab-buttons li.'+ wrapper.attr('class') +' span');
			
			var initImage = wrapper.find('li:first input.image').val();
			//var initText = wrapper.find('li:first input.text').val();
			
			var changeSector = function(image) {
				sectorImage.attr('src', image);
				
				/*
				sectorText.stop().animate({'opacity': 0}, 'fast', function(){
					sectorText.html(text).animate({'opacity': 1}, 'fast');
				});
				*/
			}
			
			changeSector(initImage);
			
			wrapper.find('li').each(function(){
				
				var li = $(this);
				var a = li.find('a');
				var image = li.find('input.image').val();
				//var text = li.find('input.text').val();
				
				document.createElement('img').src = image;
				
				li.hover(function(){
					changeSector(image);
				}, function(){
					changeSector(initImage);
				});
				
			});
			
		});
		
		

		//Quote
		$('#content p.quote').each(function(){
			$(this).append('<span class="close-quote sector-background-color"></span>');
			$(this).append('<span class="open-quote sector-background-color"></span>');
		});


		//Scroll to top
		$('a.back-to-top').click(function(){
			$('html,body').animate({'scrollTop': 0}, 'slow');
			return false;
		});


		//Timeline
		$('#timeline').each(function() {

			var wrapper = $(this);
			var ul = wrapper.find('ul.wrapper');
			var timelines = ul.children('li').hide();
			var current = 0;
			var buttonOpacity = 0.75;
			var next = $('<a class="next" href=""></a>').appendTo(wrapper).css('opacity', 0).hide();
			var prev = $('<a class="prev" href=""></a>').appendTo(wrapper).css('opacity', 0).hide();
			var working = false;


			var fadeInButton = function() {
				if( !working ) {
					$(this).stop().animate({'opacity': 1}, 'fast');
				}
			}

			var fadeOutButton = function() {
				if( !working ) {
					$(this).stop().animate({'opacity': buttonOpacity}, 'fast');
				}
			}

			var renderArrows = function() {

				if( current != 0 ) {
					prev.css({'opacity': 0, 'display': 'block'}).animate({'opacity': buttonOpacity}, 'fast');
				} else {
					prev.css({'opacity': buttonOpacity}).animate({'opacity': 0}, 'fast', function(){ $(this).css('display', 'none'); });
				}

				if( current < timelines.length - 1) {
					next.css({'opacity': 0, 'display': 'block'}).animate({'opacity': buttonOpacity}, 'fast');
				} else {
					next.css({'opacity': buttonOpacity}).animate({'opacity': 0}, 'fast', function(){ $(this).css('display', 'none'); });
				}

			}

			var change = function(id) {

				if( id != current ) {

					var oldLi = $(timelines.get(current));
					var newLi = $(timelines.get(id));

					/*
					oldLi.css('z-index', 1);
					newLi.css({'display': 'block', 'opacity': 0, 'z-index': 2}).animate({'opacity': 1}, 'slow', function() {
						oldLi.css('display', 'none');
						working = false;
					});
					*/

					ul.css('overflow', 'hidden');

					oldLi.css('z-index', 1);
					newLi.css({'display': 'block', 'z-index': 2, 'left': id < current ? '-100%' : '100%'}).animate({'left': '0%'}, 'slow', function() {
						oldLi.css('display', 'none');
						ul.css('overflow', 'visible');
						working = false;
					});


					oldLi.css({'display': 'block', 'z-index': 2, 'left': '0%'}).animate({'left': id > current ? '-100%' : '100%'}, 'slow');


					current = id;
					renderArrows();
				}

			}

			timelines.each(function() {
				var li = $(this);

				li.find('ul.timeline li').each(function() {
					var li = $(this);
					li.data('tooltip', li.find('span').css({'display': 'block', 'opacity': 0, 'bottom': '120%'}));
				}).hover(function() {
					$(this).data('tooltip').stop().animate({'opacity': 1, 'bottom': '100%'}, 'fast');
				}, function() {
					$(this).data('tooltip').stop().animate({'opacity': 0, 'bottom': '120%'}, 'fast');
				});

			});



			next.click(function() {
				if( !working ) {
					working = true;
					change( current < timelines.length - 1 ? current + 1 : 0  );
				}
				return false;
			}).hover(fadeInButton, fadeOutButton);

			prev.click(function() {
				if( !working ) {
					working = true;
					change( current > 0 ? current - 1 : timelines.length - 1  );
				}
				return false;
			}).hover(fadeInButton, fadeOutButton);

			$(timelines.get(current)).show();
			renderArrows();

		});




		//Input default text
		window.check_inputs = function() {
			$('.default-text').each(function(){
				if(!this._default_text){
					this._default_text = $(this).val();
					if(this.type == 'password'){
						this._text_input = document.createElement('input');

						this._text_input.type = 'text';
						this._text_input.className = this.className;
						this._text_input._text_password = this;
						$(this._text_input).val($(this).val());
						$(this).val('');

						$(this).removeClass('has-default-text');

						$(this._text_input).css({
							'position': 'absolute',
							'top': $(this).position().top + 'px',
							'left': $(this).position().left + 'px',
							'margin-top': '0',
							'display': 'block'
							});

						this.parentNode.appendChild(this._text_input);

						$(this.parentNode).css({'height': $(this.parentNode).outerHeight() + 'px'});

						$(this._text_input).focus(function() {
							$(this).css({'display': 'none'});
							$(this._text_password).focus();
						});

						$(this).focus(function(){
							if( $(this._text_input).css('display') == 'block' ) {
								$(this._text_input).focus();
							}
						});
						$(this).change(function(){
							if( $(this._text_input).css('display') == 'block' ) {
								$(this._text_input).focus();
							}
						});


						$(this).blur(function(){
							if( $(this).val() == '')
								$(this._text_input).css({'display': 'block'});
						});

						}
					else {
						$(this).addClass('has-default-text');
						$(this).focus(function() {
							if( $(this).val() == this._default_text) {
								this.value = '';
								$(this).removeClass('has-default-text');
							}
							return true;
						});
						$(this).blur(function() {
							if( $(this).val() == '') {
								$(this).addClass('has-default-text');
								$(this).val(this._default_text);
							}
							return true;
						});
						}
					}
			});
		};
		window.check_inputs();


		//People list
		$('.people li:nth-child(3n)').addClass('last-in-row');


		//Project list
		$('ul#projects li:nth-child(3n)').addClass('last-in-row');


		//insights
		$('#insights.grid-view li:nth-child(4n)').addClass('last-in-row');


		//Sidebar filter
		$('#sidebar .filter li a').click(function(){
			var li = $(this).parent();
			if( li.children('ul').length ) {
				if( li.hasClass('opened') ) {
					li.removeClass('opened');
				} else {
					li.addClass('opened');
				}
				return false;
			}
			return true;
		});


		//Person Lightbox
		var teamLightBox = new boxMe({
			'url': '',
			'width': 660,
			'onload': function() {
				this.center();
			}
		});
		$('.people li a.read-more, .people li .image-wrapper').css('cursor', 'pointer').click(function() {
			teamLightBox.options.url = $(this).parents('li:first').find('a.read-more').attr('href');
			teamLightBox.show().center();
			return false;
		});
		$('#key-contacts a.read-more').css('cursor', 'pointer').click(function() {
			teamLightBox.options.url = $(this).attr('href');
			teamLightBox.show().center();
			return false;
		});


		//Alternative content for widgets on hover
		$('.widget').each(function(){

			var widget = $(this);
			var original = widget.find('.original-content');
			var alternative = widget.find('.alternative-content');

			if( original.length && alternative.length ) {
				widget.hover(function(){
					original.stop().animate({'opacity': 0, 'left': '100%'}, 'fast');
					alternative.stop().css('display', 'block').animate({'opacity': 1}, 'fast');
				}, function(){
					original.stop().animate({'opacity': 1, 'left': '0'}, 'fast');
					alternative.stop().animate({'opacity': 0}, 'fast');
				});
			}

		});


		//Projects viewer for sector
		$('#project-viewer').each(function(){
			
			var working = true;
			var wrapper = $(this);
			var ul = wrapper.find('ul#projects');
			var projects = ul.find('li');
			var shortcuts = wrapper.find('ul.pagination');
			
			var height = projects.height();
			var width = projects.width() + parseInt(projects.css('margin-right'));
			var current = 0
			
			var focus = function(id) {
				
				if( id < 0 ) {
					id = 0;
				} else {
					if( id >= projects.length - 1) {
						id = projects.length - 1;
					}
				}
				
				
				current = id;
				renderCurrentShortcut();
				ul.animate({'scrollLeft': (id - 1) * width}, 'slow', function() {
					working = false;
				});
				
			}
			
			
			var renderCurrentShortcut = function() {
				
				var pages = shortcuts.find('li').not('.next, .prev');
				pages.removeClass('current').removeClass('sector-background-color');
				
				var id1, id2, id3;
				
				if( current <= 0 ) {
					id1 = 0;
					id2 = 1;
					id3 = 2;
				} else {
					
					if( current >= pages.length - 1  ) {
						id1 = pages.length - 3;
						id2 = pages.length - 2;
						id3 = pages.length - 1;
					} else {
						id1 = current - 1;
						id2 = current;
						id3 = current + 1;
					}
				}
				
				$(pages.get(id1)).addClass('current sector-background-color');
				$(pages.get(id2)).addClass('current sector-background-color');
				$(pages.get(id3)).addClass('current sector-background-color');	
				
				
				if( current <= 1 ) {
					wrapper.find('li.prev').fadeOut();
				} else {
					wrapper.find('li.prev').fadeIn();
				}
				
				if( current >= pages.length - 2 ) {
					wrapper.find('li.next').fadeOut();
				} else {
					wrapper.find('li.next').fadeIn();
				}
				
			}
			
			
			projects.each(function(id){
				var li = $(this);
				
				var shortcut = $('<li><a href="">'+ (id + 1) +'</a></li>')
					.appendTo(shortcuts)
					.click(function(){
						if( !working ) {
							working = true;
							focus(id - 1);
						}
						return false;
					})
				;
				
				li.css({
					'position': 'absolute',
					'top': 0,
					'left': id++ * width
				});
			});
			
			wrapper.find('li.next').click(function(){
				if( !working ) {
					working = true;
					focus( parseInt(current / 3) * 3 + 4 );
				}
				return false;
			});
			
			wrapper.find('li.prev').click(function(){
				if( !working ) {
					working = true;
					focus( (parseInt(current / 3) - 1) * 3 + 1 );
				}
				return false;
			});
			
			$(wrapper).mousewheel(function(ev, delta) {
				if( delta > 0 ) {
					wrapper.find('li.prev').click();
				} else {
					wrapper.find('li.next').click();
				}
				return false;
			});

			
			renderCurrentShortcut();
			ul.addClass('active').get(0).scrollLeft = 0;
			working = false;
			
		});
		
		
		var detector = new FontDetector();
		if( !detector.test('calibri') ) {

			$('body').addClass('small-type-face');

		}
		
		
		//Service icons
		$('.services-images img').each(function(){
			
			var img = $(this);
			var normal = img.attr('src');
			var hover = img.attr('alt');
			
			document.createElement('img').src = hover;
			
			img.parents('li:first').hover(function() {
				img.attr('src', hover);
			}, function() {
				img.attr('src', normal);
			});
			
		});
		
		

		if( $('html').hasClass('ie6') ) {
			var selectors = [
				//'#logo, #logo img',
				'#get-in-touch li a',
				'#top-slides .tab-wrappers li.services ul li span img',
				'#sidebar ul.sector-services li span img',
				'.sectors-filter .icon',
				'a.read-more',
				'a.back-to-top',
				'a.next-link',
				'#sidebar ul.sector-services li span img',
				'#timeline a.prev',
				'#timeline a.next',
				'#news a.up, #news a.down',
				'.sectors-filter .icon'
			];
			DD_belatedPNG.fix(selectors.join(','));
			
			var logo = $('#logo img');
			var logoS = $('<div></div>').css({
				'display': 'block',
				'width': logo.width(),
				'height': logo.height() - 150,
				'position': 'relative'
			}).insertAfter(logo.hide()).get(0).style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+ logo.attr('src') +'\', sizingMethod=\'image\')';
			
			
			
		}


	});

	window.boxMe = function(options) {

		var $this = this;
		this.options = options;
		this.wrappers = {
			'cover': $('<div class="boxme-cover">').hide(),
			'box': $('<div class="boxme-box">').hide(),
			'heightCalculator': $('<div class="height-calculator">'),
			'content': $('<div class="boxme-content">'),
			'close': $('<a class="close-button">close</a>')
		}


		var f_filterResults = function(n_win, n_docel, n_body) {
			var n_result = n_win ? n_win : 0;
			if (n_docel && (!n_result || (n_result > n_docel)))
				n_result = n_docel;
			return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
		}



		var scrollTop = function() {
			return f_filterResults (
				window.pageYOffset ? window.pageYOffset : 0,
				document.documentElement ? document.documentElement.scrollTop : 0,
				document.body ? document.body.scrollTop : 0
			);
		}


		this.center = function() {

			this.wrappers.box.css({
				'top': scrollTop() + ( $(window).height() / 2 - this.wrappers.box.height() / 2 ),
				'left': '50%',
				'marginLeft': - this.wrappers.box.width() / 2
			});

			return this;

		}


		this.show = function() {

			this.wrappers.cover.css({
				'opacity': 0.5
			})
				.hide()
				.width( '100%' )
				.height( $(document).height() );

			this.wrappers.cover.fadeIn('fast');

			this.setWidth( this.options.width || 400 );

			this.wrappers.content
				.css({'height': 'auto'})
				.html('loading...');

			this.wrappers.box
				.hide()
				.fadeIn('fast');

			$.ajax({
				'url': this.options.url,
				'success': function(data) {
					$this.wrappers.content.animate({'opacity': 0}, 'fast', function(){

						var height;
						if( typeof $this.options.height == 'undefined' ) {
							height = $this.wrappers.heightCalculator.html(data).height();
						} else {
							height = $this.options.height;
						}

						$(this).html(data);
						$this.coverHeight();

						if( typeof $this.options.onload == 'function' ) {
							$this.options.onload.call($this);
						}

						var params = {'opacity': 1};
						if( height != 'auto' ) {
							params.height =  height;
						}
						$(this).animate(params, 'slow', function(){
							if( typeof $this.options.callback == 'function' ) {
								$this.options.callback.call($this);
							}
							$(this).css('filter', '');
						});
					});
				},
				'error': function() {
					$this.wrappers.content.html('<div class="boxme-error">Error loading content! <small>Please try again later.</small></div>');
				}
			});

			return this;

		}


		this.hide = function() {

			this.wrappers.cover.hide();
			this.wrappers.box.hide();

			return this;

		}


		this.coverHeight = function() {

			this.wrappers.cover.height( $(document).height() );

			return this;

		}


		this.setWidth = function(width) {

			this.wrappers.box.width( width );
			this.wrappers.heightCalculator.width( width );
			this.wrappers.box.css({
				'top': scrollTop() + ( this.options.marginTop || 100 ),
				'left': '50%',
				'marginLeft': - width / 2
			});

			return this;

		}

		$('body')
			.append(this.wrappers.cover)
			.append(this.wrappers.box)
			.append(this.wrappers.heightCalculator)
		;

		this.wrappers.box
			.append( this.wrappers.content )
			.append( this.wrappers.close )
		;


		this.wrappers.close.click(function() {
			$this.hide();
			return false;

		});

		this.wrappers.cover.click(function() {
			$this.hide();
			return false;

		});



	}

})(jQuery);

