//////////////////////////////////////////////////////////////////////////////////////////////////////
//
// The following javascript is copyright (c) 2009 by Dimitar "coda" Christoff - christoff@gmail.com
// Reproduction and use w/o conscent from the author is strictly prohibited
// Some of these scripts are released by the author under BSD license on http://fragged.org/
//
//////////////////////////////////////////////////////////////////////////////////////////////////////
//
// dependencies: mootools 1.2+ (core & more), dci_core 1.6+, mootools_geoData.js 1.0
//
// BGYELL.CO.UK CORE FUNCTIONS.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////

var myData, $mytime;

window.addEvent("domready", function() {
    // obtuse emails back to clickable links
    $$("a.mailLink").each(function(el) {
        var mailProperties = JSON.decode(el.get("title"));
        el.set({
            "href": "mailto:" + mailProperties.user + "@" + mailProperties.domain + "?subject="+ mailProperties.subject,
            "title": "Send a mail to "+ mailProperties.user + "@" + mailProperties.domain
        });

        (function() {
            el.set({"html": mailProperties.user + "@" + mailProperties.domain}).fade(0,1);
        }).delay(1000);
    });

    // google analytics emebeding
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
        onload: function() {
            var pageTracker = _gat._getTracker("UA-10161427-1");
            pageTracker._initData();
            pageTracker._trackPageview();
        }
    });



    // code to expand main categories and show children
    $$("a.MC").each(function(el) {
        el.addEvents({
            mouseenter: function() {
                var myId = el.get("rel");
                $mytime = (function() {

                    var orig = $E("div.tempSub[rel="+myId+"]"), newOne = $E("div.mainCat[rel="+myId+"]");

                    if (!orig || !newOne)
                        return false;

                    $E("div.mainCat[rel="+myId+"]").setStyles({
                        "display": "block",
                        "margin-bottom": 8,
                        "margin-right": 10,
                        fontSize: 11
                    }).setOpacity(0).replaces(orig).fade(0,1);
                    el.removeEvents();
                }).delay(1000);

            },
            mouseleave: function() {
                $clear($mytime);
            }
        });
    });

    // remove notice
    if ($("welcomeNotice"))
        (function() {
            $("welcomeNotice").fade(0);
            (function() {
                $("welcomeNotice").dispose();
            }).delay(500);
        }).delay(5000);


    // geolocation call to get city name
    if ($("think")) {
        myData = new geoData({
            callback: "myData.setData" // needs to match instance name
        }).addEvents({
            "complete": function(data) {
                new Element("div", {
                    styles: {
                        paddingBottom: 10,
                        paddingTop: 4,
                        fontSize: "10px",
                        lineHeight: "100%"
                    },
                    html: "We think you may be in " + data.city + ", click <a href='#' id='usethis'>here</a> to use this"
                }).inject($("think"));

                $("usethis").addEvents({
                    click: function(e) {
                        new Event(e).stop();

                        $("place").set("value", data.city);
                        Cookie.write("place", data.city, {
                            duration: 7,
                            path: "/"
                        });
                        this.getParent().fade(0);
                        (function() {
                            this.getParent().dispose();
                        }).delay(1000, this);
                    }
                });
            }
        });

        myData.lookup();
    }

    var myRating = new mooRating();

    $$("div.ratings").each(function(el) {
        var val = el.get("text");
        myRating.setOptions({
            targetObject: el,
            rate: val,
            addEvents: true,
            tipShown: true,
            clickEvent: function(ratingOptions) { // save the vote
                new Request({
                    url: "/",
                    method: "get",
                    onComplete: function() {
                        // alert("saved via ajax!");
                    }
                }).send("a=vote&ratingID="+ratingOptions.ratingID+"&rating="+ratingOptions.newRating);
            }
        });

        myRating.draw();
        myRating.bindEvents();
    });


});