$(document).ready(function() {
	dialogueListener();
	makeRemoveHeaderMessageButton();
	makeTooltip();
	makeButtons();
	loginFormListener();
	resizeTextarea();
	topBarTabs();
});


function dialogueListener() {
	if ($('#dialogue').length > 0) {
		$('#dialogue').modal({
			close: false,
			overlayId: 'contactModalOverlay',
			containerId: 'contactModalContainer',
			iframeId: 'contactModalIframe'
		});
	}
}

function makeRemoveHeaderMessageButton() {
	$(".removeHeaderMessage").click(function() {
		var obj = $(this).parent().parent();
		obj.fadeOut(1000, function () {
			//obj.remove();
			var val = obj.attr("id");
			var messageId = val.substr(1,val.length);
			$.get("/ajax/removeHeaderMessage.php?messageId=" + messageId);
		});
	});
}

function makeTooltip() {
	$('.tooltip').Tooltip({
		delay: 0,
		track: true,
		extraClass: 'tooltipBox',
		showURL: false
	 });
	 $('.tooltipRight').Tooltip({
		delay: 0,
		track: true,
		extraClass: 'tooltipBox',
		showURL: false,
		right: true
	 });
}

function topBarTabs() {
	$('li.tab').hover(function() {
		$(this).children('ul.subTabs').css('display', 'block');
	}, function() {
		$(this).children('ul.subTabs').css('display', 'none');
	});
}

function makeButtons() {

	$('.button').each(function(){
		var b = $(this);
		var tt = b.text() || b.val();
		
		var button = document.createElement('a');
		
		if ($(this).get(0).tagName == "INPUT") {
			$(b).addClass("buttonSubmit");
		}
		
		$(button).addClass(this.className).
			attr('id', $(b).attr('id')).
			attr('href', $(b).attr('href'));
			
		if($(b).attr('tabindex')) {
			$(button).attr('tabindex', $(b).attr('tabindex'));
		}
		$(button).css({cursor:'pointer'}).html('<p class="leftButton"><\/p><p class="centerButton">' + tt + '<\/p><p class="rightButton"><\/p>');
		
		$(b).after($(button)).remove();
	});
	
	$('.button').hover(function() {
		$(this).addClass('buttonHover');
	}, function() {
		$(this).removeClass('buttonHover');
		$(this).removeClass('buttonClick');
	});
	
	$('.button').mousedown(function() {
		$(this).addClass('buttonClick');
	});
	
	$('.button').mouseup(function() {
		$(this).removeClass('buttonClick');
	});
	
	$('.button').click(function() {
		if ($.modal != null) {
			$.modal.impl.close(true); // close dialogue window
		}
		if ($(this).is(".buttonSubmit")) {
			$(this).parents('form').submit();
		}
		if($(this).is('.setBigCookie')) {
			setBigCheckCookie();
		}
		if($(this).is('.downloadById')) {
			clickPurchase($(this));
		}
		if($(this).is('.closeWindow')) {
			window.close();
		}		
	});
	
	$('.button').focus(function() {
		$(this).addClass('buttonHover');
	});
	
	$('.button').blur(function() {
		$(this).removeClass('buttonHover');
	});
	
	$('.button').keydown(function(e) {
		if (e.which == 13 && $(this).is(".buttonSubmit")) {
			$(this).parents('form').submit();
		}
	});
}

function resizeTextarea() {
	$('textarea.resize:not(.processed)').TextAreaResizer();
}

function loginFormListener() {
	$("#loginUsername, #loginPassword").keydown(function(event) {
		if (event.keyCode == 13) {
			$(this).parents("form").submit();
		}
	});
}