function ClearDefault(item) {
    // clear the default value of a form element
    if (item.defaultValue == undefined)
        item.defaultValue = item.value;
    if (item.defaultValue == item.value)
        item.value = '';
} // ClearDefault

function SetDefault(item) {
    // if item is empty, restore the default value
    if (item.value == '')
        item.value = item.defaultValue;
} // SetDefault

$(document).ready(function() {

  $(".cleardefault")
      .click(function(){
          ClearDefault(this);
      })
  $(".setcleardefault")
      .click(function(){
          ClearDefault(this);
      })
      .click(function(){
          SetDefault(this);
      });

  // hide border-bottom of last product on categories.html
  $('.categories_product_wrapper:last').addClass('categories_product_wrapper_last');

  // null margin and hide line on last bottom product on categories.html
  $('.categories_bottom:last').addClass('categories_bottom_last');

  // hide line in the left sidebar in conferences list
  $('#conferences_and_fairs .conference:last').addClass('conference_last');

  // homepage slider
  if($("#slider_pictures").length > 0) { // check if element exists
    $('#slider_pictures').cycle({
      fx:     'fade',
      speed:  'slow',
      // timeout: 0,
      next:   '#slider_next',
      prev:   '#slider_previous',
      pager:  '#slider_numbers'
    });
  }

  // expandable_children - open / close mechanism
  $(".expandable_children .manage a").click(function() {
    var parent_tr = $(this).parent().parent(); // current parent tr element
    var parent_id = $(this).parent().parent().attr('id'); // id of parent element

    if((parent_tr).hasClass('tr_open')) { // opened -> closed state
      $(parent_tr).removeClass('tr_open'); // remove "selected" class
      $(this).addClass('tr_plus'); // change - to +
      $(this).removeClass('tr_minus');
      $(parent_tr).children('td').addClass('tr_white'); // change background to white

      // hide child tr elements
      $('.' + parent_id).each(function(i) {
        $(this).hide();
        $(this).addClass('tr_child_hidden');
      });

    }

    else { // closed -> opened state
      $(parent_tr).addClass('tr_open'); // add "selected" class
      $(this).removeClass('tr_plus');
      $(this).addClass('tr_minus'); // change + to -
      $(parent_tr).children('td').addClass('tr_blue'); // change background to blue
      $('.' + parent_id + ':last td').addClass('tr_last'); //add padding on last "sub tr"

      // show child tr elements
      $('.' + parent_id).each(function(i) {
        $(this).show();
        $(this).addClass('tr_child_visible');
      });
    }

    return false;
  });

  // helpdesk - add border-bottom on last row

  $('#helpdesk_expand li.helpdesk_parent_li:last').addClass('helpdesk_parent_li_last');
  $('#helpdesk_download li:last').addClass('helpdesk_download_li_last');

  // helpdesk - open / close mechanism
  $(".open_helpdesk").click(function() {

    var parent_div = $(this).siblings('div'); // current parent div element

    if((parent_div).hasClass('helpdesk_visible')) { // opened -> closed state
      $(parent_div).removeClass('helpdesk_visible'); // remove "selected" class
      $(parent_div).addClass('helpdesk_hidden'); // add "selected" class
      $(this).removeClass('tr_minus');
      $(this).addClass('tr_plus'); // change + to -
      $(this).parent().removeClass('tr_parent_blue');
      $(this).parent().addClass('tr_child_hidden'); // change background to white
      $(parent_div).parent().removeClass('parent_div_noborder');
      $(parent_div).parent().addClass('parent_div_border');

      if (jQuery.browser.msie) {
        if(parseInt(jQuery.browser.version) == 6) {
          $(parent_div).parent().addClass('parent_div_bottom');
        }
      }

      // hide child elements
      $(parent_div).hide();

    }

    else { // closed -> opened state
      $(parent_div).removeClass('helpdesk_hidden'); // remove "selected" class
      $(parent_div).addClass('helpdesk_visible'); // add "selected" class
      $(this).removeClass('tr_plus');
      $(this).addClass('tr_minus'); // change + to -
      $(this).parent().removeClass('tr_child_hidden');
      $(this).parent().addClass('tr_parent_blue'); // change background to blue
      $(parent_div).parent().removeClass('parent_div_border');
      $(parent_div).parent().addClass('parent_div_noborder');

      // show child elements
      $(parent_div).show();
    }

    return false;
  });

  // hide child tr elements in facturen table
  $('.expandable_children tr').each(function(i) {
  if(!($(this).hasClass('parent_tr'))) {
    $(this).hide();
  }
  });

  // hide
  $('.helpdesk_hidden').hide();

  // change style of select's on facturen page
  if($("select#periode").length > 0) { // check if element exists
  $('select#periode').selectmenu({style:'dropdown', menuWidth: 246});
  }

  // ui tabs
  if($("#detail_menu").length > 0) { // check if element exists
  var $tabs = $('#detail_right_wrapper').tabs();
  $tabs.tabs('select', 0); //select first tab
  }

  // detail tooltip
  if($("#detail_question_wrapper").length > 0) { // check if element exists
    $('#detail_question_wrapper a#open_tooltip').click(function() {
      $('#detail_tooltip').show();
      return false;
    });
    $('#detail_question_wrapper a#close_tooltip').click(function() {
      $('#detail_tooltip').hide();
      return false;
    });

    $('#detail_question_wrapper a#open_tooltip2').click(function() {
      $('#detail_tooltip2').show();
      return false;
    });
    $('#detail_question_wrapper a#close_tooltip2').click(function() {
      $('#detail_tooltip2').hide();
      return false;
    });

    $('#detail_question_wrapper a#open_tooltip3').click(function() {
      $('#detail_tooltip3').show();
      return false;
    });
    $('#detail_question_wrapper a#close_tooltip3').click(function() {
      $('#detail_tooltip3').hide();
      return false;
    });

  }


  // detail form count

  $('.prijs_count_wrapper a').click(function() {

    var count = $(this).parent().find('.prijs_count_left').attr('value');
    var link_class = $(this).attr('class');

    if(link_class == 'prijs_count_plus') {
      $(this).parent().find('.prijs_count_left').attr({'value' : ++count });
    }

    else if(link_class == 'prijs_count_minus') {
      if(count > 0) {
        $(this).parent().find('.prijs_count_left').attr({'value' : --count });
      }
    }

    return false;
  });

});

