$(document).ready(function() {
    // Ahora el menu
    $.get('php/helper_menu.php', function(data) {
        $('div.menu').html(data);
        $("ul.sf-menu a").click(function(){
            var param=$(this).attr("rel");
            // Solo lanzo el click si hay rel
            if (!param==''){
                // Si tiene pipes es una pagina con parametros
                if (param.indexOf('|')>-1){
                    param=param.split("|");
                    var qrystr="id="+param[0]+"&animal="+param[1]+"&estado="+param[2]+"&urgente="+param[3];
                    mainAjax("php/helper_contacto_pequeno.php",qrystr);
                }
                // Si no es pagina a secas
                else {
                    if (param=='void') {
                        return;
                    }
                    mainAjax('php/'+param+'.php');
                }
            }
            else {
                mainAjax("notfound.html");
            }
            breadCrumb(this);
        //return false;
        });
        $("ul.sf-menu").supersubs({
            minWidth:    12,   // minimum width of sub-menus in em units
            maxWidth:    400,   // maximum width of sub-menus in em units
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over
        // due to slight rounding differences and font-family
        }).superfish({
            animation:   {
                opacity:'show',
                height:'show'
            }
        });  // call supersubs first, then superfish, so that subs are
    // not display:none when measuring. Call before initialising
    // containing tabs for same reason.
    });

    var url="php/helper_contacto_pequeno.php";
    mainAjax(url);
//$('a.thickbox').thickbox({
//            macFFBgHack: 'style/macFFBgHack.png',
//            loadingImage: 'style/loadingAnimation.gif',
//            width: 600
//        });

});


function breadCrumb(obj){
    var title="APATA - Asociación Protectora de Animales de Talavera de la Reina";
    var param=$(obj).text();
    document.title=title+" | "+param;
}
function mainAjax(url,data){
    if (data=='' || data==null) data="foo=1";

    $.ajax({
        type: "GET",
        async: false,
        url: url,
        dataType: "html",
        processData: false,
        data: data,
        complete: function(XMLHttpRequest, textStatus){
            closeCarga();
        },
        success: function(msg){
            $("div.body").html(msg);
            if ( $('.colorbox').length ){
                carga_galeria();
            }

        },
        beforeSend:function (XMLHttpRequest) {
            openCarga();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            mainAjax("notfound.html");
            closeCarga();
        }
    });

}
function carga_galeria(){
    $(".colorbox").colorbox({
        width:"80%",
        height:"80%",
        //photo: true ,
        onComplete:function(){
            $('.gallery_demo_unstyled').addClass('gallery_demo');
            $('ul.slide_gallery').galleria({
                history   : true, // activates the history object for bookmarking, back-button etc.
                clickNext : true, // helper for making the image clickable
                insert    : '#main_image', // the containing selector for our main image
                onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
                    // fade in the image & caption
                    if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
                        image.css('display','none').fadeIn(1000);
                    }
                    caption.css('display','none').fadeIn(1000);
                    // fetch the thumbnail container
                    var _li = thumb.parents('li');
                    // fade out inactive thumbnail
                    _li.siblings().children('img.selected').fadeTo(500,0.3);
                    // fade in active thumbnail
                    thumb.fadeTo('fast',1).addClass('selected');
                    // add a title for the clickable image
                    image.attr('title','Siguiente Imagen >>');
                },
                onThumb : function(thumb) { // thumbnail effects goes here
                    // fetch the thumbnail container
                    var _li = thumb.parents('li');
                    // if thumbnail is active, fade all the way.
                    var _fadeTo = _li.is('.active') ? '1' : '0.3';
                    // fade in the thumbnail when finnished loading
                    thumb.css({
                        display:'none',
                        opacity:_fadeTo
                    }).fadeIn(1500);
                    // hover effects
                    thumb.hover(
                        function() {
                            thumb.fadeTo('fast',1);
                        },
                        function() {
                            _li.not('.active').children('img').fadeTo('fast',0.3);
                        } // don't fade out if the parent is active
                        )
                }
            });
        }
    });
}
