/**
* jQuery RESTful Gallery - v2.0 - 5/18/2012
* http://www.flashyourweb.com/gallery2/embed_samples/g3_rest.php
*
* Copyright (c) 2012 "suprsidr" Wayne Patterson
* License: http://www.opensource.org/licenses/mit-license.php
*/
(function(window, $, PhotoSwipe){
$.fn.g3rest = function(options) {
var rest = $.extend({}, $.fn.g3rest.defaults, options);
return this.each(function() {
var el = this,
pager = {
page: 0,
html: ''
},
max = $(window).width() > $(window).height()?$(window).width():$(window).height();
// if screen width is > 1000 show fullsize else resize
max = max <= 1000?max:0;
$(window).hashchange( function(){
if(location.hash.indexOf('page') > -1){
var parts = location.hash.replace('#', '/').split('/page/');
getItems(rest.url + parts[0], {output: 'jsonp', scope: 'direct', page: parseInt(parts[1]) - 1});
} else if (location.hash != '') {
getItems(rest.url + location.hash.replace('#', '/'), {output: 'jsonp', scope: 'direct', page: 0});
} else {
getItems(rest.url + '/item/' + rest.initial, {output: 'jsonp', scope: 'direct', page: 0});
}
})
setup(rest.key);
// setup any further ajax requests to use these params
function setup(key){
$(el).empty().append($('
').addClass('ajax-title').append(''))
.append($('').attr({id: 'ajax-breadcrumbs'}))
.append($('').attr({id: 'ajax-gallery'}))
.append($('').attr({id: 'ajax-description'}).append(''))
.append($('').attr({id: 'ajax-pager'}));
$.ajaxSetup({
type: 'GET',
dataType: 'jsonp',
beforeSend: function(xhr){
xhr.setRequestHeader('X-Gallery-Request-Key', key);
}
});
$(window).hashchange()
}
function getBreadCrumbs(url){
$.ajax({
url: rest.url + '/items',
data: {output: 'jsonp', 'ancestors_for': url},
success: function(data){
$('#ajax-breadcrumbs').empty();
$.each(data, function(i, item){
if(i == data.length - 1){
$('#ajax-breadcrumbs').append(sprintf(' %s', item.entity.title));
return;
}
$('#ajax-breadcrumbs').append(sprintf('%s»', item.entity.id, item.url, item.entity.title));
})
$('#ajax-breadcrumbs a').on('click', function(e){
e.preventDefault();
$('#ajax-pager, #ajax-breadcrumbs').empty();
location.hash = '#' + $(this).attr('ref')
});
}
});
}
function getPager(ttl, cur, url){
pager.html = '';
pager.page = cur;
if(ttl == 1) return pager;
if(cur > 0){
pager.html += sprintf('« ', url, parseInt(cur)-1);
}
for(var i=0; i %s %s', i+1, sep);
continue;
}
pager.html += sprintf('%s%s', url, i, i+1, sep);
}
if(cur < ttl-1){
pager.html += sprintf(' »', url, parseInt(cur)+1);
}
return pager;
}
function getItems(url, args){
getBreadCrumbs(url);
$.ajax({
url: url,
data: args,
success: function(data){
$('.ajax-title h2').fadeOut('fast', function(e){
$(this).text(data.entity.title).fadeIn('slow');
});
$('#ajax-description p').css({display: 'none'}).html(data.entity.description.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi, '$1').replace(/\n/g, '
')).fadeIn('slow');
var members = array_chunk(data.members, rest.max_items_per_page);
pager = getPager(members.length, args.page, url);
$('#ajax-pager').empty().css({display: 'none'}).append(pager.html).fadeIn('slow');
$('#ajax-pager a').on('click', function(e){
e.preventDefault();
$('#ajax-pager, #ajax-breadcrumbs').empty();
if(window.location.hash.indexOf('page') == -1){
location.hash = location.hash + '/page/' + Math.floor(parseInt($(this).attr('ref')) + 1);
} else {
var hash = location.hash.substr(0, location.hash.lastIndexOf('/') +1 );
location.hash = hash + Math.floor(parseInt($(this).attr('ref')) + 1);
}
});
$.ajax({
url: rest.url + '/items',
data: {output: 'jsonp', urls: JSON.stringify(members[pager.page])},
success: function(data){
$('#ajax-gallery').empty();
$.each(data, function(i, item){
if(item.entity.type == 'album'){
var earl = item.entity.web_url;
var ref = "item/"+item.entity.id;
} else if(item.entity.type == 'photo') {
var earl = max == 0?item.entity.file_url_public:item.entity.resize_url_public;
var ref = '';
} else if(item.entity.type == 'movie'){
var earl = item.entity.file_url_public;
var ref = '';
}
$('#ajax-gallery').append(
$('').append(
$('').attr({
href: earl,
ref: ref,
alt: item.entity.title,
'data-entity-type': item.entity.type,
'id': item.entity.type+'-'+item.entity.id
}).css({height: item.entity.thumb_height}).data(item).append(
$('
').attr({
src: item.entity.thumb_url_public,
alt: item.entity.title,
'data-aspect': item.entity.thumb_height/item.entity.thumb_width
})
)
)
);
if(i == data.length - 1){
$('#ajax-gallery li:last-child img').on('load', function(e){
resizeIt();
if ($('.no-touch').length > 0) {
$('#ajax-gallery li').on('mouseenter', function(e){
e.preventDefault();
var data = $(this).find('a').data();
$(this).find('a').append($('').html(data.entity.title).prepend(''));
if(data.entity.description){
$(this).append($(sprintf('', data.entity.id, data.entity.id)));
$(sprintf('.info-%s', data.entity.id)).on('mouseenter', function(e){
e.preventDefault();
$('body')
.append(
$('')
.css({display:'none'})
.append($('').text(data.entity.title))
.append(data.entity.description.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi, '$1').replace(/\n/g, '
'))
);
$(sprintf('.info-%s', data.entity.id)).colorbox({
inline: true,
width: "360",
href: "#info-box",
onOpen:function(){ $('#info-box').css({display:'block'}); },
onClosed:function(){ $('#info-box').remove(); }
});
});
$(sprintf('.info-%s', data.entity.id)).on('mouseleave', function(e){
e.preventDefault();
if($('#cboxLoadedContent > #info-box').length){
return;
}
$('#info-box').remove();
});
}
});
$('#ajax-gallery li').on('mouseleave', function(e){
var data = $(this).find('a').data();
$(this).find('.pop-title').remove();
$(this).find(sprintf('.info-%s', data.entity.id)).remove();
});
}
});
}
});
// f#@k IE
if ($('.ie8').length > 0 || $('.ie9').length > 0) {
$('#ajax-gallery').append($(''));
resizeIt();
if ($('no-touch')) {
$('#ajax-gallery a').on('mouseenter', function(e){
e.preventDefault();
$(this).append($('').html($(this).attr('alt')));
});
$('#ajax-gallery a').on('mouseleave', function(e){
$(this).find('.pop-title').remove();
});
}
}
// photoswipe
var options = {
getImageMetaData: function(el){
return {
type: el.getAttribute('data-entity-type'),
id: el.getAttribute('id')
}
}
},
myPhotoSwipe = $("#ajax-gallery a").photoSwipe(options);
$(myPhotoSwipe).on(PhotoSwipe.EventTypes.onHide, function(e){
$('#multimedia').remove();
resizeIt();
});
$(myPhotoSwipe).on(PhotoSwipe.EventTypes.onBeforeShow, function(e){
if(this.getCurrentImage().metaData.type == 'movie'|| this.getCurrentImage().metaData.type == 'audio'){
if($('#multimedia').length)$('#multimedia').remove();
$('.ps-toolbar-play, .ps-toolbar-previous, .ps-toolbar-next').css({visibility: 'hidden'});
$('.ps-carousel div').css({'display': 'none'});
var data = $('#'+this.getCurrentImage().metaData.id).data();
data.entity.code = rest.code;
$('body').append($('').html(sprintf(getMediaTemplate(data.entity.mime_type), data.entity)));
$('#multimedia').css({
left: ($(window).innerWidth() - $('#multimedia object').width())/2,
top: ($(window).innerHeight() - $('#multimedia object').height())/2
});
}
});
$('#ajax-gallery a[data-entity-type="album"]').unbind('click').on('click', function(e){
e.preventDefault();
var data = $(this).data();
location.hash = '#' + $(this).attr('ref')
});
}
});
}
});
}
function array_chunk( input, size ) {
for(var x, i = 0, c = -1, l = input.length, n = []; i < l; i++){
(x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
}
return n;
}
$(window).resize(function(e){
resizeIt();
});
function resizeIt(){
if($('#multimedia').length){
$('#multimedia').css({
left: ($(window).innerWidth() - $('#multimedia object').width())/2,
top: ($(window).innerHeight() - $('#multimedia object').height())/2
});
return;
}
// Our first image sets the standard
var h = $('#ajax-gallery li:first-child a').innerWidth() * $('#ajax-gallery li:first-child img').attr('data-aspect');
var w = $('#ajax-gallery li:first-child img').innerWidth();
$('#ajax-gallery li a').each(function(){
$(this).css({
'height': h
}).addClass('visible');
});
$('#ajax-gallery li img').each(function(){
var a = $(this).attr('data-aspect');
var myH = $('#ajax-gallery li:first-child img').parent().innerWidth() * a;
if (myH > h || a > 1) {
$(this).css({
'height': '100%',
'width': h / a,
'margin-top': ''
});
} else if (a <= 1) {
$(this).css({
'height': w * a,
'width': '100%',
'margin-top': (h - w * a) / 2
});
}
});
}
});
}
$.fn.g3rest.defaults = {
initial: 1,
max_items_per_page: 20,
key: ''
};
}(window, window.jQuery, window.Code.PhotoSwipe));