var Clock = Class.create({

    initialize: function(time_zone, element_id) {
        this.time_zone = time_zone;
        this.element_id = element_id;

        this.cookie_id = element_id + '-cookie';
        this.select_id = element_id + '-select';
        this.select_area_id = element_id + '-select-area';
        this.select_show_link_id = element_id + '-select-show-link';

        this.time_zone_hash = {
            '-12': '(GMT -12) Enitwetok',
            '-11' : '(GMT -11) Samoa',
            '-10' : '(GMT -10) Hawaii',
            '-9' : '(GMT -9) Alaska',
            '-8' : '(GMT -8) Pacific Time',
            '-7' : '(GMT -7) Mountain Time',
            '-6' : '(GMT -6) Central Time',
            '-5' : '(GMT -5) Eastern Time',
            '-4' : '(GMT -4) Atlantic Time',
            '-3' : '(GMT -3) Brazil',
            '-2' : '(GMT -2) Mid-Atlantic',
            '-1' : '(GMT -1) Azores',
            '0' : '(GMT) London',
            '1' : '(GMT +1) Brussels',
            '2' : '(GMT +2) South Africa',
            '3' : '(GMT +3) Moscow',
            '4' : '(GMT +4) Abu Dhabi',
            '5' : '(GMT +5) Ekaterinburg',
            '6' : '(GMT +6) Almaty',
            '7' : '(GMT +7) Bangkok',
            '8' : '(GMT +8) Hong Kong',
            '9' : '(GMT +9) Osaka',
            '10' : '(GMT +10) Melbourne',
            '11' : '(GMT +11) Magadan',
            '12' : '(GMT +12) Fiji'
        };

		return this;
    },
 
    currentTime: function() {
        var current_time = new Date();
        var time_in_msec = current_time.setHours(current_time.getHours() + current_time.getTimezoneOffset() / 60 + this.time_zone);
        
        return new Date(time_in_msec + 3600000)
    },
    
    toString: function() {
        var time = this.currentTime()
        var hour = time.getHours() ;
        var minute = time.getMinutes();
        var second = time.getSeconds();
        
        var cur_time = "" + ((hour > 12) ? hour - 12 : hour);
        
        if (hour == 0) cur_time = "12";
        
        cur_time += ((minute < 10) ? ":0" : ":") + minute;
//        cur_time += ((second < 10) ? ":0" : ":") + second;
        cur_time += (hour >= 12) ? " PM" : " AM";
        
        return cur_time;
    },

    display: function() {
		$(this.element_id).update(this.toString());
    },

	updateTimeZone: function(time_zone) {
		this.time_zone = parseInt(time_zone);
	},

    updateTimeZoneSelect: function() {
        var time_zone = this.time_zone;

        $(this.select_show_link_id).update(this.time_zone_hash[time_zone]);

        $(this.select_id).childElements().each(function(element) {
            element.selected = parseInt(element.value) == time_zone;
        });
    },

    updateTimeZoneFromCookie: function() {
       cookie_time_zone = Cookie.get(this.cookie_id);
       if (cookie_time_zone != null && cookie_time_zone != undefined) {
           this.updateTimeZone(cookie_time_zone);
       }
    },

    initTimeZoneSelect: function() {
        var clock = this;

        $(clock.select_show_link_id).onclick = function(event) {
            event.element().hide();
            $(clock.select_id).show();
        };

        var select_element = $(clock.select_id);

        for (time_zone in clock.time_zone_hash) {
            var time_zone_name = clock.time_zone_hash[time_zone];

            var option = document.createElement('option');
            option.value = time_zone;
            option.innerHTML = time_zone_name;

            
            option.onclick = function(event) {
                var element_time_zone = event.element().value;

                clock.updateTimeZone(element_time_zone);
                clock.updateTimeZoneSelect();
                    
                Cookie.set(clock.cookie_id, element_time_zone, 30);

                $(clock.select_id).hide();
                $(clock.select_show_link_id).show();
            };

            select_element.appendChild(option);
        };
        
    },

    initClockUpdaterOnLoad: function() {
        var clock = this;
        
        Event.observe(window, 'load', function() {
            clock.updateTimeZoneFromCookie();

            clock.initTimeZoneSelect();

            clock.display();

            clock.updateTimeZoneSelect();
            
	        new PeriodicalExecuter(function(pe) {
    	        clock.display();
        	}, 1);    
        });
        
        return clock;
    }

});

