$(document).ready(function() {

	//$(".scroll-pane").jScrollPane();
	
	$('.dealer_city_button').click(function() {
		setTimeout(function() {
			$(".scroll-pane").jScrollPane();
		}, 1);
	});
	
	$('.monthly_payment_button').click(function() {
		setTimeout(function() {
			$(".city-scroll-pane").jScrollPane();
		}, 1);
	});

  $('#testimonials').cycle();

  $('a.fancy_listing').fancybox({
		'showNavArrows': true
	});
  $('a.submit_request').click(function() {
    $('#learn_more_send').submit();
  });
  $('a.calculate').click(function() {

		var state_input = $('#monthly_payment_input').attr('value');

		if (state_input == '') {
			alert('You must select a State and City.');
			return false;
		};

    $('#calculator_form').submit();
    $('input').each(function(index) {
      $(this).attr('value', '');
    });
    return false;
  });


  $('a.view_dealers').click(function() {
    var this_id = $(this).attr('id');
    var this_id = '#'+this_id;

    var dealer_name_form = $(this).parent(this_id).siblings('.form_dealer_name').text();

    var dealer_city_form = $(this).parent(this_id).siblings('.form_dealer_city').text();

    $(this).parent(this_id).children('input[name="dealership"]').each(function(index) {
      $(this).attr('value', dealer_name_form);
    });

    $(this).parent(this_id).children('input[name="dealercity"]').each(function(index) {
      $(this).attr('value', dealer_city_form);
    });

    $(this).parent(this_id).submit();
    //$(this).parent('form.view_dealer').submit();
  });


  $('a.view_dealers2').click(function() {
    $(this).parent('form.view_dealer2').submit();
  });


  $('#T .img_rotate').cycle({
    timeout:         7000
  });


  $('.gallery_listing').click(function() {
    var imgurl = $(this).attr('src');
    $('.img_rotate img:visible').attr('src', imgurl);
  });


//  minimum_price
//  number_of_bathrooms
//  square_footage
//  maximum_price
//  number_of_bedrooms

  $('a.get_results').click(function() {

    $('#search_form').submit();
    $('input').each(function(index) {
      $(this).attr('value', '');
    });
    return false;
  });

  $('#select_boxes button').click(function() {
    var dropdown =  $(this).siblings('.dropdown');
    $(this).siblings('.dropdown').show();
    $(this).siblings('.dropdown').children('ul').show();
    $('body').append('<div class="screener"></div>');
    $('div.screener').height($('#container').height());
    $('div.screener').click(function() {
      $(this).remove();
      $(dropdown).hide();
    });
    return false;
  });


	$('#minimum_price li').click(function() {
		var payment = $(this).attr('id');
		var text = $(this).text();
		$('#minimum_price_input').val(payment);
		$(this).parent('ul').parent('div').siblings('button.select_button').children('span').text(text);
		$(this).parent('ul').parent('div').hide();
		$('#minimum_price ul').hide();
		$('div.screener').remove();
	});


	$('#number_of_bathrooms li').click(function() {
		var payment = $(this).attr('id');
		var text = $(this).text();
		$('#number_of_bathrooms_input').val(payment);
		$(this).parent('ul').parent('div').siblings('button.select_button').children('span').text(text);
		$(this).parent('ul').parent('div').hide();
		$('#number_of_bathrooms ul').hide();
		$('div.screener').remove();
	});


	$('#square_footage li').click(function() {
		var payment = $(this).attr('id');
		var text = $(this).text();
		$('#square_footage_input').val(payment);
		$(this).parent('ul').parent('div').siblings('button.select_button').children('span').text(text);
		$(this).parent('ul').parent('div').hide();
		$('#square_footage ul').hide();
		$('div.screener').remove();
	});


	$('#maximum_price li').click(function() {
		var payment = $(this).attr('id');
	    var text = $(this).text();
	    $('#maximum_price_input').val(payment);
	    $(this).parent('ul').parent('div').siblings('button.select_button').children('span').text(text);
		$(this).parent('ul').parent('div').hide();
		$('#maximum_price ul').hide();
		$('div.screener').remove();
	});


	$('#number_of_bedrooms li').click(function() {
		var payment = $(this).attr('id');
		var text = $(this).text();
		$('#number_of_bedrooms_input').val(payment);
		$(this).parent('ul').parent('div').siblings('button.select_button').children('span').text(text);
		$(this).parent('ul').parent('div').hide();
		$('#number_of_bedrooms ul').hide();
		$('div.screener').remove();
	});


	$('#dealer_city div div.scroll-pane li').click(function() {
		var payment = $(this).attr('id');
		var text = $(this).text();
		$('#dealer_city_input').val(payment);
		$(this).parent('ul').parent('div').parent('div').parent('div').parent('div').siblings('button.select_button').children('span').text(text);
		$(this).parent('ul').parent('div').parent('div').parent('div').parent('div').hide();
		//$('#dealer_city ul').hide();
		$('div.screener').remove();
	});



	$('.close').click(function() {
		$(this).parent('div').hide();
	});





	//THIS IS THE SCRIPTING FOR THE SELECT STATE AND CITY BUTTONS
	//IN THE CALLOUT, WAS ONCE AN INTEREST RATE CALCULATOR
  $('#callouts button.interest_rate_button').click(function() {
    var dropdown =  $(this).siblings('.dropdown');
    $(this).siblings('.dropdown').show();
    $('body').append('<div class="screener"></div>');
    $('div.screener').height($('#container').height());
    $('div.screener').click(function() {
      $(this).remove();
      $(dropdown).hide();
    });
    return false;
  });


	$('#callouts button.monthly_payment_button').click(function() {
		if ($('button.select_button.interest_rate_button').children('span').text() == 'Select Interest Rate') {
			return false;
		} else {
			var rate = $('#interest_rate_input').val();
			var dropdown =  $(this).siblings('.dropdown');
			$(this).siblings('.dropdown').show();

			if (rate == '1') {
				$('ul.monthly_payment_1').show();
			} else if (rate == '2') {
				$('ul.monthly_payment_2').show();
			} else if (rate == '3') {
				$('ul.monthly_payment_3').show();
			} else if (rate == '4') {
				$('ul.monthly_payment_4').show();
			} else if (rate == '5') {
				$('ul.monthly_payment_5').show();
			} else if (rate == '6') {
				$('ul.monthly_payment_6').show();
			} else if (rate == '7') {
				$('ul.monthly_payment_7').show();
			} else if (rate == '8') {
				$('ul.monthly_payment_8').show();
			}

			$('body').append('<div class="screener"></div>');
			$('div.screener').height($('#container').height());
			$('div.screener').click(function() {
				$(this).remove();
				$(dropdown).hide();
				$('#monthly_payment ul').hide();
			});
			return false;
		};
	});


	$('#interest_rate li').click(function() {
		var rate = $(this).attr('id');
		var text = $(this).text();
		$('#interest_rate_input').val(rate);
		$(this).parent('ul').parent('div').siblings('button.select_button').children('span').text(text);
		$(this).parent('ul').parent('div').hide();
		$('#monthly_payment ul').hide();
		$('div.screener').remove();
	});


	$('#monthly_payment li').click(function() {
		var payment = $(this).attr('id');
		var text = $(this).text();
		$('#monthly_payment_input').val(payment);
		$('#city_select_button').children('span').text(text);
		$(this).parent('ul').parent('div').hide();
		$('#monthly_payment').hide();
		$('div.screener').remove();
	});
	//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	//THIS IS THE SCRIPTING FOR THE SELECT STATE AND CITY BUTTONS
	//IN THE CALLOUT, WAS ONCE AN INTEREST RATE CALCULATOR


  $('a').click(function() {
    if ($(this).attr('href') == '#') {return false;};
  });

  $(".select_button").button().click(function() {
  })

  .next()

  .button({
    text: false,
    icons: {
      primary: "ui-icon-triangle-1-s"
    }
  })

  .click(function() {
  })

  .parent()
  .buttonset();

	// $(".select_button").click(function() {
	//     $(this).focus();
	// });
	// $(".select_button").blur(function(){
	//     $(this).parent('div').children('div').hide();
	// });



  $('#search_checklist > tbody > tr > td > label > input:checked').parent('label').addClass('clicked');
  $('#learn_more_form > tbody > tr > td > label.checkbox > input:checked').parent('label').addClass('clicked');


  $('#search_checklist label').click(function() {

    if ( ! $(this).is('.clicked')) {
      $(this).addClass('clicked');
      $(this).children('input').attr('checked', true);
      return false;
    } else {
      $(this).removeClass('clicked');
      $(this).children('input').attr('checked', false);
      return false;
    };

  });


  $('#learn_more_form label.checkbox').click(function() {

    if ( ! $(this).is('.clicked')) {
      $(this).addClass('clicked');
      $(this).children('input').attr('checked', true);
      return false;
    } else {
      $(this).removeClass('clicked');
      $(this).children('input').attr('checked', false);
      return false;
    };

  });



  $('#learn_more_form tr').each(function(index) {
    $(this).children('td:first').addClass('align-right');
    $(this).children('td:nth-child(2)').addClass('align-left');
  });



  $('#slider_left').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });

  $('#slider_right').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });


  var scrolling = false;

    var end_pos = 0;

    $('#slider_inner').children('.feature_img').each(function(index) {
      var this_width = $(this).width();
      end_pos = end_pos+this_width+4;
    });


  jQuery(function($){
      $("#slider_right").mousedown(function(){

        var end_pos = 0;
        $('#slider_inner').children('.feature_img').each(function(index) {
          var this_width = $(this).width();
          end_pos = end_pos+this_width+4;
        });

        var pos = $("#slider_inner").position();

        if (pos.left <= -(end_pos-500)) {
          return false;
        } else {
          scrolling = false;
          startScrolling($("#slider_inner"), "-=426px");
        };

      }).mouseup(function(){
          scrolling = false;
      });
  });

  jQuery(function($){
      $("#slider_left").mousedown(function(){
          var pos = $("#slider_inner").position();
          if (pos.left >= 0) {
            return false;
          } else {
            scrolling = false;
            startScrolling($("#slider_inner"), "+=426px");
          };
      }).mouseup(function(){
          scrolling = false;
      });
  });

  function startScrolling(obj, param)
  {
      obj.animate({"left": param}, "fast", function(){
          if (scrolling)
          {
                  startScrolling(obj, param);
          }
      });
  }


  //$('.feature_img').each(function(index) {
  //  var feature_img = $(this).children('img').width();
  //
  //  $(this).width(feature_img/2);
  //
  //  var feature_img = -(feature_img/2);
  //  $(this).children('img').css('margin-left', feature_img+"px");
  //
  //});


  //$('.feature_img').hover(function() {
  //  $(this).children('img').css('margin-left', "0px");
  //}, function() {
  //  var feature_img = $(this).children('img').width();
  //  var feature_img = -(feature_img/2);
  //  $(this).children('img').css('margin-left', feature_img+"px");
  //});


  ////SLIDER
  //$('.view_port_profile').each(function(index) {
  //  $(this).css('left', index*740+'px');
  //
  //  end_pos = index*740+740;
  //
  //});
  //
  //
  //$('#view_port #right_arr').click(function() {
  //  curr_pos = $('#view_port_slider').position();
  //  curr_pos = curr_pos.left - 740;
  //
  //  if (curr_pos % 740) {
  //    return false;
  //  };
  //
  //  if (-(end_pos) != curr_pos) {
  //    $('#view_port_slider').animate({left: curr_pos}, 1000);
  //  } else {
  //    $('#view_port_slider').animate({left: 0}, 1500);
  //  };
  //
  //});
  //
  //
  //$('#view_port #left_arr').click(function() {
  //  curr_pos = $('#view_port_slider').position();
  //  curr_pos = curr_pos.left + 740;
  //
  //  if (curr_pos % 740) {
  //    return false;
  //  };
  //
  //  if (curr_pos != 740) {
  //    $('#view_port_slider').animate({left: curr_pos}, 1000);
  //  } else {
  //    $('#view_port_slider').animate({left: -(end_pos)+740}, 1500);
  //  };
  //
  //});
  //
  //
  ////SLIDER
  //$('#right_arr_port').click(function() {
  //  curr_pos = $('#view_port_slider').position();
  //  curr_pos = curr_pos.left - 740;
  //
  //  if (curr_pos % 740) {
  //    return false;
  //  };
  //
  //  var urler = -(curr_pos) / 740;
  //
  //  if (urler == urls.length) {urler = 0};
  //
  //    url = urls[urler];
  //
  //    $('#launch_website').attr('href', url);
  //
  //  if (-(end_pos) != curr_pos) {
  //    $('#view_port_slider').animate({left: curr_pos}, 1000);
  //  } else {
  //    $('#view_port_slider').animate({left: 0}, 1500);
  //  };
  //
  //});
  //
  //
  //$('#left_arr_port').click(function() {
  //  curr_pos = $('#view_port_slider').position();
  //  curr_pos = curr_pos.left + 740;
  //
  //  if (curr_pos % 740) {
  //    return false;
  //  };
  //
  //  var urler = -(curr_pos) / 740;
  //
  //  if (urler == -1) {urler = 15};
  //
  //    url = urls[urler];
  //
  //    $('#launch_website').attr('href', url);
  //
  //  if (curr_pos != 740) {
  //    $('#view_port_slider').animate({left: curr_pos}, 1000);
  //  } else {
  //    $('#view_port_slider').animate({left: -(end_pos)+740}, 1500);
  //  };
  //
  //});
  //
  //
  ////SCROLL
  //$('#recent-posts ul').jScrollPane({
  //  scrollbarWidth : 11
  //});
  //$('.scroller').jScrollPane();
  //
  //
  ////GRID TIPS
  //$('div.img_holder').each(function(index) {
  //
  //  index = index+1;
  //
  //  if (!(index%4)) {
  //    $(this).addClass('outter');
  //  };
  //
  //});
  //
  //
  //$('div.img_holder').hover(function() {
  //  $(this).children('.img_tip').fadeIn('fast');
  //}, function() {
  //  $(this).children('.img_tip').fadeOut('fast');
  //});






    //how much items per page to show
    var show_per_page = 10;
    //getting the amount of elements inside content div
    var number_of_items = $('#content').children().size();
    //calculate the number of pages we are going to have
    var number_of_pages = Math.ceil(number_of_items/show_per_page);

    //set the value of our hidden input fields
    $('#current_page').val(0);
    $('#show_per_page').val(show_per_page);

    //now when we got all we need for the navigation let's make it '

    /*
    what are we going to have in the navigation?
        - link to previous page
        - links to specific pages
        - link to next page
    */
    var navigation_html = '<a class="previous_link" href="javascript:previous();">Prev</a>';
    var current_link = 0;
    while(number_of_pages > current_link){
        navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
        current_link++;
    }
    navigation_html += '<a class="next_link" href="javascript:next();">Next</a>';


    $('#page_navigation').html(navigation_html);
    $('#page_navigation2').html(navigation_html);

    //add active_page class to the first page link
    $('#page_navigation .page_link:first').addClass('active_page');
    //add active_page class to the first page link
    $('#page_navigation2 .page_link:first').addClass('active_page');

    //hide all the elements inside content div
    $('#content').children().css('display', 'none');

    //and show the first n (show_per_page) elements
    $('#content').children().slice(0, show_per_page).css('display', 'block');

});


function previous(){

    new_page = parseInt($('#current_page').val()) - 1;
    //if there is an item before the current active link run the function
    if($('.active_page:first').prev('.page_link').length==true){
        go_to_page(new_page);
    }

}

function next(){
    new_page = parseInt($('#current_page').val()) + 1;
    //if there is an item after the current active link run the function
    if($('.active_page:first').next('.page_link').length==true){
        go_to_page(new_page);
    }

}

function go_to_page(page_num){
    //get the number of items shown per page
    var show_per_page = parseInt($('#show_per_page').val());

    //get the element number where to start the slice from
    start_from = page_num * show_per_page;

    //get the element number where to end the slice
    end_on = start_from + show_per_page;

    //hide all children elements of content div, get specific items and show them
    $('#content').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');

    /*get the page link that has longdesc attribute of the current page and add active_page class to it
    and remove that class from previously active page link*/
    $('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');

    //update the current page input field
    $('#current_page').val(page_num);
}
