var imageUrl = '/uploads/images/';

var Slideshow = function(slides, buttons) {
  this.slides = $(slides);
  this.buttons = $(buttons);
  this.current = 0;
  this.animateSpeed = 400;
  this.interval = 7000;
  this.timer = 0;
  this.last = 0;
  this.slideLength = this.slides.length;
  this.init();
};

Slideshow.prototype.start = function() {
  var that = this;
  this.timer = setInterval(function() {
    that.next();
  }, this.interval);
};

Slideshow.prototype.stop = function() {
  clearInterval(this.timer);
};

Slideshow.prototype.next = function() {
  this.current++;
  if (this.current == this.slides.length) {
    this.current = 0;
  }
  this.show();
};

Slideshow.prototype.show = function() {
  var that = this;
  $(this.slides[this.last]).fadeOut(this.animateSpeed, function() {
    $(that.buttons[that.last]).removeClass('selected');
    $(that.buttons[that.current]).addClass('selected');
    $(that.slides[that.current]).fadeIn(that.animateSpeed);
    that.last = that.current;
  });
};

Slideshow.prototype.init = function() {
  var that = this;
  $(this.slides[0]).show();

  this.buttons.each(function(element) {
    that.buttons[element].onclick = function() {
      that.stop();
      that.current = element;
      that.show();
      that.start();
    };
  });

  this.start();
};

var site = {};
site.cookie = {};
site.toggle = 'work';
site.images = [imageUrl + 'home.png', imageUrl + 'work.png', imageUrl + 'vps.co.nz.png', imageUrl + 'members-centre.png'];

site.preloadImages = function(images) {
  for (var i in images) {
    var img = new Image();
    img.src = images[i];
    delete img;
  }
};

site.cookie.set = function(name, value, days) {
  var date = new Date();
  date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  var expires = "; expires=" + date.toGMTString();
  document.cookie = name + "=" + value + expires + "; path=/";
};

site.cookie.get = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};

site.toolbarFade = function() {
  var toolbar = $('#topbar-fader');
  var isDark = false;
  var scrollYOffset = $(this).scrollTop();
  if (scrollYOffset < 10) {
    isDark = false;
    toolbar.stop().fadeTo('slow', 0.1);
  } else if (!isDark) {
    toolbar.stop().fadeTo('slow', 0.7);
    isDark = true;
  }
};

site.bodyScroll = function() {
  var current = 0;
  setInterval(function() {
    (current < -133) ? current = -1 : current -= 1;
    $('#site').css('background-position', current + 'px 0px');
  }, site.bodyScroll.config.scrollSpeed);
};

site.bodyScroll.config = {
  scrollSpeed: 70
};

site.notify = function(title, text, image) {
  $.gritter.add({
    title: title,
    text: text,
    image: imageUrl + image + '.png',
    time: 7000
  });

  return false;
};

site.loadTweets = function() {

  $("#network-status").tweet({
    username: "MaxnetNZStatus",
    join_text: "auto",
    avatar_size: 0,
    count: 1,
    auto_join_text_default: "&nbsp;", 
    auto_join_text_ed: "",
    auto_join_text_ing: "",
    auto_join_text_reply: "",
    auto_join_text_url: "",
    loading_text: "Loading status..."
  });

  $("#tweet-box-middle").tweet({
    username: "MaxnetNZ",
    join_text: "auto",
    avatar_size: 0,
    count: 3,
    auto_join_text_default: "", 
    auto_join_text_ed: "",
    auto_join_text_ing: "",
    auto_join_text_reply: "",
    auto_join_text_url: "",
    loading_text: "Loading tweets..."
  });
};

site.openLauncherPanel = function() {
  $('#overlay-2').fadeIn();
  $('#launcher-panel').css({
    left: ($(document).width()/2) - ($('#launcher-panel').outerWidth()/2) + 'px',
    top: ($(window).height() /2) - ($('#launcher-panel').outerHeight()/2) + 'px'
  }).show();
};

