//$.("{str} {fmt}",{"str": "String", "fmt": "Format"});

$.strFormat = function(str, repl){
    repl = $.extend(repl, {});
    for(i in repl){
        str = str.replace("{"+i+"}", repl[i]);
    }

    return str;
}

$.expr[":"].named = function(obj, index, meta){
    return $(obj).attr("name") === meta[3];
};

$.countdown = function(options){
    var timeout = new Date(), timerId = 0;
    options = $.extend({}, $.countdown.defaults, options);
    timeout.setHours(timeout.getHours() + options.h, timeout.getMinutes() + options.m, timeout.getSeconds() + options.s);
    timerId = setInterval(function(){
        var diff = timeout - new Date();
        if(diff < 0){
            clearInterval(timerId);
            options.timeoutCallback.apply($);
        }else{
            options.stepCallback.apply($,[diff,timerId]);
        }
    }, options.step);
};

$.countdown.defaults = {
    "h": 0,
    "m": 0,
    "s": 0,
    "step": 30,
    "stepCallback": $.noop,
    "timeoutCallback": $.noop
};

$(document).ready(function(){

    var prettyPhoto = function(selector, configOverrides) {

        var config={
            animation_speed: 'normal', /* fast/slow/normal */
            slideshow: 5000, /* false OR interval time in ms */
            autoplay_slideshow: false, /* true/false */
            opacity: 0.80, /* Value between 0 and 1 */
            show_title: true, /* true/false */
            allow_resize: true, /* Resize the photos bigger than viewport. true/false */
            default_width: 800,
            default_height: 600,
            counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
            theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
            hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
            wmode: 'opaque', /* Set the flash wmode attribute */
            autoplay: true, /* Automatically start videos: True/False */
            modal: false, /* If set to true, only the close button will close the window */
            overlay_gallery: false, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
            keyboard_shortcuts: true /* Set to false if you open forms inside prettyPhoto */
        };

        if (configOverrides){
            jQuery.each(configOverrides, function(key,value){
               config[key]=value;
            });
        }

        $(selector).prettyPhoto(config);
    };

    prettyPhoto("a.picasa-image");
    prettyPhoto("a.flickr-image");
    prettyPhoto("a.youtube-video", {
        slideshow:false
    });

    prettyPhoto("a.vimeo-video", {
        slideshow:false
    });

    $('div.image-gallery').quickPager({pageSize:8});

    $('#menu li:has(ul)').hover(function(){
        $('ul', this).show();
    }, function() {
        $('ul', this).hide();
    });

    //    Sistemo il menu
    var menuOrizz=$("#menu ul ul"), len = menuOrizz.size();
    menuOrizz.each(function(current){
        if(current >= Math.ceil(len/2)){
            $(this).addClass("menuRightAligned")
        }
    });
});

