﻿var Category = new Class({
    initialize: function(id, name) {
        this.ID = id;
        this.Name = name;
        this.Tags = new Array();
    },
    
    addTag: function(id, name, hasLogo) {
        var tag = new Tag(id, name, hasLogo);
        this.Tags[this.Tags.length] = tag;
        return tag;
    }
});

var Tag = new Class({
    initialize: function(id, name, hasLogo) {
        this.ID = id;
        this.Name = name;
        this.hasLogo = hasLogo;
    }
});

var Tags = new Class({
    initialize: function(container) {
        this.container = $(container);
        this.Categories = new Array();
        this.Favorites = this.addCategory(0, 'My Favourites');
        this.loadFavorites();
    },
    
    addCategory: function(id, name) {
        var category = new Category(id, name);
        this.Categories[this.Categories.length] = category;
        return category;
    },
    
    display: function() {
        this.container.empty();
    
        // create elements
        this.Categories.each(function(category) {
            category.wrapper = new Element('div', { 'styles': { 'padding': '4px 0 5px 0', 'margin': '0', 'border-bottom': 'solid 1px #333' }}).injectInside(this.container);
            if (category.ID == 0) category.wrapper.setStyles({ 'background-color': '#B5AF63', 'color': '#000' });
            
            category.title = new Element('div', { 'class': 'toggler', 'styles': { 'cursor': 'pointer', 'margin': '0' }, 'events': { 'click': function() {
                    if (gallery) gallery.show(category, null);
                }}}).injectInside(category.wrapper);
            new Element('img', { 'styles': { 'margin-left': '4px', 'border': 'none' }, 'src': '../images/decks/plus.gif' }).injectInside(category.title);
            new Element('span', { 'styles': { 'margin-left': '10px' }}).setText(category.Name).injectInside(category.title);
            category.container = new Element('ul', { 'class': 'taglist', 'styles': { 'padding': '2px 0 2px 0', 'margin': '0' }}).injectInside(category.wrapper);
            
            category.Tags.each(function(tag) {
                new Element('a', { 'styles': { 'cursor': 'pointer', 'color': '#fff' }, 'events': { 'click': function() {
                        if (gallery) gallery.show(category, tag);
                    }}}).setText(tag.Name).injectInside(new Element('li', { 'styles': { 'list-style-type': 'none', 'margin': '2px 0 2px 30px', 'padding': '0' }}).injectInside(category.container));
            }.bind(this));
        }.bind(this));
        
        // setup accordian view
        this.accordion = new HDDAccordion(this.container.getElements('div[class=toggler]'), this.container.getElements('ul[class=taglist]'), {
	            opacity: true,
	            onActive: function(toggler, element){
	                toggler.getElement('img').src = '../images/decks/minus.gif';
	            },
	            onBackground: function(toggler, element){
	                toggler.getElement('img').src = '../images/decks/plus.gif';
	            }
            }, this.container);
    },
    
    jumpTo: function(category, tag, deck) {
        var index = this.findCategoryIndex(category);
        if (index == -1) return;
        
        // expand accordion menu
        this.accordion.display(index);
        
        // find tag
        var t = tag ? this.findTag(this.Categories[index], tag) : null;
        
        // display
        gallery.show(this.Categories[index], t, deck);
    },
    
    findCategoryIndex: function (id) {
        var index = -1;
        for (var i=0; i<this.Categories.length; i++) {
            if (this.Categories[i].ID == id) {
                index = i;
                break;
            }
        }
        return index;
    },

    findTag: function (category, id) {
        var tag = null;
        if (category && category.Tags) {
            for (var i=0; i<category.Tags.length; i++) {
                if (category.Tags[i].ID == id) {
                    tag = category.Tags[i];
                    break;
                }
            }
        }
        return tag;
    },
    
    loadFavorites: function() {
        // format: ID||PictureID|Name|HasAdditionalPhotos|DeckPlan~ID||PictureID|Name|HasAdditionalPhotos|DeckPlan
        this.FavoriteDecks = new Array();
        
        // read from cookie
        var value = '';
	    var ca = document.cookie.split(';');
	    for(var i=0; i<ca.length; i++) {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf('favdecks=') == 0) {
		        value = c.substring(5,c.length);
		        break;
		    }
	    }
	    
	    // add to collection
        value.split('~').each(function(item) {
            var parts = item.split('|');
            if (parts.length == 5) {
                var o = new Object();
                o.ID = parseInt(parts[0]);
                o.PictureID = parseInt(parts[1]);
                o.Description = parts[2];
                o.HasAdditionalPhotos = parts[3];
                o.DeckPlan = false;
                this.FavoriteDecks[this.FavoriteDecks.length] = o;
            }
        }.bind(this));	    
    },
    
    addFavorite: function(deck) {
        // check if the deck is already in the collection
        var found = false;
        this.FavoriteDecks.each(function(fav) {
            if (fav.ID == deck.ID)
                found = true;
        });
        if (found) return;
        
        // add to collection
        var o = new Object();
        o.ID = deck.ID;
        o.PictureID = deck.PictureID;
        o.Description = deck.Description;
        o.HasAdditionalPhotos = deck.HasAdditionalPhotos;
        o.DeckPlan = false;
        this.FavoriteDecks[this.FavoriteDecks.length] = o;
        
        this.cookieFavorites();
    },
    
    removeFavorite: function(deck) {
        this.FavoriteDecks.each(function(fav) {
            if (fav.ID == deck.ID)
                this.FavoriteDecks.remove(fav);
        }.bind(this));
        this.cookieFavorites();
    },
    
    cookieFavorites: function() {
        var value = '';
        for (var i=0; i<this.FavoriteDecks.length; i++) {
            if (i>0) value += '~';
            value += this.FavoriteDecks[i].ID + '|' + this.FavoriteDecks[i].PictureID + '|' + this.FavoriteDecks[i].Description + '|' + this.FavoriteDecks[i].HasAdditionalPhotos + '|' + this.FavoriteDecks[i].DeckPlan;
        }
    	    var date = new Date();
	    date.setTime(date.getTime()+(365*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	    document.cookie = 'favdecks='+value+expires+'; path=/';
    }
});


