jQuery(function($) {

total_test();

//--------------------------------
//	Initialize link listings
//--------------------------------

//	Bind show/hide
$("div#link2listings_results_div table tr td small a").bind("click", show_hide);

//	Bind add listing
$("div#link2listings_results_div table tr td a.price").bind("click", add_listing);

//	Bind remove listing
$("div#link2listings_selections_div table tr td a[@class='minus']").bind("click", remove_listing);

//	Bind add notification
$("div#link2listings_results_div table tr td a.notify").bind("click", add_notification);

//	Bind remove notification
$("div#link2listings_selections_div table tr td a[@class='remove_notification']").bind("click", remove_notification);

//	Bind premium listing
$("div#premium_listing a#purchase_premium_listing").bind("click", premium_listing);

//--------------------------------
//	Initialize featured listings
//--------------------------------

//	Bind add listing
$("div#featured_listings_results_div table tr td a.price").bind("click", add_featured_listing);

//	Bind remove listing
$("div#featured_listings_selections_div table tr td a[@class='minus']").bind("click", remove_featured_listing);

//	Bind add notification
$("div#featured_listings_results_div table tr td a.notify").bind("click", add_featured_notification);

//	Bind remove notification
$("div#featured_listings_selections_div table tr td a[@class='remove_notification']").bind("click", remove_featured_notification);

//--------------------------------
//	Select all
//--------------------------------

$("div.select_all input[@type='checkbox']").click( function(event) {
	var status	= ( $(this).attr("checked") == true ) ? "checked": "";
	$("input[@type='checkbox']").attr("checked", status);
});

//--------------------------------
//	Listings
//--------------------------------

//	Handle listing state selection change event
$("div#listings select[@name='state']").change( function(event) {
	if ( $(this).val() != "" ) {
		var ths	= $(this);
		ths.parent("span").addClass("loading");
		$("div#link2listings_results_div").empty();
		$("div#link2listings_results_div").load( "/realtor-enroll/ajax_link2listings", {state: $(this).val()}, function(load) {
			ths.parent("span").removeClass("loading");
			$("div#link2listings_results_div table tr td a.price").bind("click", add_listing);
			$("div#link2listings_results_div table tr td a.notify").bind("click", add_notification);
			$("div#link2listings_results_div table tr td small a").bind("click", show_hide);
		});
	}
});

//	Handle featured listing state selection change event
$("div#featured_listings select[@name='state']").change( function(event) {
	if ( $(this).val() != "" ) {
		var ths	= $(this);
		ths.parent("span").addClass("loading");
		$("div#featured_listings_results_div").empty();
		$("div#featured_listings_results_div").load( "/realtor-enroll/ajax_featured_listings", {state: $(this).val()}, function(load) {
			ths.parent("span").removeClass("loading");
			$("div#featured_listings_results_div table tr td a.price").bind("click", add_featured_listing);
			$("div#featured_listings_results_div table tr td a.notify").bind("click", add_featured_notification);
			$("div#featured_listings_results_div table tr td small a").bind("click", show_hide);
		});
	}
});

//	Handle zebra table rows
function switch_rows( which )
{
	var swtch = "two";
	
	$("table#" + which + " tr").each(function(i) {
		if ( swtch == "two" ) { swtch = "one"; } else { swtch = "two"; }
		$(this).children("td").attr("class", swtch);
	});
}

// Handle showing/hiding extra info
function show_hide(event) {
	var id	= strip_location( $(this).attr("href") );
	$(this).parent("small").children("a").toggle();
	$("div#link2listings_results_div table tr." + id).toggle();
	return false;
}

//--------------------------------
// MONTHLY LISTINGS
//--------------------------------

//	Handle adding listings
function add_listing(event) {
	var link		= $(this);
	var item_id		= strip_location( $(this).attr("href") );
	var qty			= $(this).attr("rel");
	var item_cost	= $(this).attr("title");
	
	$.post( "/realtor-enroll/ajax_cart_add/" + item_id, {qty: qty, item_cost: item_cost}, function (data) {
		if ( data.substr(0,1) == "!" )
		{
			alert(data.substr(1));
		}
		else
		{
			var arr		= data.split("|",1);
			var tr		= link.parent("td").parent("tr");
			tr.children("td").children("a.price").hide();
			tr.children("td").children("span").show();
			
			var tr	= '<tr><td class="one">' + link.attr("alt") +'</td><td class="one">' + link.attr("rel") +' months</td><td class="one">' + link.html() +'</td><td class="one"><small><a href="' + strip_location( arr[0] ) + '" rel="' + item_id + '" class="minus">remove</a></small></td></tr>';
			
			$("div#link2listings_selections_div table").append(tr);
			
			switch_rows("link2listings_selections");
			
			$("div#link2listings_selections_div table tr td a[@class='minus']").unbind("click", remove_listing).bind("click", remove_listing);
			
			total_test();
		}
	});

	return false;
}

//	Handle removing listings
function remove_listing(event) {
	var link		= $(this);
	var purchase_id	= strip_location( $(this).attr("href") );
	var item_id		= $(this).attr("rel");
	
	$.post( "/realtor-enroll/ajax_cart_delete/" + purchase_id, {}, function (data) {
		if ( data.substr(0,1) == "!" )
		{
			alert(data.substr(1));
		}
		else
		{
			link.parents("tr").remove();
			switch_rows("link2listings_selections");
			
			if ( $("tr#listing" + item_id) )
			{
				$("tr#listing" + item_id + " td a[@class='price']").show();
				$("tr#listing" + item_id + " td span").hide();
			}
			
			total_test();
		}
	});
	
	return false;
}

//	Handle adding featured listings
function add_featured_listing(event) {
	var link		= $(this);
	var item_id		= strip_location( $(this).attr("href") );
	var qty			= $(this).attr("rel");
	var item_cost	= $(this).attr("title");
	
	$.post( "/realtor-enroll/ajax_cart_add/" + item_id, {qty: qty, item_cost: item_cost}, function (data) {
		if ( data.substr(0,1) == "!" )
		{
			alert(data.substr(1));
		}
		else
		{
			var arr		= data.split("|",1);
			var tr		= link.parent("td").parent("tr");
			tr.children("td").children("a.price").hide();
			tr.children("td").children("span").show();
			
			var tr	= '<tr><td class="one">' + link.attr("alt") +'</td><td class="one">' + link.attr("rel") +' month</td><td class="one">' + link.html() +'</td><td class="one"><small><a href="' + strip_location( arr[0] ) + '" rel="' + item_id + '" class="minus">remove</a></small></td></tr>';
			
			$("div#featured_listings_selections_div table").append(tr);
			
			switch_rows("featured_listings_selections");
			
			$("div#featured_listings_selections_div table tr td a[@class='minus']").unbind("click", remove_featured_listing).bind("click", remove_featured_listing);
			
			total_test();
		}
	});

	return false;
}

//	Handle removing featured listings
function remove_featured_listing(event) {
	var link		= $(this);
	var purchase_id	= strip_location( $(this).attr("href") );
	var item_id		= $(this).attr("rel");
	
	$.post( "/realtor-enroll/ajax_cart_delete/" + purchase_id, {}, function (data) {
		if ( data.substr(0,1) == "!" )
		{
			alert(data.substr(1));
		}
		else
		{
			link.parents("tr").remove();
			switch_rows("featured_listings_selections");
			
			if ( $("tr#featured_listing" + item_id) )
			{
				$("tr#featured_listing" + item_id + " td a[@class='price']").show();
				$("tr#featured_listing" + item_id + " td span").hide();
			}
			
			total_test();
		}
	});
	
	return false;
}



//--------------------------------
// NOTIFICATIONS
//--------------------------------

//	Handle adding notifications
function add_notification(event) {
	var link		= $(this);
	var entry_id	= strip_location( $(this).attr("href") );
	
	$.post( "/realtor-enroll/ajax_notification_add/" + entry_id, {days: 365}, function (data) {
		if ( data.substr(0,1) == "!" )
		{
			alert(data.substr(1));
		}
		else
		{
			link.parent("td").addClass("unavailable");
			var tr		= link.parent("td").parent("tr");
			tr.children("td").children("a.notify").hide();
			tr.children("td").children("span").show();
			
			var tr	= '<tr><td class="one">Notify Me: ' + link.attr("alt") +'</td><td class="one"></td><td class="one">Free!</td><td class="one"><small><a href="' + entry_id + '" class="remove_notification">remove</a></small></td></tr>';
			
			$("div#link2listings_selections_div table").append(tr);
			
			switch_rows("link2listings_selections");
			
			// unbind_notification_remove();
			$("div#link2listings_selections_div table tr td a[@class='remove_notification']").unbind("click", remove_notification).bind("click", remove_notification);
		}
	});
	
	return false;
}

//	Handle removing notifications
function remove_notification(event) {
	var link		= $(this);
	var entry_id	= strip_location( $(this).attr("href") );
	
	$.post( "/realtor-enroll/ajax_notification_delete/" + entry_id, {}, function (data) {
		if ( data.substr(0,1) == "!" )
		{
			alert(data.substr(1));
		}
		else
		{
			link.parents("tr").remove();
			switch_rows("link2listings_selections");
			
			if ( $("tr#listing0" + entry_id) )
			{
				$("tr#listing0" + entry_id + " td a[@class='notify']").show();
				$("tr#listing0" + entry_id + " td span").hide();
				$("tr#listing0" + entry_id + " td").removeClass("unavailable");
			}
		}
	});

	return false;
}

//	Handle adding featured notifications
function add_featured_notification(event) {
	var link		= $(this);
	var entry_id	= strip_location( $(this).attr("href") );
	
	$.post( "/realtor-enroll/ajax_notification_add/" + entry_id, {days: 365}, function (data) {
		if ( data.substr(0,1) == "!" )
		{
			alert(data.substr(1));
		}
		else
		{
			link.parent("td").addClass("unavailable");
			var tr		= link.parent("td").parent("tr");
			tr.children("td").children("a.notify").hide();
			tr.children("td").children("span").show();
			
			var tr	= '<tr><td class="one">Notify Me: ' + link.attr("alt") +'</td><td class="one"></td><td class="one">Free!</td><td class="one"><small><a href="' + entry_id + '" class="remove_notification">remove</a></small></td></tr>';
			
			$("div#featured_listings_selections_div table").append(tr);
			
			switch_rows("featured_listings_selections");
			
			// unbind_notification_remove();
			$("div#featured_listings_selections_div table tr td a[@class='remove_notification']").unbind("click", remove_featured_notification).bind("click", remove_featured_notification);
		}
	});
	
	return false;
}

//	Handle removing notifications
function remove_featured_notification(event) {
	var link		= $(this);
	var entry_id	= strip_location( $(this).attr("href") );
	
	$.post( "/realtor-enroll/ajax_notification_delete/" + entry_id, {}, function (data) {
		if ( data.substr(0,1) == "!" )
		{
			alert(data.substr(1));
		}
		else
		{
			link.parents("tr").remove();
			switch_rows("featured_listings_selections");
			
			if ( $("tr#featured_listing0" + entry_id) )
			{
				$("tr#featured_listing0" + entry_id + " td a[@class='notify']").show();
				$("tr#featured_listing0" + entry_id + " td span").hide();
				$("tr#featured_listing0" + entry_id + " td").removeClass("unavailable");
			}
		}
	});

	return false;
}

//--------------------------------
//	Premium listing
//--------------------------------

function premium_listing(event)
{
	if ( $(this).attr("rel") == "add" )
	{
		$.post( "/realtor-enroll/ajax_cart_add/" + strip_location( $(this).attr("href") ), {qty:12, item_cost:10}, function(data) {
			if ( data.substr(0,1) == "!" )
			{
				alert(data.substr(1));
			}
			else
			{
				$("div#premium_listing").load( "/realtor-enroll/ajax_premium_listing", {}, function() {
					$("div#premium_listing a#purchase_premium_listing").unbind("click", premium_listing).bind("click", premium_listing);
			
					total_test();
				});
			}
		});
	}
	else
	{
		$.post( "/realtor-enroll/ajax_cart_delete/" + strip_location( $(this).attr("href") ), {}, function(data) {
			if ( data.substr(0,1) == "!" )
			{
				alert(data.substr(1));
			}
			else
			{
				$("div#premium_listing").load( "/realtor-enroll/ajax_premium_listing", {}, function() {
					$("div#premium_listing a#purchase_premium_listing").unbind("click", premium_listing).bind("click", premium_listing);
			
					total_test();
				});
			}
		});
	}
	
	return false;
}

function total_test()
{
	var total	= 0;
	
	var premium	= strip_location( $("a#purchase_premium_listing[@rel='remove']").attr("href") );
	
	if ( premium > 0 )
	{
		total += 1;
	}
	
	$("div#link2listings_selections_div table tr td a[@class='minus']").each( function(i,n) {
		total	+= strip_location( $(n).attr("href") ) * 1;
	});
	
	$("div#featured_listings_selections_div table tr td a[@class='minus']").each( function(i,n) {
		total	+= strip_location( $(n).attr("href") ) * 1;
	});
	
	total	= total.toFixed(0);
	
	if ( total > 0 )
	{
		$("a#total_test_finish").hide();
		$("a#total_test_checkout").show();
	}
	else
	{
		$("a#total_test_finish").show();
		$("a#total_test_checkout").hide();
	}
}

function strip_location(href)
{
	if ( href == undefined ) return "";
	var location	= window.location;
	var re			= new RegExp( String( location ), 'gi');
	href			= href.replace( re, "" );
	return href;
}

});