//<!--
	jQ(document).ready(function(){
		ONAPI.getGalleryList({
			data: {gallery_list: cobj_pg_galleryList, user_id: userId},
			callback:function(data) {
				jQ('.gall_titles:first').empty();
				for (i=0; i < data.length; i++) {
					var name = (data[i].name.length > 30) ? data[i].name.substring(0,30) : data[i].name;
					jQ('.gall_titles:first').append('<span style="cursor:pointer;" class="click_gall_chooser" id="catid_'+data[i].id+'" alt="'+name+'">'+name+'</span><br/>');
				}
				gallery_bind_cat();
			}
		});
	});
		function gallery_bind_cat(){
			gallery_init_chooser();
			jQ('.click_gall_chooser').unbind().bind('click', function(){
				jQ('.selected_gall').removeClass('selected_gall');
				jQ(this).addClass('selected_gall');
				catid = this.id;	
				catid = parseInt(catid.split('_')[1]);
					init_state = false;
					setThumbSize();
					ONAPI.getPhotos({
						data: {user_id:userId, cat_id:catid},
						callback: function(data){
							crookedImages = data;
							crookedPage = 0
							getReadyToRock();
							paginate();
							init_state = true;
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){
							alert(textStatus);
						}
					});
			});
		}
		function gallery_init_chooser()
		{
			catid = jQ('.gall_titles > .click_gall_chooser:first').attr('id');
			jQ('.gall_titles > .click_gall_chooser:first').addClass('selected_gall');
			catid = parseInt(catid.split('_')[1]);
			init_state = false;
			setThumbSize();

			ONAPI.getPhotos({
				data:{user_id:userId, cat_id:catid},
				callback: function(data){
					crookedImages = data;
					crookedPage = 0
					getReadyToRock();
					paginate();
					init_state = true;
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					alert(textStatus);
				}
			});			
		}
		
		var crookedPage = 0;
		var crookedImages;
		var crookedGwidth;
		var init_state;
		
		function setThumbSize(){
			if (init_state == true) {
				gallery_init();
			} else {
				jQ('.crookedImage').css({height: crookedThumbH+'px', width: crookedThumbW+'px'});
				jQ('.crookedImageHolder').css({height: crookedThumbH+'px', width: crookedThumbW+'px'});
			}
		}
		
		
		function getReadyToRock(){
			jQ('#crookedG').empty();
			var start = crookedPage*photosPerPage;
			var end = start + parseInt(photosPerPage);
			if (end > crookedImages.length) {
				end = crookedImages.length;
			}
			var k=0;
				for (i = start; i < end; i++) {
					if (crookedImages) {
						var src = crookedImages[i].cdn_url;
						if (crookedImages[i].name) {
							var name = crookedImages[i].name;
						} else {
							var name = '';
						}
						var rand=Math.floor(Math.random()*99999);
						jQ('#crookedG').append('<div class="crookedImageHolder" style="height:'+crookedThumbH+'px;width:'+crookedThumbW+'px;position:relative;">\
													<img style="position:absolute;top:'+parseInt((crookedThumbH/2)-8)+'px;left:'+parseInt((crookedThumbW/2)-8)+'px;" src="/resources/img/objects/arrowsWhite.gif"/>\
													<img class="crookedImage" style="display:none;" height="'+crookedThumbH+'" width="'+crookedThumbW+'" title="' + name + '" id="' + src + '" src="' + src + '?h=' + crookedThumbH + '&w=' + crookedThumbW + '&b=c&bg=255255255&'+rand+'"/>\
												</div>');

						jQ('.crookedImage:last').bind('load readystatechange', function(e){
						    if (this.complete || (this.readyState == 'complete' && e.type == 'readystatechange')) {
								if (jQ.browser.msie) jQ(this).prev().remove();
								jQ(this).fadeIn(100, function(){
									if (!jQ.browser.msie) jQ(this).prev().remove();
								});
							}
						});
						
						if (jQ('#topBar').length < 1) {
							jQ('.crookedImage:last').click(function(){
								simpleLightBox(this);
							});
						}
					}
				}
			jQ('#crookedG').append('<div style="clear:both;"></div>');
		}
		
		function simpleLightBox(img){
			var src = jQ(img).attr('id');
			var title = jQ(img).attr('title');
			if (jQ.browser.msie == true) {
				var height = 440;
				var width = 580;
				jQ('body').append('<div id="lightboxBg"></div><div id="lightboxContent"><div id="imgSpacer" style="height:'+height+'px;"></div><img style="height:440px;display:none;" src=""/><div id="lightBoxInfo"><div id="imageTitle">' + title + '</div><img id="closeLightBox" src="/resources/img/delete_photo.png"></div></div>');
			} else {
				var height = 460;
				var width = 600;
				jQ('body').append('<div id="lightboxBg"></div><div id="lightboxContent"><div id="imgSpacer" style="height:'+height+'px;"></div><img style="height:460px;display:none;" src=""/><div id="lightBoxInfo"><div id="imageTitle">' + title + '</div><img id="closeLightBox" src="/resources/img/delete_photo.png"></div></div>');
			}
			
			jQ('#lightboxBg').css('width', jQ(document).width()).css('height', jQ(document).height());
			
			jQ('#loadingGalleryImage').remove(); //in case you navigate to a new image before the previous is finished loading
			jQ('#lightboxContent').append('<div id="loadingGalleryImage"><img src="/RESOURCES/IMG/ajaxload/arrowsWhite.gif"/></div>');
			
			var bgColor = jQ('.gall_chooser').css('background-color');
			bgColor = calculateBg(bgColor);
			jQ('#lightboxContent > img').attr('src', src+'?h='+height+'&w='+width+'&b=c&bg='+bgColor)
				.unbind().bind('load readystatechange', function(e){
					if (this.complete || (this.readyState == 'complete' && e.type == 'readystatechange')) {
						var img = this;
						jQ('#loadingGalleryImage').fadeOut(100, function(){
							jQ(this).remove();
							jQ(img).fadeIn(100);
							jQ('#imgSpacer').remove();
						});
					}
			});
			
			jQ('#closeLightBox').click(function() {
				jQ('#lightboxBg').fadeOut(300, function(){
					jQ(this).remove();
				});
				jQ('#lightboxContent').fadeOut(300, function(){
					jQ(this).remove();
				});
			});
			
			//center lightboxContent
			pdw = jQ('#lightboxContent').width();
			pdh = jQ('#lightboxContent').height();
			ww = jQ(window).width();
			wh = jQ(window).height();
			w = (ww - pdw) / 2;
			h = ((wh - pdh) / 2) + jQ(window).scrollTop();
			jQ('#lightboxContent').css('left', w+'px');
			jQ('#lightboxContent').css('top', h+'px');
			
			//fade in lightbox
			//bind clicks to exit lightbox
			jQ('#lightboxContent').fadeIn(300);
			jQ('#lightboxBg').css('opacity', 0.5).fadeIn(300).click(function() {
				jQ(this).fadeOut(300, function(){
					jQ(this).remove();
				});
				jQ('#lightboxContent').fadeOut(300, function(){
					jQ(this).remove();
				});
			});
		}
		
		function paginate(){
			jQ('#crookedPagination').remove();
			jQ('#crookedG').after('<br><div id="crookedPagination"></div>');
			var numPages = crookedImages.length/photosPerPage;
			if (numPages > 1) {
				for (i = 0; i < numPages; i++) {
					var j = i;
					j++;
					jQ('#crookedPagination').append('<span id="page_' + i + '" class="crookedPageButt">' + j + '</span>');
				}
				jQ('.selectedGalleryPage').removeClass('selectedGalleryPage');
				jQ('#page_' + crookedPage).addClass('selectedGalleryPage');
				jQ('.crookedPageButt').click(function(){
					page = jQ(this).attr('id');
					crookedPage = page.split('_')[1];
					jQ('.selectedGalleryPage').removeClass('selectedGalleryPage');
					jQ('#page_' + crookedPage).addClass('selectedGalleryPage');
					getReadyToRock();
				});
			}
		}
		
		function calculateBg(rgb) {
			var bg = rgb.replace('rgb(', '').replace(')', '');
			rgb = bg.split(',');
			bg = '';
			for (i=0;i<rgb.length;i++) {
				var n = rgb[i].replace(/^\s+|\s+$/g,"");
				if (n.length == 1) {
					bg += '00'+n;
				} else if (n.length == 2) {
					bg += '0'+n;
				} else if (n.length == 3) {
					bg += n;
				}
			}
			return bg;
		}
		//-->
