﻿var Gallery = new function () {

	this.Position = 0;
	var me = this;

	//attachhomeevents
	this.AttachHomeEvents = function () {
		var aGalleryLinks = f.GetElementsByClassName('a', 'g', 'divHome');
		for (var i = 0; i < aGalleryLinks.length; i++) {
			f.AttachEvent(aGalleryLinks[i], 'click', function (event) { Gallery.ShowGallery(f.GetObjectFromEvent(event)); });
		}

	}

	//show and hide gallery
	this.ShowGallery = function (o) {

		var sGallery = s.Substring(o.id, 3);
		var sGalleryInfo = f.GetValue('hid' + sGallery);

		//get info and set gallery name
		var aGalleryInfo = sGalleryInfo.split('##');
		var sGalleryName = aGalleryInfo[0];
		f.GetObject('h2GalleryTitle').innerHTML = sGalleryName;


		//gallery - build the thumbs
		if (sGalleryInfo.indexOf('soundslider.swf') < 0) {

			var sHTML = '';
			for (var i = 1; i < aGalleryInfo.length - 1; i++) {
				var aBits = aGalleryInfo[i].split('|');
				var iPosition = i - 1;
				sHTML += '<a href="#" id="a_' + iPosition + '"';
				if (iPosition == 0) {
					sHTML += ' class="selected"';
				}
				sHTML += '>';

				sHTML += '<img id="i_' + iPosition + '" src="' + aBits[0] + '" alt="' + aBits[1] + '"/>';
				sHTML += '</a>';
			}

			f.GetObject('divThumbs').innerHTML = sHTML;

			me.Position = 0;
			me.SetImageFromPosition(0);
			f.Show('divGallery');
		}


		//soundslide
		if (sGalleryInfo.indexOf('soundslider.swf') > 0) {

			var sSliderFile = aGalleryInfo[1];
			f.GetObject('soundslider').setAttribute('movie', sSliderFile + '?size=2&format=xml');
			//f.GetObject('soundslider2').setAttribute('src', sSliderFile + '?size=2&format=xml');
			f.Show('divSlideshow');
		}


		f.Hide('divHome');
		f.Show('h2GalleryTitle');
		f.Show('aHome');
		f.SetClass(document.body, 'gallery');
		me.AttachEvents();
	}

	this.HideGallery = function () {
		f.RemoveClass(document.body, 'gallery');
		if (f.Visible('divSlideshow')) {
			f.GetObject('soundslider').StopPlay();
		}
		f.Hide('divGallery');
		f.Hide('h2GalleryTitle');
		f.Hide('aHome');
		f.Hide('divSlideshow');
		f.Show('divHome');

	}



	//show
	this.Show = function (oImage) {
		me.Position = oImage.id.split('_')[1];
		me.SetImageFromPosition(me.Position);
		Gallery.SetSelected();
	}


	//set image from position
	this.SetImageFromPosition = function (iPosition) {
		var oThumb = f.GetObject('i_' + iPosition);
		var sThumbSource = oThumb.src;
		var sImageSource = s.Replace(sThumbSource, '/thumbs/', '/images/');
		f.GetObject('imgMain').src = sImageSource;
		f.GetObject('imgMain').alt = oThumb.alt;
		f.GetObject('tdCaption').innerHTML = oThumb.alt;
	}

	this.SetContentWidth = function () {
		f.GetObject('tdImage').style.width = e.GetPosition('imgMain').Width + 'px';
	}


	//set selected link
	this.SetSelected = function () {
		var aLinks = f.GetObject('divThumbs').getElementsByTagName('a');
		for (var i = 0; i < aLinks.length; i++) {
			f.SetClassIf(aLinks[i], 'selected', aLinks[i].id == 'a_' + me.Position);
		}
	}


	//previous and next
	this.Previous = function () {
		if (n.SafeInt(me.Position) > 0) {
			me.Position = n.SafeInt(me.Position) - 1;
			me.SetImageFromPosition(me.Position);
			Gallery.SetSelected();
		}
	}

	this.Next = function () {


		var iNext = n.SafeInt(me.Position) + 1;
		if (f.GetObject('i_' + iNext)) {
			me.Position = iNext;
			me.SetImageFromPosition(me.Position);
			Gallery.SetSelected();
		}

	}


	//attachevents
	this.AttachEvents = function () {

		var aThumbs = f.GetObject('divThumbs').getElementsByTagName('img');
		for (var i = 0; i < aThumbs.length; i++) {
			f.AttachEvent(aThumbs[i], 'click', function (event) { Gallery.Show(f.GetObjectFromEvent(event)); });
		}
	}



}
