$(document).ready(function() {
    $('a[rel=gallery]').each(function()
    {
        var popURL = '#?w=790&h=455';
        $(this).click(function()
        {
            var _percent = 1;
                var _w = 790
                var _h = 455;

                var _width = parseInt(_w*_percent);
                var _height = parseInt(_h*_percent);

                var _sW = $(window).width() * _percent;
                
                if(_w > _sW)
                {
                    var _p = ((_w - _sW)*100)/_w;
                    _width = _w - parseInt(_w * (_p/100));
                    _height = _h - parseInt(_h*(_p/100));

                    popURL = '#?w=' + _width + '&h=' +_height;

                    _width = parseInt(_width * _percent);
                    _height = parseInt(_height * _percent);

                }
                var _content = '<img width="' + _width+'px" height="'+_height+'px" src="'+$(this).attr('href') + '" />';
                $('#popup_modal').html(_content);
            
            hbc_popup('popup_modal', popURL, true, _percent);
            return false;
            });
    });
    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();  //fade them both out
        });
        return false;
    });

});
function hbc_privacy()
{
    $('#popup_modal').html($('#terms-conditions-html').html());
    var popID = 'popup_modal'; //Get Popup Name
    var popURL = '#?w=500'; //Get Popup href to define size
    hbc_popup(popID, popURL);
}
function hbc_popup(popID, popURL, unfloat, _percent, video)
{
    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    if(unfloat && video == null)
    {
        popWidth = popWidth*_percent;
        var popHeight = dim[1].split('=')[1];
        $('#' + popID).css({'height' : Number(parseInt(popHeight)*_percent-1) });
    }
    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({
        'width': Number( popWidth )
    }).prepend('<a href="#" class="close" ><img id="btn_close_popup" src="/images/close_pop.png" border="0" class="btn_close" title="Close Window" alt="Close" /></a>');
    if(video)
        $('#' + popID).css({ 'height' : Number(dim[1].split('=')[1])});
    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft,
        'position' : 'fixed',
        'top' : '50%',
        'padding' : '20px'
    });
    $('#btn_close_popup').css({
        'margin-top' : '-40px',
        'margin-right' : '-40px'
    });
    if(unfloat && video == null)
    {
        $('#' + popID).css({
            'margin-top' : 0,
            'margin-left' : -popMargLeft,
            'position' : 'absolute',
            'top' : ($(window).scrollTop() + 100) + 'px',
            'padding' : '0px'
            });
        $('#btn_close_popup').css({
            'margin-top' : '-25px',
            'margin-right' : '-20px'
        });
    }

    //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)'
    }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
}
