$(document).ready(function() {	
	//select all the a tag with name equal to modal
	$('.modal').click(function(e) {
		//Cancel the link behavior
		$('.frmResult').html('&nbsp;').hide();
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('rel');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height 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
		$('#popupDiv #popupContent').html($("#"+id).html());
		$('#popupDiv').css('top',  winH/2-$('#popupDiv').height()/2);
		$('#popupDiv').css('left', winW/2-$('#popupDiv').width()/2);
	
		//transition effect
		$('#popupDiv').fadeIn(2000);
	
	});
	

	$('.frmResult').ajaxError(function(e, request, settings) {
		alert('An error occured while processing your request.  Please try again later.');
		$('#ajaxWait').hide();
	});

	$('.OutOfStock').click(function(e) {
		//Cancel the link behavior
		$('.frmResult').html('&nbsp;').hide();
		e.preventDefault();
		//Get the A tag
//		var id = $(this).attr('rel');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height 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
		$('#popupDiv #popupContent').load($(this).attr('href')+'', initClose);
		$('#popupDiv').css('top',  (winH/2-$('#popupDiv').height()/2)+$(window).scrollTop());
		$('#popupDiv').css('left', winW/2-$('#popupDiv').width()/2);
	
		//transition effect
		$('#popupDiv').fadeIn(2000);
		initClose();
	
	});
	$.ajaxSetup({
		beforeSend: function (xhr) {
	                xhr.setRequestHeader('AJAX_REQUEST', 'true');
	                return xhr;
		}
	});
	initClose();
});

function closePopup() {
	$('#mask, #popupDiv').hide();
	$('#popupDiv #popupContent').html('');
	return false;
}

function fadePopup() {
	$('#mask, #popupDiv').fadeOut("fast", function () {$('#popupDiv #popupContent').html('');});
	return false;
}

var focusDiv = '';
function ajaxSubmit(frm) {
//	frm.id = 'frm-'+Math.floor(Math.random()*99999).toString();
	if (!(focusDiv = $(frm).children('.frmResult'))) {
		focusDiv = $(frm).parent('.returnFocus');
	}
//	focusDiv = $('#'+frm.id).parent('.returnFocus');
	switch(frm.method.toLowerCase()) {
		case 'post':
			$('.frmResult').fadeOut().html('&nbsp;');
			$('#ajaxWait').show();
			$.post(	frm.action+(frm.action.indexOf('?') ? '&' : '?')+"requestType=ajax",
					$(frm).serialize(),
					function(data) {
						focusDiv.html(data).show();
						$('#ajaxWait').hide();
					}
				);
			break;
		case 'get':
		default:
			break;
	}
	return false;
}

function initClose() {
	$('#popupDiv .close').one('click', function (e) {
		//Cancel the link behavior
		e.preventDefault();
		closePopup();
	});		
	
	//if mask is clicked
	$('#mask').one('click', function () {
		closePopup();
	});
}

