/*
In de gijze productbalk moet een vaste tekst komen te staan, namelijk "Intelligent Management Packs for:", daarna Management Packs vervangen door: "Operations MGR", Solution Packs vervangen voor : "Service MGR", Configuration Packs vervangen door: "Configuration MGR, Connectors en Services moeten er iets meer vandaan komen, wellicht een optische scheiding. Vergeet niet de onderliggende namen in Pivot te vervangen.
*/


var timer;

jQuery(function($) {
  replaceLinks($);
  
  $('#jsddm > li').hover( function() {
    $(this).children('ul').show();
    $(this).addClass('activepage');
    //$('#navwrapper').css('background-repeat', 'repeat');
    $(this).children('ul:has(li)').closest('#navwrapper').css('background-repeat', 'repeat');

  }, function() {
    $(this).children('ul').hide();
    $(this).removeClass('activepage');
    $('#navwrapper').css('background-repeat', 'repeat-x');
  });
  
 
  
  $('a').live('click', function() {
    uri= $(this).attr('rel');
    if(uri != "") {
      if($('body').attr('id') == "home" && uri != "frontpage") {
        loadPage($, uri ,'vervolg', 140, true);
      } else if($('body').attr('id') != "home" && uri == "frontpage") {
        loadPage($, uri ,'home', 285, true);
      } else if($('body').attr('id') == "home" && uri == "frontpage") {
        return false;
      } else {
        loadPage($, uri ,'vervolg', 140, false);
      }
    }
  });
    
    currentAnchor = null;  
    checkAnchor();  

    timer = setInterval("checkAnchor()", 300);  
    makeModal();

    $("a.fancybox").fancybox({ 'overlayShow': true, overlayOpacity: 0.25 });
	 
 /**
   * Externe links in nieuw venster
   */

   var hostname = document.location.hostname;
   $('a').each(function(){
       if (this.href && (this.hostname != hostname) && (this.protocol=='http:' || this.protocol=='https:')) {
         this.target="_blank";
       }
   });


   $('select#subject option[0]').val('Contact via de site');
  $('select#subject option').attr('selected','selected');


});

function checkAnchor(){
    $= jQuery;
    //Check if it has changes

    if(currentAnchor != document.location.hash && (document.location.hash  != '#frontpage' && document.location.hash  != '')){
        currentAnchor = document.location.hash;
        //if there is not anchor, the loads the default section  
        if(currentAnchor)  {
            uri = currentAnchor.substring(1);
            loadPage($, uri, 'vervolg', 140, false);
        }  

    }  else if (currentAnchor != document.location.hash && (document.location.hash  == '#frontpage' || document.location.hash  == '')) {
        currentAnchor = document.location.hash;
        //if there is not anchor, the loads the default section  
            uri = currentAnchor.substring(1);
            loadPage($, uri, 'home', 285, false);
    }
}

function replaceLinks($) {
    var hostname = document.location.hostname;
    $('a').not('.modal, .fancybox, .nolinkreplace').each( function(i) {
      var href= $(this).attr('href');
      
      if (this.href && (this.hostname == hostname) && (this.protocol!='http:' || this.protocol!='https:') && href.substr(0,1) != "#" && href != "/rss"  && !href.match(/\/images\//g) ) {
      
      	// $('#footerwrapper').append('<br />omkat: ' + $(this).attr('href') + ' protocol: ' + this.protocol );

        href= href.replace('www.opslogix.com/','');
        href= href.replace('opslogix.com/','');
        href= href.replace('http:/','');
        
        href= href.replace('/?p=','');
        href= href.replace('/?e=','entry-');
        href= href.replace('/pagina/','');
        href= href.replace('/page/','');
        
        $(this).attr('href', '#'+href);
        $(this).attr('rel', href);
              }
      
      
    });
}

function loadPage($, uri,bodyid, height, animate) {
    
    // niets doen als uri leeg is..
    if (uri=="") {
        return "";
    }

    if(animate === true) {
      clearInterval(timer)
      if(bodyid == "home") {
        $('body').attr('id',bodyid);
      }
      
      $('#header').animate({
              height: height+"px"
          },
          600,
          "linear",
          function(){
            if(bodyid == "vervolg") {
              $('body').attr('id',bodyid);
            }
            
            if(uri.substr(0,6) == "entry-") {
              qs= "e="+uri.substr(6);
            } else {
              qs= "p="+uri;
            }
            
            $.ajax({
              type: "GET",
              url: "/",
              data: qs,
              success: function(msg){
                contentdiv= $('div#content', msg);
                $('div#content').replaceWith(contentdiv);
                replaceLinks($);
              }
      
            });
          }
      );
      var timer = setInterval("checkAnchor()", 300);  
    } else {
            $('#header').css('height', height+"px");
            $('body').attr('id',bodyid);
            
            if(uri.substr(0,6) == "entry-") {
              qs= "e="+uri.substr(6);
            } else {
              qs= "p="+uri;
            }
            
            $.ajax({
              type: "GET",
              url: "/",
              data: qs,
              success: function(msg){
                contentdiv= $('div#content', msg);
                $('div#content').replaceWith(contentdiv);
                replaceLinks($);
                makeModal();
		$("a.fancybox").fancybox({ 'overlayShow': true, overlayOpacity: 0.25 });
              }
      
            });
            

    }
}

function makeModal() {
	 // Modal windows voor downloads..
    $('a.modal').bind('click', function(event) {
        event.preventDefault();
        openDialogFrame($(this).attr('title'), $(this).attr('href'), 450, 320);
    });
}

function openDialogFrame(title, href, dialogwidth, dialogheight) {
    
    // Make sure we have a fresh #dialogframe..
    $('#dialogframe').remove();

    if ($.browser.msie || $.browser.mozilla ) {
        // MS IE and FF don't deal with objects very well, so we
        // use an old school iframe..
        $('body').prepend('<iframe id="dialogframe" src="" border="0" scrolling="no" frameborder="0" style="width: ' +
            (parseInt(dialogwidth)-2) + 'px !important;" ><img src="/pivotx/pics/loadingAnimation.gif" ' +
            'style="display: block; margin: 50px auto;" alt="Loading.." /></iframe>');
        $('#dialogframe').attr('src', href);
    } else {
        // Object for the modern browsers.
        $('body').prepend('<object id="dialogframe" data="' + href + '" ><img src="./pics/loadingAnimation.gif" style="display: block; margin: 50px auto;" alt="Loading.." /></object>');
    }

    // Open the dialog..
    $('#dialogframe').dialog({
        bgiframe:true, 
        resizable: true,
        modal: true,
        draggable: true, 
        width: parseInt(dialogwidth),
        height: parseInt(dialogheight),
        title: title,
        open: function() {  $('#dialogframe').width( parseInt(dialogwidth) ); }
    });

}


