/******************************************************************************
 Global Functions
 
 Developer	: Miles Johnson
 Email		: mileswjohnson@gmail.com
 Website	: http://www.mileswjohnson.com/

******************************************************************************/

var IE = (document.all);

function goTo(url, text) {
	if (text != null) {
		var conf = confirm(text);
		
		if (conf) {
			document.location = url;
		}
	} else {
		document.location = url;
	}
	
	return false;
}

function toggleCheckboxes(current, form, field) {
	$("#"+ form +" :checkbox[name='"+ field +"[]']").attr("checked", current.checked);
}
function expandMenu(subMenu) {
	var menu = $("#subMenu ul."+ subMenu);
	menu.slideToggle();
}

function updateExchange(field, target) {
	var amount = $(field).val();
	var newAmount = Math.round(amount / 2);

	if (isNaN(amount)) {
		alert('Please enter a numerical value!');
		$("#"+ target).val('');
		$(field).val('');
	} else {
		$("#"+ target).val(newAmount);
	}
}

function viewSkillTree(tree, button) {
	$("#skillTabs a").removeClass("active");
	button.className = "active";
	
	$(".skillTree").hide();
	$("#"+ tree).show();
	
	return false;
}

function quickReply(scroll) {
	$("#quickReply").slideDown();

	if (scroll != null)
		$('html, body').animate({ scrollTop: $('#quickReply').offset().top }, 1000);

	return false;
}

function numeric(event) {
	var key = event.keyCode;

	return ((key >= 48 && key <= 57) || (key >= 96 && key <= 105) || (key == 8) || (key == 9));
}

function fixPostImages() {
    var posts = $('.forumPost');

    if (posts) {
        var length = posts.length;
        var width = posts.width();

        for (i = 0; i <= length; ++i) {
            var post = $(posts[i]);
            var images = post.find('img');

            if (images) {
                var imgLength = images.length;

                for (x = 0; x <= imgLength; ++x) {
                    var image = $(images[x]);

                    if (image.width() > width)
                        image.css('width', width);
                }
            }
        }
    }
}

function showBuriedPost(id) {
	$('#postHidden_'+ id).slideToggle();
	return false;
}

function ratePost(id, rate) {
	$.ajax({
		type: 'POST',
		url: '/ajax/rate_post/',
		data: {
			id: id,
			rate: rate
		},
		dataType: 'json',
		success: function(data) {
			if (data.success) {
				$('#ratings_'+ id)
					.find('.vote').remove().end()
					.find('.up').html(data.data.up).end()
					.find('.down').html(data.data.down).end();
			} else {
				Alert.open(data.data, 'failure');
			}
		},
		error: function() {
			Alert.open('An error has occurred. Please try again later.', 'failure');
		}
	});

	return false;
}

function rateHistory(id) {
	$.ajax({
		type: 'POST',
		url: '/ajax/rate_history/'+ id,
		dataType: 'html',
		success: function(data) {
			Alert.open(data);
		},
		error: function() {
			Alert.open('An error has occurred. Please try again later.', 'failure');
		}
	});

	return false;
}

var Alert = {

	initialized: false,
	object: null,

	init: function() {
		Alert.object = $('<div/>').attr('id', 'alert').hide().appendTo('body');
		Alert.initialized = true;
	},

	open: function(content, className) {
		if (!Alert.initialized)
			Alert.init();

		Alert.object
			.html(content)
			.removeAttr('class');

		if (className)
			Alert.object.addClass(className);

		Alert.position();
		Blackout.open(function() {
			Alert.close();
		});
	},

	close: function() {
		Alert.object.fadeOut('fast');
		Blackout.close();
	},

	position: function() {
		var width = Alert.object.outerWidth(true),
			height = Alert.object.outerHeight(true),
			win = $(window),
			windowWidth = win.width(),
			windowHeight = win.height();

		Alert.object.show().css({
			top: (windowHeight / 2) - (height / 2),
			left: (windowWidth / 2) - (width / 2)
		});
	}

}

var Blackout = {

	initialized: false,
	object: null,

	init: function() {
		Blackout.object = $('#blackout');
		Blackout.initialized = true;
	},

	open: function(callback) {
		var doc = $(document);

		Blackout.object
			.show()
			.unbind()
			.click(callback)
			.css({
				width: doc.width(),
				height: doc.height()
			})
	},

	close: function() {
		Blackout.object.fadeOut('fast');
	}

}


var NetworkBar = {
	
	node: null,
	menus: null,
	tabs: null,
	cache: {},
	
	init: function() {
		NetworkBar.node = $('#networkBar');
		
		NetworkBar.tabs = NetworkBar.node.find('.has-menu a.tab');
		NetworkBar.tabs.click(NetworkBar.open);
		
		NetworkBar.menus = NetworkBar.tabs.find('.menu');
	},

	open: function(e) {
		e.preventDefault();
		e.stopPropagation();
		
		var node = $(e.currentTarget),
			url = node.attr('href'),
			menu = node.siblings('.menu'),
			parent = node.parent();
			
		// Reset same tab
		if (node.hasClass('tab-active')) {
			node.removeClass('tab-active');
			parent.removeClass('opened');
			
			return false;
			
		} else {
			NetworkBar.node.find('li').removeClass('opened');
			NetworkBar.tabs.removeClass('tab-active');
		}
			
		// Cache or fetch
		if (NetworkBar.cache[url]) {
			parent.addClass('opened');
			node.addClass('tab-active');	
		} else {
			$.ajax({
				url: url,
				type: 'GET',
				dataType: 'html',
				cache: true,
				success: function(response) {
					menu.empty().html(response);
					node.addClass('tab-active');
					parent.addClass('opened');
					NetworkBar.cache[url] = true;
				}
			});
		}
		
		return false;
	}
	
};

$(function() {
	NetworkBar.init();
	Alert.init();
	Blackout.init();
})
