/**
 *
 * Written specifically for providing a button on the community map that, when
 * clicked, opens up the community map in a new window
 *
 * Note that this prototype requires jQuery.
 *
 * @ author Charles Bunata
 *
 */



DRHorton = DRHorton || {};

DRHorton.community = DRHorton.community || {};

DRHorton.community.mapHelper = DRHorton.community.mapHelper || {};

;(function($){
    
    // PRIVATE
    var _vistarusHref = '', _checkCount = 10,
    
        _onFancyFrameLoad = function(){
                        
            DRHorton.utils.testConditionOverTime({
                expression: "$('a[href*=vistarus]', $('#fancybox-frame').contents()).length > 0",
                interval: 250,
                timeLimit: 10000,
                success: function(){
                    
                    
                    $('#fancybox-outer').append("<div id='cm_open_button'>OPEN IN SEPARATE WINDOW</div>");
                    _vistarusHref = $('a[href*=vistarus]', $('#fancybox-frame').contents()).attr('href');
                
                    $('#cm_open_button').css({"display":"none"})
                
                    $('#cm_open_button').bind('mouseup', function(){
                        window.open(_vistarusHref);
                        $.fancybox.close();
                    });
                    
                    $('#fancybox-outer').height($('#fancybox-outer').height() + 30);
                    
                    $('a[href*=vistarus]', $('#fancybox-frame').contents()).bind("click", function(){
                        
                        $('#cm_open_button').css({
                            "border":"3px double #ffffff",
                            "cursor": "hand",
                            "cursor": "pointer",
                            "line-height":"20px",
                            "text-align":"center",
                            "background-color":"#002B56",
                            "color":"white",
                            "height":"20px",
                            "width":"180px",
                            "font-size":"11px",
                            "text-transform":"uppercase",
                            display: "block",
                            position: "absolute",
                            top: ($('#fancybox-content').position().top + $('#fancybox-content').height() - 10).toString() + "px",
                            left: ($('#fancybox-content').position().left + 10).toString() + "px",
                            "z-index": 2000
                        });
                    });
                }
            })
            
            
        },
    
        _init = function(){

            // remove any leftover buttons
            $('#cm_open_button').remove();
            
            var checkCount = _checkCount;
        
            if(arguments.length)
                checkCount = arguments[0];
            
            if($('#fancybox-frame').length == 0){
                if(checkCount == 0){
                    return;
                }
                else {
                    setTimeout(function(){
                        
                        _init(--checkCount);
                    }, 250)
                    return;
                }
            }
            else {          
                
                // if we already have our frame with content
                if($('#fancybox-frame').contents().length){
                    _onFancyFrameLoad();
                }
                else { // otherwise, wait for it to load
                    $('#fancybox-frame').bind('load', _onFancyFrameLoad)    
                }
                
            }
        }
    ;


    // PUBLIC 
    DRHorton.community.mapHelper.init = function(){
        _init();
    }        

})(jQuery);


$(function(){
    // if there's a community map
    if($("a[href*=Community-Map][href$=.aspx]").length){
        // bind to its click
        $("a[href*=Community-Map][href$=.aspx]").bind("click", DRHorton.community.mapHelper.init);
    }    
    
})
