$(document).ready(function() {
	$('#header a.change_city').click(function(){
		$('#change_cities').slideToggle();
		return false;
	});
});

var DealTimer = new function()
{
    this.clocks = new Array();
    this.timer = new Array();
    this.init = function(timerWrap, timeLeft)
    {
        var clockIdx = this.clocks.length;
        this.clocks[clockIdx] = new DealClock(timerWrap, timeLeft);
        this.updateClock(clockIdx);
    };

    this.updateClock = function(cIdx)
    {
        if(!this.clocks[cIdx]) {return true;}
        if(this.clocks[cIdx].timeLeft < 1) {
            this.clocks[cIdx].setDealStatus('closed');
            this.clocks.splice(cIdx,1);
            return true;
        }
        this.clocks[cIdx].changeTime();
        this.timer[cIdx] = setTimeout('DealTimer.updateClock('+cIdx+')',1000);
    }
}

function DealClock(timerWrapId, timeLeft)
{
    this.timerWrap;
    this.clockDisplay;
    this.dayDisplay;
    this.hourDisplay;
    this.minDisplay;
    this.secDisplay;
    this.initialTimeLeft = timeLeft;
    this.timeLeft = this.initialTimeLeft;
    this.setExpire = false;
    
    this.changeTime = function()
    {
        this.timeLeft -= 1;
        var daysFloat = this.timeLeft / 86400;
        var daysInt = parseInt(daysFloat);
        var hoursFloat  = (this.timeLeft - (daysInt * 86400)) / 3600;
        var hoursInt  = parseInt(hoursFloat);
        var minsFloat = (this.timeLeft - ((daysInt * 86400) + (hoursInt * 3600))) / 60;
        var minsInt = parseInt(minsFloat)
        var secsInt = parseInt(this.timeLeft - ((daysInt * 86400) + (hoursInt * 3600) + (minsInt * 60)));

        this.setTime(daysInt, hoursInt, minsInt, secsInt);
        
        if(this.timeLeft < 3600 && !this.setExpire) {
            this.setDealStatus('ending');
            this.setExpire = true;
        }
    }

    this.setTime = function(days, hours, mins, secs)
    {
        var vals = this.getParsedValue(days);
            this.dayDisplay[0].innerHTML = vals[0];
            this.dayDisplay[1].innerHTML = vals[1];
        vals = this.getParsedValue(hours);
            this.hourDisplay[0].innerHTML = vals[0];
            this.hourDisplay[1].innerHTML = vals[1];
        vals = this.getParsedValue(mins);
            this.minDisplay[0].innerHTML = vals[0];
            this.minDisplay[1].innerHTML = vals[1];
        vals = this.getParsedValue(secs);
            this.secDisplay[0].innerHTML = vals[0];
            this.secDisplay[1].innerHTML = vals[1];
        return true;
    };

    this.getParsedValue = function(value)
    {
        value = value  < 10 ? '0'+value : value+'';
        return new Array(value.charAt(0), value.charAt(1));
    };

    this.setDealStatus = function(status)
    {
        switch(status)
        {
            case 'ending':
                this.clockDisplay.className += ' numbers_expire';
            break;
            case 'closed':
                this.clockDisplay.className += ' numbers_expire'
            break;
        }
    }

    this.setClockDisplays = function(timerWrapId)
    {
        this.timerWrap = $('#'+timerWrapId);
        this.clockDisplay = $('#'+timerWrapId + ' .numbers')[0];
        this.dayDisplay = $('#'+timerWrapId + ' .numbers .days .number');
        this.hourDisplay = $('#'+timerWrapId + ' .numbers .hours .number');
        this.minDisplay = $('#'+timerWrapId + ' .numbers .minutes .number');
        this.secDisplay = $('#'+timerWrapId + ' .numbers .secs .number');
    };

    this.setClockDisplays(timerWrapId);
}
