var tcPhotoGalleryHelper = {

	init:function(tcpg) {
		tcpg.images = tcpg.allImages;

		$('.tc-photo-gallery-filter-categories .tc-photo-gallery-filter-category a').click(function(e){
            var cat = -1;
            var clname = $(this).attr('class').match(/tcpgcat-\d+/);
            if (clname) cat = clname[0].substr(8);
			$('.tc-photo-gallery-filter-category select option').attr('selected','');
			$('.tc-photo-gallery-filter-category select option[value="'+cat+'"]').attr('selected','selected');
			tcPhotoGalleryHelper.filter(tcpg, cat, null, null);
			e.stopPropagation();
			return false;
		});
		$('.tc-photo-gallery-nav-prev').click(function(e){
			tcPhotoGalleryHelper.loadImage(tcpg, tcpg.image-1);
			e.stopPropagation();
			return false;
		});
		$('.tc-photo-gallery-reflink').click(function(e){
			tcPhotoGalleryHelper.loadImage(tcpg, tcpg.image);
			e.stopPropagation();
			return false;
		});

		/* Comment/Remove the following code to prevent clicks on the photo
         * reference link from being intercepted.
		 */
		$('.tc-photo-gallery-nav-next').click(function(e){
			tcPhotoGalleryHelper.loadImage(tcpg, tcpg.image+1);
			e.stopPropagation();
			return false;
		});
	},

	filter:function(tcpg, cat, attrs, keywords) {
		var i, j, k, len, len2;
		var images, tImages;

		images = tImages = tcpg.allImages;
		if (cat >= 0) {
			images = new Array();

			len = tImages.length;
			for (i = 0, j = 0; i < len; i++) {
				if (tcpg.allImages[i][5] == cat) {
					images[j++] = tImages[i]
				}
			}
		}

		if (attrs) {
			len2 = attrs.length;
			for (k = 0; k < len2; k++) {
				if (typeof attrs[k] == 'string') attrs[k] = attrs[k].toLowerCase();
			}
			tImages = images;
			images = new Array();
			len = tImages.length;
			for (i = 0, j = 0; i < len; i++) {
				var matches = 0;
				for (k = 0; !next && k < len2; k++) {
					if (!attrs[k] || attrs[k] == -1) {
						matches++;
					} else {
						for (l = 0; !next && l < tImages[i][tcpg.firstAttribute+k].length; l++) {
							if (attrs[k] == tImages[i][tcpg.firstAttribute+k][l].toLowerCase()) {
								matches++;
								break;
							}
						}
					}
				}
				if (matches == len2) {
					images[j++] = tImages[i];
				}
			}
		}
	
		if (keywords) {
			len2 = keywords.length;
			for (k = 0; k < len2; k++) {
				keywords[k] = keywords[k].toLowerCase();
			}

			len = images.length;
			tImages = images;
			images = new Array();
			var next = false;
			for (i = 0, j = 0; i < len; i++) {
				for (k = 0; !next && k < len2; k++) {
					if (!next && (keywords[k] | 0) == keywords[k]) {
						if (keywords[k] == tImages[i][0]) {
							images[j++] = tImages[i];
							next = true;
						} 
					} else if (tcpg.showTitle && tImages[i][1].toLowerCase().indexOf(keywords[k]) != -1) {
						images[j++] = tImages[i];
						next = true;
					} else if (tcpg.showDescription && tImages[i][2].toLowerCase().indexOf(keywords[k]) != -1) {
						images[j++] = tImages[i];
						next = true;
					} else if (tImages[i][3].toLowerCase().indexOf(keywords[k]) != -1) {
						images[j++] = tImages[i];
						next = true;
					} else {
						for (l = 0; !next && l < tcpg.cAttributes; l++) {
							for (m = 0; !next && m < tImages[i][tcpg.firstAttribute+l].length; m++) {
								if (tImages[i][tcpg.firstAttribute+l][m].toLowerCase().indexOf(keywords[k]) != -1) {
									images[j++] = tImages[i];
									next = true;
								}
							}
						}
					}
				}
				next = false;
			}
		}

		tcpg.images = images;
		$('.tc-photo-gallery-nav-count').text(tcpg.images.length);
		this.loadImage(tcpg, 0);
	},

	loadImage:function(tcpg, image) {
		if (image >= tcpg.images.length) image = 0;
		else if (image < 0) image = tcpg.images.length-1;
		tcpg.image = image;

		if (tcpg.images.length == 0) {
			tcpg.images[0] = this.imageDefault;
			$('.tc-photo-gallery-nav-current').text(0);
			$('.tc-photo-gallery-image').hide();
			$('.tc-photo-gallery-image-none').show();
		} else {
			$('.tc-photo-gallery-nav-current').text(image+1);
			$('.tc-photo-gallery-image').show();
			$('.tc-photo-gallery-image-none').hide();
		}

		if (tcpg.showReference) {
			$('.tc-photo-gallery-detail-reference .tc-photo-gallery-reflink').attr('href', tcpg.refLinkBase + (image+1));
			$('.tc-photo-gallery-detail-reference .tc-photo-gallery-value').text(tcpg.images[image][0]);
		}
        if (tcpg.showTitle) $('.tc-photo-gallery-detail-title').text(tcpg.images[tcpg.image][1]);
        if (tcpg.showDescription) $('.tc-photo-gallery-detail-description').text(tcpg.images[tcpg.image][2]);
		$('.tc-photo-gallery-image').attr("src",tcpg.images[image][4]);
		$('.tc-photo-gallery-image').attr("alt",tcpg.images[image][1]);
		if (tcpg.enableImageLinks) {
			$('.tc-photo-gallery-imglink').attr("href",tcpg.images[image][4]);
		}
		if (tcpg.primary>=0 && tcpg.images[image][tcpg.firstAttribute+tcpg.primary]) $('.tc-photo-gallery-detail-primary .tc-photo-gallery-value').text(tcpg.images[image][tcpg.firstAttribute+tcpg.primary].join(', '));
		if (tcpg.secondary>=0 && tcpg.images[image][tcpg.firstAttribute+tcpg.secondary]) $('.tc-photo-gallery-detail-secondary .tc-photo-gallery-value').text(tcpg.images[image][tcpg.firstAttribute+tcpg.secondary].join(', '));
	},

	imageDefault:['', '', '', [''], ['']] 

}

