var countdownTime;
var countdownMonths;
var t;
var mtext;
var wtext;
var dtext;
var mintext;
var stext;


function runCountdown(){


var dweeks=Math.floor(countdownTime/(60*60*24*7));


var tempweeks = countdownTime%(60*60*24*7);
var dday=Math.floor(tempweeks/(60*60*24));
var tempdays = tempweeks%(60*60*24);
var dhour=Math.floor(tempdays/(60*60));
var temphours = tempdays%(60*60);
var dmin=Math.floor(temphours/60);
var dsec= temphours%60;

if(countdownMonths == 0){

	if(countdownMonths != 0){
		if(countdownMonths == 1){
			mtext = countdownMonths + ' Month, ';

		}
		else{
			mtext = countdownMonths + ' Months, ';

		}
		$("#show_months").html(mtext);

	}
	else{
		$("#show_months").html(" ");


	}

	if(dweeks == 1){
		wtext = 'Market Locks in ' + dweeks + ' Week, ';

	}
	else{
		wtext = 'Market Locks in ' + dweeks + ' Weeks, ';

	}
	$("#show_weeks").html(wtext);



	if(dday == 1){
		dtext = dday + ' Day, ';

	}
	else{
		dtext = dday + ' Days, ';

	}
	$("#show_days").html(dtext);

	if(dhour == 1){
		htext = dhour + ' Hour, ';

	}
	else{
		htext = dhour + ' Hours, ';

	}
	$("#show_hours").html(htext);
	if(dmin == 1){
		mintext = dmin + ' Minute ';

	}
	else{
		mintext = dmin + ' Minutes ';

	}
	$("#show_minutes").html(mintext);
	if(dsec == 1){
		stext =  ' and ' + dsec + ' Second, ';

	}
	else{
		stext =   ' and ' + dsec + ' Seconds';

	}
	$("#show_seconds").html(stext);
	countdownTime = parseInt(countdownTime) - 1;
	if(countdownTime == 0){
		if(countdownMonths != 0){
			countdownMonths = parseInt(countdownMonths) - 1;
			countdownTime = 2592999;

		}
		else{
			$("#show_months").html('<p style="color: red"><b>This show has ended</b></p>');
			$("#show_weeks").html('');
			$("#show_days").html('');
			$("#show_hours").html('');
			$("#show_minutes").html('');
		}

	}

	t=setTimeout("runCountdown()",1000);
}
}


$(document).ready(function(){

	if(countdownTime != ''){
		if(countdownTime == 0){
			$("#show_months").html('<p style="color: red"><b>This show has ended</b></p>');

		}
		else{
		runCountdown();
		}

	}

	$('#tvshow_edit').click(function(){
		$('#tvshow_edit_form').toggle();
		return false;
	});
	$('#contestant_add').click(function(){

		$('#contestant_add_form').toggle();
		return false;
	});

	$('.buy, .sell').each(function(){
		$(this).click(function(){
			$('#' + $(this).attr("id") + '_form').toggle();
			return false;
		});
	});



});
