$(document).ready(function () {
	/** ADMIN JS **/

	$('.menu_children').hide();

	$('.menu_parent').hover(function () {
		$(this).find('.menu_children').show();
	}, function () {
		$(this).find('.menu_children').hide();
	});

	$('.delete').click(function () {
		return confirm('Are you sure want to delete this item?\nYou will lose this item permanently.');
	});

	$('#check_all').click(function () {
		var $checkboxes = $(this).parents('table').find('tbody').find(':checkbox');

		$checkboxes.attr('checked', this.checked);
	});

	$('#select_action').change(
		function () {
			$(this).parents('form').submit();
		}
	);

	/** Product category **/

	$('#change_category_name').toggle(function () {
		$('#category_name').show();
	}, function () {
		$('#category_name').hide();
		$('#product_category_name').val('');
	});

	$('#edit').click(function () {
		var url	= $(this).parent().parent().parent().parent().find('form').attr('action');

		location.href = url;

		return false;
	});

	$('#change_image').toggle(function () {
		$('#upload_primary').show();
	}, function () {
		$('#upload_primary').hide();
	});

	$('#billing_details_button').toggle(function () {
		$('#billing_details').hide();
		$(this).html('Show Billing Details');
	}, function () {
		$('#billing_details').show();
		$(this).html('Hide Billing Details');
	});

	$('#delivery_details_button').toggle(function () {
		$('#delivery_details').hide();
		$(this).html('Show Delivery Details');
	}, function () {
		$('#delivery_details').show();
		$(this).html('Hide Delivery Details');
	});

	$('#product_category_name').focus(function () {
		$('#product_category_label').focus();
	});

	$('#product_category_label').keyup(function () {
		$('#product_category_name').attr('value', $(this).val().toLowerCase().split(' ').join('-').replace(';', '').replace('\'', '').replace('"', '').replace('%', ''));
	});

	$('#product_category_parent_id').change(function () {
		var segments	= $.url.attr('path').split('/');

		segments.pop();

		$.get($.url.attr('protocol') + '://' + $.url.attr('host') + segments.join('/') + '/ajax_order/' + $(this).val(), function (data) {
			$('#product_category_order').val(data);
		});
	});

	$('.datepicker').attr('readonly', 'readonly');

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

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

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

	$('#album_parent_id').change(function () {
		$.get(site_url + '/admin-cp/album/ajax_order/' + $(this).val(), function (data) {
			$('#album_order').val(data);
		});
	});

	$('#album_name').focus(function () {
		$('#album_title').focus();
	});

	$('#album_title').keyup(function () {
		$('#album_name').attr('value', $(this).val().toLowerCase().split(' ').join('-').replace(';', '').replace('\'', '').replace('"', '').replace('%', ''));
	});

	$('#article_name').focus(function () {
		$('#article_subject').focus();
	});

	$('#article_subject').keyup(function () {
		$('#article_name').attr('value', $(this).val().toLowerCase().split(' ').join('-').replace(';', '').replace('\'', '').replace('"', '').replace('%', ''));
	});

	$('#change_email_link').toggle(function () {
		$('#change_email').show();
	}, function () {
		$('#change_email').hide();
	});

	/** END OF ADMIN JS **/

	$('#track_list_sub').hide();

	$('#track_list').click(function  (e) {
		e.preventDefault();

		if ($('#track_list_sub').css('display') == 'block')
			$('#track_list_sub').css('display', 'none');
		else
			$('#track_list_sub').css('display', 'block');
	});

	/*
	$('#track_list').toggle(function () {
		$('#track_list_sub').show();
		alert($('#track_list_sub').css('display'));
	}, function () {
		$('#track_list_sub').hide();
		alert($('#track_list_sub').css('display'));
	});
	*/

	$('#video_category_sub').hide();

	$('#video_category').toggle(function () {
		$('#video_category_sub').show();
	}, function () {
		$('#video_category_sub').hide();
	});

	$('#archive_date_select').toggle(function () {
		$('#archive_date_dropdown').show();
	}, function () {
		$('#archive_date_dropdown').hide();
	})

	$('#archive_date_dropdown').load(base_url + 'index.php/home/archive/', function () { initArchivePageButton(); });

	$('[rel=lightbox]').lightBox();
});

function initArchivePageButton () {
	$('#ajax_archive_page_prev').click(function () {
		$('#archive_date_dropdown').load($(this).attr('rel'), function () { initArchivePageButton(); });

		return false;
	});

	$('#ajax_archive_page_next').click(function () {
		$('#archive_date_dropdown').load($(this).attr('rel'), function () { initArchivePageButton(); });

		return false;
	});

}
