
/*******************************************************************************
 Copyright (c) 2001-2011. Open lab srl, Firenze - Italy
 email: mbicocchi@open-lab.com
 site: http://open-lab.com
 ******************************************************************************/

// jQuery.support.transition
// to verify that CSS3 transition is supported (or any of its browser-specific implementations)
$.support.transition = (function(){
  var thisBody = document.body || document.documentElement,
    thisStyle = thisBody.style,
    support = thisStyle.transition !== undefined || thisStyle.WebkitTransition !== undefined || thisStyle.MozTransition !== undefined || thisStyle.MsTransition !== undefined || thisStyle.OTransition !== undefined;
  return support;
})();


if($.support.transition){
  /*******************************************************************************
   jquery-animate-css-rotate-scale ©zachstronaut.
   A jquery implementation to manipulate scale and rotate as other CSS properties.
   https://github.com/zachstronaut/jquery-animate-css-rotate-scale


   inclusion jquery-animate-css-rotate-scale
   ******************************************************************************/

  (function(b){var d=null,i=b.fn.css;b.fn.css=function(a,c){d===null&&(d=typeof b.cssProps!="undefined"?b.cssProps:typeof b.props!="undefined"?b.props:{});if(typeof d.transform=="undefined"&&(a=="transform"||typeof a=="object"&&typeof a.transform!="undefined")){var e=d,f;a:{f=this.get(0);for(var h=["transform","WebkitTransform","msTransform","MozTransform","OTransform"],g;g=h.shift();)if(typeof f.style[g]!="undefined"){f=g;break a}f="transform"}e.transform=f}if(d.transform!="transform")if(a=="transform"){if(a= d.transform,typeof c=="undefined"&&jQuery.style)return jQuery.style(this.get(0),a)}else if(typeof a=="object"&&typeof a.transform!="undefined")a[d.transform]=a.transform,delete a.transform;return i.apply(this,arguments)};var e="deg";b.fn.rotate=function(a){var c=b(this).css("transform")||"none";if(typeof a=="undefined"){if(c&&(a=c.match(/rotate\(([^)]+)\)/))&&a[1])return a[1];return 0}if(a=a.toString().match(/^(-?\d+(\.\d+)?)(.+)?$/))a[3]&&(e=a[3]),b(this).css("transform",c.replace(/none|rotate\([^)]*\)/, "")+"rotate("+a[1]+e+")");return this};b.fn.scale=function(a){var c=b(this).css("transform");if(typeof a=="undefined"){if(c&&(a=c.match(/scale\(([^)]+)\)/))&&a[1])return a[1];return 1}b(this).css("transform",c.replace(/none|scale\([^)]*\)/,"")+"scale("+a+")");return this};var h=b.fx.prototype.cur;b.fx.prototype.cur=function(){if(this.prop=="rotate")return parseFloat(b(this.elem).rotate());else if(this.prop=="scale")return parseFloat(b(this.elem).scale());return h.apply(this,arguments)};b.fx.step.rotate= function(a){b(a.elem).rotate(a.now+e)};b.fx.step.scale=function(a){b(a.elem).scale(a.now)};var j=b.fn.animate;b.fn.animate=function(a){if(typeof a.rotate!="undefined"){var b=a.rotate.toString().match(/^(([+-]=)?(-?\d+(\.\d+)?))(.+)?$/);b&&b[5]&&(e=b[5]);a.rotate=b[1]}return j.apply(this,arguments)}})(jQuery);

  /*******************************************************************************
   end inclusion
   ******************************************************************************/
}



//COOKIES
jQuery.fn.mb_setCookie = function(name,value,days) {
  var id=$(this).attr("id");
  if(!id) id="";
  if (!days) days=7;
  var date = new Date(), expires;
  date.setTime(date.getTime()+(days*24*60*60*1000));
  expires = "; expires="+date.toGMTString();
  document.cookie = name+"_"+id+"="+value+expires+"; path=/";
};

jQuery.fn.mb_getCookie = function(name) {
  var id=$(this).attr("id");
  if(!id) id="";
  var nameEQ = name+"_"+id + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
};

jQuery.fn.mb_removeCookie = function(name) {
  $(this).mb_setCookie(name,"",-1);
};


//only IT and EN
$.fn.changeLang=function(lang){
  var actualLang = lang ? lang : $(document).mb_getCookie("sitelang");
  if(!actualLang){
    if (navigator.browserlanguage) // Explorer
      actualLang = navigator.browserlanguage.indexOf("it")>-1 ? "it" : "en";
    else if (navigator.language) // FF
      actualLang = navigator.language.indexOf("it")>-1 ? "it" : "en";
    else
      actualLang = "en";
  }
  $(document).mb_setCookie("sitelang",actualLang);
  $(document).manageStrings($(document).mb_getCookie("sitelang"))
};

$.fn.manageStrings=function(lang){
  if(lang==="it"){
    $("[data-lang='en']").hide();
    $("[data-lang='it']").show();
  }else{
    $("[data-lang='it']").hide();
    $("[data-lang='en']").show();
  }
};


$(function(){

  $(document).changeLang();

  $("#header #submenu").hide();
  $("#projectsMenu").bind("mouseenter",function(){
    $("#header #submenu").fadeIn();
  }).bind("mouseleave",function(){
    $("#header #submenu").fadeOut();
  });


  $("a").not(".sel, .external").each(function(){
    $(this).bind("click",function(e){
      var url= $(this).attr("href");
      if(url=="#")
        return;
      $("#wrapper").fadeTo(1000,0,function(){
        self.location.href=url;
      });
      e.preventDefault();
      e.stopPropagation();
    });
  });

  $("a.sel").not(".external").each(function(){
    $(this).css("cursor","default");

    $(this).bind("click",function(e){
      e.preventDefault();
      e.stopPropagation();
      return false;
    });
  });

  setTimeout(function(){
    $("#wrapper").fadeTo(1000,1);
  },1000);


  //OL attr

  var OL= $("<div>").addClass("openlab").html("code: Open lab");
  var olCss=$.support.transition ? {position:"fixed",bottom:40, left:-10,font:"10px/12px 'Abel', sans-serif", color:"#8c816d", textTransform:"uppercase",zIndex:10000, cursor:"pointer"} : {position:"fixed",bottom:40, left:10,font:"10px/12px 'Abel', sans-serif", color:"#8c816d", textTransform:"uppercase",zIndex:10000, cursor:"pointer"};
  OL.css(olCss);

  if($.support.transition)
    OL.rotate("-90deg");
  OL.click(function(){
    window.open("http://www.open-lab.com");
  });

  $("body").append(OL);

  var thumbsNav=$("<div>").attr("id","galleryNav");
  $("#thumbs").after(thumbsNav);



  //initialize the gallery
  $("#linearGallery").mbLinearGallery({
    thumbPlaceHolder:"#thumbs",
    resizeEnabled:true,
    transitionTime:800,
    imageWrapperWidth:750,
    imageMinWidth:550,
    defaultScale:.8,
    defaultOpacity:.1,
    enableZoom:true,
    onCreateThumbs:function(o){
      $(o).initPagination({
        elements:$(".thumbWrapper"),
        elsPerPage:7,
        navigationID:"galleryNav",
        showNextPrev:false,
        showIdxList:true,
        maxPage:10
      });
    },
    onChange:function(idx){
      $("#thumbs").getPageFromIndex(idx,false,false);
    },
    onStart:function(idx){
      $(document).changeLang();
    }
  });
});

