/*-----------------------------------------*/
//	Include this file into the main index file.
//	This file contains all jquery, javascript, and ajax functions that may be used by any of the included modules.
//	Please be sure to comment each entry with its purpose.
//	Try to use ajax for any data communication as opposed to page-reloading php post and get form submissions.
/*-----------------------------------------*/
$(document).ready(function() {
	

	var test = '1';
		////Jquery effects for dropdown menu
		//////////////////////////////

		jQuery(" #nav ul ").css({display: "none"}); // Opera Fix
		jQuery(" #nav li").hover(function(){
				jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(400);
				},function(){
				jQuery(this).find('ul:first').css({visibility: "hidden"});
				});
				
		jQuery(" #nav2 ul ").css({display: "none"}); // Opera Fix
		jQuery(" #nav2 li").hover(function(){
				jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(100);
				},function(){
				jQuery(this).find('ul:first').css({visibility: "hidden"});
				});		
				
		jQuery("#nav2 img")
			.mouseover(function() { 
				var src = $(this).attr("name");
				$(this).attr("src", src + "_over.png");
		})
			.mouseout(function() {
				var src = $(this).attr("name");
				 $(this).attr("src", src + ".png");
		});
		
		////Change the viewing mode from list to default or vice versa
		////////////////////////////////////
		
		$('#view_mode_default').live('click', function(){
			//get existing url and add view_mode data
			var address = $.address.queryString();
			if(address.search('view_mode') == -1){
				$.address.queryString(address + '&view_mode=default');
			}else{
				$.address.parameter('view_mode', 'default')
			}
			
			reloadResults();
		});
		
		$('#view_mode_list').live('click', function(){
			//get existing url and add view_mode data
			var address = $.address.queryString();
			if(address.search('view_mode') == -1){
				$.address.queryString(address + '&view_mode=list');
			}else{
				$.address.parameter('view_mode', 'list')
			}
			$('#view_mode').html('list');
			reloadResults();
		});
		
		
		
		////Change the order of displayed results
		////////////////////////////////////
		
		$('.change_order').live('click', function(){
			var order_by = $(this).attr('rel');
			var address = $.address.queryString();
			if(address.search('order_by') == -1){
				$.address.queryString(address + '&order_by=' + order_by);
			}else{
				if(order_by == $.address.parameter('order_by')){//the user is trying to reverse the order ie clicking to order by a column that is already being ordered by that column
					if(address.search('direction') == -1){
						$.address.queryString(address + '&direction=ASC');
					}else{
						if($.address.parameter('direction') == "ASC"){
							$.address.parameter('direction', "DESC");
						}else{
							$.address.parameter('direction', "ASC");
						}
					}
				}else{
					$.address.parameter('order_by', order_by);
				}
			}
			
			$('#order_by').html(order_by);
			reloadResults();
		});

		////Ajax for classifieds signup
		//////////////////////////////
		$('#classifieds_signup').click(function() {
			var redirect = unescape($('#redirect').html());
				redirect = redirect.replace("&amp;", "&");
			var human = $('#human').val();
			var sending_data = $('form').serialize();
			var password = $('input[name=password]').val();
			var validation = $('input[name=validation]').val();
			var password2 = $('input[name=password2]').val();
			var stop = false;
			var username_taken = false;
			var ip = $('input[name=ip]').val();
			challengeField = $("input#recaptcha_challenge_field").val();
			responseField = $("input#recaptcha_response_field").val();
					
					//check for required fields
					$('.signup').map(function() {
						var rel = $(this).attr('rel');
						var val = $(this).val();
						if(rel == "req"){
							if(val == ""){
								stop = true;
								//alert('All of the required fields have not been filled in, please try again');
								
							}
						}
					});
					
					//check for human!
					var username = $('input[name=username]').val();
					
					$.ajax({
						url: "include/classified_functions.php",
						type: "POST",
						data: "check_username=" + username, 
						success: function(data){
									
														if(data == 'true'){
															username_taken = true;
															$.jnotify('The username ' + username + ' is already taken, please try another one');
															$('input[name=username]').css('border', '1px solid red');
															
														}else{
															
														
					
															if (password == password2 && stop != true){
																$.ajax({
																	url: "include/classified_functions.php",
																	type: "POST",
																	data: "write_database=true&table_name=cust_info&" + sending_data + "&recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField, 
																	success: function(data){
																								if(data != 'fail'){
																									
																									if(redirect == '' || redirect === 'undefined' || redirect == "null"){
																										$('#output').html('You have completed the signup process for highcubevan.com classifieds. We will contact you in the near future when the page construction is complete and you will be able to submit classified listings of your own. <a href="index.php?page=classifieds">Click Here to go back.</a> ');
																										//$('#output').html('Your submission is complete. You will recieve an email shortly. You can now submit a listing to the highcubevan classifieds by logging in with your username and password <a href="index.php?page=classifieds">Click Here to go back.</a> ');
																										$('#results_title').text('Signup Complete');
																										$.jnotify('Signup Complete.');
																									}else{
																										$('#output').html('Your submission is complete. You will now be redirected to the page you were previously on.');
																										$('#results_title').text('Signup Complete. You will now be redirected to the page you were previously on.');
																										$.jnotify('Signup Complete.');
																										setTimeout("window.location='" + redirect + "'",2000);
																									}
																								}else{
																									$.jnotify('The recaptcha was invalid');
																								}

																							},
																	error: function(){
																									$.jnotify("Error: ajax request failed.");

																							}
																});
															}else if(stop == true){
																$.jnotify('All of the required fields have not been filled in, please try again');
															}else if(password != password2){
																$.jnotify('The passwords did not match, please fill them out again.');
															}else{
																alert('other error');
															}
				
												}
														

												},
						error: function(){
														$.jnotify("Error: ajax request failed.");

												}
														
					});

		});
		
		
		////Ajax for offer submission
		//////////////////////////////
		$('.submit_offer_final').click(function(event) {
			event.preventDefault();
			var from_account = $('input[name=from_account]').val();
			if(from_account == 'true'){
				var sending_data = $('form[name=logged_in]').serialize();
			}else{
				var sending_data = $('form[name=not_logged_in]').serialize();
			}
			

			var validation = $('input[name=validation]').val();
			var stop = false;
			challengeField = $("input#recaptcha_challenge_field").val();
			responseField = $("input#recaptcha_response_field").val();
					
					if(from_account == 'false'){
						//check for required fields
						$('.signup').map(function() {
							var rel = $(this).attr('rel');
							var val = $(this).val();
							if(rel == "req"){
								if(val == ""){
									stop = true;
									//alert('All of the required fields have not been filled in, please try again');
									
								}
							}
						});
					}
					

						if (stop != true){
							$.ajax({
								url: "include/classified_functions.php",
								type: "POST",
								data: "write_database=true&table_name=offers&" + sending_data + "&recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField, 
								success: function(data){
															if(data != 'fail'){
															
																$.jnotify('Offer submitted');
																$('#offer_form').html('offer submitted successfully');
																setTimeout("window.location='http://www.highcubevan.com/index.php?page=classifieds'",2000);
															}else{
																$.jnotify('The recaptcha was invalid');
															}

														},
								error: function(){
																$.jnotify("Error: ajax request failed.");

														}
							});
						}else if(stop == true){
							$.jnotify('All of the required fields have not been filled in, please try again');
						}
	
		});
		
		////Ajax for accept offer client
		//////////////////////////////
		$('.accept_offer').live('click', function(){
			var offer_number = $(this).attr('rel');
			var validation = $('#validation').html();
			
			$(this).fastConfirm({
					position: "right",
					questionText: "Are you sure you want <br/>to Accept Offer?",
					onProceed: function(trigger) {
						$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "accept_offer=" + offer_number + "&validation=" + validation, 
							success: function(data){
															$.jnotify('Offer Accepted Successfully! Please contact the buyer with the contact information to sell your vehicle.');
															$('#offer_container_' + offer_number).css('background-color', '#dbffd7');
															$('#status_' + offer_number).html('Status: Accepted');

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
					},
					onCancel: function(trigger) {
					}
				});
		
		});
		
		
		////Ajax for counter offer client
		//////////////////////////////
		$('.counter_offer').live('click', function(){
			var offer_number = $(this).attr('rel');
			var validation = $('#validation').html();
			var counter_offer_amount = $('#counter_offer_' + offer_number).val();
			
			$(this).fastConfirm({
					position: "right",
					questionText: "Are you sure you want <br/>to Send Counter Offer?",
					onProceed: function(trigger) {
						$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "counter_offer=" + offer_number + "&validation=" + validation + "&counter_offer_amount=" + counter_offer_amount, 
							success: function(data){
															$.jnotify('Counter offer sent! If the buyer does not contact you in a few business days you should contact them with the supplied contact information and discuss the counter offer further.', 10000);
															$('#offer_container_' + offer_number).css('background-color', '#ffd7d7');
															$('#status_' + offer_number).html('Status: Countered');

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
					},
					onCancel: function(trigger) {
					}
				});
		
		});
		
		$('.counter_offer_from_buyer').live('click', function(){
			var offer_number = $(this).attr('rel');
			var validation = $('#validation').html();
			var counter_offer_amount = $('#counter_offer_' + offer_number).val();
			
			$(this).fastConfirm({
					position: "right",
					questionText: "Are you sure you want <br/>to Send Counter Offer?",
					onProceed: function(trigger) {
						$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "counter_offer=" + offer_number + "&validation=" + validation + "&counter_offer_amount=" + counter_offer_amount + "&from_buyer=true", 
							success: function(data){
															$.jnotify('Counter offer sent! You will be notified if the seller accepts or counters your offer.', 10000);
															$('#offer_container_' + offer_number).css('background-color', '#ffd7d7');
															$('#status_' + offer_number).html('Status: Pending');

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
					},
					onCancel: function(trigger) {
					}
				});
		
		});
		
		
		
		$('#signup_form :input[name=username]').change(function() {
		
			var username = $('input[name=username]').val();
			
			$.ajax({
					url: "include/classified_functions.php",
					type: "POST",
					data: "check_username=" + username, 
					success: function(data){
													if(data == 'true'){
														$.jnotify('The username ' + username + ' is already taken, please try another one');
														$('input[name=username]').css('border', '1px solid red');
													}else{
														$.jnotify('The username ' + username + ' is available.');
														$('input[name=username]').css('border', '1px solid #76ff73');
													}
													

											},
					error: function(){
													$.jnotify("Error: ajax request failed.");

											}
				});
		});


		/*
		//make any field with rel=req red unless it has data in it
		$('.signup').change(function(){
			$('.signup').map(function() {
				var rel = $(this).attr('rel');
				var val = $(this).val();
				if(rel == "req"){
					if(val == ""){
						$(this).css('border', '1px solid #ff8181');
					}else{
						$(this).css('border', '1px solid #ccc');
					}
				}
			});
		});
		*/
		
		
		
		////Ajax for vehicle submission
		//////////////////////////////
		$('#submit_new').click(function() {
			var sending_data = $('form').serialize();
			var cust_number = $('#cust_number').val();
			var disabled = $('#submit_new').attr('rel');
			var stop = false;

					
					//check for required fields
					$('.signup').map(function() {
						var rel = $(this).attr('rel');
						var val = $(this).val();
						if(rel == "req"){
							if(val == ""){
								stop = true;
								//alert('All of the required fields have not been filled in, please try again');
								
							}
						}
					});
				

			if (stop != true && disabled != 'true'){
				$.ajax({
					url: "include/classified_functions.php",
					type: "POST",
					data: "write_database=true&number_return=true&table_name=classifieds&" + sending_data, 
					success: function(data){

													//$('#output').html('Your submission is complete. You will be redirected to your personal page in 5 seconds');
													//$('#results_title').text('Submission Complete');
													$.jnotify('Now choose a picture of your vehicle.');
													//setTimeout("window.location='http://www.highcubevan.com/client.php'",5000);
													$('#submit_form').hide(500);
													$('#cust').html(data);
													$('#image_output').delay(500).slideDown(500);
													$('#submit_new').attr('rel', 'true');
													$('input[name=make]').attr('readonly', 'true');
													$('input[name=model]').attr('readonly', 'true');
													$('input[name=year]').attr('readonly', 'true');
													$('input[name=price]').attr('readonly', 'true');
													$('textarea[name=long_detail]').attr('readonly', 'readonly');
													
											},
					error: function(){
													$.jnotify("Error: ajax request failed.");

											}
				});
			}else if(stop == true){
				$.jnotify('All of the required fields have not been filled in, please try again');
			}
		});
		
		
		$('#submit_new_complete').live('click', function(){
			$.jnotify('Your submission is now complete and will appear on the website shortly.');
			$('#output').slideUp('slow');
			setTimeout("window.location='http://www.highcubevan.com/client.php'",5000);
		});
		
		
		//classified listing delete button
		$('.inv_small_container').mouseenter(function(){
			var inv_number = $(this).attr('rel');
			$('#delete_classified_' + inv_number).css('display', 'block');
			$('#view_classified_' + inv_number).css('display', 'block');
			$('#sold_classified_' + inv_number).css('display', 'block');
		}).mouseleave(function(){
			var inv_number = $(this).attr('rel');
			$('#delete_classified_' + inv_number).css('display', 'none');
			$('#view_classified_' + inv_number).css('display', 'none');
			$('#sold_classified_' + inv_number).css('display', 'none');
		});
		
		$('.delete_classified').live('click', function(){
			var validation = $('#validation').html();
			var inv_number = $(this).attr('rel');
			var alt = $(this).attr('alt');
			
			$(this).fastConfirm({
					position: "right",
					questionText: "Are you sure you want <br/>to delete listing?",
					onProceed: function(trigger) {
						$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "delete_classified=" + inv_number + "&validation=" + validation, 
							success: function(data){
															$.jnotify('Listing deleted successfully', 1000);
															$('#inv_small_container_' + inv_number).slideUp('slow');

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
					},
					onCancel: function(trigger) {
					}
				});
		
		});
		
//////////////////////////////
//classified offer delete button
/////////////////////////////
		$('.offer_container').mouseenter(function(){
			var offer_number = $(this).attr('rel');
			$('#delete_offer_' + offer_number).css('display', 'block');
		}).mouseleave(function(){
			var offer_number = $(this).attr('rel');
			$('#delete_offer_' + offer_number).css('display', 'none');
		});
		
		$('.delete_offer').live('click', function(){
			var validation = $('#validation').html();
			var offer_number = $(this).attr('rel');
			var cust_number = $('#cust_number').html();
			
			$(this).fastConfirm({
					position: "right",
					questionText: "Are you sure you want <br/>to delete the offer?",
					onProceed: function(trigger) {
						$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "delete_offer=" + offer_number + "&validation=" + validation + "&cust_number=" + cust_number, 
							success: function(data){
															$.jnotify('Offer deleted successfully', 1000);
															$('#offer_container_' + offer_number).slideUp('slow');

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
					},
					onCancel: function(trigger) {
					}
				});
		
		});

///////////////////////////////////////
//change a classified listing to sold	
//////////////////////////////////	
		$('.sold_classified').live('click', function(){
			var validation = $('#validation').html();
			var inv_number = $(this).attr('rel');
			var cust_number = $('#cust_number').html();
			
			$(this).fastConfirm({
					position: "right",
					questionText: "Are you sure this listing <br/>was sold?",
					onProceed: function(trigger) {
						$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "sold_classified=" + inv_number + "&validation=" + validation, 
							success: function(data){
															$.jnotify('Listing changed to sold');
															var listing_html = $('#inv_small_container_' + inv_number).html();
															$('#inv_small_container_' + inv_number).slideUp('slow');
															
															//update sold listings
															$.ajax({
																url: "include/classified_functions.php",
																type: "POST",
																data: "output_cust_sold_listings=&cust_number=" + cust_number, 
																success: function(data2){
																								
																								$('#current_client_sold_listings').html(data2);

																						},
																error: function(){
																								$.jnotify("Error: ajax request failed.");

																						}
															});
															
															

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
					},
					onCancel: function(trigger) {
					}
				});
		
		});
		
		
		$('#logout').click(function(event){
			event.preventDefault();
			del_cookie('highcubevan_client');
			$('#foreground').slideUp('slow');
			$.jnotify('Logout successfull');
			setTimeout("window.location='http://www.highcubevan.com/index.php?page=classifieds'",1000);
		});
		
		//simpleSlide({'swipe':'true'});
		simpleSlide({
			'set_speed': 300,
			'status_width': 20,
			'status_color_outside': '#aaa',
			'status_color_inside': '#fff',
			'fullscreen': 'false',
			'swipe': 'true',
			'callback': function() {
				/* function code */
			}
		});
		

//INTRO browse and large arrow for main page
///////////
		if($.address.parameter('newest') != 'true' && $.address.parameter('browse') != 'true' && $.address.parameter('search') != 'true'){

			window.setTimeout("showBrowse()", 2000);
		}
		
		window.setInterval("simpleSlideAction('.right-button', 'entrance');", 8000);
		
		$('.slideshow').live('mouseover mouseout', 
		function(event) {
			if(event.type == 'mouseover'){
				$(this).children('.left-button, .right-button').stop(true, true).fadeIn();
			}
			else {
				$(this).children('.left-button, .right-button').stop(true, true).fadeOut();
			}
		}
	);
	
	////////////////////////////////
	//Newest listings output switcher
	///////////////////////////////////
/*	
///////////////////////////////
///////next button feature
/////////////////////////////
	$('#newest_next').live('click', function(){
		var direction = $.address.parameter('direction');
		var order_by = $.address.parameter('order_by');
		var view_mode = $.address.parameter('view_mode');
		var make = $.address.parameter('make');
		var model = $.address.parameter('model');
		var type = $.address.parameter('type');
		var start = $(this).attr('rel');
		var content_type = $('#content_type').text();
			
		

		var next = parseInt(start) + 40;
		if(next <= 0){
			next = 0;
		}
		var page = next / 40;
			page += 1;

		
		var address = $.address.queryString();
			if(address.search('start') == -1){
				$.address.queryString(address + '&start=' + next);
			}else{
				$.address.parameter('start', next)
			}
		
		//show loading spinner animation
		$('#spinner').fadeIn(50);
		//content type of 0 is for displaying newest listings
		//type 1 is for search
		//type 2 is for browse
		if(content_type == "0"){
			$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_all_listings=&start=" + next + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction,  
							success: function(data){
															$('#newest_output').fadeOut(50, function(){
																$('#newest_output').html(data);
																$('#spinner').fadeOut(200);
															}).delay(50).fadeIn(50);
															$('#newest_title').text('Newest listings page ' + page);
															$('#newest_prev').attr('rel', next); 
															$('#newest_next').attr('rel', next); 

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
		}else if(content_type == "1"){
			var sending_data = $('form[name=search_form]').serialize();
			
			$.ajax({
				url: "include/classified_functions.php",
				type: "POST",
				data: "output_search_listings=&start=" + next + "&" + sending_data + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction, 
				success: function(data){
												$('#newest_output').fadeOut(50, function(){
													$('#newest_output').html(data);
													var number_of_results = $('#num_rows_result').val();
													if(view_mode == 'default'){
														var number_of_pages = Math.round((number_of_results / 40) + 0.49);
													}else{
														var number_of_pages = Math.round((number_of_results / 80) + 0.49);
													}
													$('#newest_title').text('Search results page ' + page + ' of ' + number_of_pages + ' (' + number_of_results + ' results)');
													$('#spinner').fadeOut(200);
												}).delay(50).fadeIn(50);
													
												$('#newest_prev').attr('rel', next); 
												$('#newest_next').attr('rel', next); 

										},
				error: function(){
												$.jnotify("Error: ajax request failed.");

										}
			});
		}else if(content_type == "2"){

			
			$.ajax({
				url: "include/classified_functions.php",
				type: "POST",
				data: "output_browse_listings=&start=" + next + "&make=" + make + "&type=" + type + "&order_by=" + order_by + "&model=" + model + "&view_mode="  + view_mode + "&direction=" + direction, 
				success: function(data){
												$('#newest_output').fadeOut(50, function(){
													$('#newest_output').html(data);
													var number_of_results = $('#num_rows_result').val();
													if(view_mode == 'default'){
														var number_of_pages = Math.round((number_of_results / 40) + 0.49);
													}else{
														var number_of_pages = Math.round((number_of_results / 80) + 0.49);
													}
													$('#newest_title').text('Browse results page ' + page + ' of ' + number_of_pages + ' (' + number_of_results + ' results)');
													$('#spinner').fadeOut(200);
												}).delay(50).fadeIn(50);
													
												$('#newest_prev').attr('rel', next); 
												$('#newest_next').attr('rel', next); 

										},
				error: function(){
												$.jnotify("Error: ajax request failed.");

										}
			});
		}
	
	});
	

///////////////////////////////
///////previous button feature
/////////////////////////////
	$('#newest_prev').live('click', function(){
		var direction = $.address.parameter('direction');
		var order_by = $.address.parameter('order_by');
		var view_mode = $.address.parameter('view_mode');
		var make = $.address.parameter('make');
		var model = $.address.parameter('model');
		var type = $.address.parameter('type');
		var start = $(this).attr('rel');
		
		var content_type = $('#content_type').text();
		if(view_mode == 'default'){
			var next = parseInt(start) - 40;
			if(next <= 0){
				next = 0;
			}
			var page = next / 40;
				page += 1;
				
		}else{
			var next = parseInt(start) - 80;
			if(next <= 0){
				next = 0;
			}
			var page = next / 80;
				page += 1;

		}
		
		var address = $.address.queryString();
			if(address.search('start') == -1){
				$.address.queryString(address + '&start=' + next);
			}else{
				$.address.parameter('start', next)
			}
		//show loading spinner animation
		$('#spinner').fadeIn(50);
		
		//content type of 0 is for displaying newest listings
		//type 1 is for search
		//type 2 is for browse
		if(content_type == "0"){
			$.ajax({
				url: "include/classified_functions.php",
				type: "POST",
				data: "output_all_listings=&start=" + next + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction, 
				success: function(data){
												$('#newest_output').fadeOut(50, function(){
													$('#newest_output').html(data);
													$('#spinner').fadeOut(200);
												}).delay(50).fadeIn(50);
													
												
												$('#newest_title').text('Newest listings page ' + page);
												$('#newest_prev').attr('rel', next); 
												$('#newest_next').attr('rel', next); 

										},
				error: function(){
												$.jnotify("Error: ajax request failed.");

										}
			});
		}else if(content_type == "1"){
			var sending_data = $('form[name=search_form]').serialize();
			
			$.ajax({
				url: "include/classified_functions.php",
				type: "POST",
				data: "output_search_listings=&start=" + next + "&" + sending_data + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction, 
				success: function(data){
												$('#newest_output').fadeOut(50, function(){
													$('#newest_output').html(data);
													var number_of_results = $('#num_rows_result').val();
													if(view_mode == 'default'){
														var number_of_pages = Math.round((number_of_results / 40) + 0.49);
													}else{
														var number_of_pages = Math.round((number_of_results / 80) + 0.49);
													}
													$('#newest_title').text('Search results page ' + page + ' of ' + number_of_pages + ' (' + number_of_results + ' results)');
													$.address.parameter('start', next);
													$('#spinner').fadeOut(200);
												}).delay(50).fadeIn(50);
													
												$('#newest_prev').attr('rel', next); 
												$('#newest_next').attr('rel', next); 

										},
				error: function(){
												$.jnotify("Error: ajax request failed.");

										}
			});
		}else if(content_type == "2"){
			
			$.ajax({
				url: "include/classified_functions.php",
				type: "POST",
				data: "output_browse_listings=&start=" + next + "&make=" + make + "&type=" + type + "&model=" + model + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction, 
				success: function(data){
												$('#newest_output').fadeOut(50, function(){
													$('#newest_output').html(data);
													var number_of_results = $('#num_rows_result').val();
													if(view_mode == 'default'){
														var number_of_pages = Math.round((number_of_results / 40) + 0.49);
													}else{
														var number_of_pages = Math.round((number_of_results / 80) + 0.49);
													}
													$('#newest_title').text('Browse results page ' + page + ' of ' + number_of_pages + ' (' + number_of_results + ' results)');
													$.address.parameter('start', next);
													$('#spinner').fadeOut(200);
												}).delay(50).fadeIn(50);
													
												$('#newest_prev').attr('rel', next); 
												$('#newest_next').attr('rel', next); 

										},
				error: function(){
												$.jnotify("Error: ajax request failed.");

										}
			});
		}
	
	});
	
	*/
	
	
///////////////////////////////
///////show more button feature
/////////////////////////////
	$('#show_more_listings').live('click', function(){
		var direction = $.address.parameter('direction');
		var order_by = $.address.parameter('order_by');
		var view_mode = $.address.parameter('view_mode');
		var make = $.address.parameter('make');
		var model = $.address.parameter('model');
		var type = $.address.parameter('type');
		var start = $(this).attr('rel');
		var content_type = $('#content_type').text();
			
		var next = parseInt(start) + 40;
		if(next <= 0){
			next = 0;
		}

		//show loading spinner animation
		$('#spinner').fadeIn(50);
		$('#spinner2').fadeIn(50);
		
		//content type of 0 is for displaying newest listings
		//type 1 is for search
		//type 2 is for browse
		if(content_type == "0"){
			$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_all_listings=&start=" + next + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction,  
							success: function(data){
															
															$('#newest_output').append(data);
															$('#spinner2').fadeOut(200);
															$('#spinner').fadeOut(200);
															$('#show_more_listings').attr('rel', next);

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
		}else if(content_type == "1"){
			var sending_data = $('form[name=search_form]').serialize();
			
			$.ajax({
				url: "include/classified_functions.php",
				type: "POST",
				data: "output_search_listings=&start=" + next + "&" + sending_data + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction, 
				success: function(data){
												$('#newest_output').append(data);
												$('#spinner2').fadeOut(200);
												$('#spinner').fadeOut(200);
												$('#show_more_listings').attr('rel', next);
										},
				error: function(){
												$.jnotify("Error: ajax request failed.");

										}
			});
		}else if(content_type == "2"){

			
			$.ajax({
				url: "include/classified_functions.php",
				type: "POST",
				data: "output_browse_listings=&start=" + next + "&make=" + make + "&type=" + type + "&order_by=" + order_by + "&model=" + model + "&view_mode="  + view_mode + "&direction=" + direction, 
				success: function(data){
												$('#newest_output').append(data);
												$('#spinner2').fadeOut(200);
												$('#spinner').fadeOut(200);
												$('#show_more_listings').attr('rel', next);

										},
				error: function(){
												$.jnotify("Error: ajax request failed.");

										}
			});
		}
	
	});
	
	
	//////////////////////
	//Search features
	////////////////////
	
	$('#open_search_form').live('click', function(event){
		event.preventDefault();
		$('#search_form').toggle(0);
		$('#browse_form').hide(0);
		//fade out arrow that may be there
		$('#large_arrow').fadeOut(200);
	});
	
	
	$('#do_search').live('click', function(){
		$.scrollTo( '#main_title', 800, {axis:'y'});
		$('#show_more_listings').attr('rel', '0');
		doSearch();

	});
	
	//////////////////////
	//Instant result amount lookup
	////////////////////
	
	$('form[name=search_form]').live('change', function(){
		var sending_data = $('form[name=search_form]').serialize();
		
		$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "get_number_results=&" + sending_data, 
							success: function(data){
															$.jnotify(data + " results found", 1000);
															$('#instant_results_counter').html(data + " results found, click search to see them. <img style='position:relative; top:4px;' src='images/right_arrow_blue.gif'>");
													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
	});
	
	if($.address.parameter('search') == 'true'){
		$('input[name=make]').val($.address.parameter('make'));
		$('input[name=model]').val($.address.parameter('model'));
		$('input[name=year]').val($.address.parameter('year'));
		$('input[name=vin]').val($.address.parameter('vin'));
		$('input[name=type]').val($.address.parameter('type'));
		$('input[name=lower_price]').val($.address.parameter('lower_price'));
		$('input[name=upper_price]').val($.address.parameter('upper_price'));
		doSearch('true');
	}else if($.address.parameter('browse') == 'true'){
		
		doBrowse();
	}else{
		doNewest();
	}
	
	
	//////////////////////
	//browse features
	////////////////////
	
	$('#open_browse_form').live('click', function(event){
		event.preventDefault();
		$('#search_form').hide(0);
		$('#browse_form').toggle(0);
		$('#browse_type').fadeOut(0);
		$('#browse_model').fadeOut(0);
		$('#browse_make').fadeIn(0);
		$('#large_arrow').fadeOut(200);
	});
	
	
	
	$('.browse_link').live('click', function(event){
		event.preventDefault();
		//what is the value of the selection
		var rel = $(this).attr('rel');
		//what selection are they making? make or type
		var alt = $(this).attr('alt');
		//clearing url query string
		if(alt == 'make'){
			//applying selected type variable
			var view_mode = $.address.parameter('view_mode');
			$.address.queryString('browse=true&make=' + rel + "&view_mode=" + view_mode);
			$('#newest_output').fadeOut(500);
			$('#browse_make').fadeOut(500);
			$('#browse_type').delay(500).fadeIn(500);
			$('#show_more_listings').attr('rel', '0');
			doBrowse();
			outputTypes();
		}else if(alt == 'type'){
			var address = $.address.queryString();
			$.address.queryString(address + '&type=' + rel);
			
			$('#browse_type').fadeOut(500);
			$('#browse_model').delay(500).fadeIn(500);
			$('#show_more_listings').attr('rel', '0');
			doBrowse();
			outputModels();
			
		}else{
			var address = $.address.queryString();
			$.address.queryString(address + '&model=' + rel);
			
			$('#browse_model').fadeOut(0);
			$('#browse_type').fadeOut(0);
			$('#browse_make').fadeOut(0);
			$('#browse_form').toggle(0);
			$('#show_more_listings').attr('rel', '0');
			doBrowse();
			$.scrollTo( '#main_title', 800, {axis:'y'});
			
			//fade out arrow that may be there
			$('#large_arrow').fadeOut(200);
		}
		
		//$.address.queryString()  use to get query string
		
	});
	
	//view newest listings button
	//////////////////////////
	$('#view_newest').live('click', function(event){
		event.preventDefault();
		
		//set the proper url
		$.address.queryString('newest=true');
		$('#show_more_listings').attr('rel', '0');
		doNewest();
	
	});
	
	$('#view_popular').live('click', function(event){
		event.preventDefault();
		
		$('#show_more_listings').attr('rel', '0');
		//set the proper url
		$.address.queryString('newest=true&order_by=views');
		doNewest();
	
	});
	
	
//script for showing more pages of listings on customer account page
	var num_listings = $('#number_of_listings').html();
	
	if(num_listings == '200'){
		$('#cust_listings_more_button').show();
	}
	
	$('#cust_listings_more_button').live('click', function(){
		var start = $(this).attr('rel');
		var cust_number = $('#cust_number').html();
		
		next = parseInt(start) + 200;
		
		$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_cust_listings=&start=" + next + "&cust_number=" + cust_number, 
							success: function(data){
															$('#current_client_listings').html(data);
															$('#cust_listings_more_button').attr('rel', next);
															
													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
	});
	
	
	
});




////////////////////////////////////////////////////////////////
/////////End of document.ready, functions beyond this point
/////////////////////////////////////////////////////////////

//show browse form with intro stuff like large arrow
/////
	function showBrowse(){
			$('#search_form').hide(0);
			$('#browse_form').fadeIn(200);
			$('#browse_type').fadeOut(0);
			$('#browse_make').css('margin-top', '-180px');
			$('#browse_make').animate({ 
				opacity: 0
			  }, 0 );
			$('#browse_make').fadeIn(0);
			$('#browse_make').animate({ 
				opacity: 1,
				marginTop: "0px"
			}, 800, function(){
				//ie fix -- screws up firefox
				if ( $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }

			});
				
			$('#large_arrow').animate({ 
				opacity: 0,
				marginLeft: '800px'
			  }, 0 );
			$('#large_arrow').fadeIn(0);
			$('#large_arrow').animate({ 
				opacity: 1,
				marginLeft: "450px"
			  }, 800, function(){
				//ie fix -- screws up firefox
				if ( $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }

			});
		}
	
	//Reload the results output
	//////////////////////////////////
	////////////////////////////////////////////
	
	function reloadResults(){
		var content_type = $('#content_type').html();
			//content type of 0 is for displaying newest listings
			//type 1 is for search
			//type 2 is for browse
		
		if(content_type == '0'){//newest
			doNewest();
		}
		if(content_type == '1'){//search
		
			//get variables from url
			$('input[name=make]').val($.address.parameter('make'));
			$('input[name=model]').val($.address.parameter('model'));
			$('input[name=year]').val($.address.parameter('year'));
			$('input[name=vin]').val($.address.parameter('vin'));
			$('input[name=type]').val($.address.parameter('type'));
			$('input[name=lower_price]').val($.address.parameter('lower_price'));
			$('input[name=upper_price]').val($.address.parameter('upper_price'));
			
			doSearch('true');
		}
		if(content_type == '2'){//browse
			doBrowse();
		}
	}
	// this deletes the cookie when called
	function del_cookie(name) {
		document.cookie = name +
		'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
		document.cookie = name +"=0;";
	}
	
	
	//loads up the search area with results, takes parimeter for page reload in which it realoads the previous results from the page url
	function doSearch(reload){
			var direction = $.address.parameter('direction');
			var order_by = $.address.parameter('order_by');
			var view_mode = $.address.parameter('view_mode');
			var start = 0;
			
		
		if(reload == 'true'){
			
			var sending_data = "make=" + $.address.parameter('make') + "&model=" + $.address.parameter('model') + "&year=" + $.address.parameter('year') + "&vin=" + $.address.parameter('vin') + "&type=" + $.address.parameter('type') + "&lower_price=" + $.address.parameter('lower_price') + "&upper_price=" + $.address.parameter('upper_price');
			//var sending_data = "make=" + $.address.parameter('make')  + "&model=" + $.address.parameter('model') + "&year=" + $.address.parameter('year');
			//alert(sending_data);
		}else{
			var sending_data = $('form[name=search_form]').serialize();
			var view_mode = $.address.parameter('view_mode');
			var order_by = $.address.parameter('order_by');
			if(view_mode == 'undefined'){
				view_mode = "";
			}
			if(order_by == 'undefined'){
				order_by = "";
			}
			$.address.queryString('search=true&' + sending_data + "&view_mode=" + view_mode + "&order_by=" + order_by);
			
		}
		
		$('#spinner').fadeIn(50);
		$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_search_listings=&" + sending_data + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction + "&start=" + start, 
							success: function(data){
															$('#newest_output').fadeOut(100, function(){
																$('#newest_output').html(data);
																var number_of_results = $('#num_rows_result').val();
																$('#newest_title').text('Search results: (' + number_of_results + ' results)');
																$('#spinner').fadeOut(50);
															}).delay(100).fadeIn(100);
																
															$('#content_type').html('1');
															
															//get number of results
															
															

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
	}
	
	
	//loads up the model selection browse form with appropriate models
	function outputModels(){
			//get search parimeters directly from url, easier for reloading the page
			var make = $.address.parameter('make');
			var type = $.address.parameter('type');

		$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_models=&make=" + make + "&type=" + type, 
							success: function(data){
															$('#browse_model_output').html(data);
													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
	}
	
	//loads up the type selection browse form with appropriate models
	function outputTypes(){
			//get search parimeters directly from url, easier for reloading the page
			var make = $.address.parameter('make');

		$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_types=&make=" + make, 
							success: function(data){
															$('#browse_type_output').html(data);
													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
	}
	
	
	//loads up the search area with results, takes parimeter for page reload in which it realoads the previous results from the page url
	function doBrowse(){
			//get search parimeters directly from url, easier for reloading the page
			var direction = $.address.parameter('direction');
			var make = $.address.parameter('make');
			var model = $.address.parameter('model');
			var type = $.address.parameter('type');
			var order_by = $.address.parameter('order_by');
			var view_mode = $.address.parameter('view_mode');
			var start = 0;
			
			if(make === undefined){
				make = '';
			}
			if(model === undefined){
				model = '';
			}
			if(type === undefined){
				type = '';
			}

		$('#spinner').fadeIn(50);
		
		$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_browse_listings=&make=" + make + "&type=" + type + "&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction + "&start=" + start + "&model=" + model, 
							success: function(data){
															$('#newest_output').fadeOut(100, function(){
																$('#newest_output').html(data);
																var number_of_results = $('#num_rows_result').val();
																$('#newest_title').text('Browsing results: (' + number_of_results + ' results)');
																$('#spinner').fadeOut(50);
															}).delay(100).fadeIn(100);
																
															$('#show_more_listings').attr('rel', '0'); 
															$('#content_type').html('2');
															
															//get number of results
															
															

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
	}
	
	////get newest listings and output to div with id "newest_output"
	///////////////////
	function doNewest(first){
		var direction = $.address.parameter('direction');
		var order_by = $.address.parameter('order_by');
		var view_mode = $.address.parameter('view_mode');
		var start = 0;
		if(first == 'true'){
			setTimeout("fixHead()",1000);
			order_by = 'views';
			
		}

		$('#spinner').fadeIn(50);
		$.ajax({
			
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_all_listings=0&order_by=" + order_by + "&view_mode=" + view_mode + "&direction=" + direction + "&start=" + start, 
							success: function(data){
															$('#newest_output').fadeOut(100, function(){
																$('#newest_output').html(data);
																$('#newest_title').text('Newest listings');
																$('#spinner').fadeOut(50);
															}).delay(100).fadeIn(100);
															
															
															
															$('#show_more_listings').attr('rel', '0'); 
															$('#content_type').html('0');
															
															//get number of results
															
															

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
	}
	
	/*
	////get most popular listings and output to div with id "newest_output"
	///////////////////
	function doPopular(){
		var direction = $.address.parameter('direction');
		var order_by = $.address.parameter('order_by');
		var view_mode = $.address.parameter('view_mode');
		
		
		$.ajax({
							url: "include/classified_functions.php",
							type: "POST",
							data: "output_all_listings=0&popular=true&order_by=" + order_by + "&view_mode=" + view_mode, 
							success: function(data){
															$('#newest_output').fadeOut(100, function(){
																$('#newest_output').html(data);
																$('#newest_title').text('Most popular listings');
															}).delay(100).fadeIn(100);
																
															
															$('#newest_prev').attr('rel', '0'); 
															$('#newest_next').attr('rel', '0'); 
															$('#content_type').html('0');
															
															//get number of results
															
															

													},
							error: function(){
															$.jnotify("Error: ajax request failed.");

													}
						});
	}
	*/
	
		function fixHead(){
				var address = $.address.queryString();
				if(address.search('order_by') == -1){
					$.address.queryString(address + '&order_by=views');
				}
			}
			
			
//IE FADE FIX
function removeFilter(name) {
  $('#' + name).removeAttr("filter");
}
	
