/* GuildPortal Gallery
 *  Uses jQuery colorbox (http://colorpowered.com/colorbox/) to form a compact gallery
 *
 * Copyright (c) 2010 Rob G (aka Mottie/Fudgey)
 * Dual licensed under the MIT and GPL licenses
 *
 * v 1.1 - Fri Jul 02 2010 22:25:02 GMT-0500 - Added ability to add & delete comments
 * v 1.0 - Sun May 23 2010 09:50:56 GMT-0500 - Initial release
 */
(function($){$.fn.gpGallery=function(m){function o(){var b=$("#cboxSlideshow"),d=b.text().match("start")?"ssStart":"ssStop";b.removeClass("ssStart ssStop").addClass(d)}function p(b){var d=$("."+a.galleryClass).find("li[data-imgid="+b+"]");b='<div class="gpgCommentTitle">Comments</div><ul class="gpgCommentBody" data-gid="'+d.parent().attr("data-gid")+'" data-imgid="'+b+'">';var c=d.attr("rel").split(",");d=d.closest("."+a.galleryClass).data("gallery")[parseInt(c[0],10)].Images[parseInt(c[1],10)].Comments;
if(d.length)for(var e,f,k,i=0;i<d.length;i++){c=new Date(parseInt(d[i].AddStamp.substr(6),10));k=a.dateMonthFirst?c.getMonth()+1+"/"+c.getDate():c.getDate()+"/"+(c.getMonth()+1);e="";if(a.showTime){f=c.getHours();e=f>12?f-12:f;e+=":"+("0"+c.getMinutes()).slice(-2);e+=f>12?" pm":" am"}b+='<li data-cmtid="'+d[i].ID+'" data-poster="'+d[i].ProfileID+'"><span class="gpgUser">'+d[i].UserName+'</span> <span class="gpgDate">('+k+"/"+c.getFullYear()+" "+e+')</span>: <span class="gpgComment">'+d[i].Comment+
"</span></li>"}else b+="<li>"+a.noCommentsText+"</li>";return b+"</ul>"}function n(b,d){var c=p(d);g.find(".content").html(c);$("#cboxComments").attr("title",c);c='<br><input id="gpgAddComment" type="text" value="Add a comment..." data-base="Add a comment..." />';g.find(".content").append(c).find("li").each(function(){if($(this).html()!=a.noCommentsText){var e=$("."+a.galleryClass+"[data-gid="+b+"]").data("access");$(this).prepend(e.access==4||e.deletz&&e.access>=1&&getProfileId()==$(this).attr("data-poster")?
'<div class="gpgDeleteComment"></div>':'<div class="gpgNoDelete"></div>')}})}function q(b,d,c,e){var f=Math.max(g.find(".content").height(),40);g.find(".content").html('<div id="cboxLoadingGraphic" style="position:static;height:'+f+'px"></div>');f={action:c?"addImageComment":"delImageComment",groupId:b,imageId:d};if(c)f.commentText=e;else f.commentId=e;$.ajax({url:a.jsonUrl,dataType:"xml",data:f,error:function(){n(b,d)},success:function(){$.getJSON(a.jsonUrl,{groupId:b,rand:(new Date).getTime(),action:a.action},
function(k){if(typeof k!="undefined"||k!==null)$("."+a.galleryClass+"[data-gid="+b+"]").data("gallery",k);n(b,d)})}})}function s(b){$("#cboxComments").length||$("#cboxContent").append('<div id="cboxComments" class="tooltip" rel="'+a.tooltipWidth+'"></div>');$("#cboxComments").show().click(function(){$.facebox(" ");g.draggable().css("z-index",99998);n(b.attr("data-gid"),$(this).attr("data-imgid"))});$("#cboxSlideshow").click(function(){setTimeout(function(){o()},500)})}var t={maxWidth:"100%",maxHeight:"95%",
slideShow:true,slideShowAuto:false,slideshowSpeed:2500,currentImage:"{current}/{total}",tooltipWidth:400,noCommentsText:"No comments found",dateMonthFirst:true,showTime:true,arrowUp:"&#9650;",arrowDown:"&#9660;",showTools:true,instance:null,toolbarText:"Gallery:",upload:"Upload",openGallery:"Open",classHeader:"ForumCategoryHeader",startGallery:0,hiddenGallery:"",speed:500,guildID:getGroupId(),galleryClass:"gpgallery",confirmation:"Delete this comment?",jsonUrl:"/Services/User.aspx",action:"getImageGalleryJson",
popupOptions:"scrollbars=no,resizable=yes,status=no,toolbars=no,",galleryOptions:"width=1055,height=600",uploadOptions:"width=275,height=175",editOptions:"width=400,height=300"};m=$.extend({},t,m);var a=$.meta?$.extend({},m,this.data()):m,h=$(this).addClass(a.galleryClass).attr("data-gid",a.guildID),g=$("#facebox"),j="",l,r;h.data("access",{upload:false,deletz:false,access:1});$.ajax({type:"GET",url:a.jsonUrl,data:{groupId:a.guildID,action:"getGalleryRights"},datatype:"xml",success:function(b){h.data("access",
{upload:$(b).find("CanUpload").text()=="true"?true:false,deletz:$(b).find("CanDeleteComments").text()=="true"?true:false,access:parseInt($(b).find("AccessLevel").text(),10)});l=h.data("access");if(a.showTools&&a.guildID==getGroupId()){$(".gpgToolbar .gpgTitle").html(a.toolbarText);l.access>1&&l.upload&&$("#gpgUploadImage").show().html(a.upload).click(function(){window.open("/ContentControls/Support/Upload.aspx?Group="+a.guildID,"gpGallery",a.popupOptions+a.uploadOptions);return false});if(a.instance!==
null&&(l.access==4||l.access>1&&l.deletz))$("#gpgEditGallery").show().click(function(){window.open("/CExplorer/exDlgContentEditor.aspx?ContentInstance="+a.instance+"&Group="+a.guildID,"gpGallery",a.popupOptions+a.editOptions);return false});$(".gpgToolbar").show()}}});$("#gpgFullGallery").html(a.openGallery).click(function(){window.open("/GroupGallery.aspx?GuildID="+a.guildID,"gpGallery",a.popupOptions+a.galleryOptions);return false});$("#cboxComments, #cboxSlideshow, .gpgDeleteComment").live("mouseover mouseout",
function(b){b.type=="mouseover"?$(this).addClass("hover"):$(this).removeClass("hover")});g.find("input#gpgAddComment").live("focusin focusout",function(b){if(b.type=="focusin")$(this).val()==$(this).attr("data-base")&&$(this).val("");else $(this).val()==""&&$(this).val($(this).attr("data-base"))}).live("keyup",function(b){if(b.keyCode=="13"&&$(this).val()!==""){$(this).attr("disabled","disabled");b=g.find("ul").attr("data-gid");var d=g.find("ul").attr("data-imgid");q(b,d,true,$(this).val())}return false});
g.find(".gpgDeleteComment").live("click",function(){if(confirm(a.confirmation)){var b=$(this).closest("ul").attr("data-gid"),d=$(this).closest("ul").attr("data-imgid"),c=$(this).parent().attr("data-cmtid");q(b,d,false,c)}return false});$.getJSON(a.jsonUrl,{groupId:a.guildID,rand:(new Date).getTime(),action:a.action},function(b){if(typeof b!="undefined"||b!==null){h.data("gallery",b);$.each(b,function(c){if(jQuery.inArray(b[c].Title,a.hiddenGallery.split(","))<0){r=b[c].Title==a.startGallery?" selected":
"";j+='<h2 class="'+a.classHeader+r+'">'+b[c].Title+'<span></span></h2><ul data-gid="'+a.guildID+'">';for(var e=0;e<b[c].Images.length;e++){var f=b[c].Images[e].ThumbnailUrl;j+='<li rel="'+c+","+e+'" data-imgid="'+b[c].Images[e].ID+'"><a href="'+b[c].Images[e].ImageUrl+'" title="'+b[c].Images[e].Description+'" class="tooltip" rel="gallery'+a.guildID+"-"+c+'"><img src="';j+=f==""?b[c].Images[e].ImageUrl:f;j+='" /></a></li>'}j+="</ul>"}});j+='<br style="clear:both" />';var d=h.html(j).find("h2");d.click(function(){var c=
$(this).is(".selected");d.removeClass("selected");c||$(this).addClass("selected").find("span").html(a.arrowUp).end().next().slideDown(a.speed);d.not(".selected").find("span").html(a.arrowDown).end().next().slideUp(a.speed)}).not(".selected").find("span").html(a.arrowDown).end().next().hide();d.filter(".selected").find("span").html(a.arrowUp);h.find("li a").colorbox({maxWidth:a.maxWidth,maxHeight:a.maxHeight,slideshow:a.slideShow,slideshowAuto:a.slideShowAuto,slideshowSpeed:a.slideshowSpeed,current:a.currentImage,
onOpen:function(){s(h)},onLoad:function(){$(document).trigger("close.facebox")},onComplete:function(){if($("#cboxTitle").html()=="")$("#cboxTitle").html(this.t||this.title);o();var c=$("#cboxComments"),e=$(this).parent().attr("data-imgid"),f=p(e);c.attr({title:f,"data-imgid":e});$("#tooltip").html(f)},onClosed:function(){$(document).trigger("close.facebox")}})}});return h}})(jQuery);

