Pages = {
	currentpage: 0,
	totalpages: 0,
	requested: new Array(),

	incPage: function()
		{
			Pages.currentpage = Pages.getNextPage(Pages.currentpage);
		},

	decPage: function()
		{
			Pages.currentpage = Pages.getPrevPage(Pages.currentpage);
		},

	getNextPage: function(page)
		{
			nextpage = page + 1
			if(nextpage > Pages.totalpages)
				nextpage = 1;

			return nextpage;
		},

	getPrevPage: function(page)
		{
			nextpage = page - 1
			if(nextpage < 1)
				nextpage = Pages.totalpages;
			return nextpage;
		},

	setPages: function()
		{
  		$('currentpage').innerHTML = Pages.currentpage;
  	}
};

Albums = {
	oldselection: -1,

	setSelected: function(el)
		{
			if(Albums.oldselection != -1)
			{
			  Albums.oldselection.removeClassName("selected");
			  xajax_ajax_get_album_detail(el.getAttribute('name'));
			}
			el.addClassName("selected");
			Albums.oldselection = el;
		},

	isSelected: function(el)
		{
			return (Albums.oldselection == el);
		},

	openAlbum: function(el)
		{
			window.location.href = "/gallery/albums/view/" + el.getAttribute('name');
		}
};

Pictures = {
	oldselection: -1,
	mode: 1,
	smallpages : 0,

	activateSmallMode: function(pic)
		{
			Pages.totalpages = Pictures.smallpages;
			Pages.currentpage = parseInt(pic.getAttribute("sp")) + 1;

			$$('.picture').each( function (el) {
				el.setStyle( { width: '100px', height: '80px' });
				el.setAttribute("oldclass", el.getAttribute("class"));
				el.removeClassName("color_data1");
				el.removeClassName("color_data2");

				el.descendants()[0].setStyle({ width: '80px', height: '60px', padding: '10px 5px' } );
				el.descendants()[1].hide();
			});

			new Effect.ScrollIn('pictures', { duration: 0.2, x: (Pages.currentpage - 1) * 700 - $('pictures').scrollLeft });

			$$('.gui_complete').each(function (el) { el.hide(); });
			$$('.gui_small').each(function (el) { el.show(); });

			Pictures.mode = 2;

			Pictures.selectPicture(pic);
		},

	activateLargeMode: function()
		{
			Pages.totalpages = parseInt($('totalpages').innerHTML);
			Pages.currentpage = parseInt(Pictures.oldselection.getAttribute("cp")) + 1;

			new Effect.ScrollIn('pictures', { duration: 0.2, x: (Pages.currentpage - 1) * 786 - $('pictures').scrollLeft });

			$('pictures').setStyle( { height: '500px', width: '786px', left: '0px' });
			$('area').setStyle( { height: '500px' });
			$$('.picture').each( function (el) {

				if(el.getAttribute("cp") == Pictures.oldselection.getAttribute("cp"))
					new Effect.Move(el, { duration: 0.3, mode: 'absolute', x: el.getAttribute("cx"), y: el.getAttribute("cy")});
				else
					el.setStyle( { left: el.getAttribute("cx")+'px', top: el.getAttribute("cy")+'px' } );

				el.setStyle( { width: '175px', height: '140px' });
				el.addClassName(el.getAttribute("oldclass"));

				el.descendants()[0].setStyle({ width: '120px', height: '90px', padding: '10px 0px 0px 0px' } );
				el.descendants()[1].show();

			});

			$$('.gui_complete').each(function (el) { el.show(); });
			$$('.gui_small').each(function (el) { el.hide(); });

			$('picturecomments').innerHTML = "";
			$('pictureratings').innerHTML = "";
			$('picturetags').innerHTML = "";

			Pictures.mode = 1;

		  Pictures.oldselection.removeClassName("selected");
		  Pictures.oldselection = -1;
		},

	selectPicture: function(pic)
		{
			if(pic.getAttribute("a") == "1")
			{
				window.location.href = "/gallery/albums/view/" + pic.getAttribute("aid");
				return;
			}

			if(Pictures.oldselection != -1)
			  Pictures.oldselection.removeClassName("selected");

			pic.addClassName("selected");
			Pictures.oldselection = pic;

			var newheight = parseInt(pic.getAttribute("oh")) + 195

			if(newheight != parseInt($('pictures').style.height))
			{
				$('picturedetail').innerHTML = "";
				$('pictures').setStyle( { height: newheight + 'px', width: '700px', left: '43px' });
				$('area').setStyle( { height: newheight + 'px' });

				var newheight = parseInt(pic.getAttribute("oh")) + 115

				$$('.picture').each( function (el) {

					if(el.getAttribute("sp") == pic.getAttribute("sp"))
						new Effect.Move(el, { duration: 0.3, mode: 'absolute', x: el.getAttribute("sx"), y: newheight});
					else
					{
						el.setStyle( { left: el.getAttribute("sx")+'px', top: newheight+'px' } );
					}
				});
			}

			xajax_ajax_get_picture_detail(pic.getAttribute('name'));
		}
};

