$(document).ready(function() {

	

	//ANY TIME YOU HIT A TRIGER SHOW THE WINDOW
	$('.zl_window_trigger').click(function() {
		var id = $(this).attr("id");
		zl_window(id);
	});
	

	//*******************************************************************************************************************
	//THIS FUNCTION WILL SHOW THE ZL DIALOG WINDOW
	//*******************************************************************************************************************
	function zl_window(item) {
	
	
		//OFFSET TO RAISE THE HEIGHT OF THE WINDOW 
		var offset = 0;
	
		//LETS HIDE AND ANO OTHER ZL WINDOW INSTANCES JUST TO BE SAFE
		hide_zl_window();
		
		//LETS GET THE ITEM OBJECT
		var zl_window = $(".zl_window").filter("div[title='" + item + "']");

		//CALCULATE THE PROPER INITIAL POSITION OF THE WINDOW AND ADJUST ACCORDINGLY
		zl_position(zl_window);
		
		//SHOW THE WINDOW
		show_zl_window(zl_window);
		
		//CREATE EVENT TO HIDE WINDOW ON HIT OF ESC
		$(window).bind('keydown', function(e) {	 if (e == null) {  keycode = event.keyCode; } else { keycode = e.which; } if(keycode == 27){ hide_zl_window(); } });
		
		//IF SOMEONE CLICKS OFF THE ELEMENT THEN CLOSE IT
		$(document).bind('click', function(e) { var clicked = $(e.target); if (!(clicked.is("zl_window div[title='" + item + "']") || clicked.parents().is("div[title='" + item + "']") || clicked.is('.zl_window_trigger') || clicked.parents().is('.zl_window_trigger')) ) {  hide_zl_window(); } });
		
		//IF THE WINDOW GETS RESIZED WHEN THE DIALOUG IS UP
		$(window).bind('resize', function (e) { zl_position(zl_window); });
		
		//CREATE A CLOSE BUTTON
		$('.close_zl_window').bind('click', hide_zl_window);
			
		//HIDE THE WINDOW UPON REQUEST
		function hide_zl_window() {
			$('.zl_window').fadeOut('fast');
			$('#zl_overlay').fadeOut('fast');
			$(window).unbind('keydown');
			$(document).unbind('click');
			$(window).unbind('resize');
			$('.products_window_right').unbind('click');
			
			//THIS SCRIPT ONLY
			$('.products_window_error').html("").hide();
			
		}
		
		//ADJUSTS THE POSITION
		function zl_position(zl_window) {
			var height = zl_window.height();
			var width = zl_window.width();
			var browser_height = $(window).height();
			var browser_width = $(window).width();
			var zl_top = ((browser_height - height) / 2) - offset;
			var zl_left = (browser_width - width) / 2;
			zl_window.css({ top: + zl_top, left: + zl_left });
		}
		
		//SHOWS THE WINDOW
		function show_zl_window(zl_window) {
			$('#zl_overlay').fadeIn('normal');
			zl_window.fadeIn('normal');
		}
		
	}


	// If checkbox is clicked
	$('.lightbox_checkbox').click(function(){
		
		// Save the old src
		var old_src = $(this).attr('src');
		
		// Split the old src by an _ so we have the beginning of the ur
		// and the end of the image name (checked.gif or unchecked.gif)
		var split 	= old_src.split('_');
		
		// Split the end of the image name with a period so we have
		// checked or unchecked and gif
		var split2	= split[1].split('.');
		
		// If this box was checked, uncheck it
		if(split2[0] == "checked") {
			var switchit = "unchecked";
		}
		// And vise versa
		else if(split2[0] == "unchecked") {
			var switchit = "checked";
		}
		
		// Join together the beginning url,
		// the underscore we lost in the split,
		// the new file name (checked or unchecked),
		// thhe period we lost in the split,
		// and the file extension (should be gif)
		var new_src = split[0]+'_'+switchit+'.'+split2[1];
		
		// Send our new src to the img tag :)
		$(this).attr('src',new_src);
		
		// Figure out which checkbox this is
		var index = $('.lightbox_checkbox').index(this);
		
		// Save the old href
		var old_href = $('#appointment').attr('href');
		
		// If it is becoming checked
		// we add the id to the end of the url with a trailing pipe
		if(switchit == "checked") {
			var new_href = old_href + index + 'x';
		}
		// If it is becoming unchecked,
		// we remove the id and it's trailing pipe
		else if(switchit == "unchecked") {
			var new_href = old_href.replace(index + 'x', '');
		}
		
		// Apply our new href
		$('#appointment').attr('href',new_href)
		
	});

			
});
