// Thoughtbubble Simple Lightbox
/* !!!	JQUERY REQUIRED

	Action:
	Opens the href in a new div.
	If a rel="" is present that URL is used instead (used for JS & non-JS versions).

*/

//PARAMS
var sClose_image = '<img src="/winners/images/icons/close.gif" width="15" height="15" alt="Close" border="0" align="absbottom" />';
var sLoad_image = '<img src="/winners/images/loader.gif" width="32" height="32" alt="Loading" border="0" class="loader" />';
var nBoxOffset = 20; // The gap between windoe and top of the box
var bLimitScroll = true; // Limit scrolling when page is long? (removes page content
var sAreaToHide = "#mainWrapper"; // The selector that is hidden to limit scrolling


// THE CODE
var isIE6 = jQuery.browser.msie && jQuery.browser.version.substr(0,1)=="6" ? true : false; // WARNING: THIS DETECTION IS DEPRECIATED IN JQUERY 1.3

$(document).ready(function(){
	tb_init();
});

// Check window size on resize
$(window).resize(function(){ tb_check_sizing(); });

// For IE 6 we use a fudge to mimic position:fixed
function tb_ie6_pos() {
	$("#TBBoxDisplay").css("top", ($(window).scrollTop()+nBoxOffset) + "px");
}

if(isIE6) {
	$(window).scroll(function() {
		if ($("#TBBoxDisplay").height() < $(window).height()-nBoxOffset) {
	    	tb_ie6_pos();
		}
	});
}

// Hunt and active links
function tb_init() {
	$(".tb-box").unbind("click").click(function(event){
		return this.rel != "" ? tb_show(this.rel) : tb_show(this.href);
	})
}

// Check size of lightbox to switch fixed or absolute positioning
function tb_check_sizing() {
		// The line below is all you need if you don't want Dom's scrolling fix.  
		//$("#TBBoxDisplay").height() > $(window).height()-nBoxOffset ? $("#TBBoxDisplay").css("position","absolute").css("top", ($(window).scrollTop()+nBoxOffset)+"px") : $("#TBBoxDisplay").css("position","fixed").css("top",nBoxOffset+"px");

	if ($("#TBBoxDisplay").height() > $(window).height()-nBoxOffset) {
		if (bLimitScroll) {
			$("#TBBoxDisplay").css("position","absolute").css("top", nBoxOffset+"px") ;
			$(window).scrollTop(0);
			$(sAreaToHide).hide();
		}
		if(!isIE6) {
			$("#TBBoxBlackOut").height($(window).height() > $("#TBBoxDisplay").height() ? $(window).height()+(nBoxOffset*2) : $("#TBBoxDisplay").height()+(nBoxOffset*2) );
		}
	} else {
		$(sAreaToHide).show()
		$("#TBBoxBlackOut").height($("body").height() > $(window).height() ? $("body").height()+(nBoxOffset*2) : $(window).height()+(nBoxOffset*2) );
		if(!isIE6) {
			// This is messing something up in IE (6,7&8)
			$("#TBBoxDisplay").css("position","fixed").css("top",nBoxOffset+"px");
		}
	}
}

function tb_show(sURL) {
	if ( $("#TBBoxDisplay").length > 0 ) { tb_close(); }
	$("body").append('<div id="TBBoxDisplay" class="TBPosFixed"><div id="TBBoxClose">Close '+sClose_image+'</div><div id="TBBoxInner">'+sLoad_image+'</div><!--[if lte IE 6.5]><iframe></iframe><![endif]--></div><div id="TBBoxBlackOut"></div>'); // Add Container
	$("#TBBoxClose").click(tb_close); // Make the close do something
	$("#TBBoxBlackOut").click(tb_close); // Make the black out close
	$("#TBBoxBlackOut").height($(window).height());
	if(isIE6) { tb_ie6_pos(); }
	$.ajax({ type: "GET", cache:true, url:sURL,
		data: "inBox=1", dataType: "html",
		success: function(html){ $("#TBBoxInner").html(html); },
		complete: function(){ tb_check_sizing(); },
		error: function(html,statusText,err){$("#TBBoxInner").html("<p align='center'>There has been an error retrieving this entry.<br />Please try again.</p>"); }
	});
	return false;
}

function tb_close() {
	$("#mainWrapper").show()
	$("#TBBoxDisplay").remove(); // Remove box and everything in it
	$("#TBBoxBlackOut").remove(); // Remove black out
}
