/* Function to copy an element, including itself */
jQuery.fn.outer = function() {
  return $($('<div></div>').html(this.clone())).html();
}


$(document).ready(function() {
  /* The navigation already exists in the markup,
   * so push each item into an array and remove it
   * so that jQuery cycle can use them as it's pager nav
   */
  var sels = new Array();
  $('#pan_selectors li').each(function(index) {
    sels[index] = $(this).outer(); 
    $(this).remove();
  });
  /* On with the cycle */
  $('#panoramas').cycle({
    fx: 'scrollLeft,scrollRight',
    timeout: 0,
    pager: '#pan_selectors',
    pagerAnchorBuilder: function(index, element) {
      return sels[index];
    },
    cleartype: true,
    cleartypeNoBg: true
	});

  /* Select car modal window */
  var carselected = $.cookie('carselected');
  
  /* Launch modal on page load */
  $('#pickacar').dialog({width:750,draggable:false,resizable:false,position:['center',140]});
  
  $('#carlist li').click(function() {
    $.cookie('carselected', $(this).attr('id'), { expires: 30 });
    $('#pickacar').dialog('close');
  });

  /* Reset the last town selected cookie so when they start clicking it works properly */
  $.cookie('lastcity', null);

  /* Change town to car icon when clicked */
  $('#pan_selectors li a').click(function() {
    var mycar = $.cookie('carselected');
    if (mycar == null) {
      mycar = 'car1';
    }
    var mycarimg = '/images/cars/' + mycar + '.png';
    /* update the last city and move car to current city */
    /* if we are moving from city to city, update the timer */
    var city = $(this).attr('href');
    city = city.replace('http://www.dutchcountryroads.com/',''); // Because IE is stupid
    city = city.replace('http://dutchcountryroads.com/',''); // Because IE is stupid
    var lastcity = $.cookie('lastcity');
    //$('#pan_selectors li').css('background','none');
    if(lastcity != null) { 
      //$(lastcity+'_city').css('background','none');
      $.cookie('lastcity', city, { expires: 30 });
      updateTimer(lastcity, city);
    }
    else { $.cookie('lastcity', city, { expires: 30 }) }
    var position = $(city + '_city').position();
    var posleft = parseInt(position.left) - 25;
    var postop = parseInt(position.top) - 10;
    $('#car').css('top',postop).css('left',posleft).css('display','block').css('background','url('+mycarimg+')');
  });
  
  /* contact modal */
  $('#contact_btn').click(function() {
    $('#contact_modal').dialog({width:400,draggable:false,resizable:false,position:['center',140]});
  });

  /* region map modal */
  $('.region_btn').click(function() {
    $('#pickacar').dialog('close');
    $('#region_modal').dialog({width:800,draggable:false,resizable:false,position:['center',140]});
  });

  /* modal close links */
  $('#region_close').click(function() {
    $('#region_modal').dialog('close');
  });
  $('#contact_close').click(function() {
    $('#contact_modal').dialog('close');
  });

  /* destination dropup */
  $('#destination_btn').click(function() {
    $('#destination_popup').toggle();
    return false;
  });

  /* contact form submission */
  $('#contact_submit').click(function() {
    var name = $("input#name").val();
    var email = $("input#email").val();
    var address1 = $("input#address1").val();
    var address2 = $("input#address2").val();
    var city = $("input#city").val();
    var state = $("input#state").val();
    var zip = $("input#zip").val();
    var comments = $("textarea#comments").val();
    if (name == "") {
      $("input#name").focus();
      $("input#name").css('border','solid 1px #f00');
      return false;
    }
    if (email == "") {
      $("input#email").focus();
      $("input#email").css('border','solid 1px #f00');
      return false;
    }
    var dataString = 'send=yes&name='+ name + '&email=' + email + '&address1=' + address1 + '&address2=' + address2 + '&city=' + city + '&state=' + state + '&zip=' + zip + '&comments=' + comments;
    $.ajax({
      type: "POST",
      url: "formsubmit.php",
      data: dataString,
      success: function(ret) {
        if(ret == 1) {
          $('#form_container').html("<p><strong>Thank you!</strong></p><p>We will be in touch soon.</p>");
        }
      }
    });  
    return false;
  });

  /* Timer reset button */
  $('#timerreset').click(function() {
    $('#timertext').html('<img src="/images/timer/0.png"/><img src="/images/timer/0.png"/> <img src="/images/timer/0.png"/><img src="/images/timer/0.png"/>');
    $('#timerto').html('');
    $('#timerfrom').html('');
    $('#car').css('display','none');
    $.cookie('lastcity', null);
  });

});