site.load = function() {
  $(window).scroll(site.toolbarFade);
  site.bodyScroll();
  site.toggle = site.cookie.get('realm') || site.toggle;
  site.isMouseOverMenu = false;

  if (site.toggle === 'home') {
    $('.toggle-home').show();
    $('#realm-toggle').css('background-position', '0%');
  } else {
    $('.toggle-work').show();
    $('#realm-toggle').css('background-position', '100%');
  }

  $('#realm-toggle').click(function() {
    $('.slide').hide();
    site.slideshowHome.stop();
    site.slideshowWork.stop();
    if (site.toggle === 'work') {
      site.slideshowHome.start();
      site.slideshowHome.next();
      $(this).animate({backgroundPosition: 0}, 500);
      var num = 0;
      var workElementsLength = $('.toggle-work').length;
      $('.toggle-work').fadeOut('fast', function() {
        num++;
        $('.toggle-work').hide();
        if (num == workElementsLength) {
          $('.toggle-home').fadeIn('fast');
          site.notify('Residential Options', 'Showing menu options for @home', 'home');
        }
      });
      site.toggle = 'home';
      site.cookie.set('realm', site.toggle, 500);
    } else {
      site.slideshowWork.start();
      site.slideshowWork.next();
      $(this).animate({backgroundPosition: 106}, 500);
      var num = 0;
      var homeElementsLength = $('.toggle-home').length;
      $('.toggle-home').fadeOut('fast', function() {
        num++;
        $('.toggle-home').hide();
        if (num == homeElementsLength) {
          $('.toggle-work').fadeIn('fast');
          site.notify('Business Options', 'Showing menu options for @work', 'work');
        }
      });
      site.toggle = 'work';
      site.cookie.set('realm', site.toggle, 500);
    }

  });

  $('.tip-n').tipsy({gravity: 'n'});
  $('.tip-nw').tipsy({gravity: 'nw'});
  $('.tip-ne').tipsy({gravity: 'ne'});
  $('.tip-s').tipsy({gravity: 's'});
  $('.tip-sw').tipsy({gravity: 'sw'});
  $('.tip-se').tipsy({gravity: 'se'});
  $('.tip-e').tipsy({gravity: 'e'});
  $('.tip-w').tipsy({gravity: 'w'});

  $('.blink').focus(function() {
    if (this.defaultValue === this.value) {
      this.value = '';
      $(this).removeClass('blink');
    }
  }).blur(function() {
    if (this.value === '') {
      $(this).addClass('blink');
      this.value = this.defaultValue;
    }
  });

  site.preloadImages(site.images);

  $('#navbar li.menu-button').hoverIntent(function() {
    var el = $(this);
    el.addClass('hover');
    var left = ($(window).width() / 2) - 475;
    el.find('.menu-item').css('left', left).show();
    $('#overlay-1').fadeIn(200);
    $('#logo').stop().animate({'margin-top': -50}, 500);
  }, function() {
    var el = $(this);
    el.removeClass('hover');
    el.find('.menu-item').hide();
    $('#logo').stop().animate({'margin-top': 0}, 500);
    $('#overlay-1').fadeOut(200);
  });
  
  $('#btn-launcher').click(site.openLauncherPanel);
  $('#launcher-panel-close').click(function() {
    $('#overlay-2').fadeOut();
    $('#launcher-panel').hide();
  });
  $('#overlay-2').click(function() {
    $(this).fadeOut();
    $('#launcher-panel').hide();
  });

  $("#sales-form").validate({
    rules: {
      first_name: {
        required: true
      },
      last_name: {
        required: true
      },
      email: {
        required: true,
        email: true
      },
      company: {
        required: true
      }
    },
    messages: {
      first_name: 'Your first name is required',
      last_name: 'Your last name is required',
      email: 'A valid email is required',
      company: 'Company name is required'
    }
  });

  $("#contact-form").validate({
    rules: {
      name: {
        required: true
      },
      email: {
        required: true,
        email: true
      },
      enquiry: {
        required: true
      }
    },
    messages: {
      name: 'Your name is required',
      email: 'A valid email is required',
      enquiry: 'A message is required'
    }
  });
  
  if (screen.width < 1000) {
   $('#topbar, #topbar-fader').css('position', 'absolute');
  }
};

$(window).load(function() {
  site.slideshowHome = new Slideshow('#slideshow-home .slide', '#slideshow-home .slideshow-buttons a');
  site.slideshowWork = new Slideshow('#slideshow-work .slide', '#slideshow-work .slideshow-buttons a');
  site.loadTweets();
});

$(site.load);


