$(document).ready(function(){
   if($('#page-intro').length < 1){
         $('#background').fadeIn("slow", function(){
             switchContent('/IManager/Content/8966/wYhB/mt-2/mi71258/mu1242040255/mv4449', 'home_txt', 'span_1');
         });
   }
   
setTimeout(function(){
   
   if($('#newsTicker').length > 0){
     if($('.item',$('#newsTicker')).length > 1){
      // initialize scrollable together with the autoscroll plugin
      var root = $("#newsTicker").scrollable({circular: true}).autoscroll({ autoplay: true, interval:5000, steps: 1 });
      
     }
     $("#newsTicker .desc a.read-more").click(function(){
       return LoadNewsItem($(this));
     });
   }   
}, 1500);

   
    
   var maxWidth = 662;
   var minWidth = 30;
   var CurrentAccordion = "span_1";
   var NewAccordion = "";
   var bMoving = false;
   var TotalItems = $('.content_wrap').length;
   var PreviousAccordion = "span_1";
   var PreviousItem = "home";
   
   $(".content_wrap a.navigation").click(
       function(){
           var element = this;
           
           if(element.parentNode.id != CurrentAccordion && !bMoving) {
              var CurrentId = $(this).attr("id");
                if(CurrentId == "nav_portfolio") {
                      PortfolioSpecial = 'yes';
                } else {
                     PortfolioSpecial = '';
                } 
              
               bMoving = true;
               NewAccordion = element.parentNode.id;
 
               $("#" + CurrentAccordion + " .text").slideUp("slow");
 
               $('.subnav').fadeOut("slow");        
                var NewCurrent = element.rel; 
                for(var AllBefore = 1; AllBefore <= NewCurrent; AllBefore++) {
                    $("#span_" + AllBefore).animate({left: minWidth * (AllBefore - 1) +"px"}, 400,"linear");         
                } 
                var AfterCurrent = parseInt(NewCurrent) + 1;
                for(var AllAfter = AfterCurrent; AllAfter <= TotalItems; AllAfter++) {
                    $("#span_" + AllAfter).animate({left: maxWidth + (minWidth * (AllAfter - 2)) +"px"}, 400,"linear");         
                }
                setTimeout(function() {
                    if($('.subnav.' +element.title+ ' .subnav_item').hasClass('active')){
                        $('.subnav.' +element.title+ ' .subnav_item').removeClass('active');  
                    }
                    
                    $('.subnav.'+element.title+' .subnav_item:first').addClass('active');
                    
                    CurrentAccordion = NewAccordion;    
                    bMoving = false;

                    if($('.subnav.'+element.title).length > 0){
                       $('.subnav.'+element.title).fadeIn("slow");
                    }
                    var url = $('.subnav.'+element.title + ' a').attr('href');
                    
                    
                    if(PortfolioSpecial == 'yes'){
                      switchContent(url, element.title+'_txt', CurrentAccordion);
                      $('#span_3 .space30').remove();
                    } else {
                      switchContent(url, element.title+'_txt', CurrentAccordion);
                    }
                    
                   
                    
                    $("#" + CurrentAccordion + " a.navigation img").remove();
                    $("#" + CurrentAccordion + " a.navigation").append('<img src="/pics/btn_'+element.title+'_active.gif" />');
                    $("#" + CurrentAccordion + " a.navigation").addClass('active');
                    $("#" + PreviousAccordion + " a.navigation img").remove();
                    $("#" + PreviousAccordion + " a.navigation").append('<img src="/pics/btn_'+PreviousItem+'.gif" /><img src="/pics/btn_'+PreviousItem+'_hover.gif" />');
                    $("#" + PreviousAccordion + " a.navigation").removeClass('active');
                    PreviousAccordion = CurrentAccordion;
                    PreviousItem = element.title;
                }, 500);         
           }
       }
   );
    
});

function bindTeamPage(obj){
    var container = $('#animatie-container');
    container.css('position','absolute');
    $('a.teamLink',obj).bind('click',function()
    { 
        link = this;
        if(swfobject.hasFlashPlayerVersion('8.0.0'))
        {
            var offsetLeft = $(this).parent().offset().left - $('ul.team').offset().left  + parseInt($('ul.team').parent().css('padding-left').substr(0,$('ul.team').parent().css('padding-left').length - 2));
            var offsetTop =  $(this).parent().offset().top - $('ul.team').offset().top + parseInt($('ul.team').parent().css('padding-top').substr(0,$('ul.team').parent().css('padding-top').length - 2));
            swfobject.embedSWF('http://zinmaastricht.ivengi.com/media/teampage_block.swf','team_animation','589','468','8.0.0',false,{'offsetLeft':offsetLeft,'offsetTop':offsetTop},{wmode: 'transparent'},{});
            container.show();
        }
        setTimeout(function(){teamDone(link);}, 2000);
        return false
    });
}

