jQuery.fn.exists = function () {
    return jQuery(this).length > 0;
}

function debug(message) {
    var console = window['console'];
    if (console && console.log) {
        console.log(message);
        return;
    }
}


var WallInterface = $.Class.create({
    initialize: function() {
    },

    replaceBricks: function(newBricks) {
        var effect = "fade"; // puff, blind

        $("#newsArticles ul").removeClass("newWall").addClass("currentWall");
        $("#newsArticles").append(newBricks);
        $(".newWall li").hide();

        $(".currentWall li").hide(effect, "slow",
                                 function() {
                                     $(".currentWall").remove();
                                     $(".newWall li").show(effect, {direction: "right"});
                                 });
        return this;
    },

    highlightBrick: function(brick) {
        brick.show();

    },

    lowlightBrick: function(brick) {
        brick.hide();
    },

    removeShowingBricks: function() {
    },

    showBrick: function(name, url) {
        var href = url.replace(/\?inFrame=1/g, "") + "?inFrame=1";
        var brickLink = $("a[href='" + url + "']");


        if (brickLink.length == 0) {
            // element is not on the screen
            $.colorbox({open:true, width:"980", iframe:false, href:href});
        } else {
            brickLink.attr("href", href);
            if (brickLink.hasClass("cboxElement"))
                brickLink.colorbox({width:"980", iframe:false});
            else
                brickLink.colorbox({open:true, width:"980",  iframe:false});
        }
    }
});


var Wall = $.Class.create({
    initialize: function () {
        this._currentList = "";
    },

    video: function() {
        this._updateWall("/listing/video");
        $('.link').removeClass('highlight');
        $('.Video').addClass('highlight');
    },

    news: function() {
        this._updateWall("/listing/news");
         $('.link').removeClass('highlight');
        $('a.News').addClass('highlight');
    },

    images: function() {
        this._updateWall("/listing/gallery");
        $('.link').removeClass('highlight');
         $('a.Gallery').addClass('highlight');
    },

 //   shows: function() {
 //       this._updateWall("/listing/shows");
 //        $('.link').removeClass('highlight');
 //        $('a.Shows').addClass('highlight');
 //   },

    music: function() {
        this._updateWall("/listing/music");
        $('.link').removeClass('highlight');
         $('a.Music').addClass('highlight');
    },

    home: function() {
        this._updateWall("/listing/home");
         $('.link').removeClass('highlight');
         $('a.Home').addClass('highlight');
    },

    highlightBrick: function(brick) {
        var heading = brick.find("div.ArticleTitle");
        if (heading.parent().parent().hasClass("static"))
            return;

        wInterface.highlightBrick(heading);
    },

    lowlightBrick: function(brick) {
        var heading = brick.find("div.ArticleTitle");
        if (heading.parent().parent().hasClass("static"))
            return;

        wInterface.lowlightBrick(heading);
    },

    showBrick: function(brick) {
        var url = brick.attr("href");
        this._showBrick(url);
    },

    _showBrick: function(url) {
        var name = url.replace(/\//g, "_");

        var urlMangler = new BrickUrlHelper();
        urlMangler.newBrick(name, "item");

        url = url + "?inFrame=1";


        wInterface.showBrick(name, url);
    },

    hideBrick: function() {
        wInterface.removeShowingBricks();

        var urlHelper = new BrickUrlHelper();
        if (this._currentBrick == "" || this._currentBrick == "undefined" || this._currentBrick == null) {
            window.location.href = urlHelper.getCleanUrl();
        } else {
            window.location.href = urlHelper.getCleanUrl() + this._currentBrick;
        }

    },

    apply: function() {
        var urlHelper = new BrickUrlHelper();
        var currentBrick = urlHelper.currentBrick();
        if (currentBrick == null) {
            wall.home();
            return;
        }

        var currentType = urlHelper.currentType();
        var url = new NameNormaliser().convertToUrl(currentBrick);
        if (currentType == "list")
            this._updateWall(url);
        else
            this._showBrick(url);
    },

    _updateWall: function(url) {
        var name = url.replace(/\//g, "_");
        var urlMangler = new BrickUrlHelper();
        var brickName = urlMangler.newBrick(name, "list");

        this._currentBrick = brickName;

        $.get(url, function(data) {
            wInterface.replaceBricks(data);
        });
    }
});

var BrickUrlHelper = $.Class.create({
    initialize: function () {

    },

    cleanUrl: function() {
        window.location.href = this.getCleanUrl();
    },

    newBrick: function(name, type) {
        var currentUrl = this.getCleanUrl();
        var brickName = "#" + type + "-" + name;
        window.location = currentUrl + brickName;
        return brickName;
    },

    currentBrick: function() {
        var currentUrl = window.location.href;
        var idx = currentUrl.indexOf("#", 0);
        if (idx == -1) {
            return null;
        }

        var hashValue = currentUrl.substring(idx + 1, currentUrl.length);
        var type = hashValue.substring(0, hashValue.indexOf("-", 0) - 1);
        var brick = hashValue.substring(hashValue.indexOf("-", 0) + 1);
        return brick;
    },

    currentType: function() {
        var currentUrl = window.location.href;
        var idx = currentUrl.indexOf("#", 0);
        if (idx == -1) {
            return null;
        }

        var hashValue = currentUrl.substring(idx + 1, currentUrl.length);
        var type = hashValue.substring(0, hashValue.indexOf("-", 0));
        return type;
    },

    getCleanUrl: function() {
        var currentUrl = window.location.href;
        var idx = currentUrl.indexOf("#", 0);
        if (idx == -1) {
            return window.location.href;
        }
        return currentUrl.substr(0, idx - 1);
    }
});


var NameNormaliser = $.Class.create({
    initialize: function () {

    },

    clean: function(nameToClean) {
        var cleanName = nameToClean.replace(/-/g, "_");
        cleanName = cleanName.replace(/\(/g, "_");
        cleanName = cleanName.replace(/\)/g, "_");

        return cleanName;
    },

    convertToUrl: function(name) {
        return name.replace(/_/g, "/");
    }
});

var wInterface = new WallInterface();

