var imageArray = new Array;
var activeImage;

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
    for(i = 0; i < this.length; i++){
        for(j = this.length-1; j>i; j--){        
            if(this[i][0] == this[j][0]){
                this.splice(j,1);
            }
        }
    }
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
//	Lightbox Class Declaration
//	- initialize()
//	- start()
//	- changeImage()
//	- resizeImageContainer()
//	- showImage()
//	- updateDetails()
//	- updateNav()
//	- enableKeyboardNav()
//	- disableKeyboardNav()
//	- keyboardNavAction()
//	- preloadNeighborImages()
//	- end()


var prevbox = Class.create();

prevbox.prototype = {
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Calls updateImageList and then
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function() {	

		this.updateImageList();
		//this.

	},
		updateImageList: function() {		
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');
		var areas = document.getElementsByTagName('area');
		var prevboxImg = document.getElementById('prevboxImage');
		// loop through all anchor tags
		Element.hide('prevLi');
		Element.hide('nextLi');
		Element.hide('prevPage');
		Element.hide('nextPage');
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('prevbox'))){
				anchor.onclick = function () {myprevbox.start(this); return false;}
			}
			
		}
		if(prevboxImg && (prevboxImg.src=='' || prevboxImg.src=='NULL')){
			Element.hide('prevboxImage');
			Element.setInnerHTML( 'numberDisplay', "Выберите картинку для просмотра");
		}
		//	anchor.onload = function () {myprevbox.start(this); return false;}
			//alert(imArray[0]);
			//this.startFirst(imArray[0]);
			//this.start(imArray[0]);
		//}
		// loop through all area tags
		// todo: combine anchor & area tag loops
		for (var i=0; i< areas.length; i++){
			var area = areas[i];
			
			var relAttribute = String(area.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (area.getAttribute('href') && (relAttribute.toLowerCase().match('prevbox'))){
				area.onclick = function () {myprevbox.start(this); return false;}
			}
		}
		
		
	},
	lightBoxSrc: function(url){
		var anchors = document.getElementsByTagName('a');
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (relAttribute.toLowerCase().match('lightbox')){
				//alert(url);
				Element.setHref(anchor, url);
				//anchor.src=url;
			}
			
		}
	},

	start: function(imageLink) {	
		imageArray = [];
		imageNum = 0;		

		if (!document.getElementsByTagName){ return;}
		var anchors = document.getElementsByTagName( imageLink.tagName);

		// if image is NOT part of a set..
		if((imageLink.getAttribute('rel') == 'prevbox')){
			// add single image to imageArray
			imageArray.push(new Array(imageLink.getAttribute('href'), imageLink.getAttribute('title')));			
		} else {
		// if image is part of a set..

			// 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.getAttribute('href') && (anchor.getAttribute('rel') == imageLink.getAttribute('rel'))){
				//alert(anchor.getAttribute('title'));
					imageArray.push(new Array(anchor.getAttribute('href'), anchor.getAttribute('title')));
				}
			}
			imageArray.removeDuplicates();
			while(imageArray[imageNum][0] != imageLink.getAttribute('href')) { imageNum++;}
		}

		this.changeImage(imageNum);
	},
	changeImage: function(imageNum) {	
		activeImage = imageNum;	// update global var
		Element.hide('prevLi');
		Element.hide('nextLi');
		Element.hide('prevPage');
		Element.hide('nextPage');
		imgPreloader = new Image();
				// once image is preloaded, resize image container
		imgPreloader.onload=function(){
			Element.setSrc('prevboxImage', imageArray[activeImage][0]);
			Element.show('prevboxImage');
			
			imgPreloader.onload=function(){};	//	clear onLoad, IE behaves irratically with animated gifs otherwise 
		}
		imgPreloader.src = imageArray[activeImage][0];
		this.lightBoxSrc(imageArray[activeImage][0].replace(/\/m_im\w*/gi,''));
		initLightbox();
		this.showImage();
	},
	showImage: function(){
		//Element.hide('loading');
		//new Effect.Appear('prevboxImage', { duration: resizeDuration, queue: 'end', afterFinish: function(){	myprevbox.updateDetails(); } });
		myprevbox.updateDetails();
	
	},

	//
	//	updateDetails()
	//	Display caption, image number, and bottom nav.
	//
	updateDetails: function() {
	
		// if caption is not null
		if(imageArray[activeImage][1]){
			//Element.show('caption');
			//alert(imageArray[activeImage][1]);
			Element.setInnerHTML('caption', imageArray[activeImage][1]);
		}
		
		// if image is part of set display 'Image x of x' 
		if(imageArray.length > 1){
			Element.show('numberDisplay');
			Element.setInnerHTML( 'numberDisplay', "Фото " + eval(activeImage + 1) + " из " + imageArray.length+".  Нажмите право/влево для перехода вперед/назад.");
		}

		myprevbox.updateNav();
	},

	//
	//	updateNav()
	//	Display appropriate previous and next hover navigation.
	//
	updateNav: function() {

		//Element.show('hoverNav');				

		// if not first image in set, display prev image button
		if(activeImage != 0){
			Element.show('prevLi');
			document.getElementById('prevLi').onclick = function() {
				myprevbox.changeImage(activeImage - 1); return false;
			}
		}
		if(activeImage == 0){
			Element.show('prevPage');
		}

		// if not last image in set, display next image button
		if(activeImage != (imageArray.length - 1)){
			Element.show('nextLi');
			document.getElementById('nextLi').onclick = function() {
				myprevbox.changeImage(activeImage + 1); return false;
			}
		}
		if(activeImage == (imageArray.length - 1)){
			Element.show('nextPage');
		}
		
		//this.enableKeyboardNav();
	}
}

// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
function pause(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}
/*
function pause(numberMillis) {
	var curently = new Date().getTime() + sender;
	while (new Date().getTime();	
}
*/
// ---------------------------------------------------
//function sz(first){
//myprevbox.start(first);
//}
function initprevbox() { myprevbox = new prevbox(); }
Event.observe(window, 'load', initprevbox, false);


