a fix
[scribo] / www / plugins / galleria.ssp.js
1 /*!
2  * Galleria SlideShowPro Plugin v 1.1
3  * http://galleria.aino.se
4  *
5  * Copyright 2010, Aino
6  * Licensed under the MIT license.
7  */
8
9 (function() {
10    
11 var G = window.Galleria; 
12 if (typeof G == 'undefined') {
13     return;
14 }
15
16 var S = G.SSP = function(url) {
17     var query = "select * from xml where url='" + url + "' limit 1";
18     var encodedQuery = encodeURIComponent(query.toLowerCase());
19     var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodedQuery + '&format=json&callback=?';
20     var scope = this;
21     
22     if (url.substr(0,7) == 'http://') {
23         var a = document.createElement('a');
24         a.href = url;
25         this.domain = a.protocol+'//'+a.hostname;
26         this.path = url.substring(0,(url.lastIndexOf("/")) + 1);
27     }
28     
29     $.getJSON(yql, function(data) {
30         scope.data = data;
31     });
32     
33 }
34
35 S.prototype = {
36     data: null,
37     domain: '',
38     path:'',
39     getAlbum: function(index, callback) {
40         this.ready(function() {
41             var data = this.parseData(index);
42             callback.call(this, data);
43         });
44         return this;
45     },
46     getAbsoluteUrl: function(path) {
47         if (path.substr(0,7) != 'http://') {
48             if (path.substr(0,1) != '/') {
49                 path = this.path + path;
50             } else {
51                 path = this.domain + path;
52             }
53         }
54         return path;
55     },
56     parseData: function(index) {
57         var albums = this.data.query.results.gallery.album;
58         var album = albums[index] || albums;
59         var path = album.lgPath || '';
60         var thumbPath = album.tnPath || path;
61         
62         var arr = [];
63         var scope = this;
64
65         G.prototype.loop(album.img, function(img) {
66             var obj = {
67                 image: scope.getAbsoluteUrl(path+img.src),
68                 thumb: scope.getAbsoluteUrl(thumbPath+img.src),
69                 title: img.title
70             };
71             if (img.caption) {
72                 obj.description = img.caption
73             }
74             if (img.link) {
75                 obj.link = img.link;
76             }
77             arr.push(obj);
78         });
79         return arr;
80     },
81     ready: function(callback) {
82         var scope = this;
83         G.prototype.wait(function() {
84             return !!scope.data;
85         }, function() {
86             callback.call(scope);
87         }, function() {
88             G.raise('YQL not available.')
89         }, 10000);
90     }
91 }
92
93 })();