$(document).ready(function(){Portalfw.Gallery.loadGalleries();});Portalfw={};Portalfw.Gallery={debug:false,loadGalleries:function(){$('div.portalfw-gallery').each(function(){var gallery_div_id=$(this).attr('id');if(gallery_div_id.indexOf('gallery-',0)!=0){return;}
var gallery_id=gallery_div_id.substr(8);if(Portalfw.Gallery.debug){console.log('Creating gallery #'+gallery_id);}
if($(this).is($('div.left_content').children())||$(this).is($('div.right_content').children())){$(this).attr('id',($(this).attr('id')+'-sb'));Portalfw.Gallery.getGalleryAjax(gallery_id,true,Portalfw.Gallery.createGallery);}else{Portalfw.Gallery.getGalleryAjax(gallery_id,false,Portalfw.Gallery.createGallery);}});},getGalleryAjax:function(gallery_id,is_sidebox,callback){var action="get_gallery_ajax";if(is_sidebox){action="get_sidebox_gallery_ajax";}
$.ajax({data:{action:action,gallery_id:gallery_id},url:'/gallery/',dataType:"json",type:"POST",cache:false,timeout:10000,success:function(data,textStatus,XMLHttpRequest){if(Portalfw.Gallery.debug){console.log('Gallery data recieved:');console.log(data);}
callback(data,is_sidebox);},error:function(data,textStatus,XMLHttpRequest){if(Portalfw.Gallery.debug){console.log('Gallery data recieving failed: '+textStatus);}
return;}});},createGallery:function(data,is_sidebox){if(Portalfw.Gallery.debug){console.log('Inserting gallery HTML code...');}
if(is_sidebox){$('div#gallery-'+data.dir_id+'-sb').html(data.html);$("div#gallery-"+data.dir_id+"-sb div.scrollable").scrollable({circular:true,keyboard:false,onBeforeSeek:function(event){Portalfw.Gallery.setupSideboxGalleryDesign(data.dir_id);}}).autoscroll({interval:10000});}else{$('div#gallery-'+data.dir_id).html(data.html);$("div#gallery_"+data.dir_id).scrollable({circular:true,prev:'.prev-'+data.dir_id,next:'.next-'+data.dir_id,onBeforeSeek:function(event){Portalfw.Gallery.setupGalleryDesign(data.dir_id);}});$(window).load(function(){Portalfw.Gallery.setupGalleryDesign(data.dir_id);});}
if(Portalfw.Gallery.debug){console.log('Gallery #'+data.dir_id+' loaded.');}},setupGalleryDesign:function(gallery_id){console.log('Setup gallery design #'+gallery_id);$("#gallery_"+gallery_id+" img").each(function(index,element){var width=$(this).innerWidth();var height=$(this).outerHeight();var maxwidth=parseInt($(this).css('max-width'));var maxheight=parseInt($(this).css('max-height'));if(width<maxwidth){$(this).css('position','relative');$(this).css('left',((maxwidth-width)/2));$(this).parent().children('p.image_description').width(width-20);$(this).parent().children('p.image_description').css('margin-left',((maxwidth-width)/2));}
else if(height<maxheight){$(this).css('position','relative');$(this).css('top',((maxheight-height)/2)-1);var p_height=$(this).parent().children('p.image_description').height();if(p_height<=(((maxheight-height)/2))){$(this).parent().children('p.image_description').css('margin-top',(((maxheight-height)/2)-p_height));}else{$(this).parent().children('p.image_description').css('margin-top',((maxheight-height)/2));}}});},setupSideboxGalleryDesign:function(gallery_id){console.log('Setup sidebox gallery design #'+gallery_id);$("#gallery-"+gallery_id+"-sb div img").each(function(index,element){var width=$(this).innerWidth();var height=$(this).outerHeight();var maxwidth=parseInt($(this).css('max-width'));var maxheight=parseInt($(this).css('max-height'));if(width<maxwidth){$(this).css('position','relative');$(this).css('left',((maxwidth-width)/2)-1);}
else if(height<maxheight){$(this).css('position','relative');$(this).css('top',((maxheight-height)/2)-1);}});}};
