var pageLoading;

jQuery(function() {
	addLinks();
	if(location.hash) {
		var uri = uriTrimSlashes(location.hash.substring(1));
		if(uri.length == 0)
			loadPage('main');
		else {
			$.ajax({url: uri, type: 'GET', success: function() {
					loadPage(uri);
				}, error: function() {
					loadPage('main');
				}});
		}
	} else {
		loadPage('main');
	}
})

function addLinks() {
	$("a[href^='#/']").click(function(e){
		uri = uriTrimSlashes($(this).attr('href').substring(1));
		$(document).unbind('click');
		$(this).unbind('click');
		this.blur();
		loadPage(uri);
		return false;
	});
	$("a[href^='http://theforumagency.com/#/']").click(function(e){
		uri = uriTrimSlashes($(this).attr('href').substring(27));
		$(document).unbind('click');
		$(this).unbind('click');
		this.blur();
		loadPage(uri);
		return false;
	});
}

function uriTrimSlashes(uri) {
	if(uri.length) {
		while (uri.charAt(0) == '/') {
			uri = uri.substring(1);
		}
		while (uri.charAt(uri.length) == '/') {
			uri = uri.substring(0,-1);
		}
	}
	return uri;
}

function uriAddSlashes(uri) {
	if(uri.length) {
		if (uri.charAt(uri.length-1) != '/') {
			uri = uri + '/';
		}
		if (uri.charAt(0) != '/') {
			uri = '/' + uri;
		}
	}
	return uri;
}

function dimPhotos(domPhotos) {
	$(domPhotos).fadeTo("fast", 1.0);
	$(domPhotos).hover(
		function() {
			$(this).fadeTo("fast", 0.7);
		},
		function() {
			$(this).fadeTo("fast", 1.0);
		}
	);
}

function startMain() {
	tb_init('a.thumb_link');
	if(!$.browser.msie)
		dimPhotos('div.thumb_holder');
	$('.more_jobs').fadeTo(0,0.0);
	$('.category').each(function() {
		$(this).hover(
			function() {
				$(this).find('.more_jobs').fadeTo("fast", 1.0);
			}, function() {
				$(this).find('.more_jobs').fadeTo("fast", 0.0);
			}
		);
	});
	
	$('.category .cat_title').each(function() {
		$(this).hover(
			function() {
				$(this).find('.cat_name').css('color', '#70bfe3');
				$(this).find('.more_jobs').css('color', '#70bfe3');
			}, function() {
				$(this).find('.cat_name').css('color', 'white');
				$(this).find('.more_jobs').css('color', 'white');
			}
		).click(function(){
			$(this).find('a').click();
		});
	});
}

function startCat() {
	$('div.jobBack').fadeTo(0,0.0);
	$('div.jobContainer').each(function(){
		$(this).hover(
			function() {
				$(this).children('.jobBack').fadeTo("fast", 0.5);
			}, function() {
				$(this).children('.jobBack').fadeTo("fast", 0.0);
			}
		);
		$(this).click(function(){
			$(this).find('a:first').click();
		});
	});
}

function startContact() {
	
	$('#contactForm').submit(function() {
		sendContact();
		return false;
	});
}

function sendContact() {
	var Name = $('#nameInput').val();
	var Phone = $('#phoneInput').val();
	var Email = $('#emailInput').val();
	var Comment = $('#commentInput').val();
	var f=0;
	
	f=0;
	
	if (Name.length > 0) {
		f++;
		$('#nameLabel').removeClass('error');
	} else $('#nameLabel').addClass('error');
	if (Email.length > 0) {
		f++;
		$('#emailLabel').removeClass('error');
	} else $('#emailLabel').addClass('error');
	if (Comment.length > 0) {
		f++;
		$('#commentLabel').removeClass('error');
	}
		else $('#commentLabel').addClass('error');
	
	
	if(f > 2 && pageLoading !== 1) {
		pageLoading = 1;
		offset = "-" + ($("div#cb").height() + 40) + "px";
		$("div#cb").animate({
			"marginTop": offset
		}, 1200, "swing", function(){
			$("div#cb").hide();
			$("div#body").prepend("<p class='loading'>Sending...<br /><img src='images/ajax-loader.gif' alt='sending...'/ ></p>");
			$.post('/commentPost.php',{ name : Name, phone : Phone, email : Email, comment : Comment },function(data, textStatus) {
				if(textStatus == "success")
					$('#formBody').empty().append('<p id="successText">Thank you. Your comments are appreciated.</p>');
				else
					$('#formBody').empty().append('<p id="successText">There was an error. Please try again later.</p>');
				$("p.loading").remove();
				$("div#cb").show().animate({
					"marginTop": "0px"
				},1200);
				pageLoading = 0;
				addLinks();
			});
		});
	}
}

function loadPage(uri) {
	if(pageLoading !== 1 && (location.hash != "#"+uriAddSlashes(uri) || typeof(pageLoading) == "undefined")) {
		pageLoading = 1;
		offset = "-" + ($("div#cb").height() + 40) + "px";
		$("div#cb").animate({
			"marginTop": offset
		}, 1200, "swing", function(){
			$("div#cb").hide();
			$("div#cb").empty();
			$("div#body").prepend("<p class='loading'>Loading...<br /><img src='images/ajax-loader.gif' alt='loading...'/ ></p>");
			$("div#cb").load(uri,function(){
				offset = "-" + ($("div#cb").height() + 40) + "px";
				$(this).css("margin-top", offset);
				$("p.loading").remove();
				$(this).show();
				$(this).animate({
					"marginTop": "0px"
				},1200);
				
				pageLoading = 0;
				addLinks();
			});
		});
		
		location.hash = uriAddSlashes(uri);
	}
}