function mouseOver(element){
    if(!$(element).hasClass('active')){
        element.childNodes[0].style.display='none'; element.childNodes[1].style.display='block';
    }
}

function mouseOut(element){
    if(!$(element).hasClass('active')){
        element.childNodes[0].style.display='block'; element.childNodes[1].style.display='none';
    }
}

function animate()
{
    $('.content_wrap a.navigation').each(function ()
    {
        var href = $(this).attr("href");
        $(this).attr('href','javascript:void(0);');
    });
}
 
function ahah(url, target, CurrentActive) {
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target, CurrentActive);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target, CurrentActive) {
  if (req.readyState == 4) { // only if req is "loaded"
      document.getElementById(target).innerHTML = req.responseText;
      if($("#"+target).parent().find('.space30').css('display') == 'none'){
        $("#"+target).parent().find('.space30').css('display','block');
      }
      $("#"+CurrentActive+" .text").css('display','none');
      $("#"+CurrentActive+" .text").slideDown("slow", function(){
          if($('ul.team').length > 0){
            bindTeamPage($('ul.team'));    
          }
      });
  }
}

function load(name, div, CurrentActive) {
    ahah(name,div,CurrentActive);
    setTimeout(function(){
    if($('#map').length > 0){
        loadMaps();   
    }    
    return false;
    },400);
}

function switchContent(url, div, CurrentActive, element, special){
    if(element != null){
        if(!$(element).hasClass('active')){
            if(special != "yes"){
              $("#"+CurrentActive+" .text").slideUp("slow", function(){
                  load(url, div, CurrentActive);
                  if(CurrentActive == 'span_3'){
                    if($("#creatie_txt").parent().find('.space30').length <= 0){
                      $("#creatie_txt").parent().find('a.navigation').after('<div class="space30">&nbsp;</div>');   
                    }
                  }
              });
            }
            if(special == "yes"){
              loadPortfolio(url, div, CurrentActive);  
            }
            var className = element.parentNode.parentNode.parentNode.className;
            var subItem = className.split(" ");
            
            $('.subnav.'+subItem[1]+" .subnav_item").removeClass('active');
            $(element).addClass('active');
            if(special == "yes"){
              
            }
        }        
    } else {
        if(special != "yes"){
          load(url, div, CurrentActive);
        }
        
        if(special == "yes"){
          loadPortfolio(url, div, CurrentActive);  
        }
        if($('.subnav.home').hasClass('first')){
            $(".subnav.home.first li a").addClass('active');
            $('.subnav.home.first').fadeIn("slow");
        }
        if($('.subnav.home.first').hasClass('first')){
                $('.subnav').removeClass('first'); 
        } 
    }  
}

function loadPortfolio(name, div, CurrentActive) {    
  bAjaxSliderLoaded = false;
  $('#creatie_txt .text').slideUp("slow",function(){
        $('#creatie_txt').load(name, function(){
          $("#creatie_txt").parent().find('a.navigation').after('<div class="space30">&nbsp;</div>');
          $("#creatie_txt").parent().find('.space30').css('display','none');
          $("#creatie_txt").css("display",'none');
          $("#creatie_txt").slideDown("slow");  
        });
       
    });
}

function loadPortItem(url){
  $('#ajaxContent').css('display', 'none');  
  $('ul.portfolio').fadeOut('slow',function(){
    $("#creatie_txt .text").load(url, function(){
        $('#ajaxContent').fadeIn('slow');
    });  
  });    
  return false;  
}

function createSlides(PicArray){
    for( i = 0; i < PicArray.length; i ++){
        $("#imgNav").append('<a href="' + PicArray[i] + '" title="Image" onclick="return loadImage(this);"></a>')
    }
    return false; 
}

var bFading = false;

function loadImage(element){
  if(!bFading) {
      bFading = true;    
      $(".imgContainer img.fadeOut").css({'display':'none'});
      $(".imgContainer img.fadeOut").load(function() {
                                            $(".imgContainer img.fadeOut").unbind('load');
                                            $(".imgContainer img.fadeOut").fadeIn("slow",function(){
                                              $(".imgContainer img.fadeIn").attr("src", $(element).attr("href"));    
                                              bFading = false;
                                            });   
                                          });
      $(".imgContainer img.fadeOut").attr("src", $(element).attr("href"));
  }
  return false;  
}

var bAjaxSliderLoaded = false;

function InitAjaxSlider()
{
  if(!bAjaxSliderLoaded) {
    var ajaxContentParentNode = $('#ajaxContent').parent();
    
    var MoveContainer = document.createElement('div');
    MoveContainer.id = 'MoveContainer';
    MoveContainer.className = 'SlideContainer';
    MoveContainer.style.left = '-632px';
    MoveContainer.style.zIndex = '12';
    
    var CurrentContainer = document.createElement('div');
    CurrentContainer.id = 'CurrentContainer';
    CurrentContainer.className = 'SlideContainer';   
    CurrentContainer.style.zIndex = '9'; 
    
    ajaxContentParentNode.append(MoveContainer);  
    ajaxContentParentNode.append(CurrentContainer);  
     
    var AjaxContentClone = $('#ajaxContent').clone(true);
    AjaxContentClone.attr("id","ajaxContentClone");
    $('#CurrentContainer').append(AjaxContentClone);
    $('#ajaxImage').attr("id","ajaxImageOld");  
    $('#ajaxContent').remove(); 
    bAjaxSliderLoaded = true;  
  }
}