/* Preload images so there's no lag */
if (document.images) {
pic0 =  new Image(80,11);
pic0.src="images/timer/cn_wrightsville.png";
pic1 =  new Image(80,11);
pic1.src="images/timer/cn_middletown.png";
pic2 =  new Image(80,11);
pic2.src="images/timer/cn_greencastle.png";
pic3 =  new Image(80,11);
pic3.src="images/timer/cn_gettysburg.png";
pic4 =  new Image(21,36);
pic4.src="images/timer/5.png";
pic5 =  new Image(21,36);
pic5.src="images/timer/8.png";
pic6 =  new Image(80,11);
pic6.src="images/timer/cn_mechanicsburg.png";
pic7 =  new Image(80,11);
pic7.src="images/timer/cn_intercourse.png";
pic8 =  new Image(80,11);
pic8.src="images/timer/cn_york.png";
pic9 =  new Image(80,11);
pic9.src="images/timer/cn_hamburg.png";
pic10 =  new Image(80,11);
pic10.src="images/timer/cn_hanover.png";
pic11 =  new Image(80,11);
pic11.src="images/timer/cn_annville.png";
pic12 =  new Image(21,36);
pic12.src="images/timer/0.png";
pic13 =  new Image(80,11);
pic13.src="images/timer/cn_strasburg.png";
pic14 =  new Image(21,36);
pic14.src="images/timer/9.png";
pic15 =  new Image(21,36);
pic15.src="images/timer/2.png";
pic16 =  new Image(21,36);
pic16.src="images/timer/7.png";
pic17 =  new Image(21,36);
pic17.src="images/timer/1.png";
pic18 =  new Image(21,36);
pic18.src="images/timer/3.png";
pic19 =  new Image(80,11);
pic19.src="images/timer/cn_newport.png";
pic20 =  new Image(80,11);
pic20.src="images/timer/cn_boyertown.png";
pic21 =  new Image(80,11);
pic21.src="images/timer/cn_reading.png";
pic22 =  new Image(80,11);
pic22.src="images/timer/cn_mtgretna.png";
pic23 =  new Image(80,11);
pic23.src="images/timer/cn_fairfield.png";
pic24 =  new Image(80,11);
pic24.src="images/timer/cn_carlisle.png";
pic25 =  new Image(21,36);
pic25.src="images/timer/4.png";
pic26 =  new Image(80,11);
pic26.src="images/timer/cn_harrisburg.png";
pic27 =  new Image(80,11);
pic27.src="images/timer/cn_duncannon.png";
pic28 =  new Image(80,11);
pic28.src="images/timer/cn_newoxford.png";
pic29 =  new Image(80,11);
pic29.src="images/timer/cn_newbloomfield.png";
pic30 =  new Image(80,11);
pic30.src="images/timer/cn_lebanon.png";
pic31 =  new Image(80,11);
pic31.src="images/timer/cn_mercersburg.png";
pic32 =  new Image(80,11);
pic32.src="images/timer/cn_shippensburg.png";
pic33 =  new Image(80,11);
pic33.src="images/timer/cn_chambersburg.png";
pic34 =  new Image(80,11);
pic34.src="images/timer/cn_lancaster.png";
pic35 =  new Image(21,36);
pic35.src="images/timer/6.png";
pic36 =  new Image(80,11);
pic36.src="images/timer/cn_hershey.png";
}

