$(document).ready(function(){

	$('#add_comment textarea').focus(function(){
		if(this.value == 'Type your comment here...'){
			this.value = '';
		}
	}).blur(function(){
		if(this.value == ''){
			this.value = 'Type your comment here...';
		}
	});

	$('#make_id').change(function(){

		$.ajax({
			method: 'GET',
			url: ROOT +'/ajax/vehicle-model-list',
			data: 'make_id='+ this.value,
			success: function(data){
				var result_arr = unserialize(data);
				var result_list = $('#model_id').get(0);
				result_list.length = 0;

				for(var i in result_arr){
					var result = result_arr[i];
					var result_item = document.createElement('option');
					result_item.value = result.id;
					$(result_item).text(result.label);

					result_list.appendChild(result_item);
				}
			}
		});

	});

	$('#album_id').change(function(){

		var result_list = $('#media_list').get(0);

		if(this.value){
			result_list.innerHTML = 'Loading, please wait...';

			$.ajax({
				method: 'GET',
				url: ROOT +'/ajax/media-album-list',
				data: 'album_id='+ this.value,
				success: function(data){
					var result_arr = unserialize(data);
					result_list.innerHTML = 'Choose a thumbnail from below:<br />';

					for(var i in result_arr){
						var result = result_arr[i];
						var result_item = document.createElement('div');
						result_item.style.display = 'none';
						result_item.innerHTML = '<img src="'+ result.thumbnail_url +'" class="medium_border_white" /><input type="radio" name="main_photo_id" value="'+ result.id +'" />';

						result_list.appendChild(result_item);
						$(result_item).pause(10 * (i + 1)).fadeIn(100);
					}
				}
			});
		}else{
			result_list.innerHTML = '';
		}

	});

	$('.page_btn').click(function(){

		//de-select the current tab
		$('.page_btn').parent('li').removeClass('current');

		//select this tab
		$(this).parent('li').addClass('current');

		//hide all the content areas
		$('.content').hide();

		//show this content area
		$('#'+ this.id +'_content').show();

		//return false so the browser doesn't refresh
		return false;
	});

	$('form[name="add_comment"] textarea').focus(function(){
		if(this.value == 'Type your comment here...'){
			this.value = '';
		}
	});

	$('form[name="add_comment"] textarea').blur(function(){
		if(this.value == ''){
			this.value = 'Type your comment here...';
		}
	});

	$('select#country_select').change(function(){
		if(this.value){
			$('#address_global').show();

			if(this.value == 'US'){
				$('#address_us').show();
				$('#address_other').hide();
			}else if(this.value){
				$('#address_us').hide();
				$('#address_other').show();
			}
		}else{
			$('#address_global').hide();
			$('#address_us').hide();
			$('#address_other').hide();
		}
	});
	$('select#country_select').change();

	$('input[name="main_photo_id"]').live('click', function(){
		if(this.checked){
			$('input[name*="use_gravatar"]').removeAttr('checked');
		}
	});

	$('input[name*="use_gravatar"]').live('click', function(){
		if(this.checked){
			$('input[type="radio"][name="main_photo_id"]').removeAttr('checked');
		}
	});

	$('#product_selector').change(function(){
		if(this.value){
			var option = this.options[this.selectedIndex];
			var label = option.text;
			var id = this.value;

			this.selectedIndex = 0;
			$(option).hide();

			addProduct(label, id);
		}
	});

	$('.remove_product').live('click', function(){
		product_count--;

		var id = this.id;

		$('#'+ id +'_option').show();
		$('#'+ id +'_data').remove();

		$('#product_selector').get(0).selectedIndex = 0;

		if(!product_count){
			$('#product_list_msg').show();
		}
	});

	$('.popup').click(function(){
		GB_showCenter(this.title, this.href, 500, 500);
		return false;
	});

	$('a.flag').click(function(){
		if(confirm("Are you sure you want to flag this item?")){
			$.ajax({
				method: 'GET',
				url: this.href,
				data: 'ajax=1',
				success: function(status){
					if(status == 'success'){
						alert('Thank you for helping us keep this site clean and appropriate!');
					}else{
						alert('There was an error processing your request.');
					}
				}
			});
		}

		return false;
	});

	$('a.delete_comment').click(function(){
		if(confirm("Are you sure you want to permanently delete this comment?")){
			var obj = $(this);

			$.ajax({
				method: 'GET',
				url: this.href,
				data: 'ajax=1',
				success: function(status){
					if(status == 'success'){
						var comment_id = obj.attr('id').replace('comment_delete_', '');
						$('#comment_'+ comment_id).slideUp('fast');
					}else{
						alert('There was an error processing your request.');
					}
				}
			});
		}

		return false;
	});

});

var product_count = 0;
function addProduct(label, id){
	product_count++;

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

	if(!(obj = $('#product_'+ id +'_data').get(0))){
		var id_value = 'product_'+ id;

		var product_data = document.createElement('div');
		product_data.id = id_value +'_data';

		var input = document.createElement('input');
		input.type = 'hidden';
		input.name = 'User_Vehicle_gearlist_arr[]';
		input.value = id;

		var remove_btn = document.createElement('a');
		remove_btn.href = 'javascript: void(0);';
		remove_btn.id = id_value;
		remove_btn.className = 'remove_product';
		remove_btn.title = 'Remove this product';
		remove_btn.appendChild(document.createTextNode('Delete'));

		var product = document.createTextNode(' '+ label);

		product_data.appendChild(input);
		product_data.appendChild(remove_btn);
		product_data.appendChild(product);

		$(product_data).appendTo($('#product_list'));
	}else{
		$(obj).show();
	}
}