jQuery(function(){
	
	var content = $("#ajaxContainer");
	var socialIcons = $("#social-icons");
	var socialLink = $("#social-icons a");
	var navElements = $("#nav a.ajaxify");
	var ajaxElements = $("a.ajaxify");
	var value;
	var validValues = new Array();
	var dest;
	
	Compass = new function(){
		
		$('.noscript').hide();		
		
		this.bind = function(){
			ajaxElements.live('click', function(){
				Compass.pageTransition($(this));
				return false;
			});	
			navElements.bind('click', function(){
				Compass.pageTransition($(this));
				return false;
			});	
		}
		
		this.getDest = function(value){
			value = value.split("/");
			count = 0;
			validValues = new Array();
			$.each(value, function(i, e){
				/*if(e == "http:" && ( ! $.browser.msie || ($.browser.msie && $.browser.version > 7))){*/
				if(e == "http:"){
					validValues[count] = "home";
					return false;
				} else {
					if(e != ''){
						validValues[count] = e;
						count++;
					}
				}
			});
			if(validValues.length > 1){
				dest = validValues[validValues.length - 1];
			} else {
				dest = validValues[0];	
			}

			return dest;
		}
		
		this.loadPage = function(dest){
			if(dest == "contact-us"){
				$.get("ajax_contact.php", {seo_url:dest}, function(data){
					content.html(data);
				});
			} else if(dest == "courier-collection"){
				$.get("ajax_courier.php", {seo_url:dest}, function(data){
					content.html(data);
				});
			} else if(dest == "become-a-client"){
				$.get("ajax_client.php", {seo_url:dest}, function(data){
					content.html(data);
				});
			} else if(dest == "costs-wars"){
				$.get("ajax_costs_wars.php", {seo_url:dest}, function(data){
					content.html(data);
				});
			} else {
				$.get("ajax_loader.php", {seo_url:dest}, function(data){
					content.html(data);
				});
			}
		}
		
		this.pageTransition = function(clicked){
			content.hide(500, function(){
				dest = Compass.getDest(clicked.attr("href"));
				Compass.loadPage(dest);
			}).animate({
				left:0
			}, 500).show(500);
		}
		
		this.fromFlash = function(str){
			var obj = $('#'+str);
			if(obj.hasClass('ajaxify')){
				Compass.pageTransition(obj);
			} else {
				if(str !== 'nav-link-blog'){
					window.open(obj.attr('href'));
				} else {
					window.location.href = obj.attr('href');
				}
			}
		}
		
		this.fromBlog = function(str){
			var obj = $('#'+str);
			window.location.href = obj.attr('href');
		}
		
		this.newsletterSignup = function(){
			$("#submit_newsletter").trigger('click');		
		}
		
		this.dropSocialIcons = function(){
			socialIcons.css('top','5px');	
		}
		
		this.autoScroll = function(){
			var ul = $(".news-container ul");
			var countLi = ul.children.length;
			var maxHeight = Compass.getHeight(ul);
			maxHeight = maxHeight * -1;
			var liHeight = Compass.getHeight($(".news-container ul li"));
			
			$(document).everyTime(9000, function(i){
				ulPos = ul.css('top');
				ulPos = ulPos.substr(0,ulPos.length-2);
				combined = ulPos - liHeight;
				//alert(ulPos + ' - ' + combined + ' - ' + maxHeight);
				if(ulPos - liHeight > maxHeight){
					ul.animate({
						top:ulPos - liHeight
					}, 1000, 'easeInOut');
				} else {
					ul.animate({
						top:0
					}, 1000, 'easeInOut');
				}
			}, 0);			
		}
		
		this.getHeight = function(el){
			elHeight = el.height();
			elMargin = el.css("paddingTop");
			elMargin = elMargin.substr(0,elMargin.length-2);
			elHeight = parseInt(elHeight);
			elMargin = parseInt(elMargin);
			return elHeight + elMargin;
		}
		
	};
	
	socialLink.mouseenter(function(){
		$(this).stop().animate({
			top:'-10px'
		}, 'fast');
	});
	
	socialLink.mouseleave(function(){
		$(this).stop().animate({
			top:'0'
		}, 'fast');
	});
	
	Compass.bind();
	Compass.dropSocialIcons();
	if(window.location.pathname != '/blog/'){
		Compass.autoScroll();
	}
		
});
