function gotoAnchor(anchor) {
    location.href="#" + anchor;
}
function resizePopup()
{
    $('#popup_modal').css('height', parseInt(($(window).height()*0.8))+'px');    
}
$(document).ready(function() {
   
    $('#download-link').click(function()
    {
        $('#popup_modal').html($('#download-form').html());
        hbc_popup('popup_modal', '#?w=465');
        return false;
    });
      
    $('#privacy').click(function()
    {
        $('#popup_modal').html($('#privacy-content').html());
        hbc_popup('popup_modal', '#?w=910&h=450');
        return false;
    });
    $('#terms').click(function()
    {
        $('#popup_modal').html($('#terms-content').html());
        hbc_popup('popup_modal', '#?w=910&h=450');
        return false;
    });
    $('a[rel=video]').each(function()
    {
        $(this).click(function(){
            var scr = '<iframe title="YouTube video player" width="640" height="390" src="'+$(this).attr('href')+'?rel=0&amp;autoplay=1&amp;hd=1" frameborder="0" allowfullscreen></iframe>';
            $('#popup_modal').html(scr);
            hbc_popup('popup_modal', '#?w=640&h=390');
            return false;
        });
    });
    $('a[rel=gallery]').each(function()
    {
        var popURL = '#?w=790&h=455';
        $(this).click(function()
        {            
            var spl = $(this).attr('href').toString().split('#')[1];            
            var _percent = 1;
            
            var _w = parseInt(spl.split('&')[0].toString().split('=')[1]);
            var _h = parseInt(spl.split('&')[1].toString().split('=')[1]);
            popURL = '#?w='+_w+'&h='+_h;

            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
            $('#popup_modal').html('');
        });
        return false;
    });
    var _ar = document.location.toString().split('#');
    var _str = "";
    if(_ar.length > 1)
        _str = _ar[1].toString();
    if(_str.toLowerCase() == "download")
    {
        $("#download-link").trigger("click");
        /*if($("#download-link"))
        {

            /*$('#popup_modal').html($('#download-form').html());
            hbc_popup('popup_modal', '#?w=465');
            return false;
        }*/
    }

});

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

    
    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({
        'width': Number( popWidth )
    }).prepend('<a href="#" class="close" style="position: absolute; float: right; right: 8px; top: -8px;" ><img id="btn_close_popup" src="/images/buttons/close.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',
        'display' : 'block'
    });
    
   

    //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
}
