/*
Unified javascripts for infofarm 
*/
$(document).ready(function() {
    /**
    Cross Site Request Forgery protection
    While the above method can be used for AJAX POST requests, it has some inconveniences: 
    you have to remember to pass the CSRF token in as POST data with every POST request. 
    For this reason, there is an alternative method: on each XMLHttpRequest, 
    set a custom X-CSRFToken header to the value of the CSRF token. 
    This is often easier, because many javascript frameworks provide hooks that allow headers 
    to be set on every request. In jQuery, you can use the ajaxSend event as follows:
    **/
    $('html').ajaxSend(function(event, xhr, settings) {
        function getCookie(name) {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
        if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
            // Only send the token to relative URLs i.e. locally.
            xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
        }
    });
    
    /* homepage tabbing */
    if ($("#featured ul.boxLinks").length > 0){
        $("#featured ul.boxLinks").tabs("div#box > div.box", {
            'current': 'selected',
            onBeforeClick: function(event, tabIndex) {
                current_tab = this.getCurrentTab();
                next_tab = this.getTabs().eq(tabIndex);
            
                current_tab.closest('li').removeClass('selected');
                next_tab.closest('li').addClass('selected');
                this.getPanes().hide().eq(tabIndex).show();
            }
        });
    }
    
    if ($("#services ul.boxLinks").length > 0){
        $("#services ul.boxLinks").tabs("div#box-services > div.box-service", {
            'current': 'selected',
            onBeforeClick: function(event, tabIndex) {
                current_tab = this.getCurrentTab();
                next_tab = this.getTabs().eq(tabIndex);
            
                current_tab.closest('li').removeClass('selected');
                next_tab.closest('li').addClass('selected');
                this.getPanes().hide().eq(tabIndex).show();
            }
        });
    }
    
    if ($("#products ul.boxLinks").length > 0){
        $("#products ul.boxLinks").tabs("div#box-products > div.box-product", {
            'current': 'selected',
            onBeforeClick: function(event, tabIndex) {
                current_tab = this.getCurrentTab();
                next_tab = this.getTabs().eq(tabIndex);
            
                current_tab.closest('li').removeClass('selected');
                next_tab.closest('li').addClass('selected');
                this.getPanes().hide().eq(tabIndex).show();
            }
        });
    }
    /* Apply fancybox to multiple items */
    if ($("a.grouped_elements").length>0)
        $("a.grouped_elements").fancybox({
            'transitionIn':'elastic',
            'transitionOut':'elastic',
            'speedIn':600, 
            'speedOut':200, 
            'overlayShow':true
        });
    
    if ($('a[href$=/image_view_fullscreen]').length){
        $('a[href$=/image_view_fullscreen]').fancybox({
            'transitionIn':'elastic',
            'transitionOut':'elastic',
            'speedIn':600, 
            'speedOut':200, 
            'filter': '#wrapper-padding-plone a',
            'overlayShow':true
        });
    }
    
    if ($(".scrollable-gallery").length>0)
        $(".scrollable-gallery").scrollable().navigator();

    /* Home news carousel*/
    if ($("#gallery-events .scrollable").length>0)
        $("#gallery-events .scrollable").scrollable({circular: false}).autoscroll({ autoplay: false });

    /* clear input box on focus */
    $('form input[type="text"], form input[type="password"]').live('click', function(event){
            target = $(event.target);
            if (target.val() == 'cerca i prodotti' || target.val() == 'search products' || target.val() == 'Cerca nel sito'){
                target.attr('value', '');
            }
            target.select();
    });
    
    /* Accordion box */
    $("#accordion").tabs("#accordion div.pane",
    {
      tabs: 'ul.group',
      effect: 'slide',
      initialIndex: 0
    });
    
});

// Youtube portlets
function play(id, holder, width, height)
{
    var s1 = new SWFObject("http://www.youtube.com/v/"+id,"prodotti",width, height,"7");
    s1.addVariable("width", width);
    s1.addVariable("height", height);
    s1.addParam("wmode", "transparent");
    s1.addVariable("transition", "fade");
    s1.write(holder);
};
  
//grab a youtube id from a (clean, no querystring) url (thanks to http://jquery-howto.blogspot.com/2009/05/jyoutube-jquery-youtube-thumbnail.html)
function youtubeid(url) {
    var ytid = url.match("[\\?&]v=([^&#]*)");
    ytid = ytid[1];
    return ytid;
};
