jQuery.fn.alcoBox = function (options) {
    
	var options = options || {}; // options object for extending
    
	if ($('#zoom').length == 0) {
        //var ext = $.browser.msie ? 'gif' : 'png';
        var html = '<div id="overlay" style="display:none; background:#000; width:100%; position:absolute; top:0; left:0; bottom:0; right:0; z-index:1001; height:100%"></div> \
				<div id="zoom" style="display:none; z-index:1101; padding-bottom:20px"> \
                  <div id="zoom_content" style="border:2px solid #666; border-radius: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,.9); -moz-box-shadow:0 0 10px rgba(0,0,0,.9); -webkit-box-shadow:0 0 10px rgba(0,0,0,.9); text-align:left; margin:10px; background:#fff; height:100%" > \
				    <div id="zoom_inner" style="height:100%"></div>\
                  </div> \
                  <a href="#" title="Close" id="zoom_close" style="position:absolute; top:0; right:0;"> \
                    <img src="images/closebox.png" alt="Close" style="border:none; margin:0; padding:0;" /> \
                  </a> \
                </div>';
        $('body').append(html);
        $('html').click(function (e) {
            if ($(e.target).parents('#zoom:visible').length == 0) hide();
        });
        $(document).keyup(function (event) {
            if (event.keyCode == 27 && $('#zoom:visible, #overlay:visible').length > 0) hide();
        });
        $('#zoom_close').click(hide);
    }
	
    var zoom = $('#zoom');
    var zoom_close = $('#zoom_close');
    var zoom_content = $('#zoom_content');
    var zoom_holder = $('#zoom_inner');
	
	
    // Assign event handlers
    this.each(function (i) {
        $($(this).attr('href')).hide();
        if ($(this).is('area')) {
            $(this).hover(preview, previewOut).mousemove(function (e) {
                $("#preview").css("top", (e.pageY - 150) + "px").css("left", (e.pageX - ($('#preview').width() / 2)) + "px");
            });
        }
        $(this).click(show);
    });
    return this;
	
	// preview ajax call
	var pxhr
	
    function preview(e) {
		var pLoad = $(this).attr('href').replace('#', '');
		
        $("body").append("<div id='preview' style='overflow:hidden; background: #fff url(images/ajax.gif) center no-repeat; z-index: 1000; text-align:center; display: none; position: absolute; border:2px solid #666; border-radius: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; width:100px; height:87px; padding-top:13px; box-shadow: 0 0 10px rgba(0,0,0,.9); -moz-box-shadow:0 0 10px rgba(0,0,0,.9); -webkit-box-shadow:0 0 10px rgba(0,0,0,.9);'>&nbsp;</div>");
        $("#preview").css("top", (e.pageY - 150) + "px").css("left", (e.pageX - 50) + "px");
        $("#preview").fadeIn(300);
		
        pxhr = $.ajax({
			xhr: function() {
        	if ($.browser.msie && $.browser.version.substr(0,1) <= 7)
            	return new ActiveXObject("Microsoft.XMLHTTP");
        	else
            	return new XMLHttpRequest();
    		},
            url: 'meet/' + pLoad + '.html',
            cache: false,
            dataType: "html",
            success: function (data) {
                $(data).find("img[src*=thumbs]").attr({'width':'75', 'height':'75'}).css('padding-right','5px').hide().appendTo('#preview');
				var imagesCount = $('#preview').find('img').length;
				var imagesLoaded = 0;
				$('#preview').find('img').load(function () {
					++imagesLoaded;
					if (imagesLoaded >= imagesCount) {
						if (imagesCount > 1) {
							$('#preview')
								.css('background', '#fff')
								.animate({width: (25 + (80*imagesCount - 5)) + 'px', left: e.pageX - (13 + (80*imagesCount -5)/2) + 'px' }, 50, function () { 
									$('#preview img').fadeIn('fast') 
							});
						} else {
							$('#preview').css('background', '#fff');
							$('#preview img').fadeIn('fast');
						}
					}
				});
			}
		});
    }
	
	
    function previewOut() {
       $("#preview").remove();
	   pxhr.abort();

    }
	
	
    function show(e) {
        
        var pLoad = $(this).attr('href').replace('#', '');
        var zoom_width = 600;
        var zoom_height = 400;
        var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
        var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
        var x = window.pageXOffset || (window.document.documentElement.scrollLeft || window.document.body.scrollLeft);
        var y = window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop);
        var window_size = {
            'width': width,
            'height': height,
            'x': x,
            'y': y
        }
        var width = (zoom_width) + 60;
        var height = (zoom_height) + 60;
        var d = window_size;
        // ensure that newTop is at least 0 so it doesn't hide close button
        var newTop = Math.max((d.height / 2) - (height / 2) + y, 0);
        var newLeft = (d.width / 2) - (width / 2);
        var curTop = e.pageY;
        var curLeft = e.pageX;
		
        zoom_close.attr('curTop', curTop);
        zoom_close.attr('curLeft', curLeft);
        $('#overlay').hide().css({
            height: ($(document).height() + 60)
        });
		
        $('#zoom').hide().css({
            position: 'absolute',
            top: curTop + 'px',
            left: curLeft + 'px',
            width: '1px',
            height: '1px'
        });
		
        zoom_close.hide();
        zoom_holder.html('');
		
        $('#overlay').animate({
            opacity: .6
        }, 0).show(0, function () {
            $('#zoom').animate({
                top: newTop + 'px',
                left: newLeft + 'px',
                opacity: "show",
                width: width,
                height: height
            }, 500, function () {
                zoom_content.css('background', '#fff url(images/ajax.gif) center no-repeat');
                zoom_close.show();
                $.ajax({
                    url: 'meet/' + pLoad + '.html',
                    cache: false,
                    dataType: "html",
                    success: function (data) {
                        zoom_holder.hide();
                        $(data).appendTo(zoom_holder);
                        iniload();
                    }
                });
                $('a.dm').live('click', function () {
                    var state = $(this).parents('div.state').css('background-image');
                    if (state != undefined) state = state.split('/').pop().substr(0, 2);
                    //alert(state);
                    var nlink = $(this).attr('href').replace(window.location, '').replace('#', '');
                    zoom_content.css('background', '#fff url(images/ajax.gif) center no-repeat');
                    zoom_holder.html('');
                    zoom_close.show();
                    $.ajax({
                        url: 'meet/' + nlink + '.html',
                        cache: false,
                        dataType: "html",
                        success: function (data) {
                            zoom_holder.hide();
                            $(data).appendTo(zoom_holder);
                            iniload();
                            $('<a class="dm" style="position:absolute; top:30px; right: 40px;" href="#' + state + '">Back to State</a>').insertBefore('#zoom_inner h4');
                        }
                    });
                    return false;
                });
            });
        })
        return false;
    }
	
    function iniload() {
        // image slider
        var assoc = {
            lastli: $("#associate li:first-child"),
            iSize: 70,
            reSize: 200,
            leng: $('#associate').find('li').length,
            bg: $("#associate li:last-child img").attr('src')
        };
        assoc.iSize = Math.floor((392 / (assoc.leng - 1)) - 5);
        $('#associate li').width(assoc.iSize).fadeTo(0, .6);
        $(assoc.lastli).width(200).fadeTo(0, 1);
        $("#associate li").hover(function () {
            $(assoc.lastli).stop().fadeTo(0, .6).animate({
                width: assoc.iSize
            }, 150);
            $(this).stop().fadeTo(0, 1).animate({
                width: assoc.reSize
            }, 150);
            assoc.lastli = this;
        });
        var imagesCount = zoom_holder.find('img').length;
        var imagesLoaded = 0;
        zoom_holder.find('img').load(function () {
            ++imagesLoaded;
            if (imagesLoaded >= imagesCount) {
                zoom_holder.fadeIn('fast');
                zoom_content.css('background', '#fff');
            }
        });
        // set time out incase preloaded
        var timeout = setTimeout(function () { zoom_holder.fadeIn('fast'); zoom_content.css('background', '#fff'); }, 5000);
    }
	
    function hide() {
        $('#zoom').unbind('click');
        zoom_holder.html('');
        zoom_close.hide();
        $('#overlay').hide();
        $('#zoom').animate({
            top: zoom_close.attr('curTop') + 'px',
            left: zoom_close.attr('curLeft') + 'px',
            opacity: "hide",
            width: '1px',
            height: '1px'
        }, 500, null, function () {}); // Call back once hidden
        return false;
    }
}
