/*
* some code from Cody Lindley (http://www.codylindley.com)
*/

var collection;
var descCollection;
/*
* Function to show the light box with content
*/
function showLightBox(heading, containerDiv, width, height) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");
		}

		ajaxContentW = width - 30;
		ajaxContentH = height - 45;

		$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+heading+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a> or Esc Key</div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
		$("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
		$("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
		$("#TB_ajaxContent")[0].scrollTop = 0;
		$("#TB_ajaxWindowTitle").html(heading);
		$("#TB_closeWindowButton").click(tb_remove);
		$("#TB_ajaxContent").append($('#' + containerDiv).children());
		$("#TB_window").unload(function () {
				$('#' + containerDiv).append( $("#TB_ajaxContent").children() );
			});
		tb_position(width,height);
		$("#TB_load").remove();
		$("#TB_window").css({display:"block"}); 

		document.onkeyup = function(e){ 	
			if (e == null) {
				keycode = event.keyCode;
			} else {
				keycode = e.which;
			}
			if(keycode == 27){
				tb_remove();
			}	
		};
		
	} catch(e) {
	}
}

/*
* Function to remove tb
*/
function tb_remove() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

/*
* Function to handle position of box
*/
function tb_position(width,height) {
$("#TB_window").css({marginLeft: '-' + parseInt((width / 2),10) + 'px', width: width + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) {
		$("#TB_window").css({marginTop: '-' + parseInt((height / 2),10) + 'px'});
	}
}

/*
* Function to get page Size
*/
function tb_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 = [w,h];
	return arrayPageSize;
}

/*
* Mac and Firefox??
*/
function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

/*
* Show an image
*/
function showImage(heading, image, desc)
{
	var arrPageSize = tb_getPageSize();
	var imgString =  '<div class=logoDescription>'+desc+'</div><br /><img src=' + image.replace('_small','') + ' width=800 height=600 class=logoExample />';
	var width;
	width = (arrPageSize[0] > 800) ? width = 870 : width = 600;
	showImageBox(heading, imgString, width, arrPageSize[1] - 100);
}

/*
* Create an image group
*/
function imageGroup(queryString, heading, div, descs) 
{
	collection = queryString.split('?');
	descCollection = descs.split('?');
	var logos = document.getElementById('logos');	
	var count = 0;
	var newDiv = document.createElement('div');
	newDiv.setAttribute('id',div);
	for (x in collection)
	{
		var newImage = document.createElement('img');
		newImage.setAttribute('id','exampleImage' + count);
		newImage.setAttribute('src',collection[x]);
		newImage.setAttribute('class','galleryImage');
		newImage.setAttribute('className','galleryImage');
		newImage.setAttribute('alt', descCollection[count]);
		newDiv.appendChild(newImage);
		logos.appendChild(newDiv);
		newImage.onmouseover = function(){this.className = "galleryImageHover";}
		newImage.onmouseout = function(){this.className = "galleryImage";}
		newImage.onclick = function(){showImage(heading, this.getAttribute('src'), this.getAttribute('alt'))}
		count++;
	}
	document.getElementById(div).style.display = 'none';
}

/*
* shows the image box
*/
function showImageBox(heading, text, width, height)
{
	var theBoxContent = document.getElementById('theBoxContent');
	theBoxContent.innerHTML = '<br />' + text;
	showLightBox(heading, 'theBox', width, height);
}

/*
* shows the box
*/
function showBox(heading, text)
{
	var theBoxContent = document.getElementById('theBoxContent');
	var arrPageSize = tb_getPageSize();
	theBoxContent.innerHTML = text;
	showLightBox(heading, 'theBox',  625, arrPageSize[1] - 100);
}

/*
* toggles a div
*/
function toggleDiv(div,labelToUpdate)
{
	if(document.getElementById(div).style.display == 'none')
	{
		document.getElementById(labelToUpdate).innerHTML = '[-]';
		document.getElementById(div).style.display='block';
	}
	else
	{
		document.getElementById(labelToUpdate).innerHTML = '[+]';
		document.getElementById(div).style.display='none';
	}
}
