$(document).ready(function(){
	/*------HT Button Fading Function-------*/
		$("#btn_page, #btn_page2").hover(function() { //On hover...
	
			var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
			//Set a background image(thumbOver) on the <a> tag - Set position to bottom
			
			$(this).css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
			//Animate the image to 0 opacity (fade it out)
			$(this).find("img").stop().fadeTo('normal', 0 , function() {
				$(this).hide() //Hide the image after fade
			});
		} , function() { //on hover out...
			//Fade the image to full opacity 
			$(this).find("img").stop().fadeTo('normal', 1).show();
		});
		
		/*--HT Lightbox--*/
		//When you click on a link with class of poplight and the href starts with a # 
		$('a.poplight[href^=#]').click(function() {
			fix_flash();  /*HT edited - To over write flash content */
				
			//Fade in Background
			$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
			$('#fade').css({'filter' : 'alpha(opacity=80)'}).show(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 									
			
			var pageURL= $(this).attr('rel'); //Get Iframe page URL
			var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
			var urlType = pageURL.toLowerCase().match(urlString);
			var imgCaption = $(this).attr('title');
			
			var popURL = $(this).attr('href'); //Get Popup href to define size

					
			if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){
				//<a href="#w=600&h=600" class="poplight" rel="images/background_06.jpg" title="asa 1">
				$('body').append('<div id="tb_window"></div>');
				var container = $('div#tb_window').append('<img src="' + pageURL +' " />');
				$('div#tb_window').append('<div id="img_caption"> '+ imgCaption + '</div>');
				$('div#tb_window').css({ 'width': Number( popWidth ),  'height': Number( popHeight )});
				
			}else{
				//<a href="#index2.html?w=500&h=600" rel="popup1" class="poplight" >Test URL</a> 
				//Pull Query & Variables from href URL
				var query = popURL.split('#');
				var dim= query[1].split('&');//split the query into two parts: dim[0] is before & dim[1] after &
				var popWidth = dim[0].split('=')[1]; //Gets the first query string value
				var popHeight = dim[1].split('=')[1]; //Gets the first query string value of second =
			
				if(popWidth == null || popHeight == null){
					var de = document.documentElement;
					//alert("1 - Width: " + popWidth + " height: " + popHeight + " || URL: " + pageURL );
					var pagesize = tb_getPageSize();
					var popWidth = pagesize[0] - 250;
					var popHeight = pagesize[1]  - 200;
				}			
				//alert("2 - Width: " + popWidth + " height: " + popHeight + " || URL : " + pageURL );	
									
				//Fade in Content Window
				$('body').append('<div id="tb_window"></div>');
				$('#tb_window').css({ 'width': Number( popWidth ),  'height': Number( popHeight )}).hide();;
				$('#tb_window').append('<iframe src=" '+ pageURL  +' " width="100%" height="'+ popHeight +'" scrolling="auto" frameborder="0" ></iframe>');
			}//end if
			
			$('#tb_window').prepend('<a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" border="0"/></a>').fadeTo(100, 1);
			tb_position();
			
			return false;
		});
});

//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	tb_remove();
});

document.onkeydown = function(e){ 	
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	if(keycode == 27){ // close
		tb_remove();
	} 
};
/*---------------*/

/*Lightbox extra function*/
function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth;
	var h =  window.innerHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function fix_flash() {
	// loop through every embed tag on the site
	var embeds = document.getElementsByTagName('embed');
	for (i = 0; i < embeds.length; i++) {
		embed = embeds[i];
		var new_embed;
		// everything but Firefox & Konqueror
		if (embed.outerHTML) {
			var html = embed.outerHTML;
			// replace an existing wmode parameter
			if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
				new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
			// add a new wmode parameter
			else
				new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
			// replace the old embed object with the fixed version
			embed.insertAdjacentHTML('beforeBegin', new_embed);
			embed.parentNode.removeChild(embed);
		} else {
			// cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
			new_embed = embed.cloneNode(true);
			if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
				new_embed.setAttribute('wmode', 'transparent');
			embed.parentNode.replaceChild(new_embed, embed);
		}
	}
	// loop through every object tag on the site
	var objects = document.getElementsByTagName('object');
	for (i = 0; i < objects.length; i++) {
		object = objects[i];
		var new_object;
		// object is an IE specific tag so we can use outerHTML here
		if (object.outerHTML) {
			var html = object.outerHTML;
			// replace an existing wmode parameter
			if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
				new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
			// add a new wmode parameter
			else
				new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
			// loop through each of the param tags
			var children = object.childNodes;
			for (j = 0; j < children.length; j++) {
				try {
					if (children[j] != null) {
						var theName = children[j].getAttribute('name');
						if (theName != null && theName.match(/flashvars/i)) {
							new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
						}
					}
				}
				catch (err) {
				}
			}
			// replace the old embed object with the fixed versiony
			object.insertAdjacentHTML('beforeBegin', new_object);
			object.parentNode.removeChild(object);
		}
	}
}

function tb_position() {
		var popMargTop = ($('#tb_window').height() + 80) / 2;
		var popMargLeft = ($('#tb_window').width() + 80) / 2;
		
		//Apply Margin to Popup
		$('#tb_window').css({
			'margin-top' : -popMargTop + 20,
			'margin-left' : -popMargLeft + 30
		});	
		alert("2 - TOP: " + popMargTop + " left: " + popMargLeft + " || URL : " + pageURL );
		return false;
}

function tb_remove(){
		$('#fade , .tb_window, #tb_window').fadeTo('normal', 0, function() {
			$('#fade, a.close, .tb_window, #tb_window').remove();  //fade them both out
		});
		return false;
}	
/**/
