(function($) {  
		
	jQuery.fn.cdtStart = function(config){

		config = jQuery.extend({
				path : 'js/cdt/',
				weekendSkip : false
			},config);

		var rewrite_set = {
			image : {
				background : {
					name : 'cdt_background.gif',
					width : 498,
					height : 49
				},
				number : {
					name : 'cdt_number.gif',
					width : 26,
					height : 461
				}
			}
		};

		var target = this;

		createTimberLayout();

		var limit_time = getLimitTime();


		this.everyTime(1000, 'controlled', function(i){
			config.dateNow.setSeconds(config.dateNow.getSeconds()+1);
			limit_year = limit_time.getYear();
			if(limit_year < 2000){
				limit_year += 1900;
			}

			passtime = limit_time.getTime() - config.dateNow.getTime();

			count_day = Math.floor(passtime/(1000*60*24));
			passtime = limit_time.getTime() - config.dateNow.getTime();

			count_hours = Math.floor(passtime/(1000*60*60));
			passtime = passtime - (count_hours*(1000*60*60));

			count_min = Math.floor(passtime/(1000*60));
			passtime = passtime - (count_min*(1000*60));

			count_sec = Math.floor(passtime/1000);
			passtime = passtime - (count_sec*1000);

			if(count_hours<10){ count_hours = "0"+count_hours; }
			if(count_min<10){ count_min = "0"+count_min; }
			if(count_sec<10){ count_sec = "0"+count_sec; }
			
			point = Math.round(rewrite_set.image.number.height / 10);

			if((limit_time-config.dateNow) > 0){
				$("#timer_image\\[1\\]").css({top : parseInt(String(count_hours).charAt(0))*point*-1});
				$("#timer_image\\[2\\]").css({top : parseInt(String(count_hours).charAt(1))*point*-1});
				$("#timer_image\\[4\\]").css({top : parseInt(String(count_min).charAt(0))*point*-1});
				$("#timer_image\\[5\\]").css({top : parseInt(String(count_min).charAt(1))*point*-1});
				$("#timer_image\\[7\\]").css({top : parseInt(String(count_sec).charAt(0))*point*-1});
				$("#timer_image\\[8\\]").css({top : parseInt(String(count_sec).charAt(1))*point*-1});
			} else {
				target.css({
					'background-position' : '0px -49px'
				});
				for(var i=0; i<9; i++){
					$('#timer\\[' + i + '\\]').show();
				}
				year = config.dateNext.getYear();
				if(year < 2000){
					year += 1900;
				}
				mon  = config.dateNext.getMonth();
				day  = config.dateNext.getDate();
				hour = config.switchHour;;
				limit_time = new Date(year, mon, day, hour, 00, 00);
			}

			if(config.weekendSkip){
				if(getWeekendSkip()){
					target.css({
						'background-position' : '0px -98px'
					});
					for(var i=0; i<9; i++){
						$('#timer\\[' + i + '\\]').hide();
					}
				} else {
					if(!getSwitch()){
						target.css({
							'background-position' : '0px 0px'
						});
					} else {
						target.css({
							'background-position' : '0px -49px'
						});
					}

				}
			} else {
				if(!getSwitch()){
					target.css({
						'background-position' : '0px 0px'
					});
				} else {
					target.css({
						'background-position' : '0px -49px'
					});
				}
			}


		});

		function createTimberLayout(){

			var background_image_path   = config.path + rewrite_set.image.background.name;
			var background_image_width  = rewrite_set.image.background.width;
			var background_image_height = rewrite_set.image.background.height;

			var number_image_path   = config.path + rewrite_set.image.number.name;
			var number_image_width  = rewrite_set.image.number.width;
			var number_image_height = Math.round(rewrite_set.image.number.height / 10 - 8);

			target.css({
				background : 'url(' + background_image_path + ') no-repeat',
				'background-position-y' : 0,
				height : background_image_height,
				width : background_image_width
			});

			target.append('<div style="height:8px"></div>');
			for(var i=0; i<9; i++){
				target.append('<div id="timer[' + i + ']">');
				switch(i){
					case 0:
						$('#timer\\[' + i + '\\]').css({
							'float' : 'left',
							height : number_image_height,
							width : 306
						});
						break;
					case 3:
						$('#timer\\[' + i + '\\]').css({
							'float' : 'left',
							height : number_image_height,
							width : 14
						});
						break;
					case 6:
						$('#timer\\[' + i + '\\]').css({
							'float' : 'left',
							height : number_image_height,
							width : 14
						});
						break;
					default:
						$('#timer\\[' + i + '\\]').css({
							'float' : 'left',
							height : number_image_height,
							overflow : 'hidden',
							position : 'relative',
							width : number_image_width
						})
						.html(
							'<img id="timer_image[' + i + ']" src="' + number_image_path + '" />'
						);
						$('#timer_image\\[' + i + '\\]').css({
							position : 'relative'
						});
				}
			}

		}

		function getLimitTime(){
			var year = null;
			var mon = null;
			var day = null;
			var hour = null;
			var limittime = null;
			if(!getSwitch()){
				year = config.dateNow.getYear();
				if(year < 2000){
					year += 1900;
				}
				mon  = config.dateNow.getMonth();
				day  = config.dateNow.getDate();
				hour = config.switchHour;
				limittime = new Date(year, mon, day, hour, 00, 00);
			} else {
				year = config.dateNext.getYear();
				if(year < 2000){
					year += 1900;
				}
				mon  = config.dateNext.getMonth();
				day  = config.dateNext.getDate();
				hour = config.switchHour;
				limittime = new Date(year, mon, day, hour, 00, 00);
			}
			return limittime;
		}

		function getSwitch(){
			var nowHour = config.dateNow.getHours();
			if((nowHour >= 0) && (nowHour <= (config.switchHour - 1))){
				return false;
			} else if((nowHour >= (config.switchHour - 1)) && (nowHour <= 23)){
				return true;
			}
		}

		function getWeekendSkip(){
			switch(config.dateNow.getDay()){
				case 0:
					if(!getSwitch()){
						return true;
					}
					break;
				case 5:
					if(getSwitch()){
						return true;
					}
					break;
				case 6:
					return true;
					break;
			}
			return false;
		}

	};

})(jQuery);
