
var list = [
  { name:'att', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'earthlink', href:'/services.php', description:'Voice, Internet & Data Servicess', section:[] },
  { name:'windstream', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'level3', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'verizon', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'xo', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'tw-telecom', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'century-link', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'twc', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'acc-business', href:'/services.php', description:'Internet & Data Services', section:[] },
  { name:'paetec', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'qwest', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'access2go', href:'/services.php', description:'Internet & Data Services', section:[] },
  { name:'broadvox', href:'/services.php', description:'VOIP & SIP Trunking Services', section:[] },
  { name:'broadsmart', href:'/services.php', description:'VOIP Services', section:[] },
  { name:'megapath', href:'/services.php', description:'Internet & Data Services', section:[] },
  { name:'nitel', href:'/services.php', description:'Internet & Data Services', section:[] },
  { name:'mettel', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'aire-spring', href:'/services.php', description:'Voice, Internet & Data Services', section:[] },
  { name:'telepacific', href:'/services.php', description:'Voice, Internet & Data Services', section:[] }
];

$.domReady(function () {
  
  var tab = $('#requestTab');
  var bar = $('#requestAQuote');
  var body = $('body');
  var open = false;
  
  tab.bind('click', function (e) {
    bar.animate({ top:(open) ? '-316px' : '0px', duration:300 });
    body.animate({ 'margin-top':(open) ? '0px' : '316px', duration:300 });
    open = !open;
  });
  
  $('.request-a-quote').bind('click', function (e) {
    e.preventDefault();
    bar.animate({ top:'0px', duration:300 });
    body.animate({ 'margin-top':'316px', duration:300 });
    window.scrollTo(0, 0);
    open = true;
  });
  
  $('.submit', bar[0]).bind('click', function (e) {
    e.preventDefault();
    // Submit Form
    reqwest({
      url:'/process-contact.php',
      method:'POST',
      data:reqwest.serialize(bar[0]),
      type:'json',
      success: function (response) {
        $('.grid_12', bar[0]).html('<section class="thank-you"><h3>Thank you! Your request has been processed, you will be contacted immediately.</h3></section>');
      },
      error: function () {
        // ignore
      }
    });
    
  });
  
  // Checkbox
  $('.checkbox').bind('click', function () {
    $(this).toggleClass('checked');
    var hidden = $('input', this);
    if ($(this).hasClass('checked'))
      hidden.val('yes');
    else
      hidden.val('no');
  });
  
  // Email
  $('.e').each(function () {
    var e = $(this).text() + '@' + 'bro' + 'adbandbrokers' + '.c' + 'om';
    $(this).text(e).attr('href', 'mailto:' + e);
  });
  
  // Build List
  $('.list > div').each(function () {
    var container = $(this);
    var ul = '<ul class="clean">';
    var ulContent = '';
    
    // Add Images
    for (var i = 0; i < list.length; i++) {
      ulContent += '<li> \
        <a href="' + list[i].href + '"></a> \
        <div class="img"><img src="/images/list/' + list[i].name + '.jpg"></div> \
        <div class="copy"> \
          <h4>Trusted provider for:</h4> \
          <p>' + list[i].description + '</p> \
        </div> \
      </li>';
    }
    ul += ulContent + ulContent;
    
    ul = $(ul + '</ul>');
    $('.list div div')[0].appendChild(ul[0]);
    
    var gridWidth = 960;
    var pages = Math.ceil(list.length / 4);
    var index = 0;
    
    var left = container.find('.arrow-left');
    var right = container.find('.arrow-right');
    
    left.bind('click', function (e) {
      if (index > 0)
        index -= 1;
      ul.animate({
        left:-(gridWidth * index),
        duration:500
      });
      e.preventDefault();
    });
    
    right.bind('click', function (e) {
      if (index+1 <= pages)
        index += 1;
      ul.animate({
        left:-(gridWidth * index),
        duration:500,
        after: function () {
          // If at end, flip to beginning
          if (index+1 > pages) {
            index = 0;
            ul.css({ left:0 });
          }
        }
      });
      
      e.preventDefault();
    });
    
  });
  
  var mapElement = document.getElementById('map');
  if (mapElement) {
    
    var latlng = new google.maps.LatLng(35.874508, -78.643535);
    var myOptions = {
      zoom: 19,
      center: latlng,
      disableDefaultUI: true,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(mapElement, myOptions);
    
    var image = new google.maps.MarkerImage(
      '/images/marker.png',
      new google.maps.Size(143,87),
      new google.maps.Point(0,0),
      new google.maps.Point(72,83)
    );

    marker = new google.maps.Marker({
      draggable: false,
      raiseOnDrag: false,
      icon: image,
      map: map,
      position: latlng
    });
    
  }
  
  $.ender({
    slideDown: function (duration) {
      duration = duration || 1000;
      var height = this.offsetHeight;
      $(this).css({
        height:0,
        overflow:'hidden'
      }).animate({
        height:'200px',
        duration:duration
      });
      return this;
    },
    slideUp: function (duration) {
      duration = duration || 1000;
      var height = this.offsetHeight;
      $(this).css({
        height:height,
        overflow:'hidden'
      }).animate({
        height:0,
        duration:duration
      });
      return this;
    },
    slideTo: function (height, duration) {
      duration = duration || 1000;
      $(this).css({
        overflow:'hidden'
      }).animate({
        height:height,
        duration:duration
      });
      return this;
    }
  }, true);
  
  // Accordion
  var accordion = $('.accordion');
  if (accordion && accordion.length) {
    
    var titles = $('.accordion dt');
    var contents = $('.accordion dd');
    
    var heights = [];
    var texts = [];
    
    for (var i = 0; i < contents.length; i++)
      heights[i] = (contents[i].offsetHeight+15) + 'px';
    contents.css({ height:0, overflow:'hidden' });
    
    titles.each(function (e, i) {
      
      var title = $(this);
      var content = $(this).next();
      
      texts[i] = this.innerText || this.textContent;
      texts[i] = texts[i].split('+ ').join('');
      
      title.bind('click', function () {
        $(this).addClass('open').siblings().removeClass('open');
        
        // Titles
        titles.each(function (e, j) {
          if (title[0] == this)
            this.innerHTML = '- ' + texts[j];
          else
            this.innerHTML = '+ ' + texts[j];
        });
        
        contents.each(function () {
          if (this == content[0]) {
            content.slideTo(heights[i], 300);
          } else
            $(this).slideUp(300);
        });
        
      });
      
    });
    
    //if(method === 'slidedown') {
		//	// Set overflow to hidden
		//	Spark(element).css({overflow: 'hidden', display: 'block'});
    //
		//	// Get original height
		//	original = Spark(element).attribute().offsetHeight;
    //
		//	// Set height to 0
		//	Spark(element).css({height: 0});
    //
		//	// Slide height to original
		//	Spark(element).animate({height: original}, timeframe, easing);
		//}
		//else if(method === 'slideup') {	
		//	// Get original height
		//	original = Spark(element).attribute().offsetHeight;
    //
		//	// Set overflow to hidden
		//	Spark(element).css({overflow: 'hidden', height: original});
    //
		//	// Slide height to 0
		//	Spark(element).animate({height: 0}, timeframe, easing, function() {
		//		// Set height to original
		//		Spark(element).css({height: original + 'px', display: 'none'});
		//	});
		//}
    
  }
  
});