//<![CDATA[

// this variable will collect the html which will eventualkly be placed in the side_bar
var side_bar_html = new Array()

// arrays to hold copies of the markers used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
var i = 0;

function ToggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

// A function to create the marker and set up the event window
function createMarker(division,point,name,html) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  // save the info we need to use later for the side_bar
  gmarkers[i] = marker;
  // add a line to the side_bar html
  if (side_bar_html[division] == null) {
    side_bar_html[division] = '<li><a href="javascript:myclick(' + i + ')">' + name + '</a></li>';
  } else {
    side_bar_html[division] += '<li><a href="javascript:myclick(' + i + ')">' + name + '</a></li>';
  }
  i++;
  return marker;
}


// This function picks up the click and opens the corresponding info window
function myclick(i) {
  GEvent.trigger(gmarkers[i], "click");
}

function LoadGoogle(markerfile, visible) {
  if (GBrowserIsCompatible()) {

    // create the map
    var map = new GMap2(document.getElementById("googlemap"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(47.963539,5.623227), 13);
    map.setZoom(4);
    // ===== Start with an empty GLatLngBounds object =====
    var bounds = new GLatLngBounds();

    // Read the data from example.xml
    var request = GXmlHttp.create();
    request.open("GET", markerfile, true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        var xmlDoc = GXml.parse(request.responseText);
        // obtain the array of markers and loop through it
        var markers = xmlDoc.documentElement.getElementsByTagName("marker");

        for (var i = 0; i < markers.length; i++) {
          // obtain the attribues of each marker
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
          var point = new GLatLng(lat,lng);
          var html = GXml.value(markers[i].getElementsByTagName("infowindow")[0]) + '<br><br><a href="http://maps.google.com/maps?saddr=&daddr=' + point.toUrlValue() + '" target ="_blank">Directions</a>';
          var label = markers[i].getAttribute("label");
          var division = markers[i].getAttribute("division");
          // create the marker
          var marker = createMarker(division,point,label,html);
          map.addOverlay(marker);

          // ==== Each time a point is found, extent the bounds ato include it =====
          bounds.extend(point);
        }
        // put the assembled side_bar_html contents into the side_bar div
        var side_bar = "";
        for (var x in side_bar_html) {
          side_bar += '<h3><a href="javascript:ToggleLayer(\'' + x + '\')">' + x + '</a></h3><ul id="' + x +  '" class="' + visible + '">' + side_bar_html[x] + '</ul>';
//          side_bar += '<h3><a href="javascript:ToggleLayer(\'' + x + '\')">' + x + '</a></h3><div id="' + x +  '" class="' + visible + '"><ul>' + side_bar_html[x] + '</div></ul>';
        }
        document.getElementById("side_bar").innerHTML = side_bar;

        // ===== determine the zoom level from the bounds =====
        map.setZoom(map.getBoundsZoomLevel(bounds));

        // ===== determine the centre from the bounds ======
        map.setCenter(bounds.getCenter());
      }
    }
    request.send(null);
  }

  else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
  }
}
//]]>


