// JavaScript Document
var category;
var hideNames = true;
Event.observe(window, "load", function() 
{
	try{
		if($('category')) {
			category = $('category').value;
		}
		// horizontal slider control
		
		// disable horizontal scrolling if text doesn't overflow the div
		if ($('album_select').scrollWidth > $('album_select').offsetWidth) {
			albumSlider = new Control.Slider('album_handle', 'album_slide', {
				onSlide: function(v) { scrollHorizontal(v, $('album_select'), albumSlider);  },
				onChange: function(v) { scrollHorizontal(v, $('album_select'), albumSlider); }
			});
			scrollRange = 	$('album_select').scrollWidth - $('album_select').offsetWidth;
			albumSlidable = true;
			$("photo_slide").appear();
		}
		
		Event.observe("album_left","mouseover",scrollLeft);
		Event.observe("album_left","mouseout",stopScroll);
		Event.observe("album_right","mouseover",scrollRight);
		Event.observe("album_right","mouseout",stopScroll);
	
		Event.observe($('open_albums'), "click", openAlbums);
	} catch(e) {
		
	}
});
var albumSlider;
var scrolling;
var scrollRange;
// scroll the element horizontally based on its width and the slider maximum value
function scrollHorizontal(value, element, slider) {
	element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth));
}

function scrollLeft() {
	scrolling = window.setInterval(function () {
		var element = $('album_select');
		var x = element.scrollLeft - 4;
		//$('album_select').scrollLeft = x;
		albumSlider.setValue(x/scrollRange);
												 },10);
}

function scrollRight() {
	scrolling = window.setInterval(function () {
		var element = $('album_select');
		var x = element.scrollLeft + 4;
		if(x < 0 ) return;
		//$('album_select').scrollLeft = x;
		albumSlider.setValue(x/scrollRange);
												 },10);
}

function stopScroll() {
  clearInterval(scrolling);
}

function openAlbums() {
	$('loading_bar').hide();
	$('images').style.visibility = "hidden";
	$('images').innerHTML = "";
	this.hide();
	if(albumSlidable) $('album_slide').show();
	$('albums').appear();
	$('photo_slide').hide();
	$('captions').hide();
}

var jsonPhotos;
var jsonShoot;
function getPhotosForImageFlow(id, path, name) {
	$('album_slide').hide();
	$('albums').dropOut();
	$('images').style.visibility = "hidden";
	$('images').innerHTML = "";
	$('photo_slide').hide();
	$('captions').hide();
	$('loading_bar').show();
	flowLoaded = false;
	numImagesLoaded = 0;
	jsonShoot = {'path':path, 'id':id, 'name':name};
	new Ajax.Request('get_photos.php?id='+id+"&&portfolio=true", {
					 method:'get',
					 onSuccess: function(transport){
 					    jsonPhotos = transport.responseText.evalJSON();
						if(jsonPhotos.success == 1) {
							if(jsonPhotos.images.length > 0) {
								totalImages = jsonPhotos.images.length;
								for(i=0; i<jsonPhotos.images.length; i++) {
									if(jsonPhotos.images[i].active == 1) {
										var element = document.createElement('img');
										$('images').appendChild(element);
										element.src = "libraries/reflect_v21.php?img="+jsonPhotos.portfolio + jsonPhotos.category + "/" + jsonPhotos.album + "/tn_" + jsonPhotos.images[i].path + "&&merge=true";
										element.alt = jsonPhotos.images[i].name;
										element.id = jsonPhotos.images[i].id;
										if(element.complete) {
											imageLoaded();	
										} else {
											element.onerror = imageLoaded;
											element.onload = imageLoaded;
										}
									} else {
										totalImages--;	
									}
								}
								$("total_count").innerHTML = totalImages;
								$("loaded_count").innerHTML = "0";
							} else {
								$('open_albums').appear();
								$('loading_bar').style.visibility = "hidden";
								$('images').innerHTML = "<p style='text-align:center'>This album has no images yet.</p>";
								$('images').style.visibility = "visible";
							}
						}
					 }
					 });
}

// Sin Creation addition for Ajax
function imageLoaded() {
	numImagesLoaded++;
	$("loaded_count").innerHTML = numImagesLoaded;
	if(numImagesLoaded == totalImages) {
		flowLoaded = true;
		$('open_albums').appear();
		refresh(true);
	}
}

// Prepare window
var winPhoto;
function photoWindow(id) {
	for(var i=0; i<jsonPhotos.images.length; i++) {
		if(id == jsonPhotos.images[i].id) {
			break;	
		}
	}
	var img = document.createElement('img');
	img.src = 'images/portfolio/' + category + "/" + jsonShoot.path + "/" + jsonPhotos.images[i].path;
	img.alt = jsonPhotos.images[i].name;
	Modal.prototype.content.innerHTML = "";
	Modal.prototype.content.appendChild(img);
	Modal.prototype.start()
}