/*
CSS Browser Selector v0.3.5 (Feb 05, 2010)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',o='opera',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);

function isInteger(s) {
    var i;
    if (isEmpty(s))
        if (isInteger.arguments.length == 1) return 0;
    else return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}
function isDigit(c) {
    return ((c >= "0") && (c <= "9"))
}
function PostBackOnReturn(event, postbackMethod) {
    if (event) {
        if (event.keyCode == 13) {
            __doPostBack(postbackMethod, '');
        }
    }
}

function isValidEmail(email) {
    var regex = /([\w\d\-_]+)(\.[\w\d\-_]+)*@([\w\d\-_]+\.)([\w\d\-_]+\.)*([\w]{2,3})/;
    if (regex.test(email))
        return true;
    else
        return false;
}
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
    chars = chars || '\\s';
    return str.replace(new RegExp('^[" + chars + "]+', 'g'), '');
}
function rtrim(str, chars) {
    chars = chars || '\\s';
    return str.replace(new RegExp('[" + chars + "]+$', 'g'), '');
}
function setupDefaultTextReset(obj, message) {
    obj.click(function () {
        if (obj.val() == message)
            obj.val('');
    });
    obj.blur(function () {
        if (obj.val() == '')
            obj.val(message);
    });
}
function PostBackOnReturn(event, postbackMethod) {
    if (event) {
        if (event.keyCode == 13) {
            __doPostBack(postbackMethod, '');
        }
    }
}

String.prototype.startsWith = function (str) { return (this.match("^" + str) == str); }
String.prototype.trim = function () { return trim(this); }

/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

/* ~~~~~~~~~~~~~ browser upgrade ~~~~~~~~~~~~~*/
$(document).ready(function () {
    if ($.cookie('ie6message') == '1') {
        $('#ie6').hide();
    }
    $('#close_ie6upgrade').mousedown(function () {
        $.cookie('ie6message', '1', { expires: 7 });
        $('#ie6').slideUp();
        return false;
    })
});


var autoPlay = true;
var timeoutID;
var slideTime = 10000;
$(document).ready(function () {
    if ($("#slides").length > 0 && $("#slides div.galleryItem").length > 1) {
        $('#slides div.galleryItem:not(:first)').hide().find("img, .caption, .quoteL").hide();
        $('#slides div.galleryItem:first').addClass("first").addClass("active");
        $('#slides div.galleryItem:last').addClass("last");
        autoScrollInit();
    }

    $("#subscribeEmail").keypress(function (e) {
        if (e.which == 13) {
            $("#subscribe").click();
            e.preventDefault();
        }
    });

    $("#subscribe").click(function (e) {
        e.preventDefault();
        subscribe();
    });

    $("input.autoFill").each(function (idx) {
        if ($(this).val() == '') {
            $(this).val($(this).attr("title"));
        }
    });
    $("input.autoFill").focus(function () {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val('').addClass("active");
        }
    });
    $("input.autoFill").blur(function () {
        if ($(this).val() == '') {
            $(this).val($(this).attr("title")).removeClass("active");
        }
    });
});
function autoScrollInit() {
    if (autoPlay == true) {
        timeoutID = window.setInterval(autoScroll, slideTime);
    }
}

function autoScroll() {
    var current = $("#slides div.galleryItem.active");
    var next = current.next();
    if (current.hasClass("last")) {
        next = $("#slides div.galleryItem.first");
    }

    current.fadeOut('fast', function () {
        current.find("img, .caption, .quoteL").hide();
        next.show(function () {
            next.find('.quoteL').fadeIn('slow');
            next.find('img').fadeIn('slow');
            next.find('.caption').slideDown();
        });
    });

    current.removeClass('active');
    next.addClass('active');
}

function subscribe() {
    var emailEle = $("#subscribeEmail");

    var email = emailEle.val();

    var error = $("#subscribeError");
    if (error.length == 0) {
        error = $("<span />").attr("id", "subscribeError").addClass("error");
        emailEle.before(error);
    }

    error.html('').hide();
    if (email.trim() == '' ||
        email.trim() == 'enter your email address') {
        error.html('Please enter your email address');
    }
    else if (!isValidEmail(email)) {
        error.html('Please enter a valid email address');
    }

    if (error.html() != '') {
        error.fadeIn('fast');
    } else {
        var info = $("<span />").attr("id", "subscribeInfo").addClass("info").text("Please wait..").hide();
        emailEle.before(info.fadeIn('fast'));

        $.post("/handlers/NewsletterSubscriptionHandler.ashx", { email: email }, function (data) {
            info.hide();

            if (data.indexOf('Error:') != -1) {
                $('#subscribeError').html('Unfortunately an error has occured saving your details, please try again or contact us').fadeIn('fast');
            }
            else if (data.indexOf('Success') != -1) {
                $('#subscribeInfo').html('Thank you for joining the Auckland Arts Festival email list. You will receive a welcome email from us shortly.').fadeIn('fast');
            }
        });
    }
}