function loadPrevItem(url,element){                                         
  $.get(url, function(data) {        
    $('#MoveContainer').css('left','-632px');   
    $('#MoveContainer').html(data);
    $('#ajaxImage img').load(function () {
      $("#ajaxImage img").unbind('load');
      $('#MoveContainer').animate({left: "0px"}, 
                                           1500,
                                           function() {
                                                  var AjaxContentClone = $('#ajaxContent').clone(true);
                                                  $('#ajaxContentClone').remove(); 
                                                  AjaxContentClone.attr("id","ajaxContentClone");
                                                  $('#ajaxImage').attr("id","ajaxImageOld");
                                                  $('#CurrentContainer').append(AjaxContentClone);
                                                  getScript('CurrentContainer');   
                                           });
    });
  });
  return false;  
}

function loadNextItem(url,element){
  $.get(url, function(data) {       
    $('#MoveContainer').css('left','632px'); 
    $('#MoveContainer').html(data);
    $('#ajaxImage img').load(function () {
      $("#ajaxImage img").unbind('load');    
      $('#MoveContainer').animate({left: "0px"}, 
                                           1500,
                                           function(){
                                                  var AjaxContentClone = $('#ajaxContent').clone(true);
                                                  $('#ajaxContentClone').remove();  
                                                  $('#ajaxContent #ajaxImage').attr("id","ajaxImageOld");  
                                                  AjaxContentClone.attr("id","ajaxContentClone");
                                                  $('#CurrentContainer').append(AjaxContentClone); 
                                                  getScript('CurrentContainer'); 
                                           }
                                  );
    });
  });
  return false;
}

function getScript(DivID){
  var scripts = document.getElementById(DivID).getElementsByTagName('script');
  for (var i=0;i<scripts.length;i++) { 
    if(scripts[i].innerHTML != "") {
      eval(scripts[i].innerHTML); 
    }
  }
}
 
function switchColor(element){
    $(element).css('border', '1px solid #f2507a');    
    $(element).css('background-color', '#fafafa');
    $(element).addClass('active');
}

function resetColor(element){
    $(element).css('border', '1px solid #f2a0ca');
    $(element).removeClass('active');
    $(element).css('background-color', '#fff');
}

function switchColorSubmit(element){
    element.style.color = "#000";
    $(element).css('border', '1px solid #f2507a');
    $(element).css('background-color', '#fafafa');    
}

function resetColorSubmit(element){
    element.style.color = "#e2001a";
    $(element).css('border', '1px solid #f2a0ca');
    $(element).css('background-color', '#fff');        
}

function switchColorOver(element){
    if(!$(element).hasClass('active')){
        $(element).css('border', '1px solid #f2507a');    
        $(element).css('background-color', '#fafafa');
    }    
}

function resetColorOut(element){
    if(!$(element).hasClass('active')){
        $(element).css('border', '1px solid #f2a0ca');
        $(element).css('background-color', '#fff');
    }
}
  
function flash_done()
{ 
    $('#site').css('top','50%');
    $('#site').fadeIn(500,function(){
        $('#page-intro').remove();
    });
}

function ahahTeam(url, target, CurrentActive) {
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    if(url != 'undefined'){
        req.onreadystatechange = function() {ahahTeamDone(url, target, CurrentActive);};
        req.open("GET", url, true);
        req.send("");
    }
  }
}  

function ahahTeamDone(url, target, CurrentActive) {
  if (req.readyState == 4) { // only if req is "loaded"
      document.getElementById(target).innerHTML = req.responseText;
      $("#"+CurrentActive+" .text .container_txt").css('display','none');
      $("#"+CurrentActive+" .text .container_txt").fadeIn("slow",
        function(){
            var activeTitle = $('.text .text_left h1').html();
            $('.subnav.team .subnav_item').each(function(){
                if($(this).hasClass('active')){
                    $(this).removeClass('active');
                }
                if($(this).attr('title') == activeTitle){
                    $(this).addClass('active');
                }
            });

        }
      );
  }
}

function LoadNewsItem(ClickedElement){
  $('#home_txt .text').slideUp('slow', function(){
    $('#home_txt').load(ClickedElement.attr('href'), function(){
      $("#home_txt a.load-item").click(function(){
        return LoadNewsItem($(this));
      });
      $('#home_txt').slideDown('slow');
    });
  });
  return false;
}

function teamDone(link){
    ahahTeam(link,'team_txt','span_5');
}

