// ========================================================================
// Copyright 2003-2005, Lexitech Ltd (http://www.lexitech.co.uk)
// All rights reserved.
// ========================================================================
// $Id: util.js 16 2005-05-23 00:12:32Z tprice $
// ========================================================================

// stop bad bots from trawling for email addresses
// (adapted from a tip by Joseph McLean)

function mailAnchor(who, where, preamble, text, subject, title, extraAttribs) {

    str = "";
    if (who == null || who.length == 0 || where == null || where.length == 0) return str;
    if (text == null || text.length == 0) {
        text = who + "@" + where;
    }
    if (preamble != null && preamble.length > 0) {
        str += preamble;
    }
    str += "<a"
    if (extraAttribs != null && extraAttribs.length > 0) {
        str += " " + extraAttribs;
    }
    str += " href=\"mail" + "to:" + who + "@" + where;
    if (subject == null || subject.length== 0) {
        str += "\"";
    } else {
        str += "?subject=" + subject + "\"";
    }
    if (title != null && subject.length > 0) {
        str += " title=\"" + title + "\"";
    }
    str += ">" + text + "</a>";
    // alert("Mail str: '" + str + "'");
    return str;
}

function writeMail(who, where, preamble, text, subject, title, extraAttribs) {
    document.write(mailAnchor(who, where, preamble, text, subject, title, extraAttribs));
}

function longDayFromShort(dayStr) {
    d = dayStr.toLowerCase();
    if (d == "sun") return "Sunday";
    else if (d == "mon") return "Monday";
    else if (d == "tue") return "Tuesday";
    else if (d == "wed") return "Wednesday";
    else if (d == "thu") return "Thursday";
    else if (d == "fri") return "Friday";
    else if (d == "sat") return "Saturday";
    else return dayStr;
}

function longMonthFromShort(monthStr) {
    m = monthStr.toLowerCase();
    if (m == "jan") return "January";
    else if (m == "feb") return "February";
    else if (m == "mar") return "March";
    else if (m == "apr") return "April";
    else if (m == "may") return "May";
    else if (m == "jun") return "June";
    else if (m == "jul") return "July";
    else if (m == "aug") return "August";
    else if (m == "sep") return "September";
    else if (m == "oct") return "October";
    else if (m == "nov") return "November";
    else if (m == "dec") return "December";
    else return monthStr;
}

function currDate() {
    dateStr = "";
    currDate = new Date();
    if (currDate && currDate.toDateString) {
        dateStr = currDate.toDateString();
    } else {
        dateStr = currDate.toString();
    }
    if (dateStr && dateStr.split) {
        split = dateStr.split(' ');
        munged = longDayFromShort(split[0]) + ", ";
        munged += " " + longMonthFromShort(split[1]);
        munged += " " + split[2];
        munged += " " + currDate.getFullYear()
        dateStr = munged;
    }
    // alert("Current date is '" + dateStr + "'");
    return dateStr;
}

function writeCurrDate() {
    document.write(currDate());
}




jQuery.fn.RotateNext = function () {
    return this.each(function () {
        var parentID = '#' + this.id;
        var current = ($('ul li.show', this).length ? $('ul li.show', this) : $('ul li:first', this));
        var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('ul li:first', this) : current.next()) : $('ul li:first', this));

        //there is a description to hide
        if ($('div.block', current).length > 0) {
            //hide the current description
            $('div.block', current).slideUp(800, function () {
                //fade out the current image
                current.removeClass('show')
                .hide();
                //IE-PNG-Issue//.animate({ opacity: 0.0 }, 800);
                //fade in the next image
                //IE-PNG-Issue//next.css({ opacity: 0.0 })
                next.hide()
                .addClass('show')
                .show();
                //IE-PNG-Issue//.animate({ opacity: 1.0 }, 800, function() {
                if ($('div.block', next).length > 0) {
                    //show the next description
                    $('div.block', next)
                        .css({ opacity: 0.85 })
                        .slideDown(800);
                }
                //IE-PNG-Issue//})
                //set the image opacity to 100% so that the description appears semi-transparent
                next.find('img').show();
                //.css({ opacity: 1.0 });
            });
        }
        //there is no description to hide
        else {
            //fade out the current image
            current.removeClass('show')
            .hide();
            //IE-PNG-Issue//.animate({ opacity: 0.0 }, 800);
            //fade in the next image
            //IE-PNG-Issue//next.css({ opacity: 0.0 })
            next.hide()
            .addClass('show')
            .show();
            //IE-PNG-Issue//.animate({ opacity: 1.0 }, 800, function() {
            if ($('div.block', next).length > 0) {
                //show the next description
                $('div.block', next)
                    .css({ opacity: 0.85 })
                    .slideDown(800);
            }
            //IE-PNG-Issue//})
            //set the image opacity to 100% so that the description appears semi-transparent
            next.find('img').show();
            //.css({ opacity: 1.0 });
        }
    });
};

