jQuery.noConflict();

(function (jQuery) {
jQuery.fn.photoStack = function() {
        var self = this;
        self.lastPicId = 0;
				this.init = function() {
					
					self.user = new Array();
					self.album = new Array();
					self.stack = null;
					
					self.availablePics = new Array();
					self.curPicDetails = new Array();
					self.expire = null;
					self.preload = new Array();
					self.direction = null;
					self.xhr = null;
					self.hasAborted = null;
					
					self.cleanHtml(true, true);
				},
				
				this.loadingIn = function() {
					jQuery('#picture-details').addClass('loading_cnt');					
				},
				
				this.loadingOut = function() {
					jQuery('#picture-details').removeClass('loading_cnt');
				},
				
				this.load = function(picId, userId, fullLoad) {
					
					if(!fullLoad) {
						if(picId != self.currentPicId) return;
					}
					
					self.xhr = jQuery.ajax({
					url: PUBLIC_URL + 'ajax/get-picture-details',
					type: 'POST',
		            dataType: 'json',
		            data: {picId: picId, userId: userId , fullLoad: fullLoad}, 
					success: function(transport) {
							if(typeof transport.hasErrors == 'undefined' || transport.hasErrors) {
								self.redirect(transport.errorNo);
								return false;
							}

							if(fullLoad) {
								self.stack = transport.pics;
								self.album = transport.album;
								self.user = transport.user;
								self.expire = Math.round(new Date().getTime() / 1000) + 60;
								self.updatePic(picId);
							}
													
							self.curPicDetails = transport.picDetails;
							self.updateDetails(picId);
							self.loadingOut();
	
						},
					error: function(obj, status, e) {
							return true;	
						}	
		
					});
				},
				
				this.updatePic = function(picId) {
					
					var list = self.stack.details;
					var id = self.stack.availablePics[picId];
					var isIE6 = (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7) ? true : false;
					jQuery('#ajax_src').attr("src", list[id].src);
					imgCnt = jQuery('#picture-main-src');
					
					if (!isIE6) {
						jQuery('#ajax_src').each(function() {
							if (!this.complete) {
								imgCnt.addClass('loading_cnt');
								this.onload = function() { 
									imgCnt.removeClass('loading_cnt');
								};
							}
						});
					}
					
					self.preparePreloading(picId);
				
					//owner
					jQuery('#ajax_edit').attr("onClick", "document.location='" + list[id].edit + "'");
					jQuery('#ajax_delete').attr("onClick", "document.location='" + list[id].del + "'");
					
					//album link
					jQuery('#ajax_album_link').attr("href", self.album.link);
					jQuery('#ajax_album_link').html(self.album.name);
					
					//user link
					jQuery('#ajax_user_link').attr("href", self.user.link);
					jQuery('#ajax_user_link').html(self.user.username);
					
					//pos
					jQuery('#ajax_pic_pos').html(list[id].posText);
					
					//nex link
					jQuery('#ajax_next').html(lang['21707_next']);
					
					//prev link
					jQuery('#ajax_prev').html(lang['21706_previous']);
					
					//date added
					jQuery('#ajax_date_added').html(list[id].time);
														
					jQuery('#pageData').show();
					
					if(isIE6) jQuery('#pic-ads').css('display','block');
					next = self.updateNav(picId);
					jQuery('#deletePhoto').html('<a onclick="delThisPic('+picId+','+self.user.id+','+next+')" id="ajax_delete_photo" href="javascript:void(0)"><strong class="f_red">Delete photo</strong></a>');
				},
				
				this.updateDetails = function(picId) {
					
					//comments
					jQuery('#ajax_comments').html(self.curPicDetails.comments);
					
					//rate
					jQuery('#starRate').html(self.curPicDetails.rate);
					
					//send-to-friend
					jQuery('#ajax_sendToFriend').html(self.curPicDetails.sendToFriend);
					
					//disorders
					jQuery('#ajax_disorders').html(self.curPicDetails.disorders);
					
					//profile pic
					jQuery('#ajax_profilePic').html(self.curPicDetails.profilePic);
					
					//tags
					jQuery('#ajax_tags').html(self.curPicDetails.tags);
					
					//desc
					jQuery('#ajax_description').html(self.curPicDetails.descriptionHtml);
					
					//meta
					jQuery('meta[name=description]').attr("content", self.curPicDetails.desc);
					jQuery('meta[name=keywords]').attr("content", self.curPicDetails.keywords);
					document.title = self.curPicDetails.title;
				},
				
				this.updateNav = function(picId){
					
					pos = self.stack.availablePics[picId];
					ret = null;

					if(pos == 1) {
						linkPrev = 'javascript:stack.showPicture('+self.album.prev+','
																+ self.user.id + ', false, "prev");';
						langPrev = lang['22316_previous_album'];										
					} else {
						linkPrev = 'javascript:stack.showPicture('+self.stack.details[pos - 1].id+','
																+ self.user.id + ', false, "prev");';
						langPrev = lang['21706_previous'];										
					}
					
					if(pos == self.album.totalPics) {
						linkNext = 'javascript:stack.showPicture('+self.album.next+','
																+ self.user.id + ', false, "next");';
						langNext = lang['22315_next_album'];
						ret = self.album.next;
					} else {
						linkNext = 'javascript:stack.showPicture('+self.stack.details[pos + 1 ].id+','
																+ self.user.id + ', false , "next");';
						langNext = 	lang['21707_next'];
						ret = self.stack.details[pos + 1 ].id;
					}
					
					//replace
					jQuery('#ajax_prev').attr('href', linkPrev);
					jQuery('#ajax_prev').html(langPrev);
										
					jQuery('#ajax_next').attr('href', linkNext);
					jQuery('#ajax_picture_link').attr('href', linkNext);
					jQuery('#ajax_next').html(langNext);
					
					return ret;
				}
				
				this.redirect = function(errorNo) {
					switch(errorNo) {
						case 1: 
						case 3:
						case 4: url = 'error/not-found';
								break;
						case 2: url = 'error/profile-disabled';
								break;
						case 5: url = 'error/forbidden';
								break;					
						default: url = 'error/not-found'; 
					}
					
					document.location = PUBLIC_URL + url;
				},
				
				this.showPicture = function(picId, userId, fromUrl, direction) {
					isIE6 = (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7) ? true : false;					
					if(self.lastPicId > 0 && picId == self.lastPicId) return;		
					var tsNow = Math.round(new Date().getTime() / 1000);
					
					self.direction = direction;
					self.preload = new Array();
					if(fromUrl && false != (tmp = self.checkFragment())) picId = tmp;
					
					if(self.xhr && self.xhr.readyState < 4) {
						self.hasAborted = true;
						self.xhr.abort();
					}
					self.currentPicId = picId;
					if(self.stack != null && 
							self.stack.availablePics[picId] && self.expire > tsNow) {
						self.cleanHtml(false, isIE6);
						self.updatePic(picId);
						if(!isIE6) self.loadingIn();
						setTimeout(function () { self.load(picId, userId, 0) }, 2000);
					} else {
						if(!isIE6) self.loadingIn();
						self.init(picId, userId);
						self.load(picId, userId, 1);
					}
					self.updateFragment(picId);
					self.lastPicId = picId;
				},
				
				this.preparePreloading = function(picId) {
					
					if(self.stack == null) return;
					
					if(self.direction != null && self.direction == 'prev') {
						if((pos = self.stack.availablePics[picId]) > 1) 
							self.preload.push(self.stack.details[pos - 1 ].src);
					} else {
						if((pos = self.stack.availablePics[picId]) < self.album.totalPics) 
							self.preload.push(self.stack.details[pos + 1 ].src);
					}
				
				},
				
				this.enablePreloading = function() {
					jQuery('#ajax_src').load( function() {
						self.preloadImg();
					});
				}
				
				this.checkFragment = function() {
					tmp = document.location.toString().split('#');
					if(tmp[1] && parseInt(tmp[1]) != NaN) return tmp[1];
					return false; 
				},
				
				this.updateFragment = function(picId) {
					tmp = document.location.toString().split('#');
					if(tmp[1]) window.location.href = tmp[0] + '#' + picId;
					else window.location.href += '#' + picId; 
				},
				
				this.preloadImg = function() {
					if(self.preload[0]) {	
						jQuery('#preload_img').attr('src', self.preload.shift());
					}
				},
				
				this.cleanHtml = function(all, isIE) {
					jQuery('#starRate').html('');
					jQuery('#ajax_comments').html('');
					if(all || isIE6) {
						jQuery('#ajax_profilePic').html('');
						jQuery('#ajax_sendToFriend').html('');
						jQuery('#ajax_disorders').html('');
						jQuery('#ajax_tags').html('');
						jQuery('#ajax_description').html('');
						if(isIE6) jQuery('#pic-ads').css('display','none');
					}	
				}
				
		return this;
				
}})(jQuery);				

function delThisPic(picId, userId, next) {
	deletePhoto(picId, userId);
	document.location = PUBLIC_URL + 'photos/by/'+userId+'/view/'+next;
}	
