function getId(id)
{
	pos = id.lastIndexOf('_');
	if (pos != -1) {
		return id.substring(pos+1);
	} else {
		return '';
	}
}


function calculateOrder()
{
	// disable buy button
	$('#buy_submit_btn').attr('disabled', 'disabled');
	$.post(base_url+'buy/calculate', $('#buy').formSerialize(), function(data) {
		$('#buy_order').html(data);
		$('#buy_order').corners();
		// set amount for paypal payment
		$('#buy_paypal_amount').val($('#buy_total').val());
		// enable buy button
		if ($('#buy_terms_agree').attr('checked') == true)
			$('#buy_submit_btn').removeAttr('disabled');
	}, 'html');
}


function insertEml()
{
	// insert mail link
	$('.eml').wrapInner('<a href="mailto:dsbr'+String.fromCharCode(117)+''+'t@gmail.com"></a>').removeClass('eml');
}


function placeOrder()
{
	// check if we have some products selected
	var showErrorMsg = true;
	$('.buy_product_pcs').each(function() {
		if ($(this).val() > 0)
			showErrorMsg = false;
	});
	if (showErrorMsg) {
		alert('No products selected.');
		return false;
	}

	// check if all required form fields have been filled
	showErrorMsg = false;
	$('.buy_ship_required').each(function() {
		if ($(this).val() == '') {
			$(this).addClass('buy_ship_missing');
			showErrorMsg = true;
		} else {
			$(this).removeClass('buy_ship_missing');
		}
	});
	if (showErrorMsg) {
		alert('Please fill out all required fields in the form above.');
		return false;
	}

	// disable buy button
	$('#buy_submit_btn').attr('disabled', 'disabled');
	$.post(base_url+'buy/order', $('#buy').formSerialize(), function(data) {
		if (parseInt(data) > 0) {
			// set order_id
			$('#buy_paypal_item_name').val('DS brut ('+data+')');
			// start paypal payment
			$('#buy_paypal').submit();
		} else {
			alert('There was a problem placing your order.\nIf this persists please drop us a mail with your order.');
			// enable buy button
			$('#buy_submit_btn').removeAttr('disabled');
		}
	}, 'text');
}


function updateContent(data)
{
	// set content
	$('#content').html(data);

	// insert email links
	insertEml();

	// replace images
	$('#dsbluetooth_img').bind('mouseover', function () {
		$(this).attr('src', '/static/dsbluetooth_front.png');
	});
	$('#dsbluetooth_img').bind('mouseout', function () {
		$(this).attr('src', '/static/dsbluetooth_back.png')
	});

	// ### buy ###

	// round corners
	$('#content #buy_products td').wrapInner('<div class="td_round"></div>');
	$('.td_round').corners();
	$('#content table:not(#buy_products)').corners();

	// update order on change
	$('.buy_product_pcs').change(calculateOrder);
	$('#buy_ship_country').change(calculateOrder);

	// make submit button available when terms and conditions are checked
	$('#buy_terms_agree').click(function() {
		if ($(this).attr('checked') == true) {
			$('#buy_submit_btn').removeAttr('disabled');
		} else {
			$('#buy_submit_btn').attr('disabled', 'disabled');
		}
	});

	// handle submit button press
	$('#buy_submit_btn').click(placeOrder);

	// fill in email address
	$('#buy_paypal_email').val('gordan.savi'+String.fromCharCode(99)+''+'ic@gmail.com');
}


$(document).ready(function(){
	$('#news').corners();
	$('.news_item:last').css('border-bottom-style', 'none');

	$('.menu_item').corners('bottom');

	$('.menu_item').mouseover(function() {
		$(this).find('div:first').stop();
		$(this).find('div:first').animate({
			paddingBottom: "7px"
		}, 200, 'swing');
	});
	$('.menu_item').mouseout(function() {
		$(this).find('div:first').stop();
		$(this).find('div:first').animate({
			paddingBottom: "0px"
		}, 200, 'swing');
	});

	$('.news_item').click(function() {
		jQuery.get(base_url+'news/'+getId(this.id)+'/raw', function(data) {
			$('#content').html(data);
		});
	});

	$('.menu_item').click(function() {
		if (this.id == 'menu_item_about') {
			jQuery.get(base_url+'static/about.html', updateContent);
		} else if (this.id == 'menu_item_hardware') {
			jQuery.get(base_url+'static/hardware.html', updateContent);
		} else if (this.id == 'menu_item_develop') {
			jQuery.get(base_url+'static/develop.html', updateContent);
		} else if (this.id == 'menu_item_bluetooth') {
			jQuery.get(base_url+'static/bluetooth.html', updateContent);
		} else if (this.id == 'menu_item_buy') {
			jQuery.get(base_url+'buy', updateContent);
		}
	});

	// insert email links
	insertEml();

	// replace images
	$('#dsbluetooth_img').bind('mouseover', function () {
		$(this).attr('src', '/static/dsbluetooth_front.png');
	});
	$('#dsbluetooth_img').bind('mouseout', function () {
		$(this).attr('src', '/static/dsbluetooth_back.png')
	});
});