$(function() {

	/* SLIDEBOX */

	$('#slideBox').find('.answer').hide().end().find('.header').click(function() {

		// If box is already open let's close it and toggle icon
		if($(this).hasClass('open')) {
			$(this).next().slideUp('fast').end();
			$(this).toggleClass('open');
		}

		// Otherwise we close all and show clicked one and toggle icons
		else {
			$('.header').removeClass('open');
			$('.answer').slideUp('fast');
			$(this).toggleClass('open');
			$(this).next().slideDown('fast');
		}
	});


	/* VOTEBOX (HOITOPOLUT) */

	$('a.voteYes').click(function(e) {
		e.preventDefault();
		currentId = $(this).attr('id');
		currentUrl = $(this).attr('href');
		voteButtons = $(this).parent();
		voteCount = $(this).parents('.box').find('.download .voteCount');

		// Fading out
		voteButtons.html('&nbsp;');
		voteCount.hide();

		// Sending request
		$.ajax({
			type: 'POST',
			data: 'action=voteYes&id='+ currentId,
			url: currentUrl +'/d/',
			success: function(msg) {

				// Fading in
				voteButtons.hide();
				voteButtons.html('Kiitos äänestäsi!');
				voteButtons.fadeIn(500);
				voteCount.html(msg);
				voteCount.fadeIn(500);
			}
		});
	});

	$('a.voteNo').click(function(e) {
		e.preventDefault();
		currentId = $(this).attr('id');
		currentUrl = $(this).attr('href');
		voteButtons = $(this).parent();
		voteCount = $(this).parents('.box').find('.download .voteCount');

		// Fading out
		voteButtons.html('&nbsp;');
		voteCount.hide();

		// Sending request
		$.ajax({
			type: 'POST',
			data: 'action=voteNo&id='+ currentId,
			url: currentUrl +'/d/',
			success: function(msg) {

				// Fading in
				voteButtons.hide();
				voteButtons.html('Kiitos äänestäsi!');
				voteButtons.fadeIn(500);
				voteCount.html(msg);
				voteCount.fadeIn(500);
			}
		});
	});


	/* VOTEBOX (VINKKILISTAT) */

	$('a.btnThumbYes').click(function(e) {
		e.preventDefault();
		currentId = $(this).attr('id');
		currentUrl = $(this).attr('href');
		voteButtons = $(this).parent();
		voteCount = $(this).parents('.center').find('.right .voteCount');

		// Fading out
		voteButtons.html('&nbsp;');
		voteCount.hide();

		// Sending request
		$.ajax({
			type: 'POST',
			data: 'action=voteYes&id='+ currentId,
			url: currentUrl +'/d/',
			success: function(msg) {

				// Fading in
				voteButtons.hide();
				voteButtons.html('<img src="assets/img/titleKiitosAanestasi.gif" width="128" height="13" title="Kiitos äänestäsi!" alt="Kiitos äänestäsi!" border="0" />');
				voteButtons.fadeIn(500);
				voteCount.html(msg);
				voteCount.fadeIn(500);
			}
		});
	});

	$('a.btnThumbNo').click(function(e) {
		e.preventDefault();
		currentId = $(this).attr('id');
		currentUrl = $(this).attr('href');
		voteButtons = $(this).parent();
		voteCount = $(this).parents('.center').find('.right .voteCount');

		// Fading out
		voteButtons.html('&nbsp;');
		voteCount.hide();

		// Sending request
		$.ajax({
			type: 'POST',
			data: 'action=voteNo&id='+ currentId,
			url: currentUrl +'/d/',
			success: function(msg) {

				// Fading in
				voteButtons.hide();
				voteButtons.html('<img src="assets/img/titleKiitosAanestasi.gif" width="128" height="13" title="Kiitos äänestäsi!" alt="Kiitos äänestäsi!" border="0" />');
				voteButtons.fadeIn(500);
				voteCount.html(msg);
				voteCount.fadeIn(500);
			}
		});
	});


	/* IMAGE POPUPS (FANCYBOX) */

	$("a.singleImage").fancybox({
		'titleShow' : false,
		'speedIn' : 250,
		'speedOut' : 250,
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade'
	});


	/* NEWSLETTER POPUPS (FANCYBOX) */

	$("a.newsletter").fancybox({
		'titleShow' : false,
		'width': 800,
		'height': '95%',
		'speedIn' : 250,
		'speedOut' : 250,
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
		'type' : 'iframe'
	});


	/* ADD ITEM TO CART */

	$('a.btnLisaaKoriin').click(function(e) {
		e.preventDefault();
		currentItemCode = $(this).attr('id');
		currentUrl = $(this).attr('href');
		currentAmount = $(this).parent('.form').find('#amount').val();
		text = $(this).parent('.form').find('span');

		// Let's make sure that input is filled correctly
		if(currentAmount.length >= 1 && currentAmount.length <= 3) {

			// Fading out & resetting input
			text.hide();
			$(this).parent('.form').find('#amount').val('');

			// Sending request
			$.ajax({
				type: 'POST',
				data: 'action=addItem&itemCode='+ currentItemCode +'&amount='+ currentAmount,
				url: currentUrl +'/d/',
				success: function(msg) {

					// Fading in & returning to original value 
					text.html('Lisätty');
					text.fadeIn(500, function() {
						text.delay(1000).fadeOut(250, function() {
							text.html('Kpl');
							text.fadeIn(250);
						});
					});

					// Updating cart total item count
					$('#cart p:first').fadeOut(250, function() {
						$('#cart p:first').html(msg);
						$('#cart p:first').fadeIn(250);
					});
				}
			});
		}
	});


	/* REMOVE ITEM FROM CART */

	$('a.removeItem').click(function(e) {
		e.preventDefault();
		currentItemCode = $(this).attr('id');
		currentUrl = $(this).attr('href');
		field = $(this).parents('tr');

		// Sending request
		$.ajax({
			type: 'POST',
			data: 'action=removeItem&itemCode='+ currentItemCode,
			url: currentUrl +'/d/',
			success: function(msg) {

				// Removing field
				field.fadeOut(500, function() {
					field.remove();
				});

				// Updating cart total item count
				$('#cart p:first').fadeOut(250, function() {
					$('#cart p:first').html(msg);
					$('#cart p:first').fadeIn(250);
				});
			}
		});
	});


	/* REFRESH CART */

	$('a#refreshCart').click(function(e) {
		e.preventDefault();
		currentUrl = $(this).attr('href');
		values = $('#shoppingCartForm').serialize();
		text = $(this).parent('.refresh').find('a');

		// Sending request
		$.ajax({
			type: 'POST',
			data: 'action=refreshCart&'+ values,
			url: currentUrl +'/d/',
			success: function(msg) {

				// Fading in & returning to original value 
				text.fadeOut(250, function() {
					text.html('Päivitetty');
					text.fadeIn(500, function() {
						text.delay(1000).fadeOut(250, function() {
							text.html('Päivitä ostoskori');
							text.fadeIn(250);
						});
					});
				});
			}
		});
	});


	/* EQUALIZING COLUMN HEIGHTS */

	equalHeight($(".shoppingMedicineBox .content .contentEqualizer1"));
	equalHeight($(".shoppingMedicineBox .content .contentEqualizer2"));
	equalHeight($(".shoppingMedicineBox .content .contentEqualizer3"));
	equalHeight($(".shoppingMedicineBox .content .contentEqualizer4"));
	equalHeight($(".shoppingMedicineBox .content .contentEqualizer5"));
	equalHeight($(".shoppingMedicineBox .content .contentEqualizer6"));

	equalHeight($(".medicineBox .link .contentEqualizer1"));
	equalHeight($(".medicineBox .link .contentEqualizer2"));
	equalHeight($(".medicineBox .link .contentEqualizer3"));
	equalHeight($(".medicineBox .link .contentEqualizer4"));
	equalHeight($(".medicineBox .link .contentEqualizer5"));
	equalHeight($(".medicineBox .link .contentEqualizer6"));


	// Radiobuttons hack
	$('input[name=q1]').click(function() {
		if($(this).val() == 'e') {
			$('#questionC_topics').show();
		}
		else {
			$('#questionC_topics').hide();
		}
	});

});


function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function doClear(text) {
	if(text.value == text.defaultValue) {
		text.value = '';
		text.style.color = '#333333';
	}
}

var inputs;
var imgFalse = 'assets/img/checkboxNo.gif';
var imgTrue = 'assets/img/checkboxYes.gif';

function replaceChecks() {
	inputs = document.getElementsByTagName('input');
	for(var i=0; i < inputs.length; i++) {
		if(inputs[i].getAttribute('type') == 'checkbox') {
			var img = document.createElement('img');
			if(inputs[i].checked) {
				img.src = imgTrue;
			} else {
				img.src = imgFalse;
			}
			img.id = 'checkImage'+i;
			img.onclick = new Function('checkChange('+i+')');
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			inputs[i].style.display='none';
		}
	}
}

function checkChange(i) {
	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
	}
}

window.onload = replaceChecks;

