$(document).ready(function() {	

	//if close button is clicked
	$('.modal-window .modal-close').click(function (e) {
		//Cancel the link behavior
		stopEvent(e);
		
		$('#mask').hide();
		$('.modal-window').hide();
	});		
	
	document.getElementById('register-button').onclick = function() { document.location = '?reg_form=1'; };
	
	document.getElementById('tc-link').onclick = function (e) { stopEvent(e); launch_modal_window('#tc-dialog'); };
	document.getElementById('privacy-link').onclick = function (e) { stopEvent(e); launch_modal_window('#privacy-dialog'); };
	document.getElementById('contact-link').onclick = function (e) { stopEvent(e); launch_modal_window('#contact-dialog'); };
	document.getElementById('affiliate-link').onclick = function (e) { stopEvent(e); launch_modal_window('#affiliate-dialog'); };
	
	document.getElementById('info-box-about-link').onclick = function (e) { stopEvent(e); launch_modal_window('#about-caa-dialog'); };
	document.getElementById('info-box-current-link').onclick = function (e) { stopEvent(e); launch_modal_window('#about-current-dialog'); };
	document.getElementById('info-box-buyers-link').onclick = function (e) { stopEvent(e); launch_modal_window('#bs-dialog'); };
	
	if (load_reg_form == 1) launch_modal_window('#register-dialog');
	if (load_bid_error_form == 1) launch_modal_window('#bid-error-dialog');
	if (load_bid_success_form == 1) launch_modal_window('#bid-success-dialog');
	
	var be = document.getElementById('bid-email');
	var betogval = be.value;
	if(be.value != 'Email') { be.style.color = "#000"; }
	be.onblur = function (e) { if(this.value == '') { this.value = betogval; this.style.color = "#666"; } };
	be.onfocus = function (e) { if(this.value == betogval) { this.value = ''; this.style.color = "#000"; } };
	
	var bp = document.getElementById('bid-price');
	var bptogval = bp.value;
	if(bp.value != 'Bid $') { bp.style.color = "#000"; }
	bp.onblur = function (e) { if(this.value == '') { this.value = bptogval; this.style.color = "#666"; } };
	bp.onfocus = function (e) { if(this.value == bptogval) { this.value = ''; this.style.color = "#000"; } };
	
//	var bp = document.getElementById('bid-price');
//	var bptogval = bp.value;
//	bp.onblur = function (e) { if(this.value == '') this.value = bptogval; };
//	bp.onfocus = function (e) { this.select(); };
	
	var pw = document.getElementById('bid-pw');
	var pwh = document.getElementById('pw-hider');
	pw.value = '';
//	if(pw.value == '') { pw.style.display = "none"; pwh.style.display = "inline"; } else { pwh.style.display = "none"; }
	pw.onblur = function (e) { if(this.value == '') { this.style.display = "none"; pwh.style.display = "inline"; } };
	pwh.onfocus = function (e) { this.style.display = "none"; pw.style.display = "inline"; pw.focus();};
	
});

function stopEvent(e) {
	if(!e) var e = window.event;
	
	//e.cancelBubble is supported by IE - this will kill the bubbling process.
	e.cancelBubble = true;
	e.returnValue = false;

	//e.stopPropagation works only in Firefox.
	if (e.stopPropagation) {
		e.stopPropagation();
		e.preventDefault();
	}
	return false;
}

//select all the a tag with name equal to modal
function launch_modal_window( id ) {
//	stopEvent(e);
	
	//Get the A tag
//	var id = '#register-dialog';

	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	
	var popupBottom = 100 + $(id).height();
	
	if (popupBottom > maskWidth) maskWidth = popupBottom;

	//Set heigth and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	//transition effect		
//	$('#mask').fadeIn(1000);	
	$('#mask').fadeTo("slow",0.8);	

	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
          
	//Set the popup window to center
//	var popupH = $(id).height();
//	var top = winH/2-popupH/2 + window.pageYOffset;
	
	
	var top = 50; 
	window.scrollTo(0,0);
	
	$(id).css('top',  top);
	$(id).css('left', winW/2-$(id).width()/2);

	
	
	//transition effect
	$(id).fadeIn("slow"); 
}