/*only for large screens */

var timer = {
time: 0,
now: function(){ return (new Date()).getTime(); },
start: function(){ this.time = this.now(); },
since: function(){ return this.now()-this.time; }
}


if(window.screen.height >= 600 && window.screen.width >= 800){

  /* remove no javascript stylesheet*/
  var sheetNoJS = getEl('sheet-no-javascript');
  if (sheetNoJS){
    sheetNoJS.parentNode.removeChild(sheetNoJS);
  }

  timer.start();

  
  /* flash headers */
 flashHeaders.add({
    leading:-10,
    fontPath:'/media/site/swf/fleischmann.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'div#content-framework h1',
    size:32,
    paddingLeft:-2,
    color:'000000',
    lineHeights:[42,75,106]
  });
  // Accurat, vanuit content vervangen
  flashHeaders.add({
    fontPath:'/media/site/swf/accurat.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'div#content-inner h2',
    size:18,
    color:'000000',
    paddingLeft:-3,
    thickness:100,
    sharpness:50,
    gridFitType:'subpixel',
    lineHeights:[24,43,55]
  });
  // Accurat, 12 pixels groot
  flashHeaders.add({
    leading:-2,
    fontPath:'/media/site/swf/accurat.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'h2.accurat12',
    size:12,
    color:'000000',
    paddingLeft:-3,
    paddingBottom:0,
    //sharpness:50,
    thickness:50,
    gridFitType:'subpixel',
    lineHeights:[14,43,61]
  });
  // Accurat, 14 pixels groot
  flashHeaders.add({
    leading:-2,
    fontPath:'/media/site/swf/accurat.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'h2.accurat14',
    size:14,
    color:'000000',
    paddingLeft:-3,
    paddingBottom:2,
    //sharpness:50,
    thickness:50,
    gridFitType:'subpixel',
    lineHeights:[24,43,61]
  });
  // Accurat, 16 pixels groot
  flashHeaders.add({
    leading:-2,
    fontPath:'/media/site/swf/accurat.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'h2.accurat16',
    size:18,
    color:'000000',
    paddingLeft:-3,
    paddingBottom:2,
    sharpness:50,
    thickness:100,
    gridFitType:'subpixel',
    lineHeights:[24,43,61]
  });
  // Accurat, 20 pixels groot
  flashHeaders.add({
    leading:-2,
    fontPath:'/media/site/swf/accurat.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'h2.accurat18',
    size:18,
    color:'000000',
    paddingLeft:-3,
    paddingBottom:2,
    //sharpness:50,
    thickness:50,
    gridFitType:'subpixel',
    lineHeights:[23,43,61]
  });
  // Accurat, 20 pixels groot
  flashHeaders.add({
    leading:-2,
    fontPath:'/media/site/swf/accurat.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'h2.accurat20',
    size:20,
    color:'000000',
    paddingLeft:-3,
    paddingBottom:2,
    //sharpness:50,
    thickness:50,
    gridFitType:'subpixel',
    lineHeights:[23,43,61]
  });
  
  // Uitgaan is headers
  flashHeaders.add({
    leading:-2,
    fontPath:'/media/site/swf/accurat.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'body.case-page h1',
    size:44,
    color:'000000',
    paddingLeft:-3,
    paddingBottom:2,
    //sharpness:50,
    letterSpacing: 2,
    thickness:400,
    gridFitType:'subpixel',
    lineHeights:[23,43,61]
  });

  // Artis headers
  flashHeaders.add({
    leading:-2,
    fontPath:'/media/site/swf/accurat.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'body.artis h1',
    size:36,
    color:'FFFFFF',
    paddingLeft:-3,
    paddingBottom:2,
    //sharpness:50,
    letterSpacing: 2,
    thickness:400,
    gridFitType:'subpixel',
    lineHeights:[23,43,61]
  });
  flashHeaders.add({
    leading:-10,
    fontPath:'/media/site/swf/fleischmann.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'h2.fleischmann16',
    size:16,
    paddingLeft:-2,
    color:'000000',
    lineHeights:[42,75,106]
  });
  flashHeaders.add({
    leading:-10,
    fontPath:'/media/site/swf/fleischmann.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'h2.fleischmann20',
    size:20,
    paddingLeft:-2,
    color:'000000',
    lineHeights:[42,75,106]
  });
  flashHeaders.add({
    leading:-10,
    fontPath:'/media/site/swf/fleischmann.swf',
    fontPath7Down:'/media/site/swf/accurat-7down.swf',
    cssSelector:'h2.fleischmann24',
    size:24,
    paddingLeft:-2,
    color:'000000',
    lineHeights:[42,75,106]
  });

  
  flashHeaders.start({
    path:'/media/site/swf/hg.swf',
    calibrationId:'skip',
    calibrationHeight:17
  });
  
  /* print logo in gif instead of png */
  setLogoForPrintIE6();
  
  


  
  /* navigation fold out for project page */
  var navProjects = new NavProjects({
    containerId:'nav-projects',
    speed:75,
    easeIn:2,
    easeOut:5
  });
  
  
  /* sizes text & visual columns on project page */
  var projectVisualSizer = new ProjectVisualSizer({
    containerId:'nav-projects',
    speed:15,
    easeIn:2,
    easeOut:10
  });
  
  
  /* scalable website */
  addEvent(window,'DOMContentLoaded',scaleSite);
  addEvent(window,'resize',scaleSite);
  
  addEvent(window,'DOMContentLoaded',setWieberContainer);
  
  
  /* clash browser */
  var projectStr = window.clashId ? '&projectIndex=' + window.clashId : '';

  /* create all flash objects */
  addEvent(window,'DOMContentLoaded',createFlashObjects);
  
  /* nav sub header */
  addEvent(window,'DOMContentLoaded',createNavSubHeader);
  
  /* fill email adresses on employee page with javascript (anti SPAM) */
  addEvent(window,'DOMContentLoaded',fillEmployeeContactList);
  


  //projectBrowser
  if (typeof(ProjectBrowserData) != 'undefined'){
    addEvent(window,'DOMContentLoaded',function(){ProjectBrowser.setup({
      data:ProjectBrowserData
    })});
  }
	
  
   //wat was waar
  createFlashObject({
    id:'www-swf',
    parentId:'www-container',
    uri:'watwaswaar.swf',
    params:{'scale':'noscale','salign':'lt'},
    className:'flash-object',
    width:455,
    height:341,
    requiredVersion:8
  });
}

