//  $Id: script.js,v 1.1 2007/12/11 17:27:18 skennedy Exp $
//  Copyright (C)2006 Gorges Web Sites. All Rights Reserved.

//  browser detection

var isIE5 = (document.all && (navigator.appVersion.indexOf('MSIE 5') != -1)) ? 1 : 0;
var isIE6 = (document.all && (navigator.appVersion.indexOf('MSIE 6') != -1)) ? 1 : 0;

//  text trimming

function trim(text) {
  if (text.length > 0) {
    while (text.substring(0,1) == ' ')
      text = text.substring(1, text.length);
    while (text.substring(text.length - 1, text.length) == ' ')
      text = text.substring(0, text.length - 1);
  };
  return text;
}

//  finding an element

function get_element(id) {
  if (document.getElementById)
    return document.getElementById(id);
  else if (document.all)
    return document.all[id];
  return null;
}

//  inner text

function get_inner(obj) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  return obj ? (document.all ? obj.innerText : obj.innerHTML) : '';
}
function set_inner(obj, text) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    if (document.all)
      obj.innerText = text;
    else
      obj.innerHTML = text;
  }
}
function get_value(form, obj) {
  if (typeof(obj) == 'string')
    obj = eval('document.' + form + '.' + obj);
  return obj ? obj.value : null;
}
function set_value(form, obj, value) {
  if (typeof(obj) == 'string')
    obj = eval('document.' + form + '.' + obj);
  if (obj) {
    if (typeof(obj.selectedIndex) == 'number')
      obj.selectedIndex = value;
    else
      obj.value = value;
  }
}

//  radio helpers

function get_radio(obj) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    var len = obj.length;
    if (typeof(len) == 'undefined')
      return obj.checked ? obj.value : '';
    for (var i = 0;  i < len;  i++)
      if (obj[i].checked)
        return obj[i].value;
  }
  return '';
}
function set_radio(obj, value) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    var len = obj.length;
    if (typeof(len) == 'undefined')
      obj.checked = (obj.value == value.toString());
    for (var i = 0;  i < len;  i++)
      obj[i].checked = (obj[i].value == value.toString());
  }
}

//  enable/disable items

function enable_control(obj, state) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj)
  obj.disabled = state ? false : true;
    //document.$form_name.filter.disabled = (search == 'string') ? false : true;
}

//  help

function help_click(id) {
  var help = get_element(id);
  if (help) {
    var visible = help.style.visibility;
    help.style.visibility = (visible == 'visible') ? 'hidden' : 'visible';
    var icon = get_element('i' + id.substring(1));
    if (icon)
      icon.className = "help-icon help-" + ((visible == 'visible') ? '' : 'no') + "help";
  }
}

//  calendar

function y2k(number) {
  return (number < 1000) ? number + 1900 : number;
}
var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());
var field = '';
function padout(number) {
  return (number < 10) ? '0' + number : number;
}
function restart() {
  if (field)
    eval('document.' + field + '.value = "" + padout(month - 0 + 1) + "/" + padout(day) + "/" + year');
  mywindow.close();
}
function popup_calendar(field2) {
  field = field2;
  mywindow = open('calendar.html', 'Calendar', 'location=no,status=no,directories=no,menubar=no,resizable=no,width=350,height=270');
  mywindow.location.href = 'calendar.html';
  mywindow.focus();
  if (mywindow.opener == null)
    mywindow.opener = self;
}

//  style attributes

function set_opacity(obj, value) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    if (document.all)
      obj.style.filter = (value < 1) ? 'alpha(opacity=' + (100 * (.75 * value)) + ')' : null;
    else if (typeof(obj.style.MozOpacity) != 'undefined')
      obj.style.MozOpacity = value;
    else
      obj.style.opacity = value;
//* For Safari (pre version 1.2), Konqueror: layer.style.KHTMLOpacity = .5;
  }
}
function set_display(obj, value) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj)
    obj.style.display = value;
}

//  shrink/expand

function shrink(url, id, state) {
  var obj;
  //  current state
  var current = ($('#r' + id).css('display') != 'none');
  //  toggle?
  if (state < 0)
    state = !current;
  if (current != state) {
    //  show/hide module
    if (state)
      $('#r' + id).slideDown();
    else
      $('#r' + id).slideUp();
    //  triangle
    $('#' + (state ? 'e' : 's') + id).show();
    $('#' + (state ? 's' : 'e') + id).hide();
    //  state persistence
    $.get(url, { name:id, value:state });
  }
  return false;
}
function shrinks(url, ids, state) {
  for (var i = 0;  i < ids.length;  i++)
    shrink(url, ids[i], state);
  return false;
}

//  locations

function get_left(obj) {
  var result = 0;
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    if (obj.offsetParent)
      while (true) {
        result += obj.offsetLeft;
        if (!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
    else if (obj.x)
      result += obj.x;
  }
  return result;
}
function get_top(obj) {
  var result = 0;
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    if(obj.offsetParent)
      while (true) {
        result += obj.offsetTop;
        if (!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
    else if(obj.y)
      result += obj.y;
  }
  return result;
}

//  popup

function popup(query) {
  mywindow = open('instance_popup.php?' + query, 'Edit', 'location=no,status=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=450');
  mywindow.location.href = 'instance_popup.php?' + query;
  mywindow.focus();
  if (mywindow.opener == null)
    mywindow.opener = self;
}

function preview_comment() {
  var data = 'name=' + $('#name').val();
  data += '&email=' + $('#email').val();
  data += '&web=' + $('#web').val();
  data += '&message=' + $('#message').val();
  $.ajax({ url     : "comment_preview.php",
           type    : "get",
           data    : data,
           success : function(msg){
                       $('#preview_comment').html(msg);
                     }
         });
}

try {
	$.fn.show_spinner = function() {
	  $(this).html('<img src="images/indicator.gif" class="indicator" />');
	    return this;
	}
	
	$.fn.hide_spinner = function() {
	  $(this).html('');
	}
	
	$(document).ready(function(){
	    $('#comment_switch').click(function(){
	      $('#comment_form').slideToggle();
	    });
	  });
} catch(e) {}