$(function() {
	/*
	//hide flash messages
	$('#flash-success').delay(3000).fadeTo('fast', 0.01, function() {
		$(this).slideUp('fast', function() {
			$(this).remove();	
		})
	});
	*/
	
	//get selected city and remove city options
	var selectedCityId = $('#ProfileCityId').val();
	$('#ProfileCityId option').remove();
	
	//get city options by state
	var getCityOptionsByState = function() {
		if($('#ProfileCityId option').length == 0) {
			$('#ProfileCityId').append('<option value="">Sorry, no city directories available for this state.</option>');
			$('#ProfileCityId').attr('disabled', true);	
		} else {
			$('#ProfileCityId').val(selectedCityId);
			$('#ProfileCityId').prepend('<option value="">Select...</option>');
			$('#ProfileCityId').attr('disabled', false);
		}
	}	

	//set city options if state id value set
	if($('#ProfileStateId').val() != '') {
		$('#ProfileCityId').load(
			'/profiles/getcityoptionsbystate',
			$('#ProfileStateId').serialize(),
			getCityOptionsByState
		);
		$('option[value=""]', this).remove();
	} else {
		$('#ProfileCityId').attr('disabled', true);
	}

	//refresh city options if state id value changes
	$('#ProfileStateId').change(function(event) {
		$('#ProfileCityId').load(
			'/profiles/getcityoptionsbystate',
			$('#ProfileStateId').serialize(),
			getCityOptionsByState
		);
		$('option[value=""]', this).remove();	
	});
	
	//portfolio piece colorbox
	$('.portfolio-piece .pp-images a.ppImage').colorbox();
	$('.lpp-images a.lppImage').colorbox();

	//crop
	if($('#cropThumbnailsContainer').hasClass('cropThumbnail')) {
		$('.pb-logo-thumbnail-content img').addClass('cropPreview');
	}
	
	$('#cropImage').Jcrop(
		{
			allowSelect: false,
			aspectRatio: 1,
			minSize: [$('#minSizeWidth').val(), $('#minSizeHeight').val()],
			setSelect: [$('#defaultX1').val(), $('#defaultY1').val(), $('#defaultX2').val(), $('#defaultY2').val()],
			bgColor: 'black',
			bgOpacity: .5,
			onSelect: updateCoords,
			onChange: updateCoords
		}
	);

	function updateCoords(c) {
		$('#x1').val(c.x);
		$('#y1').val(c.y);
		$('#width').val(c.w);
		$('#height').val(c.h);		

		var rx = $('#cropWidth').val() / c.w;
		var ry = $('#cropHeight').val() / c.h;
		
		$('.cropPreview').attr('src', $('#cropImage').attr('src'));

		$('.cropPreview').css({
			width: Math.round(rx * $('#cropImage').width()) + 'px',
			height: Math.round(ry * $('#cropImage').height()) + 'px',
			marginLeft: '-' + Math.round(rx * c.x) + 'px',
			marginTop: '-' + Math.round(ry * c.y) + 'px'
		});
	}

	//purchases
	$('.defaultSubmits').hide();
	$('.javaScriptSubmits').show();
	$('.adjustAdStatusNoJs').hide();
	$('.at-ad-buy').show();

	$('#ad-table input:checkbox').live('click', function(event) {
		var url = '/advertising/adjust-ad-status/' + $(this).val();
		url = url.replace(/\\/g, '/');

		$.ajax({
			async: false,
			cache: false,
			dataType: 'json',
			error: function(xhr, textStatus, errorThrown) {
				alert('Cart Error');
			},
			url: url,
			success: function(data, textStatus, xhr) {
				changeAdStatus(data.adStatus, data.adTdId);
				reloadCart();
			},
			type: 'get'
		});
	});
	
	$('#shoppingCartSmall a.adjustAdStatus').live('click', function(event) {
		event.preventDefault();
		
		var url = $(this).attr('href');

		$.ajax({
			async: false,
			cache: false,
			dataType: 'json',
			error: function(xhr, textStatus, errorThrown) {
				window.location.replace('/account/login');
			},
			url: url,
			success: function(data, textStatus, xhr) {
				changeAdStatus(data.adStatus, data.adTdId);
				reloadCart();
			},
			type: 'get'
		});
	});	
	
	$('#cart-table a.adjustAdStatus').live('click', function(event) {
		event.preventDefault();
		
		var url = $(this).attr('href');
		var trId = $(this).closest('tr').attr('id');

		$.ajax({
			async: false,
			cache: false,
			complete: function(xhr, textStatus) {
				if($('#cart-table tr').length === 1) {
					window.location.replace('/advertising');
				}
			},
			dataType: 'json',
			error: function(xhr, textStatus, errorThrown) {
				window.location.replace('/account/login');
			},
			url: url,
			success: function(data, textStatus, xhr) {
				if(data.adStatus === 'available') {
					$('tr#' + trId).remove();
					reloadCartHideItems();
				}
			},
			type: 'get'
		});
	});
	
	function changeAdStatus(adStatus, adTdId) {
		switch(adStatus) {
			case 'available':
				$('#' + adTdId).removeClass('in-cart bought-it sold-out ad-excluded').addClass('available');
				$('#' + adTdId + ' .at-ad-text').html('Open');
				$('#' + adTdId + ' .at-ad-buy-holder').show();
				$('#' + adTdId + ' .at-ad-buy').show();
				$('#' + adTdId + ' input').attr('checked', false);
				break;		
			case 'incart':
				$('#' + adTdId).removeClass('available bought-it sold-out ad-excluded').addClass('in-cart');
				$('#' + adTdId + ' .at-ad-text').html('Cart');
				$('#' + adTdId + ' .at-ad-buy-holder').show();
				$('#' + adTdId + ' .at-ad-buy').show();
				$('#' + adTdId + ' input').attr('checked', true);
				break;
			case 'boughtit':
				$('#' + adTdId).removeClass('available in-cart sold-out ad-excluded').addClass('bought-it');
				$('#' + adTdId + ' .at-ad-amount').hide();
				$('#' + adTdId + ' .at-ad-buy-holder').hide();
				$('#' + adTdId + ' .at-ad-text').html('Bought');
				$('#' + adTdId + ' input').attr('checked', false);
				break;
			case 'soldout':
				$('#' + adTdId).removeClass('available in-cart bought-it ad-excluded').addClass('sold-out');
				$('#' + adTdId + ' .at-ad-amount').hide();
				$('#' + adTdId + ' .at-ad-buy-holder').hide();
				$('#' + adTdId + ' .at-ad-text').html('Sold<br/>Out');
				$('#' + adTdId + ' input').attr('checked', false);
				break;
			case 'excluded':
				$('#' + adTdId).removeClass('available in-cart bought-it sold-out').addClass('ad-excluded');
				$('#' + adTdId + ' .at-ad-amount').hide();
				$('#' + adTdId + ' .at-ad-buy-holder').hide();
				$('#' + adTdId + ' .at-ad-text').html('N/A*');
				$('#' + adTdId + ' input').attr('checked', false);
				break;					
			case 'loginredirect':
				window.location.replace('/account/login');
				break;
		}
	}
	
	function reloadCart() {
		$('#shoppingCartSmall').load('/advertising/shopping-cart-small #shoppingCartSmallContent');	
	}
	
	function reloadCartHideItems() {
		$('#shoppingCartSmall').load('/advertising/shopping-cart-small/1 #shoppingCartSmallContent');
	}		
});
