/*!
 * Galleria - Adobe Business Catalyst Plugin v 1.0
 * created by http://www.AtlantaWebDesignGA.com
 * Jonathan Beacher
 * 
 * based on galleria-ssp.js plug-in created by
 * http://galleria.aino.se
 *
 * Copyright 2010, Aino
 * Licensed under the MIT license.
 *
 * Purpose: this plugin enables use of the Adobe Business Catalyst Photo Gallery module to create 
 * gallery definitions of photos with titles, descriptions and links that are then output in the BC admin
 * as XML files that this plug-in can import into the JQuery Galleria slide show plug-in. Galleria can then
 * display a slide show as a page header, with superimposed titles, descriptions and links over the header photos.
 * 
 * Instructions: in the Business Catalsyt admin use Modules -> Photo Gallery to create a gallery of photos, and in 
 * the single Descriptions field enter up to three items of your content separated by the ^ character, in this order as follows:
 * photo title^photo description sentence^/link.html
 * 
 * If there is no title enter as follows:
 * ^photo description sentence^/link.html
 *
 * If there is no link enter as follows:
 * photo title^photo description sentence
 *
 * formatting of the style and location of the title and description is controlled by attaching a Galleria theme, and any options included in the script on the page calling Galleria plug in.
 */

(function() {
   
var G = window.Galleria; 
if (typeof G == 'undefined') {
    return;
}

var S = G.BC = function(url) {
    var query = "select * from xml where url='" + url + "' limit 1";
    var encodedQuery = encodeURIComponent(query.toLowerCase());
    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodedQuery + '&format=json&callback=?';
    var scope = this;
    
    if (url.substr(0,7) == 'http://') {
        var a = document.createElement('a');
        a.href = url;
        this.domain = a.protocol+'//'+a.hostname;
        this.path = url.substring(0,(url.lastIndexOf("/")) + 1);
    }
    
    $.getJSON(yql, function(data) {
        scope.data = data;
    });
    
}

S.prototype = {
    data: null,
    domain: '',
    path:'',
    getAlbum: function(index, callback) {
        this.ready(function() {
            var data = this.parseData(index);
            callback.call(this, data);
        });
        return this;
    },
    getAbsoluteUrl: function(path) {
        if (path.substr(0,7) != 'http://') {
            if (path.substr(0,1) != '/') {
                path = this.path + path;
            } else {
                path = this.domain + path;
            }
        }
        return path;
    },
    parseData: function(index) {
        var albums = this.data.query.results.gallery.album;
        var album = albums[index] || albums;
        var path = album.lgPath || '';
        var thumbPath = album.tnPath || path;
        
        var arr = [];
        var scope = this;
		

        G.prototype.loop(album.img, function(img) {
            var obj = {
                image: scope.getAbsoluteUrl(path+img.src),
                thumb: scope.getAbsoluteUrl(thumbPath+img.src),
                title: img.alt
            };
			
			var array = obj.title.split('^');
			if ( array[1] ) {
				obj.description = array[1];;
			}
			if ( array[2] ) {
				obj.link = array[2];
			}
			if ( array[0] ) {
				obj.title = array[0];
			}
            arr.push(obj);
        });
        return arr;
    },
    ready: function(callback) {
        var scope = this;
        G.prototype.wait(function() {
            return !!scope.data;
        }, function() {
            callback.call(scope);
        }, function() {
            G.raise('YQL not available.')
        }, 10000);
    }
}

})();
