
// Copyright (c) 2010 Semlyen IT Consultants (http://www.semlyen.net)

function semlyenCountDown(container, imagePath) {

    var container = document.getElementById(container);
    this.children = container.childNodes;
    this.months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    this.images = new Array();
    this.imagePath = imagePath;
    this.customDifference = 0;

    this.start = function(intYear, intMonth, intDay, intHour, intMinute, intSecond) {
        this.futureString = this.months[intMonth - 1] + " " + intDay + ", " + intYear + " " + intHour + ":" + intMinute + ":" + intSecond;
        for (i = 0; i < this.children.length; i++) {
            if (this.children[i].nodeName == 'IMG') {
                this.images[this.images.length] = this.children[i];
            }
        }
        this.update();
        this.timer = setInterval(methodize(this.update,this), 1000);
    }
    
    this.stop = function() {
        clearInterval(this.timer);
    }

    this.setDate = function(intYear, intMonth, intDay, intHour, intMinute, intSecond) {
        var today = new Date()
        var todayYear = today.getYear();
        if (todayYear < 1000) {
            todayYear += 1900;
        }
        var todayMonth = today.getMonth();
        var todayDay = today.getDate();
        var todayHour = today.getHours();
        var todayMin = today.getMinutes();
        var todaySec = today.getSeconds()
        var localTime = this.months[todayMonth] + " " + todayDay + ", " + todayYear + " " + todayHour + ":" + todayMin + ":" + todaySec;
        this.customTime = this.months[intMonth - 1] + " " + intDay + ", " + intYear + " " + intHour + ":" + intMinute + ":" + intSecond;
        var local = Date.parse(localTime);
        var custom = Date.parse(this.customTime);
        if (local > custom) {
            this.customDifference = local - custom
        } else {
            this.customDifference = custom - local;
        }
    }

    this.update = function() {
        var today = new Date()
        var todayYear = today.getYear();
        if (todayYear < 1000) {
            todayYear += 1900;
        }
        var todayMonth = today.getMonth();
        var todayDay = today.getDate();
        var todayHour = today.getHours();
        var todayMin = today.getMinutes();
        var todaySec = today.getSeconds();

        this.todayString = this.months[todayMonth] + " " + todayDay + ", " + todayYear + " " + todayHour + ":" + todayMin + ":" + todaySec;

        var dateDifference = Date.parse(this.futureString) - Date.parse(this.todayString);
        if (this.customDifference > 0) {
            dateDifference = dateDifference - this.customDifference;
        } else if (this.customDifference < 0) {
            dateDifference = dateDifference + this.customDifference;
        }
        var remainingDays = Math.floor(dateDifference / (60 * 60 * 1000 * 24) * 1);
        var remainingHours = Math.floor((dateDifference % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
        var remainingMinutes = Math.floor(((dateDifference % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
        var remainingSeconds = Math.floor((((dateDifference % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
        if (remainingDays <= 0 && remainingHours <= 0 && remainingMinutes <= 0 && remainingSeconds <= 0) {
            this.updateImages(0, 0, 0, 0);
            this.stop();
        } else {
            this.updateImages(remainingDays, remainingHours, remainingMinutes, remainingSeconds);
        }
    }

    this.updateImages = function(intDays, intHours, intMinutes, intSeconds) {
        intDays = this.pad(intDays, 3, '0') + '';
        intHours = this.pad(intHours, 2, '0') + '';
        intMinutes = this.pad(intMinutes, 2, '0') + '';
        intSeconds = this.pad(intSeconds, 2, '0') + '';
        imageValues = new Array(intDays.substring(0,1), intDays.substring(1,2), intDays.substring(2), intHours.substring(0,1), intHours.substring(1), intMinutes.substring(0,1), intMinutes.substring(1), intSeconds.substring(0,1), intSeconds.substring(1));

        for (i = 0; i < this.images.length; i++) {
            if (typeof(this.images[i]) == 'object') {
                this.images[i].src = this.imagePath + imageValues[i] + '.gif';
            }
        }
    }

    this.pad = function(string, length, character) {
        string = string + '';
        if (string.length < length) {
            while (string.length != length) {
                string = character + '' + string;
            }
        }
        return string;
    }

    this.objExists = function(theVal) {
        if (document.getElementById(theVal) != null) {
            return true;
        } else {
            return false;
        }
    }

    function methodize(methodize_func,methodize_scope){
        return (function(){methodize_func.call(methodize_scope);});
    }
}