/** @Name		IceBox
 ** @URI		http://pxoink.net/articles/icebox.html
 ** @Author		PxO Ink
 ** @AuthorURI	http://www.pxoink.net/
 ** @License	Proprietary.
 ** @Copyright	© 2011 PxO Ink. All Rights Reserved.
 **/

/** Function:		icebox
 ** Arguments:		ele
 **/

function icebox(ele) {
	//Declare variables.
	var	content	=	jQuery(ele).html();
	var	markup	=	new Array();
	
	//Assemble markup.
	markup.push(
		'<div id="ice-shadow"></div>',
		'<div id="icebox">',
			'<a class="icebox-close" href="javascript:void(0)" title="Close" onclick="iceboxClose()"><img src="/images/red-x.png" alt="Close" title="Close" /></a>',
			'<div id="icebox-header"></div>',
			'<div id="icebox-content">',
				content,
			'</div>',
		'</div>'
	);
	
	//Join markup to implement the new element.
	jQuery('body').append(markup.join(''));
	
	//If there is a header.
	if (jQuery('#icebox-content .icebox-header').html()) {
		//Reposition the header.
		jQuery('#icebox-header').html(jQuery('#icebox-content .icebox-header').html()).show();
		
		//Remove the header element.
		jQuery('#icebox-content .icebox-header').remove();
	}
	
	//Position the element in the center.
	jQuery('#icebox').css("top", (((jQuery(window).height() - jQuery('#icebox').outerHeight()) / 2) + jQuery(window).scrollTop()));
	jQuery('#icebox').css("left", (((jQuery(window).width() - jQuery('#icebox').outerWidth()) / 2) + jQuery(window).scrollLeft()));
	
	//Fade the new element in.
	jQuery('#ice-shadow').fadeIn('medium', function() {
		jQuery('#icebox').fadeIn('medium');
	});
	
	//Insure that hover elements are treated differently.
	jQuery('#icebox-content').children('form').attr('id', 'hover-form');
	jQuery('#icebox-content').children('form').children('input.green-button').attr('onclick', 'formDisplayProcessRequest(this)');
	jQuery('#icebox-content div.captcha > span').each(function() {
		//Get the match.
		var	match	=		jQuery(this).attr('id');
		
		//Remove the span.
		jQuery(this).remove();

		//Replace the match, if there's a positive test.
		jQuery('#icebox-content div.captcha div.captcha-controls a').each(function() {			
			if (jQuery(this).attr('onclick').match(match)) {
				//Remove the onclick.
				jQuery(this).attr('onclick', '');
				
				//Set the title.
				jQuery(this).children('img').attr('title', jQuery.trim(jQuery('.' + match + ':first').text()));
			}
		});
	});
}

function iceboxClose() {
	jQuery('#icebox').fadeOut('medium', function() {
		jQuery('#ice-shadow').fadeOut('medium', function() {
			jQuery('#ice-shadow').remove();
		});
		jQuery('#icebox').remove();
	});
	
	
}
