//behaviour.js

$(document).ready(function(){
	//Table striping and highlight.
	$('#options tr:even').addClass('striped');
	$('table.zebra tr:even').addClass('striped');
	$('table.zebra tr').mouseover(function(){$(this).addClass('highlight');});
	$('table.zebra tr').mouseout(function(){$(this).removeClass('highlight');});

	//Table striping and highlight.
	$('#order_about table tr:even').addClass('striped');
	$('#order_checklist > li:even').addClass('striped');
	$('#portfolio > li:even').addClass('striped');

	//Add website option checkbox events.
	calculateOptionsTotal();
	$('#options :checkbox').bind('click', function(){
		calculateOptionsTotal();
	});

	//Portfolio hover states
	$('ul#portfolio img').mouseover(function(){
		$(this).fadeTo('medium', 1);
		$(this).parentsUntil('ul').find('h3').css('color', '#22761A');
	});
	$('ul#portfolio img').mouseout(function(){
		$(this).fadeTo('medium', 0.6);
		$(this).parentsUntil('ul').find('h3').css('color', '#111111');
	});

	//If the order form has been submitted show it.
	if (($('body').attr('id') == '/ordering') && (showForm == 1)){
		$('#order_about').hide();
	}
	//Otherwise show checklist.
	else{
		$('#order_form').hide();
	}

	//Checklist order form toggle.
	$('a.open_order').click(function(){
		$('#order_about').hide('100', function(){$('#order_form').show('slow');});
	});
	$('a.close_order').click(function(){
		$('#order_form').hide('100', function(){$('#order_about').show('slow');});
	});

	//Checklist toggling.
	$('#order_checklist div.check_summ b').click(function(){
		if ($(this).hasClass('more'))
			$(this).addClass('close').removeClass('more').parents('li').children('.check_info').slideDown();
		else
			$(this).addClass('more').removeClass('close').parents('li').children('.check_info').slideUp();
	});

	//Order form list effects
	$('#orderform ul ul').hide();
	$('#orderform :checkbox:checked').siblings('ul').show();
	$('#orderform :checkbox').change(function(){
		if ($(this).is(':checked')){
			$(this).parent().children('ul').eq(0).slideDown('fast');
		}
		else
		{
			$(this).parent().children('ul').eq(0).slideUp('fast');
		}
	});

	//Admin form list effects
	$('#pane_accn ul ul').hide();
	$('#pane_accn :checkbox:checked').parent().children('ul').eq(0).show();
	$('#pane_accn :checkbox').change(function(){
		if ($(this).is(':checked'))
		{
			$(this).parent().children('ul').eq(0).slideDon('fast');
		}
		else
		{
			$(this).parent().children('ul').eq(0).slideUp('fast');
		}
	});

	//Website order editing interface.
	$('.formsection form').hide();
	$('.formsection h5 b.cancel').hide();
	$('.formsection h5 b').click(function(){
		if ($(this).attr('class') == 'edit')
		{
			$('.formsection').children('form').hide();
			$('.formsection h5 b.edit').show();
			$('.formsection h5 b.cancel').hide();
			$(this).parents('.formsection').children('form').slideDown('fast');
		}
		else
		{
			$(this).parents('.formsection').children('form').slideUp('fast');
		}

		$(this).siblings('b').show();
		$(this).hide();
	});


	//Sales interface.
	$('input.datetime').datetimepicker({
		numberOfMonths: 2,
		showSecond: false,
		hourMin: 6,
		hourMax: 21,
		stepHour: 1,
		stepMinute: 5,
		hourGrid: 2,
		minuteGrid: 15
	});

	$('#comment-check').click(function(){
		if ($(this).attr('checked'))
		{
			$('li.comment').slideDown(500);
		}
		else
		{
			$('li.comment').slideUp(500);
		}
	});
	$('#notice-check').click(function(){
		if ($(this).attr('checked'))
		{
			$('li.notice').slideDown(500);
		}
		else
		{
			$('li.notice').slideUp(500);
		}
	});
	$('#result').change(function(){
		if ($(this).attr('value') != '0')
		{
			$('li.contact').slideDown(500);
			if ($('#notice-check').attr('checked'))
			{
				$('li.notice').slideDown(500);
			}

			if ($('#comment-check').attr('checked'))
			{
				$('li.comment').slideDown(500);
			}
		}
		else
		{
			$('li.contact').slideUp(500);
			$('li.notice').slideUp(500);
			$('li.comment').slideUp(500);
		}
	});
	//Initial setting
	if ($('#result').attr('value') != '0')
	{
		$('li.contact').show();
		if ($('#notice-check').attr('checked'))
		{
			$('li.notice').show();
		}

		if ($('#comment-check').attr('checked'))
		{
			$('li.comment').show();
		}
	}

});

function calculateOptionsTotal()
{
	var total = 0;
	$('#options :checked').each(function(i){
		total = total + parseInt(this.value);
	});

	//Add product discount.
	total = total * 0.8;
	var number = new Number(total);
	total = number.toFixed(0);

	//Change total display value.
	$('#options_total').text('$' + total);
}



