(function($){$(document).ready(function(){

	var loggedin = $.cookie('LOGGEDIN').toLowerCase() == 'true';

	/* Init Shadowbox */
	Shadowbox.init({
		animate:false,
		skipSetup:true
	});

	/* =================================================================
		Popup boxes for listing tools
	================================================================= */
	$('a.pop').shadowbox({
		player:'iframe',
		width:440
	});

	/* Save listing links */
	$('div.tools li.save a').click(function(){
		ajaxResult = { loggedin:'false' };

		$this = $(this);
		var listingID = $this.attr('id').split('_')[1];

		var callbackFunc = function(){
			if (ajaxResult.loggedin.toLowerCase() == 'true') {
				if (ajaxResult.listingstatus.toLowerCase() == 'saved') {
					$this.addClass('on')
						.html('Remove')
						.attr('title', 'Remove this listing from your Search Organizer')
						.attr('href', window.location.pathname + '?a=organizer.listingRemove&ListingID=' + listingID);
				} else {
					$this.removeClass('on')
						.html('Save Listing')
						.attr('title', 'Save this listing to your Search Organizer')
						.attr('href', window.location.pathname + '?a=organizer.listingSave&ListingID=' + listingID);
					$('a#note_' + listingID)
						.removeClass('on')
						.html('Add a Note')
						.attr('title', 'Add a note to this listing');
				}

				/* Update the search organizer quick view */
				$.ajax({
					url:window.location.pathname + '?a=organizer.ajax.quickView',
					type:'post',
					dataType:'html',
					success:function(html){
						$('div#yso_quickView').replaceWith(html);
					}
				});

			} else {
				Shadowbox.open({
					player:'iframe',
					content:$this.attr('href'),
					width:440
				});
			}
		}

		$.ajax({
			url:window.location.pathname,
			type:'post',
			data:{
				a:$this.hasClass('on') ? 'organizer.ajax.listingRemove' : 'organizer.ajax.listingSave',
				ListingID:listingID
			},
			dataType:'script',
			success:callbackFunc,
			error:callbackFunc
		});

		return false;
	});

	/* =================================================================
		Require login for viewing over _x_ pages and listing detail
	================================================================= */
	if (typeof(organizer_limitListingResults) != 'undefined') {
		if (organizer_limitListingResults > 0 && !loggedin) {
			$('div.pageSet a').click(function(){
				$this = $(this);
				var urlParams = parseURL($this.attr('href'));
				organizer_continue_url = $this.attr('href');
				if (urlParams.startrow >= organizer_limitListingResults) {
					Shadowbox.open({
						player:'iframe',
						content:window.location.pathname + '?a=organizer.listingResults',
						width:440
					});
					return false;
				}
			});
		}
	}

	if (typeof(organizer_limitListingDetail) != 'undefined') {
		if (organizer_limitListingDetail > 0 && !loggedin) {
			$('a.viewDetail').click(function(){
				organizer_continue_url = $(this).attr('href');
				Shadowbox.open({
					player:'iframe',
					content:window.location.pathname + '?a=organizer.listingDetail',
					width:440
				});
				return false;
			});
		}
	}

});})(jQuery);
