function searchClear(target, searchText)
{
  if (target.value == searchText)
  {
    target.value = '';
  }
}

function searchRestore(target, searchText)
{
  if (target.value == '')
  {
    target.value = searchText;
  }
}

function bar(url, title) {
	wasOpen = false;
	win     = window.open(url, title);    
	return (typeof(win) == 'object') ? true : false;
}

function myPopImage(imageURL,imageTitle,imageWidth,imageHeight) {

  PositionX = 30;
  PositionY = 30;
  
  ratio = imageWidth/imageHeight;
  maxHeight = screen.height - 105;
  maxWidth = maxHeight*ratio;
  if (maxWidth > screen.width-30) {
    maxWidth = screen.width - 80;
    maxHeight = maxWidth/ratio;
  }
    
  var opt='width=320,height=240,left='+PositionX+',top='+PositionY+',status=0,toolbar=0,menubar=0,location=0';
  imgWin=window.open('about:blank','null',opt);
  with (imgWin.document) {
    writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');
    writeln('<sc'+'ript>');
    writeln('function reSizeToImage(){');
    writeln('if (document.images[0].height > '+maxHeight+' || document.images[0].width > '+maxWidth+'){');
    writeln('height = '+maxHeight+';');
    writeln('width = '+maxWidth+';');
    writeln('document.images[0].width = '+maxWidth+';');
    writeln('document.images[0].height = '+maxHeight+';');
    writeln('window.resizeTo(width+40,height+100);}');
    writeln('else {');
    writeln('height = document.images[0].height;');
    writeln('width = document.images[0].width;');
    writeln('window.resizeTo(width+40,height+100);}}');
    writeln('function doTitle(){document.title="'+imageTitle+'";}');
    writeln('</sc'+'ript>');
    writeln('</head><body bgcolor="FFFFFF" onload="reSizeToImage();doTitle();self.focus()">');
    writeln('<img alt="'+imageTitle+'" title="'+imageTitle+'" src="'+imageURL+'" style="display:block; margin:0 auto;" /></body></html>');
    close();
  }
}

function myPopWindow(windowURL, windowName, windowFeatures) {
  var defaultFeatures = {"status": 0, "toolbar": 0, "location": 0, "menubar": 0,
                         "directories": 0, "resizeable": 1, "scrollbars": 1,
                         "width": 640, "height": 480};
  var features = '';

  for (i in windowFeatures) { defaultFeatures[i] = windowFeatures[i]; }
  for (i in defaultFeatures) { features += i + '=' + defaultFeatures[i] + ','; }

  window.open(windowURL, windowName, features);
  return false;
}

function switchBmk(n, css) {
  if (document.getElementById('bmk_header')) {
    var num = document.getElementById('bmk_header').getElementsByTagName('div').length;
    for (var i=1;i<=num;i++) {
      var elt = document.getElementById('bmk_title_' + i);
      var elc = document.getElementById('bmk_content_' + i);
      if (i == n) {
        elt.className = css + '_title active';
        elc.className = css + '_content visible';
      }
      else {
        elt.className = css + '_title';
        elc.className = css + '_content invisible';
      }
    }
  }
}

document.observe("dom:loaded", function() {
  if (window.location.hash && !window.location.hash.split('#')[1].empty()) {
    var hashValue = window.location.hash.split('#')[1];
    if (hashValue.indexOf('bmk') != -1) {
      var bmkVars = hashValue.split('-');
      switchBmk(bmkVars[1], bmkVars[0]);
    }
  }
});

function attrFnc(url, id) {
  var key = 0;
  var imgObj = new Element('img');
  var imgObjLink = new Element('a');
  var timer = null;
  var imgData = [];
  var imgTexts = [];
  new Ajax.Request(url, {
    method: 'post',
    onSuccess: function(transport) {
      var imgs = transport.responseXML.getElementsByTagName('image');
      if (imgs.length > 1) {
        timer = loop.delay(7, 1);
      }
      for (i=0;i<imgs.length;i++) {
        var texts = imgs[i].getElementsByTagName('text');
        var txtValue = '';
        for (j=0;j<texts.length;j++) {
          var textCnt = texts[j].text || texts[j].textContent;
          if (j == 0) {
            txtValue += textCnt;
          } else {
            txtValue += '<br />' + textCnt;
          }
        }
        imgTexts[i] = txtValue;
        imgData[i] = [];
        imgData[i]['src'] = imgs[i].getAttribute('src');
        imgData[i]['target'] = imgs[i].getAttribute('target');
      }
      imgObj.src = imgData[key]['src'];
      imgObjLink.href = imgData[key]['target'];
      imgObjLink.addClassName('img');
      $('img_cnt_text_'+id).update(imgTexts[key]);
      $('img_cnt_text_bg_'+id).setOpacity(0.7).setStyle({'height': $('img_cnt_text_'+id).getHeight() + 'px'});
      $('img_cnt_'+id).insert(imgObjLink);
//      $('prev-'+id).insert({'after': imgObjLink}).show();
//      $('next-'+id).show();
      imgObj.onload = function(){
        $('img_cnt_'+id).setStyle({'height': imgObj.getHeight() + 'px'});
      }
      imgObjLink.insert(imgObj);
//setObserve(key);
    }
  });
/*
  function setObserve(k) {
    var keyPrev, keyNext;
    if (imgData[k-1]) {
      keyPrev = k-1;
    } else {
      keyPrev = imgData.length - 1;
    }
    if (imgData[k+1]) {
      keyNext = k+1;
    } else {
      keyNext = 0;
    }
    $('prev-'+id).onclick = function(event) {
      window.clearTimeout(timer);
      changeImg(keyPrev);

    }
    $('next-'+id).onclick = function(event) {
      window.clearTimeout(timer);
      changeImg(keyNext);
    }
  }
*/
  function changeImg(k) {
    imgObj.fade({duration: 0.5});
    Element.update.delay(0.25, 'img_cnt_text_'+id, imgTexts[k]);
    Element.writeAttribute.delay(0.5, imgObj, 'src', imgData[k]['src']);
    Element.writeAttribute.delay(0.5, imgObjLink, 'href', imgData[k]['target']);
    Element.appear.delay(0.6, imgObj, {duration: 0.5, queue: 'end'});
    //setObserve(k);
  }

  function loop(k) {
    changeImg(k);
    if (imgData[k+1]) {
      timer = loop.delay(7, k+1);
    } else {
      timer = loop.delay(7, 0);
    }
  }
}
