var greyboxDisplayed = false;
var icrGreyBox = null;

/**
 * Abstract class for overlay.
 * 
 * @return void 
 */
function tlibgreybox(){
	this.parent = "body";
	this.windowId = null;
	this.content = null;
	this.width = 300;
	this.height = 405;
}

/**
 * Contructs the overlay header.
 */
tlibgreybox.prototype.getHeader = function()
{
	alert('Greybox Header not implemented');
};

/**
 * Contructs the overlay body.
 */
tlibgreybox.prototype.getBody = function()
{
	alert('Greybox Body not implemented');
};

/**
 * Contructs the overlay footer.
 */
tlibgreybox.prototype.getFooter = function()
{
	alert('Greybox Footer not implemented');
};

/**
 * Transforms the overlay into a dom tree
 * 
 * @return Object
 */
tlibgreybox.prototype.toHTML = function()
{
	alert('Greybox toHTML not implemented');
};

/**
 * Closes the overlay
 */
tlibgreybox.prototype.closeGreyBox = function()
{
	return function(){
		$(".tlibgreybox-window").remove();
		$(".tlibgreybox-overlay").remove();
		// $(".close-window-container").remove();
		ICRgreyboxDisplayed = false;
	};
};

/**
 * Sets the overlay dimensions
 * 
 * @param h int The height
 * @param w int The width
 */
tlibgreybox.prototype.setWindowSize = function(h,w)
{
	this.height = h;
	this.width = w;
};
/**
 * Return the width
 * 
 * @return int The width
 */
tlibgreybox.prototype.getWidth = function() { return this.width;};

/**
 * Return the height 
 * 
 * @return int The height
 */
tlibgreybox.prototype.getHeight = function() { return this.height;};

/*Inline Checkrates Grey Box Definitions*/
function CheckRatesGB(){ }
CheckRatesGB.prototype = new tlibgreybox();

//inherit greybox functions - Think of it as an abstract class

//Implementing abstract methods
CheckRatesGB.prototype.toHTML = function()
{
	this.windowId = "CheckRatesGB";
	var container = new jQuery('<div />');
	this.setWindowSize(200,438);
	
	container.append(this.getHeader());
	container.append(this.getBody());
	container.append(this.getFooter());
	
	ICRgreyboxDisplayed = true;
	
	$('.tlibgreybox-overlay').click(function(){
		$(".tlibgreybox-window-centre").remove();
		$(".tlibgreybox-overlay").remove();
		ICRgreyboxDisplayed = false;
	});
	
	return container;
}

CheckRatesGB.prototype.getHeader = function()
{
	var overlay = new jQuery('<div />');
	overlay.attr('class', 'tlibgreybox-overlay');
	overlay.click(this.closeGreyBox());
	
	return new jQuery('<div />').append(overlay);
}

CheckRatesGB.prototype.getBody = function()
{
	var checkRatesDiv = new jQuery('<div />');
	var containerDiv = new jQuery('<div />').append(checkRatesDiv);
	var closeWindowContainer = new jQuery('<div />');
	var closeWindowLink = new jQuery('<a />');
	containerDiv.attr('id', 'crgb');
	containerDiv.attr('class', 'checkrates_container');
	var closeFunction = this.closeGreyBox();

	$('.checkrates_container').find('.' + $.datepicker.markerClassName).removeClass($.datepicker.markerClassName);
	
	$('.checkrates_container').children().each(function (i, e) 
	{
		$(e).clone().appendTo(containerDiv);
	});
	
	
	//Removing IDs from main ICR, so they dont duplicate with those
	//of the overlay.
	//We will need to restore them on close.
	$('.checkrates_container').find(".checkin_date").attr('id', '');
	$('.checkrates_container').find(".checkout_date").attr('id', '');
	
	$('.checkrates_container').find('input[type=checkbox].vendor').each(function (i, e) 
	{
		$(e).attr('id', '');
	})
	//End removing IDs
	
	//Rebinding Events
	containerDiv.find('.submit_checkrates').click( function () { openPopups(containerDiv); } );
	containerDiv.find(".checkin_date").datepicker( { onClose: function(date, e) { updateOutDate(date, e); }, maxDate: maxDate, minDate: minDate, defaultDate: inDate, dateFormat: 'dd/mm/yy' } );
	containerDiv.find(".checkout_date").datepicker( { onClose: function(date, e) { updateInDate(date, e); }, maxDate: maxDate, minDate: minDate, defaultDate: inDate, dateFormat: 'dd/mm/yy' } );
	//End Rebinding Events
	
	closeWindowContainer.attr('class', 'close-window-centre-container');
	closeWindowLink.attr('class','close-window');
	closeWindowLink.append('[X Close Window]');
	closeWindowLink.click(closeFunction);
	closeWindowContainer.append(closeWindowLink);

	containerDiv.append(checkRatesDiv);
	containerDiv.append(closeWindowContainer);
	
	this.content = new jQuery('<div />').append(containerDiv);
}

CheckRatesGB.prototype.getFooter = function()
{
	var overlayWindow = new jQuery('<div />');
	overlayWindow.attr('class', 'tlibgreybox-window-centre');
	overlayWindow.attr('style', "width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;");
	
	overlayWindow.attr('id', this.windowId);
	overlayWindow.append(this.content);
	
	var footerContainer = new jQuery('<div />');
	footerContainer.append(overlayWindow);
	return footerContainer;
}