function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function lightbox(url, width, height, ieHeight) {
    if (navigator.appName == 'Microsoft Internet Explorer') {
        $('a[href="#'+url+'"]').colorbox({iframe: true, width: width+'px', height: ieHeight+'px', scrolling: false, close: 'sluit', opacity: 0.7, href: url});
    } else {
        $('a[href="#'+url+'"]').colorbox({iframe: true, width: width+'px', height: height+'px', scrolling: false, close: 'sluit', opacity: 0.7, href: url});
    }
}


$(document).ready(function () {

    // External links
    var h = "fabrique.nl";
    $("a[href^='http']:not([href^='http://" + h + "']):not([href^='http://www." + h + "']):not([href^='http://blog.fabrique.nl']), a[href$='.pdf']").attr("target", "_blank");
 
    // Easteregg!
    var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
    $(document).keydown(function(e) {
        kkeys.push( e.keyCode );
        if ( kkeys.toString().indexOf( konami ) >= 0 ){
            $(document).unbind('keydown',arguments.callee);
            easterEgg();
        }
    });    
    
    function easterEgg() {
        $('.employee .photo').each( function() {
            var src = $(this).attr('src');
            $(this).attr('src', src.replace("faces", "faces-mustache"));
        });
    }

    // share button
    function shareInit() {
        $(".share").each(function (e) {
            var elem = this;
            shareClose(elem);
        });
    }
    
    
    function shareClose(elem) {
        var elem = $(elem);

        elem.click(function(e) {
            e.stopPropagation();
        });
        elem.find('.message').hide();
        elem.find(".share-button span").html('deel deze vacature');

        elem.removeClass('open');
        elem.removeClass('service');

        $('body').unbind("click");
        elem.find('.share-button').unbind("click");
        elem.find('.share-button').click(function() {
            shareServices(elem);
            return false;
        });
    }
    function shareServices(elem) {
        var elem = $(elem);


        elem.find("li.twitter a").attr('href', 'http://twitter.com/home?status='+$.URLEncode('Fabrique zoekt: '+$('h1 div.print-header').html()+' - '+window.location));
        elem.find("li.facebook a").attr('href', 'http://www.facebook.com/sharer.php?u='+$.URLEncode(window.location)+'&t='+$.URLEncode('Fabrique zoekt: '+$('h1 div.print-header').html()));
        elem.find(".share-button span").html('deel deze vacature');

        elem.removeClass('service');
        elem.addClass('open');

        elem.find('.email').unbind("click");
        elem.find('.email').click(function() {
            shareEmailService(elem);
            return false;
        });
        elem.find('.share-button').unbind("click");
        elem.find('.share-button').click(function() {
            shareClose(elem);
            return false;
        });
        $('body').click(function() {
            shareClose(elem);
            //return false;
        });
    }
    function shareEmailService(elem) {
        var elem = $(elem);
        elem.find(".share-button span").html('stuur door');

        elem.addClass('open');
        elem.addClass('service');

        elem.find('.share-button').unbind("click");
        elem.find('.share-button').click(function() {
            shareServices(elem);
            return false;
        });
        elem.find('.share-exit').unbind("click");
        elem.find('.share-exit').click(function() {
            shareClose(elem);
            return false;
        });
        elem.find('button.submit').click(function() {
            submitEmailShare(elem);
            return false;
        });

    }
    function submitEmailShare(elem){
        var elem = $(elem);

        $.ajax({
          type: "POST",
          url: '/email_share/',
          data: {'name':elem.find('#id_email_share #id_name').val(),'email':elem.find('#id_email_share #id_email').val(),'name_receiver':elem.find('#id_email_share #id_name_receiver').val(),'email_receiver':elem.find('#id_email_share #id_email_receiver').val(), 'message':'Fabrique zoekt: '+$('h1 div.print-header').html()},
          dataType:'html',
          success: function(data) {
            elem.find('.message').remove();
            elem.find('form').replaceWith(data);
            elem.find('.submit').click(function() {
                submitEmailShare(elem);
                return false;
            });

            elem.find('.message.success').each(function (e) {
                elem.find('form').hide();
                elem.find('.message.success').show();
                var f = function() {shareResetForm(elem); };
                setTimeout(f,2000);
            });


          }
        });

    }
    function shareResetForm(elem){
        var elem = $(elem);
        elem.find('.message.success').each(function (e) {
            $(this).fadeOut(600).queue(function() {
                shareClose(elem);
                elem.find('form').show();
            });
        });
    }
    shareInit();
    //enableShare();


    // Searchbox
    $('#query').placeholder();
    
    
    $("a[target=lightbox]").each(function (e) {

        var elem = $(this);
        var url = elem.attr('href');
        // voeg slash toe als die er niet staat aan het eind!
        url = url.substring(url.length-1) != '/' ? url = url+'/' : url;
        $.ajax({
            url: url+'data/',
            dataType: 'json',
            success: function(data) {
                elem.colorbox({iframe: true, width: (data.width+60)+'px', height: (data.height+80)+'px', scrolling: false, close: 'sluit', opacity: 0.7});
            }
        });
    });
    
    $("a.vimeo").each(function (e) {

        var elem = $(this);
        var slug = elem.attr('href').substr(1);
          $.ajax({
              url: '/vimeo/'+slug+'/data/',
              dataType: 'json',
              success: function(data) {
                  elem.colorbox({iframe: true, width: (data.width+60)+'px', height: (data.height+80)+'px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/vimeo/'+slug+'/'});
              }
          });
    });

    
    // Tooltips!
    $('body').append('<span class="tooltip-popup" style="display: none;"/>');
    $("a.tooltip").mouseenter(function (e) {
        cursorPos = getPosition(e);
        var tooltip = $(this).find('.tooltip-popup');
        if (tooltip.length == 0) {
            anchor = $(this);
            title = anchor.html();
            var offset = $(this).offset();
            var width = $(this).width() + 15;
            var left = offset.left + width;
            if(left > ($(window).width() - 230)) {
              $(this).addClass('left');
              left = ($(this).offset().left - 235);
            }
            
            $.get(
                '/tooltip/',
                {id: anchor.attr('id'), language: $('html').attr('lang')},
                function (data) {
                    $('.tooltip-popup').appendTo(anchor).css('left', left).css('top', (offset.top + 7)).html('<h3>' + title + '</h3>' + data).show();
                    var tooltipHeight = $('.tooltip-popup').height();
                    var tooltipOffsetTop = $('.tooltip-popup').offset().top - $(window).scrollTop();
                    //alert('tooltipHeight: ' + tooltipHeight + ', tooltipOffsetTop: ' + $('.tooltip-popup').offset().top + ', windowHeight: ' + $(window).height() + ', windowScrollTop: ' + $(window).scrollTop());
                    if (tooltipHeight + tooltipOffsetTop > $(window).height()) {
                      $('.tooltip-popup').css('top', $(window).height() + $(window).scrollTop() - tooltipHeight);
                    }
                }
            );
            
            
        } else if (tooltip.css('display') == 'none') {
            tooltip.show();
        }
    });
    $("a.tooltip").mouseout(function() {
        var tooltip = $(this).find('.tooltip-popup');
        tooltip.hide();
    });
    $("a.tooltip").click(function() {
        return false;
    });



    // Colorbox!
    if (navigator.appName == 'Microsoft Internet Explorer') {
        $('a[href="#video"]').colorbox({iframe: true, width: '590px', height: '450px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/wij/bist-du/'});
        $('.right-banner a[href="#inside"]').colorbox({iframe: true, width: '410px', height: '450px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/wij/inside/'});
        $('.right-banner a[href="#special-species"]').colorbox({iframe: true, width: '700px', height: '470px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/wij/special-species/'});
        //$('.right-banner a[href="#fable-elements"]').colorbox({iframe: true, width: '700px', height: '592px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/video/fable-elements/'});
        $('a[href="#bistdu"]').colorbox({iframe: true, width: '590px', height: '450px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/wij/bist-du/'});
    } else {
        $('.right-banner a[href="#inside"]').colorbox({iframe: true, width: '410px', height: '430px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/wij/inside/'});
        $('.right-banner a[href="#special-species"]').colorbox({iframe: true, width: '700px', height: '440px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/wij/special-species/'});
        //$('.right-banner a[href="#fable-elements"]').colorbox({iframe: true, width: '700px', height: '560px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/video/fable-elements/'});
        $('a[href="#bistdu"]').colorbox({iframe: true, width: '590px', height: '420px', scrolling: false, close: 'sluit', opacity: 0.7, href: '/wij/bist-du/'});
    }
});

$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

