(function(){
    var preloads = [];
    var mapImage = false;
    jQuery.fn.extend({
        popup:function(){
            return $(this).each(function(){
                $(this).click(function(){
                    var size = $(this).attr('rel')?$(this).attr('rel').split('x'):[200,200];
                    window.open($(this).attr('href'), null, 'width='+size[0]+',height='+size[1]+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
                    return false;
                });
            });
        },
        floorMapLocator:function(){
            if (!mapImage) {
                mapImage = $('.map-image');
                $('#floor-map').css({
                    background: 'url(' + mapImage.attr('src') + ') no-repeat left top'
                }).width(mapImage.width());
                var baseMap = mapImage.attr('src');
            }
            var currentShop = false;
            return $(this).each(function(){
                var tId = $(this).attr('href').substr(1);
                var altMap = "/images/floors/"+$(this).attr('rel');
                var img = new Image();
                img.src = altMap;
                preloads.push(img);
                $(this).hoverIntent(
                    function(){
                        if (!currentShop || currentShop.attr('id') != tId) {
                            mapImage.stop().attr('src', altMap).fadeTo(0, 0).fadeTo(300, 1);
                            if (currentShop) currentShop.hide();
                            currentShop = $('#' + tId);
                            currentShop.show().fadeTo(0, 0).fadeTo(300, 1);
                        }
                    },
                    function(){
                    }
                ).click(function(){
                    location.href = $('#'+tId+" a").attr('href');
                    return false
                });
            });
        },
		flip:function(){
			return $(this).each(function(){
				$(this).mouseover(function(){
					var src = $(this).children('img').attr('src').replace(/thumbnails\//, '');
					$('.main img').attr('src', src);
					return false;
				});
			});
		}
    });
    jQuery(function($){
        $('a.print-button').popup();
        if ($('body').is('.certe_floor_guide')) {
            var setCatMode = function(){
                $('#floor-guide-heading-box .floor-search').show();
                $('#floor-guide-heading-box .category-search').hide();
                $('#floored-wrapper').hide();
                $('#categorized-wrapper').show();
                $('#category-selectors a').click(function(){
                    var target = $(this).attr('href');
                    $('.categorized-shop-list').hide().filter(target).fadeIn();
                    return false;
                });
            };
            if (!location.hash) {
                $('#floor-guide-heading-box .floor-search').hide();
                $('#floor-guide-heading-box .category-search').show().click(setCatMode);
                $('.floor-base').children('.floor-image').fadeTo(0, 0.5).end().hover(function(){
                    $(this).addClass('mouseOn').children('.floor-image').fadeTo(200, 1);
                }, function(){
                    $(this).removeClass('mouseOn').children('.floor-image').fadeTo(200, 0.5);
                }).click(function(){
                    location.href="/floors/"+$(this).attr('id')+".html";
                });
            }
            else {
                setCatMode();
                $(location.hash).show();
            }
        }
        if ($('body').is('.certe_floor_map')) {
            $('area, #categorized-shoplist a').floorMapLocator();
        }
        if ($('body').is('.certe_shop_page')) {
            $('.thumbnails').flip();
        }
    });
})();
