
//on page load call TB_init
$(document).ready(TB_init);

//add thickbox to href elements that have a class of .thickbox
function TB_init(){
	$("a.thickbox").click(function()
	    {
        //create variable to set the title
	    var t = this.title || this.innerHTML || this.href;
        //run the function to show the overlay and picture
	    TB_show(t,this.href);
        //blur... ??
	    this.blur();
	    return false;
	    }
	);
}

function TB_show(caption, url) {//function called when the user clicks on a thickbox link
	try {
		$("body").append("<div id='TB_load'><div id='TB_loadContent'><img src='css/images/circle_animation.gif' /></div></div>");
        //use the 'body' element of the page
		$("body")
        //add the 'TB_overlay' and 'TB_window' elements to this 'body' element of the page
		.append("<div id='TB_overlay'></div><div id='TB_window'></div>");
        //if the 'TB_overlay' element is clicked, then run the TB_remove function
		$("#TB_overlay").click(TB_remove);
        //if the window is resized, then run the TB_position function
		$(window).resize(TB_position);
        //if th window is scrolled, then run the TB_position funtion
		$(window).scroll(TB_position);
 		
        //display the page with the new element
		$("#TB_overlay").show();
        //add the 'TB_load' and 'TB_loadContent' elements to the page - inside the 'TB_loadContent' is the circle animation gif		
		$("body").append("<div id='TB_load'><div id='TB_loadContent'><img src='css/images/circle_animation.gif' /></div></div>");
		
        //create variables for the url	
		var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.html|\.htm|\.php|\.cfm|\.asp|\.aspx|\.jsp|\.jst|\.rb|\.txt/g;
		var urlType = url.match(urlString);
		
        //if the url is an image then run the follwing code
		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif'){//code to show images

			var imgPreloader = new Image();
			imgPreloader.onload = function(){
				
			// Resizing large images added by Christian Montoya
        //if the image is larger than the page, then they will be resized to fit within the page
			var pagesize = getPageSize();
			var x = pagesize[0] - 150;
			var y = pagesize[1] - 150;
			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;
			if (imageWidth > x) {
				imageHeight = imageHeight * (x / imageWidth); 
				imageWidth = x; 
				if (imageHeight > y) { 
					imageWidth = imageWidth * (y / imageHeight); 
					imageHeight = y; 
				}
			} else if (imageHeight > y) { 
				imageWidth = imageWidth * (y / imageHeight); 
				imageHeight = y; 
				if (imageWidth > x) { 
					imageHeight = imageHeight * (x / imageWidth); 
					imageWidth = x;
				}
			}
			// End Resizing
			
			
            //set the width of the element surrounding the image to be 30px wider and 60px longer than the image
			TB_WIDTH = imageWidth + 30;
			TB_HEIGHT = imageHeight + 60;
            //append the text to the outside element of the image - the image title and the Close command
			$("#TB_window").append("<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>"
								 + "<div id='TB_caption'>"+caption+"</div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton'>close</a></div>"); 
            //if the Close button is clicked, the run the TB_remove function to close the image
			$("#TB_closeWindowButton").click(TB_remove);
            //run the TB_position function
			TB_position();
            //remove the TB_load element
			$("#TB_load").remove();
            //?
			$("#TB_ImageOff").click(TB_remove);
            //?
			$("#TB_window").slideDown("normal");
			}
	  
			imgPreloader.src = url;
		}
		
        //if the url is not an image, then run the following code
		if(urlType=='.htm'||urlType=='.html'||urlType=='.php'||urlType=='.asp'||urlType=='.aspx'||urlType=='.jsp'||urlType=='.jst'||urlType=='.rb'||urlType=='.txt'||urlType=='.cfm'){//code to show html pages
			
            //set the variables required
			var queryString = url.replace(/^[^\?]+\??/,'');
			var params = parseQuery( queryString );
			
            //set the width and height of the TB_WIDTH and TB_HEIGHT elements
			TB_WIDTH = (params['width']*1) + 30;
			TB_HEIGHT = (params['height']*1) + 40;
			ajaxContentW = TB_WIDTH - 30;
			ajaxContentH = TB_HEIGHT - 45;
			$("#TB_window").append("<div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
			$("#TB_closeWindowButton").click(TB_remove);
			$("#TB_ajaxContent").load(url, function(){
			TB_position();
			$("#TB_load").remove();
			$("#TB_window").slideDown("normal");
			});
		}
		
    //catch any errors and display a message
	} catch(e) {
		alert( e );
	}
}

//helper functions below

function TB_remove() {
	$("#TB_window").fadeOut("fast",function()
	    {
	        $('#TB_window,#TB_overlay').remove();
	     });
	$("#TB_load").remove();
	return false;
}

function TB_position() 
{
	var pagesize = getPageSize();
  
  	if (window.innerHeight && window.scrollMaxY) 
  	    {	
		    yScroll = window.innerHeight + window.scrollMaxY;
	    } 
	    else if (document.body.scrollHeight > document.body.offsetHeight)
	    { // all but Explorer Mac
		    yScroll = document.body.scrollHeight;
	    } 
	    else 
	    { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		    yScroll = document.body.offsetHeight;
  	    }
	
	    var arrayPageScroll = getPageScrollTop();
    	
	    $("#TB_window").css(    
	        {
	            width:TB_WIDTH+"px",height:TB_HEIGHT+"px",
	            left: ((pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-TB_HEIGHT)/2))+"px" 
	        });
	    $("#TB_overlay").css("height",yScroll +"px");

}

function parseQuery ( query ) 
{
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}


function getPageScrollTop()
{
	var yScrolltop;
	if (self.pageYOffset) {
		yScrolltop = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScrolltop) 
	return arrayPageScroll;
}

function getPageSize()
{
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}

