var total_segments	= $.url.segment();

$(document).ready(function () {
	var base_url;

	$(window).bind('resize', function() {
		//if (resizeTimer) clearTimeout(resizeTimer);
		//resizeTimer = setTimeout(initResize, 100);
		
		initResize();
	});
	
	resizeTimer = setTimeout(initResize, 100);

	for (i = 0; i <= total_segments; i++) {
		if ($.url.segment(i) == 'index.php') break;
	}

	if ($.url.segment(i - 1) != null)
		base_url	= $.url.attr('protocol') + '://' + $.url.attr('host') + '/' + $.url.segment(i - 1) + '/';
	else
		base_url	= $.url.attr('protocol') + '://' + $.url.attr('host') + '/';

	if (typeof $('a[rel*=facebox]').facebox() == 'function') {
		$('a[rel*=facebox]').facebox({
			loadingImage : base_url + 'js/facebox/loading.gif',
			closeImage   : base_url + 'js/facebox/closelabel.gif'
		});
	}

	initResize();

	refreshSCart();

	if (typeof $('.datepicker').datePicker == 'function') {
		$('#register_date_of_birth').attr('readonly', 'readonly');
		//$('.datepicker').datePicker();
	}

	$('#copy_billing').click(function () {
		$('#shipping_first_name').val($('#billing_first_name').val());
		$('#shipping_last_name').val($('#billing_last_name').val());
		$('#shipping_country').val($('#billing_country').val());
		$('#shipping_state').val($('#billing_state').val());
		$('#shipping_city').val($('#billing_city').val());
		$('#shipping_address').val($('#billing_address').val());
		$('#shipping_postal').val($('#billing_postal').val());
		$('#shipping_phone').val($('#billing_phone').val());
		$('#shipping_mobile').val($('#billing_mobile').val());
	});

	$('#submit_all').click(function () {
		/*
		$.ajax({
			type		: 'POST',
			url			: $('#form_shipping_details').attr('action'),
			data		: 'shipping_first_name=' + $('#shipping_first_name').val() + '&' +
						  'shipping_last_name=' + $('#shipping_last_name').val() + '&' +
						  'shipping_country=' + $('#shipping_country').val() + '&' +
						  'shipping_state=' + $('#shipping_state').val() + '&' +
						  'shipping_city=' + $('#shipping_city').val() + '&' +
						  'shipping_address=' + $('#shipping_address').val() + '&' +
						  'shipping_postal=' + $('#shipping_postal').val() + '&' +
						  'shipping_phone=' + $('#shipping_phone').val() + '&' +
						  'shipping_mobile=' + $('#shipping_mobile').val() + '&' +
						  'shipping_note=' + $('#shipping_note').val() + '&' +
						  'save_shipping=' + $('#save_shipping').val()
		});

		$.ajax({
			type		: 'POST',
			url			: $('#form_billing_details').attr('action'),
			data		: 'billing_first_name=' + $('#billing_first_name').val() + '&' +
						  'billing_last_name=' + $('#billing_last_name').val() + '&' +
						  'billing_country=' + $('#billing_country').val() + '&' +
						  'billing_state=' + $('#billing_state').val() + '&' +
						  'billing_city=' + $('#billing_city').val() + '&' +
						  'billing_address=' + $('#billing_address').val() + '&' +
						  'billing_postal=' + $('#billing_postal').val() + '&' +
						  'billing_phone=' + $('#billing_phone').val() + '&' +
						  'billing_mobile=' + $('#billing_mobile').val() + '&' +
						  'save_billing=' + $('#save_billing').val()
		});

		$.ajax({
			type		: 'POST',
			url			: $('#form_billing_details').attr('action'),
			data		: 'shipping_area=' + $('#shipping_area').val() + '&' +
						  'save_shipping_fee=' + $('#save_shipping_fee').val()
		});

		location.href	= $(this).parent().attr('action');
		*/
		if ($('#conf_shipping_id').val() == '0') {
			alert('You haven\'t save the delivery details');
			return false;
		}
	});

	$('#shipping_area_id').change(function () {
		for (i = 0; i <= total_segments; i++) {
			if ($.url.segment(i) == 'index.php') break;
		}

		$.get(base_url + $.url.segment(i) + '/checkout/get_shipping_price/' + $(this).val(), function (data) {
			$('#shippping_price').html(data);
		});
	});

	$('#register').click(function () {
		$.ajax({
			type		: 'POST',
			url			: $('#form_billing_details').attr('action'),
			data		: 'billing_first_name=' + $('#billing_first_name').val() + '&' +
						  'billing_last_name=' + $('#billing_last_name').val() + '&' +
						  'billing_country=' + $('#billing_country').val() + '&' +
						  'billing_state=' + $('#billing_state').val() + '&' +
						  'billing_city=' + $('#billing_city').val() + '&' +
						  'billing_address=' + $('#billing_address').val() + '&' +
						  'billing_postal=' + $('#billing_postal').val() + '&' +
						  'billing_phone=' + $('#billing_phone').val() + '&' +
						  'billing_mobile=' + $('#billing_mobile').val() + '&' +
						  'save_billing=' + $('#save_billing').val()
		});
	});

	$('#page').change(function () {
		var parent_form	= $(this).parent();

		parent_form.attr('action', parent_form.attr('action') + '/page/' + $(this).val());
		parent_form.submit();
	});

	$('#register_date_of_birth').DatePicker({
		format:'d/m/Y',
		date: $('#register_date_of_birth').val(),
		current: $('#register_date_of_birth').val(),
		starts: 1,
		position: 'right',
		onBeforeShow: function(){
			$('#register_date_of_birth').DatePickerSetDate($('#register_date_of_birth').val(), true);
		},
		onChange: function(formated, dates){
			$('#register_date_of_birth').val(formated);
		}
	});
});

function initResize () {
	if ($(this).height() < $('#outer_container').height()) {
		$('#outer_container').css('position', 'relative');
	} else {
		$('#outer_container').css('position', 'fixed');
	}
}

function initFBScript () {
	$('#add_to_cart').click(function () {
		$(this).parent().parent().submit();
		$.facebox.close();
		refreshSCart();
		return false;
	});

	$('#product_detail_container').submit(function () {
		$.ajax({
			type		: 'POST',
			url			: $(this).attr('action'),
			data		: 'item_size=' + $('#item_size').val(),
			error		: function () {
				alert('Sorry, there is an error while processing your request');
			},
			complete	: function () {
				refreshSCart();
			}
		});
		return false;
	});

	$('#forgot_password').submit(function () {
		$.ajax({
			type		: 'POST',
			url			: $(this).attr('action'),
			data		: 'email=' + $('#email').val(),
			error		: function () {
				alert('Sorry, there is an error while processing your request');
			},
			complete	: function () {
				$.facebox.close();
			}
		});
		return false;
	});
}

function refreshSCart () {
	var base_url;

	for (i = 0; i <= total_segments; i++) {
		if ($.url.segment(i) == 'index.php') break;
	}

	if ($.url.segment(i - 1) != null)
		base_url	= $.url.attr('protocol') + '://' + $.url.attr('host') + '/' + $.url.segment(i - 1) + '/';
	else
		base_url	= $.url.attr('protocol') + '://' + $.url.attr('host') + '/';

	$.get(base_url + $.url.segment(i) + '/shopping_cart/ajax', function (data) {
		$('#s_cart_items').html(data);
	});
}