jQuery.fn.RotatePrevious = function () {
    return this.each(function () {
        var parentID = '#' + this.id;
        var current = ($('ul li.show', this).length ? $('ul li.show', this) : $('ul li:first', this));
        var prev = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $('ul li:last', this) : current.prev()) : $('ul li:last', this));

        //there is a description to hide
        if ($('div.block', current).length > 0) {
            //hide the current description
            $('div.block', current).slideUp(800, function () {
                //fade out the current image
                current.removeClass('show')
                .hide();
                //IE-PNG-Issue//.animate({ opacity: 0.0 }, 800);
                //fade in the previous image
                //IE-PNG-Issue//prev.css({ opacity: 0.0 })
                prev.hide()
                .addClass('show')
                .show();
                //IE-PNG-Issue//.animate({ opacity: 1.0 }, 800, function() {
                if ($('div.block', prev).length > 0) {
                    //show the previous description
                    $('div.block', prev)
                        .css({ opacity: 0.85 })
                        .slideDown(800);
                }
                //IE-PNG-Issue//})
                //set the image opacity to 100% so that the description appears semi-transparent
                prev.find('img').show();
                //.css({ opacity: 1.0 });
            });
        }
        //there is no description to hide
        else {
            //fade out the current image
            current.removeClass('show')
            .hide();
            //IE-PNG-Issue//.animate({ opacity: 0.0 }, 800);
            //fade in the previous image
            //IE-PNG-Issue//prev.css({ opacity: 0.0 })
            prev.hide()
            .addClass('show')
            .show()
            //IE-PNG-Issue//.animate({ opacity: 1.0 }, 800, function() {
            if ($('div.block', prev).length > 0) {
                //show the previous description
                $('div.block', prev)
                    .css({ opacity: 0.85 })
                    .slideDown(800);
            }
            //IE-PNG-Issue//})
            //set the image opacity to 100% so that the description appears semi-transparent
            prev.find('img').show();
            //.css({ opacity: 1.0 });
        }
    });
};

jQuery.fn.Rotate = function (options) {

    options = jQuery.extend({
        delay: 6000,
        buttons: false
    }, options);

    return this.each(function () {
        var parent = $(this);
        var parentID = '#' + this.id;
        $('ul li', parent).hide(); //.css({ opacity: 0.0 });
        var current = ($('ul li.show', parent).length ? $('ul li.show', parent) : $('ul li:first', parent));

        //show the current image
        //IE-PNG-Issue//$(current).css("opacity", "1.0").find('img').css({ opacity: 1.0 });
        $(current).show().find('img').show();

        //show the description
        if ($('div.block', current).length > 0) {
            $('div.block', current)
        .css({ opacity: 0.85 })
        .slideDown(800);
        }

        //build the command to change the slides
        var rotateCommand = "$('" + parentID + "').RotateNext();";

        if (options.buttons == true) {
            $('ul li', parent).each(function () {
                $(this).append('<div class="left" title="Previous slide"></div>');
                $(this).append('<div class="right" title="Next slide"></div>');
            });
            $('ul li div.left', parent).click(function (event) {
                $(parentID).RotatePrevious();
            });
            $('ul li div.right', parent).click(function (event) {
                $(parentID).RotateNext();
            });
        }

        //register the timer
        setInterval(rotateCommand, options.delay);
    });
};
