var gmap;
$(document).ready(function() {
    if($('select#topic_id').length) {
        $('#input_new_topic').addClass('hidden');
        $('select#topic_id').bind('change', function() {
            if($(this).val() == '-1' ) {
                $('#input_new_topic').removeClass('hidden');
            } else {
                $('#input_new_topic').addClass('hidden');
            }
        });
    }

//    $(".intro_mask_more").click(function(e) {
//        $(this).addClass('hidden');
//        $('#intro_mask_less').removeClass('hidden');
//        $('#intro_mask').removeClass('small');
//    });
//    $(".intro_mask_less").click(function(e) {
//        $(this).addClass('hidden');
//        $('#intro_mask_more').removeClass('hidden');
//        $('#intro_mask').addClass('small');
//    });
    $('.intro_mask_more').each(function() {
        $(this).bind('click', function() {
            $(this).addClass('hidden');
            $(this).siblings('.intro_mask_less').removeClass('hidden');
            $(this).siblings('.intro_mask').removeClass('small');
        });
    })
    $('.intro_mask_less').each(function() {
        $(this).bind('click', function() {
            $(this).addClass('hidden');
            $(this).siblings('.intro_mask_more').removeClass('hidden');
            $(this).siblings('.intro_mask').addClass('small');
        });
    })
    
    $('#submenu-body a.submenu-link').each(function() {
        $(this).bind('click', function() {
            $('ul#js-container li.second-column-item').each(function() {
                $(this).hide(); 
            });
            $('ul#menu-js-container li.menu-listing-item').each(function() {
                $(this).hide(); 
            });
            $('li#menu-listing-' + $(this).attr('alt')).show();
            $('li#second-column-' + $(this).attr('alt')).show();
            return false;
        });
    });
    
    FB.init({appId: '158175370905162', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('auth.login', function(response) {
        window.location.reload();
    });
    
    if($('#content-panel img.hide-overlay')) { 
        var $obj = $('#content-panel img.hide-overlay');
        $obj.attr('width', $obj.parent().width());
        $obj.attr('height', $obj.parent().height());
    }
    
    $('form').each(function() {
        $(this).find('label').each(function() {
            $(this).html($(this).html().replace('*', '<span class="required">*</span>'));    
        });
    });
    
//    $a = $('#second-column');
//    $b = $('#third-column-background');
//    if($a && $b) {
//        if($a.height() - 56 >= $b.height()) {
//            $b.height($a.height() - 56);
//        } else {
//            $a.height($b.height() + 56);
//        }
//    }
    
    if($('#gmaps').length) {
        var options = {
            zoom: 1,
            center: new google.maps.LatLng(0,0),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        gmap = new google.maps.Map(document.getElementById("gmaps"), options);
    }
    if($('#menu_body_container .menu_continent').length) {
        $('#menu_body_container .menu_continent').click(function(e) {
            if($(e.target).is('a')) {
                return;
            }
            $('#menu_body_container .menu_continent .menu_continent_children').slideUp(function() {
            });
            $(this).children('.menu_continent_children').slideDown(function() {
                realign($(this).children('.menu_left'), $(this).children('.menu_right'), 'height');
            });
        });
        
    }
    if($('#menu_body_container .menu_continent')) {
        $('.menu_country').hover(
            function(){
                $(this).children('.menu_arrow').addClass('hover');
            }, function() {
                $(this).children('.menu_arrow').removeClass('hover');
            });
    }
    // add a "rel" attrib if Opera 7+
    if(window.opera) {
        if ($("a.jqbookmark").attr("rel") != "") { // don't overwrite the rel attrib if already set
            $("a.jqbookmark").attr("rel","sidebar");
        }
    }

    $("a.jqbookmark").click(function(event){
        event.preventDefault(); // prevent the anchor tag from sending the user off to the link
        var url = this.href;
        var title = this.title;

        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url,"");
        } else if( window.external ) { // IE Favorite
            window.external.AddFavorite( url, title);
        } else if(window.opera) { // Opera 7+
            return false; // do nothing - the rel="sidebar" should do the trick
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
             alert('Unfortunately, this browser does not support the requested action,'
             + ' please bookmark this page manually.');
        }
    });
    $('#QuestionsFilterForm input').click(function() {
        var sortKey = $('#sort').val();
        var filterKey = $('#QuestionsFilterForm :checked').val();
        var url = '/profile/questions?sort=' + sortKey + '&filter=' + filterKey;
        document.location.href=url;
        return false;
    });

    $('#filterSubmit').hide();
});
function addMarker(name, count, url, lat, lng)
{
    var contentStr =   '<h1 id="location_name">' + name + '</h1>' +
                    '<p class="small">Great! ' + count + ' local(s) found.</p>' +
                    '<p class="small"><a href="/trips/'+url+'">Guides</a> | <a href="/ask/'+url+'?sort=newest">Forum</a> </p>'; // | <a href="/ask/'+url+'?sort=newest">Tips</a>
    var infowindow = new google.maps.InfoWindow({content: contentStr});
    var latlng = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({
         position: latlng,
         map: gmap,
         noWrap: true
    });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(gmap,marker);
    });
}
function checkTerms()
{
    if($('#agree_terms').is(':checked')) {
        return true;
    }
    $('#terms-error').html('You have to agree to terms and conditions');
    return false;
}
function realign(a, b, param) {
    if(parseInt(a.css(param)) > parseInt(b.css(param))) {
        b.css(param, a.css(param));
    } else if (parseInt(b.css(param)) > parseInt(a.css(param))) {
        a.css(param, b.css(param));
    }
}
