$(function(){
//無料ドクターメール相談
$('.blank').click(function(){
window.open($(this).attr('href') , '' , 'width=577 , height=681 , resizable=no , scrollbars=yes');
return false;
});

$id = $('body').attr('id');
$class = $('body').attr('class');
$class = $class.split(' ');
$class = $class[0];
if($class == undefined){
	$class = $('body').attr('class');
	$class = $class.split(' ');
	$class = $class[1];
}

var flashvars = {};
var params = {};
params.wmode = "transparent";
var attributes = {};
swfobject.embedSWF("http://www.skin-gym.com/swf/info.swf", "infoswf", "129", "170", "9.0.0", false, flashvars, params, attributes);

if($class == 'studio'){
var flashvars = {};
var params = {};
params.wmode = "transparent";
var attributes = {};
var flashvars = {clinic:"skingym"};
swfobject.embedSWF("http://www.skin-gym.com/swf/pic.swf", "picswf", "530", "350", "9.0.0", false, flashvars, params, attributes);
}

if($id == 'reservation'){

var $target = $('table .reservedate td ul li dl dd select');

$target.change(function(){

var $class = $(this).parents('li').attr('class');
var $month = $(this).parent().children().siblings('select.month').attr('value');
var $date = $(this).parent().children().siblings('select.day').attr('value');
var $objHour = $(this).parent().children().siblings('select.hour');
var $hour = $(this).parent().children().siblings('select.hour').attr('value');

$today = new Date();
var $thisyear = $today.getFullYear();
var $thismonth = $today.getMonth()+1;
var $thisdate = $today.getDate();

if($month >= $thismonth){
$year = $thisyear;
} else {
$year = $thisyear + 1;
}

$day = new Date($year,$month-1,$date).getDay();

if($date != undefined){
if($day == 6 || $day == 0){
$objHour.children().each(function(){
if($(this).attr('value') == 9){
$(this).before('<option value="8">8</option>');
}
if($(this).attr('value') == 19 || $(this).attr('value') == 18){
$(this).remove();
}
});
} else {
if($class == 'first' && $objHour.children().length == 10 || $class == 'second' && $objHour.children().length == 11 || $class == 'third' && $objHour.children().length == 11){
$objHour.children('option:first').remove();
$objHour.append('<option value="18">18</option>');
$objHour.append('<option value="19">19</option>');
}
}
}
});

}

function countdown(){
today = new Date();
todaySec = today.getTime();

var year;
var month;
var lastday; //末日
year = new Date().getFullYear();
month = new Date().getMonth()+1;

var nextYear;
var nextMonth;
	
//次月取得
if (month == 12){
//翌年の1月に変更
nextYear = year+1;
nextMonth = 1;
} else {
nextYear = year;
nextMonth = month+1;
}

//末日取得
lastday = new Date(nextYear, nextMonth-1, 0);
lastday = lastday.getDate();

//endday = new Date(nextYear,nextMonth-1,1);
var endYear = $("input[name='endYear']").attr('value');
var endMonth = $("input[name='endMonth']").attr('value') - 1;
var endDay = $("input[name='endDay']").attr('value');
endday = new Date(endYear,endMonth,endDay,23,59,59);
//endday = new Date(2012,0,31,23,59,59);
enddaySec = endday.getTime();

least = enddaySec - todaySec;

oneday = 1000 * 60 * 60 * 24;
onehour = 1000 * 60 * 60;
oneminute = 1000 * 60;
onesecond = 1000;


leastDay = Math.floor(least / oneday);

leastHour = Math.floor((least - leastDay * oneday) / onehour);

leastMinute = Math.floor((least - ((leastDay * oneday) + (leastHour * onehour))) / oneminute);

leastSecond = Math.floor((least - ((leastDay * oneday) + (leastHour * onehour) + (leastMinute * oneminute))) / onesecond);

function format(num,x) {
    num = String(num);
    return (num.length < x) ? new Array((x - num.length) + 1).join('0') + num: num;
};

leastDay = String(leastDay);
leastHour = format(leastHour,2);
leastMinute = format(leastMinute,2);
leastSecond = format(leastSecond,2);

$targetDay = $('span.days');
$targetHour = $('span.hour');
$targetMinute = $('span.minute');
$targetSecond = $('span.second');

$targetDay.empty();
$targetHour.empty();
$targetMinute.empty();
$targetSecond.empty();

$targetDay.prepend(leastDay);
$targetHour.prepend(leastHour);
$targetMinute.prepend(leastMinute);
$targetSecond.prepend(leastSecond);
}

setInterval(countdown,1000);

});