CheckRatesGB.prototype.closeGreyBox = function()
{
	return function()
	{
		//Restoring IDs to main ICR
		$('.checkrates_container').find(".checkin_date").addClass($.datepicker.markerClassName);
		$('.checkrates_container').find(".checkout_date").addClass($.datepicker.markerClassName);
		$('.checkrates_container').find(".checkin_date").attr('id', 'checkin_date');
		$('.checkrates_container').find(".checkout_date").attr('id', 'checkout_date');
		
		//each checkbox from the overlay ICR
		$(".tlibgreybox-window-centre .checkrates_container").find('input[type=checkbox].vendor').each(function (i, e) 
		{
			//gets equivalent element in inline ICR
			var checkbox = $('.checkrates_container').find('input[type:checkbox].vendor:eq('+i+')');
			checkbox.attr('id', $(e).attr('id')); //copy id
		})
		
		//End restoring IDs to main ICR
		
		$('#checkrates_hidden').appendTo('body').css('display','none');
		$(".tlibgreybox-window-centre").remove();
		$(".tlibgreybox-overlay").remove();
		// $(".close-window-container").remove();
		ICRgreyboxDisplayed = false;
	};
};

//Inherit greybox functions - Think of it as an abstract class
TlibICRGreyBox.prototype = new tlibgreybox;
TlibICRGreyBox.prototype.constructor = new TlibICRGreyBox;
/*Inline Checkrates Grey Box Definitions*/
function TlibICRGreyBox(source)
{
	this.source = source;
}

//Implementing abstract methods
TlibICRGreyBox.prototype.toHTML = function()
{
	this.windowId = "mytlibGB";
	var container = new jQuery('<div />');
	
	container.append(this.getHeader());
	container.append(this.getBody());
	container.append(this.getFooter());
	
	greyboxDisplayed = true;
	
	return container;
};

TlibICRGreyBox.prototype.getSource = function()
{
	return this.source;
};

TlibICRGreyBox.prototype.getHeader = function()
{
	var overlay = new jQuery('<div />');
	overlay.attr('class', 'tlibgreybox-overlay');
	overlay.click(this.closeGreyBox());
	
	return new jQuery('<div />').append(overlay);
};

TlibICRGreyBox.prototype.getBody = function()
{
	var containerDiv = new jQuery('<div />');
	containerDiv.attr('id', 'icr_blocked');
	containerDiv.append(new jQuery('<h1 />').append('Your Popup Blocker is On'));
	containerDiv.append(new jQuery('<p />').append('Please click each Check Rates button to show the sites you selected'));
	
	var vendorDiv = new jQuery('<div />');
	vendorDiv.attr('class', 'vendors');
	
	var source = this.getSource();
	
	var ul = new jQuery('<ul />');
	this.getSource().find('input[type=checkbox]:checked').each(function (i, e)
	{ 
		var vendorName = $('#label_'+ $(e).attr('id')).html();
		var vendorURL = $(e).val();
		
		var li = new jQuery('<li />');
		var a = new jQuery('<a />');
		
		var img = new jQuery('<img />');
		img.attr('border', '0');
		img.attr('src', '/images/checkrates.gif');
		img.attr('alt', 'Compare Prices Button');
		img.attr('id', 'vendor_blocked_' + i + '_img');
		
		a.append(img);
		a.append(vendorName);
		
		a.attr('id', 'vendor_blocked_' + i);
		a.attr('class', 'vendor_blocked');
		a.attr('name', vendorURL);
		a.attr('href', '#');
		
		a.data('alreadyVisited', false);
		a.click(function () {return openBlockedPopup(this, source); });
		
		li.append(a);
		ul.append(li);
		
		//Greying out already visited links 
		if(popups.findPopup(a.attr('id')) != null)
		{
			a.css('color', '#f0f0f0');
			img.animate({opacity: "0.4"}, 500);
			a.data('alreadyVisited', true);
		}
	});
	vendorDiv.append(ul);
	containerDiv.append(vendorDiv);
	var closeWindowContainer = new jQuery('<div />');
	closeWindowContainer.attr('class', 'close-window-container');

	var closeWindowLink = new jQuery('<a />');
	closeWindowLink.attr('class','close-window');
	closeWindowLink.append('[X Close Window]');
	closeWindowLink.click(this.closeGreyBox());
	
	closeWindowContainer.append(closeWindowLink);
	containerDiv.append(closeWindowContainer);
	this.content = new jQuery('<div />').append(containerDiv);
};

TlibICRGreyBox.prototype.getFooter = function()
{
	var overlayWindow = new jQuery('<div />');
	overlayWindow.attr('class', 'tlibgreybox-window');
	
	overlayWindow.attr('style', "width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;");
	
	overlayWindow.attr('id', this.windowId);
	overlayWindow.append(this.content);
	
	var footerContainer = new jQuery('<div />');
	footerContainer.append(overlayWindow);
	return footerContainer;
};

TlibICRGreyBox.prototype.closeGreyBox = function()
{
	return function(){
		$(".tlibgreybox-window").remove();
		$(".tlibgreybox-overlay").remove();
		// $(".close-window-container").remove();
		greyboxDisplayed = false;
	};
};