Effect.ScrollIn = Class.create();
Object.extend(Object.extend(Effect.ScrollIn.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    this.start(Object.extend({x: 0, y: 0}, arguments[1] || {}));
  },
  setup: function() {
    var scrollOffsets = (this.element == window)
                ? document.viewport.getScrollOffsets()
                : Element._returnOffset(this.element.scrollLeft, this.element.scrollTop) ;
    this.originalScrollLeft = scrollOffsets.left;
    this.originalScrollTop  = scrollOffsets.top;
  },
  update: function(pos) {
    this.element.scrollLeft = Math.round(this.options.x * pos + this.originalScrollLeft);
    this.element.scrollTop = Math.round(this.options.y * pos + this.originalScrollTop);
  }
});

function initAlbums() {
	initShare();

	$('pages').scrollLeft = (Pages.currentpage - 1) * 410;
	Albums.setSelected($('page1').childElements()[0]);

	Event.observe($('arrow_left'), 'click', function() {
		Pages.decPage();
		Pages.setPages();
		if(Pages.effect != 0)
			Pages.effect.cancel();
		Pages.effect = new Effect.ScrollIn('pages', { duration: 0.2, x: (Pages.currentpage - 1) * 410 - $('pages').scrollLeft });
	});
	Event.observe($('arrow_right'), 'click', function() {
		Pages.incPage();
		Pages.setPages();
		if(Pages.effect != 0)
			Pages.effect.cancel();
		Pages.effect = new Effect.ScrollIn('pages', { duration: 0.2, x: (Pages.currentpage - 1) * 410 - $('pages').scrollLeft });
	});
	$$('.album').each( function(el) {
		Event.observe(el, 'click', function() {
			if(Albums.isSelected(el))
				Albums.openAlbum(el);
			else
				Albums.setSelected(el);
		});
	});
}

function initPictures(smallpages) {
	initShare();
	Pictures.smallpages = smallpages;

	$('pictures').scrollLeft = (Pages.currentpage - 1) * 786;

	Event.observe($('complete_left'), 'click', function() {
		Pages.decPage();
		Pages.setPages();
		if(Pages.effect != 0)
			Pages.effect.cancel();
		Pages.effect = new Effect.ScrollIn('pictures', { duration: 0.2, x: (Pages.currentpage - 1) * 786 - $('pictures').scrollLeft });
	});
	Event.observe($('small_left'), 'click', function() {
		Pages.decPage();
		Pages.setPages();
		if(Pages.effect != 0)
			Pages.effect.cancel();
		Pages.effect = new Effect.ScrollIn('pictures', { duration: 0.2, x: (Pages.currentpage - 1) * 700 - $('pictures').scrollLeft });
	});
	Event.observe($('small_right'), 'click', function() {
		Pages.incPage();
		Pages.setPages();
		if(Pages.effect != 0)
			Pages.effect.cancel();
		Pages.effect = new Effect.ScrollIn('pictures', { duration: 0.2, x: (Pages.currentpage - 1) * 700 - $('pictures').scrollLeft });
	});
	Event.observe($('complete_right'), 'click', function() {
		Pages.incPage();
		Pages.setPages();
		if(Pages.effect != 0)
			Pages.effect.cancel();
		Pages.effect = new Effect.ScrollIn('pictures', { duration: 0.2, x: (Pages.currentpage - 1) * 786 - $('pictures').scrollLeft });
	});
	$$('.picture').each( function(el) {
		Event.observe(el, 'click', function() {
			if(Pictures.mode == 1 && el.getAttribute("a") != "1")
				Pictures.activateSmallMode(el);
			else
				Pictures.selectPicture(el);
		});
	});
	$$('.small_up').each( function(el) {
		Event.observe(el, 'click', function() {
			if(Pictures.mode == 2)
				Pictures.activateLargeMode();
		});
	});
}

function clickUserTag(e)
{
  xPosition = Event.pointerX(e) - $('thepicture').cumulativeOffset().left;
  yPosition = Event.pointerY(e) - $('thepicture').cumulativeOffset().top;

  xajax_ajax_post_user_tag(Pictures.oldselection.getAttribute("name"), xPosition, yPosition);
}

function removeUserTag()
{
	xajax_ajax_remove_user_tag(Pictures.oldselection.getAttribute("name"));
}

function initShare() {
	Pages.currentpage = Math.floor($('currentpage').innerHTML);
	Pages.totalpages = Math.floor($('totalpages').innerHTML);
	Pages.effect = 0;
}

/*
Author: Addam M. Driver
Date: 10/31/2006
*/

var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

// Rollover for image Stars //
function rating(num){
	sMax = 0;	// Isthe maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;
		}
	}

	s = num.id; // Get the selected star
	a = 0;
	for(i=1; i<=sMax; i++){
		if(i<=s){
			document.getElementById(i).className = "on";
			holder = a+1;
			a++;
		}else{
			document.getElementById(i).className = "";
		}
	}
}

// For when you roll out of the the whole thing //
function off(me){
	if(!preSet){
		for(i=1; i<=sMax; i++){
			document.getElementById(i).className = "";
		}
	}else{
		rating(preSet);
	}
}

// When you actually rate something //
function rateIt(me){
	if(!rated){
		preSet = me;
		sendRate(me);
		rating(me);
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel){
	xajax_ajax_set_rating(Pictures.oldselection.getAttribute("name"), parseInt(sel.id));
}

