var replaceAnchors= function (){

var anchors = document.getElementsByTagName('a');

    // loop through anchors, find other images in set, and add them to imageArray
    for (var i=0; i<anchors.length; i++){

	    var anchor = anchors[i];
		
	    if (!anchor.onclick && anchor.getAttribute('targetType')) { 
	    if (anchor.getAttribute('targetType') == 'content' ){
	        anchor.onclick = function () {getHTML(this.getAttribute('href'), this.getAttribute('tagId')); return false;}
	    } else {
		    if (anchor.getAttribute('targetType') == 'new' ){
		        anchor.target = "new";
	   	    } else {
	   	    anchor.target = "_top";
	   	    }	
	    }
	    }
    }
}
    
replaceAnchors();
	
var linkArray = new Array;

function getHTML(theUrl, target)
{
	if (target == null) {
		target = "content";
	}
  urchinTracker(theUrl);	
	hashListener.setHash(theUrl+"?"+target);
}
    
function init() {
	hashListener.init();
	new Ajax.Updater( 'content', 'main.html', { method: 'get', evalScripts:true,asynchronous:true, onComplete: hello});
}

hashListener.onHashChanged = function () {
	if (document.location.hash.substring(1) != "") {
		theUrl = document.location.hash.substring(1);
	} else {
		theUrl = "home.html";
	}

	hidden=document.location.href.substring(document.location.href.indexOf('?')+1);
  var ajax=  	new Ajax.Updater( document.location.href.substring(document.location.href.indexOf('?')+1) , theUrl, { method: 'get', evalScripts:true,asynchronous:true, onComplete: hello});
}	

function toggleScrollbar() {
  
  if ($('content').scrollHeight > 537) {    
    $('handle1').style.height = (537 * (537/$('content').scrollHeight))+"px";
    new Control.Slider('handle1','track1',{axis:'vertical',
    onSlide:function(v){javascript:move_up_3(v)},
    onChange:function(v){('content').innerHTML='changed! '+v}}); 
    document.getElementById('content').scrollTop = 0;
    Element.show('track1');
  }else {
    Element.hide('track1');
  }
}

function hello(re) {
  toggleScrollbar();
}
          function move_up_3(v) {
            document.getElementById('content').scrollTop = (document.getElementById('content').scrollHeight- 537) * v ;
          }
	
//=============================================================================
// Image Preloader
function ImagePreloader(images,callback)
{
	// store the callback
	this.callback = callback;

	// initialize internal state.
	this.nLoaded = 0;
	this.nProcessed = 0;
	this.aImages = new Array;

	// record the number of images.
	this.nImages = images.length;

	// for each image, call preload()
	for ( var i = 0; i < images.length; i++ ) 
		this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image)
{
	// create new Image object and add to array
	var oImage = new Image;
	this.aImages.push(oImage);
	
	// set up event handlers for the Image object
	oImage.onload = ImagePreloader.prototype.onload;
	oImage.onerror = ImagePreloader.prototype.onerror;
	oImage.onabort = ImagePreloader.prototype.onabort;
	
	// assign pointer back to this.
	oImage.oImagePreloader = this;
	oImage.bLoaded = false;
	oImage.source = image;
	
	// assign the .src property of the Image object
	oImage.src = image;
}
ImagePreloader.prototype.onComplete = function()
{
	this.nProcessed++;
	alert(this.nProcessed +" "+ this.nImages)
	if ( this.nProcessed == this.nImages ) {
    new Effect.Appear(callback);
  }
}
ImagePreloader.prototype.onload = function()
{
	this.bLoaded = true;
	this.oImagePreloader.nLoaded++;
	this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function()
{
	this.bError = true;
	this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function()
{
	this.bAbort = true;
	this.oImagePreloader.onComplete();

}

function onPreload(aImages)
{
  new Effect.Appear(callback);
}

function GalleryMenu(content)  {
  var html = '<ul id="horizontal">';
  
  content.each( function(menu){

var style = '';
   if (menu.isActive) {
      style = "style='border-bottom:0.5em solid white; color:white;font-size: 13px;'";
   } else {
      style = "";
   }

    html = html + '<li style="'+style+'font-size: 13px;"><a '+style+'href="'+menu.url+'" targetType="content"><em>'+menu.title+'</em></a></li>';
	});

  html = html+'</ul></li>';
  
  $('menu').innerHTML = html;
}

function ImageMenu(content)  {

  var html = '';
  content.each( function(image){  
    html = html + '<div style="text-align: center;float:left;width:120px;padding-right:10px;"><a border=0 targetType="content" href="'+image.url+'"><img border="0" src="'+image.thumbnail+'"/><p style="text-align: center;">'+image.title+'</p></a></div>';
  });
  
  $('imageMenu').innerHTML = html;
  
  toggleScrollbar();
}	

function ImageGallery(content)  {
   
 
  var html = '';

  content.each( function(image){  
    html = html + '<div style="text-align: center;float:left;width:120px;padding-right:10px;padding-top:10px;"><img src="'+image.thumb+'" rel="lightbox"/><p style="text-align: center;">&nbsp;</p></div>';
  });
  
  $('imageMenu').innerHTML = html;
  
  toggleScrollbar();
}	

var scroller = {
  init:   function() {
var aThumb = document.getElementById("thumb");
var scrolldiv=document.getElementById("content");
Drag.init(aThumb, null, 0, 0, 0, 150);
aThumb.onDrag = function(x, y) {
 scrolldiv.style.top=y * (-1) +"px";    
 
 }
  }
}

