var Toolroom = {
    player: false,
    videoPlayer: false,

    configure: function() {
        var links = $$("a");
        $ES("#tabs #tabbiolink, #tabs #tabtrackslink", $E(".tabs-artists")).each(function(el) {
            links.remove(el);
        });
        new SmoothScroll({ "links": links });

        new Tips($$('.tooltip'), {
            initialize: function() {
                this.fx = new Fx.Style(this.toolTip, 'opacity', { duration: 400, wait: false }).set(0);
            },
            onShow: function(toolTip) {
                this.fx.start(1);
            },
            onHide: function(toolTip) {
                this.fx.start(0);
            }
        });

        $$(".volatile").each(function(el) {
            $(el).addEvent("focus", function() {
                if (this.getValue() == this.getProperty("title")) {
                    this.value = "";
                }
            });
        });

        $$("a[rel*='external']").each(function(a) {
            $(a).setProperty("target", "_blank");
        });

        $$("a[rel*='tandcpopup']").each(function(a) {
            $(a).addEvent("click", function(e) {
                new Event(e).stop();
                window.open(a.href, "termsandconditions", "toolbar=0,menubar=0,resizable=0,width=600,height=400");
            });
        });

        if ($("mp3Player")) {
            var autoplay = !Cookie.get("autoplay") ? "true" : "false";
            var so = new FlashObject("/_colony/shop/player/xmlmp3player/xml_player.swf", "xmlPlayer", "570", "60", "8", "#0D1C31");
            so.addVariable("playlist_url", "/_colony/shop/player/homepage-player.asp");
            so.addVariable("equalizer_url", "/_colony/shop/player/xmlmp3player/equalizer.swf");
            so.addVariable("autoplay", autoplay);
            so.write($("mp3Player"));
        }

        /*
        $$(".featureCycle").each(function(el, index){
        if(index == 0){
        new Ticker(el, {"interval": 6000, "mouseStop": false});
        } else {
        (function(){
        new Ticker(el, {"interval": 6000, "mouseStop": false});
        }).delay(3000);
        }
        });
        */

        $$(".featureCycle").each(function(el, index) {
            if (index == 0) {
                new MooSlideshow(el, el.getChildren(), { "wait": 6000, "fxDuration": 1000 });
            } else {
                (function() {
                    new MooSlideshow(el, el.getChildren(), { "wait": 6000, "fxDuration": 1000 });
                }).delay(3000);
            }
        });

        $$(".addtoplaylist").each(function(el) {
            el.addEvents({
                "click": function(e) {
                    new Event(e).stop();
                    Toolroom.stopXmlPlayer();
                    Toolroom.addToPlaylist(this.getProperty("id"));
                }
            });
        });

        $$(".video_preview").each(function(el) {
            el.addEvents({
                "click": function(e) {
                    new Event(e).stop();
                    Toolroom.openVideoPlayer(this.getProperty("id"));
                }
            });
        });

        if ($E(".tabs-artists")) {
            var tabs = $ES("#tabs #tabbiolink, #tabs #tabtrackslink", $E(".tabs-artists"));
            var tabContent = $ES("#tabcontent #bio, #tabcontent #tracks", $E(".tabs-artists"));

            tabs.each(function(tab, index) {
                if (tabs.length > 1) {
                    tab.addEvent("click", function(e) {
                        new Event(e).stop();
                        if (index == 0) {
                            tabs[0].getParent().addClass("selected");
                            tabs[1].getParent().removeClass("selected");
                        } else {
                            tabs[0].getParent().removeClass("selected");
                            tabs[1].getParent().addClass("selected");
                        }
                    });
                }
            });
            if (tabs.length > 1)
                new Accordion(tabs, tabContent, { "show": 1 });
        }

        $$('.mob-tandc-body').each(function(el) {
            var customLink = new Fx.Slide(el.id, {
                "duration": 200,
                "transition": Fx.Transitions.linear
            });

            customLink.hide();

            $(el.id + '_toggle').addEvent('click', function(e) {
                new Event(e).stop();
                customLink.toggle();
            });
        });

        $$(".selectall", ".select_all").each(function(el) {
            var selectCriteria = el.getProperty("rel");
            el.addEvent("click", function() {
                $$("input[rel=" + selectCriteria + "]").each(function(checkBox) {
                    if (!$(checkBox).hasClass("selectall") && !$(checkBox).hasClass("select_all")) {
                        $(checkBox).checked = $(el).checked;
                        $(checkBox).fireEvent("click");
                    }
                });
            });
        });

        if ($E("input[id=Keywords]")) {
            var mm = new MysticMeg($E("input[id=Keywords]"), {
                "ajaxUrl": "/_colony/shop/search.asp",
                "listClass": "predictive",
                "leftOffset": 155,
                "highlight": true,
                "multipleSelect": false,
                "onSelect": function(v, f, o) {
                    o.flush();
                    o.hideResults();
                    if (v.url != "") {
                        window.location.href = v.url;
                    }
                }
            });
        }

        /*
        if($('SECpaymentform') && $('SECpaymentbutton')) {
        new Asset.image('/_images/buttons/payment-unavailable.gif', null);
        $('SECpaymentbutton').addEvent("click", function(el) {
        $('SECpaymentbutton').setProperty('disabled', 'disabled');
        $('SECpaymentbutton').setProperty('src', '/_images/buttons/payment-unavailable.gif');
        });
        }
        */
    },

    stopXmlPlayer: function() {
        if ($("mp3Player")) {
            if (window.ie) {
                window["xmlPlayer"].stopTrack();
            } else {
                document["xmlPlayer"].stopTrack();
            }
        }
    },

    openPlayer: function() {
        Toolroom.player = window.open("/_colony/shop/player/player.asp", "player", "width=320,height=415");
        Toolroom.player.focus();
    },

    openVideoPlayer: function(videoID) {
        Toolroom.videoPlayer = window.open("/_colony/shop/player/videoplayer.asp?video_id=" + videoID, "videoplayer", "width=320,height=240");
        Toolroom.videoPlayer.focus();
    },

    addToPlaylist: function(data) {
        if (Toolroom.player && !Toolroom.player.closed) {

            new Ajax("/_colony/shop/playlist.asp", {
                "method": "get",
                "data": "add=" + data,
                "timeout": 10000,
                "onComplete": function(track) {
                    Toolroom.player.tracklist.addToPlaylistAndPlay(Json.evaluate(track));
                }
            }).request();

            Toolroom.player.focus();

        } else {

            new Ajax("/_colony/shop/playlist.asp", {
                "method": "get",
                "data": "add=" + data,
                "timeout": 10000
            }).request();

            Toolroom.openPlayer();

        }
    },

    updateBasket: function(html) {
        if ($("minibasket")) {
            $("minibasket").replaceWith(new Element("div").setHTML(html).getFirst());
        }
    }


};

Ajax = Ajax.extend({
    request: function(){
		if (this.options.timeout) {
			this.timeoutTimer = this.callTimeout.delay(this.options.timeout, this);
			this.addEvent('onComplete', this.removeTimer);
			this.addEvent('onFailure', this.removeTimer);
		}
		this.parent();
    },

    callTimeout: function () {
        this.transport.abort();
        this.onFailure();
        if (this.options.onTimeout) {
            this.options.onTimeout();
        }
    },

    removeTimer: function() {
        $clear(this.timeoutTimer);
    }
});

function xmlPlayerStopped(){
	Cookie.set("autoplay","false");
}

window.addEvent("domready", function(){
	Toolroom.configure();
});

window.addEvent("load", function(){
	//new Lightbox();
});

