function resizeSWF (size_id){
    switch (size_id){
        case 1:
            $("#subheader-swf").css("height", "410px");
            break;
        default:
            if ($("#subheader-swf").hasClass("minified")) {
                $("#subheader-swf").css("height", "320px");
            }
    }
}

function playVideo(){
    closeMap();
    var obj = swfobject.getObjectById("swfheader");
    if (obj) {
        obj.verVideo();
    }
    return false;
}

var mapOpened = false;

function viewMap (city_id, category_id, map_path) {
    var obj = swfobject.getObjectById("swfheader");
    if (obj) {
        try {
            obj.verMapa(city_id, category_id, map_path);
            mapOpened = true;
        } catch (err) {
            //alert(err.description);
        }
    }
    return false;
}

function closeMap () {
    if (mapOpened) {
        var obj = swfobject.getObjectById("swfheader");
        if (obj) {
            try {
                obj.cerrarMapa();
                mapOpened = false;
            } catch (err) {
                //alert(err.description);
            }
        }
    }
    return false;            
}

function slug(str) {
  str = str.replace(/^\s+|\s+$/g, ''); // trim
  
  // remove accents, swap ñ for n, etc
  var from = "ÀÁÄÂÈÉËÊÌÍÏÎÒÓÖÔÙÚÜÛàáäâèéëêìíïîòóöôùúüûÑñÇç·/_,:;";
  var to   = "aaaaeeeeiiiioooouuuuaaaaeeeeiiiioooouuuunncc------";
  for (var i=0, l=from.length ; i<l ; i++) {
    str = str.replace(new RegExp(from[i], "g"), to[i]);
  }

  str = str.replace(/[^a-zA-Z0-9 -]/g, '') // remove invalid chars
    .replace(/\s+/g, '-') // collapse whitespace and replace by -
    .toLowerCase();
  return str;
}

$(function() {
    $("#login-link").click(function() {
        $("#login-form").fadeIn();
        $("#UserUsername").focus();
        return false;
    });
    $("#login-form").click(function(event) {
        event.stopPropagation();
    });
    $("#user-menu-link").click(function() {
        $("#user-menu").css("width", $(this).width()-3);
        $("#user-menu").fadeIn();
        return false;
    });
    $("#language_selected").click(function(event) {
        event.stopPropagation();
        $("#language_selector").fadeIn();
    });
    $("body").click(function() {
        $("#login-form").fadeOut();
        $("#user-menu").fadeOut();
        $("#language_selector").fadeOut();
    });
    $('.add-to-trip-link').live('click', function(e) {
        if( (!$.browser.msie && e.button == 0) || ($.browser.msie) ) {
            $(this).parent().load($(this).attr('href'), {}, function (responseText, textStatus, XMLHttpRequest){
                if (textStatus == 'error') {
                    //TODO
                }
            });
        }
        return false;
    });

    // if the function argument is given to overlay, it is assumed to be the onBeforeLoad event listener 
    window.overlayApi = $('#overlay').overlay({
                            api:true,
                            expose: {
                                color: '#000',
                                opacity: 0.4
                            }
                        }); 

    // define function that opens the overlay 
    window.openOverlay = function (element) {
        var wrap = $('#overlay').find('div.wrap'); 
        wrap.empty();
        $('#overlay-loader').css('display', 'block');
        wrap.load($(element).attr('href'), null, function(){ $('#overlay-loader').css('display', 'none'); $('.contact-link').colorbox({opacity:0.15, width:"440px", height:"345px"});});
        overlayApi.load(); 
    }         

    var minimized_description_size = 46;

    if ($("#main-content div.minimized:first").height() > minimized_description_size) {
        description_height = $("#main-content div.minimized:first").height();
        $("#main-content div.minimized:first").css("height", minimized_description_size);
        $("#main-content div.minimized:first").after('<div id="toggle-description"><a href="#">'+description_show_label+'</a></div>');
        $("#toggle-description a").toggle(
            function () {
                $(this).blur();
                $(this).html(description_hide_label);
                $("#main-content div.minimized:first").animate({height: description_height}, 500);
            },
            function () {
                $(this).blur();
                $(this).html(description_show_label);
                $("#main-content div.minimized:first").animate({height: minimized_description_size}, 500);
            }
        )
    }

    if ($("#main-content div.minimizable:first").height() > minimized_description_size) {
        description_height = $("#main-content div.minimizable:first").height();
        $("#main-content div.minimizable:first").after('<div id="toggle-description"><a href="#">'+description_hide_label+'</a></div>');
        $("#toggle-description a").toggle(
            function () {
                $(this).blur();
                $(this).html(description_show_label);
                $("#main-content div.minimizable:first").animate({height: minimized_description_size}, 500);
            },
            function () {
                $(this).blur();
                $(this).html(description_hide_label);
                $("#main-content div.minimizable:first").css("height", "auto");
            }
        )
    }
    $("#sidebar-a li a, #user-menu li a").each(function(){
        $(this).click( function(event) {
            event.stopPropagation();
        });
    });
    $("#sidebar-a li, #user-menu li").each(function(){
        $(this).click( function() {
            var a = $(this).children("a");
            if(a.click()){
                if (a.attr("href") && !a.hasClass('dont-follow'))
                window.location = a.attr("href");
            }
        });
    });

    $('div.event div.event-contact div.event-links a.book-link').live('click', function(e) {
        if( (!$.browser.msie && e.button == 0) || ($.browser.msie) ) {
            try {
                pageTracker._trackPageview('/booking-link/' + slug($(this).parent().parent().parent().find('div.event-title a').html()));
            } catch(err) {}
        }
    });

    $('div.event-view div.description a.book-link').click( function(e) {
        try {
            pageTracker._trackPageview('/booking-link-ad/' + slug($('#sidebar-a div.logo div.title').html()));
        } catch(err) {}
    });
});

