// cufon
Cufon.replace('.neybor-font', { hover: true });


// pretty photo
$(document).ready(function() {
  $("a[rel^='prettyPhoto']").prettyPhoto();
  
  // image preloading  
  $.preloadCssImages();
  
  // fun stuff hover:P
  $('.drain_fun_stuff').hover(function() {Cufon.replace('.drain_fun_stuff .neybor-font', { color: '#fff' });}, function() {Cufon.replace('.drain_fun_stuff .neybor-font', { color: '#5f2b0f' });})
});


// nav hovering
$(document).ready(function() {
  if (document.all) {
    $(".icon-nav li").hoverClass("sfHover");
  }
});

$.fn.hoverClass = function(c) {
  return this.each(function() {
    $(this).hover( 
      function() { $(this).addClass(c);    },
      function() { $(this).removeClass(c); }
    );
  });
};  


// homepage slideshow
var HomeSlide = function(containerId) {
  this.containerId = containerId;

  this.init();
};

HomeSlide.prototype = {
  TIMEOUTS: [ 3750, 1500, 5000, 12300, 12600, 6500, 4000, 10000 ],

  TRANSITION: 750,

  init: function() {
    this.container = $('#' + this.containerId);
    this.imgs      = this.container.children('.home-slide');
  },

  play: function() {
    this.playing = true;

    this.scheduleNext();
  },

  rewind: function() {
    this.playing = false;

    this._goto(0);

    this.container.children('.tv-spots').fadeOut(this.TRANSITION);
  },

  advance: function() {
    this._goto(this.pos + 1);
  },

  end: function() {
    this.playing = false;

    var theend = this.imgs.length - 1;

    if (this.pos != theend) {
      this._goto(theend);
    }

    this.container.children('.tv-spots').fadeIn(this.TRANSITION);
  },

  _goto: function(pos) {
    if (this.timer) { clearTimeout(this.timer); }

    this.pos       = pos;

    this.outgoing  = this.incoming;
    this.incoming  = this.imgs[this.pos];

    this.transition(this.outgoing, this.incoming);
  },

  transition: function(outgoing, incoming) {
    if (outgoing) { $(outgoing).fadeOut(this.TRANSITION); }
    if (incoming) { $(incoming).fadeIn( this.TRANSITION); }

    this.onTransition();
  },

  onTransition: function() {
    if (this.playing) { this.scheduleNext(); }
  },

  scheduleNext: function() {
    if (this.pos < this.imgs.length - 1) {
      var self = this;
      var proceed = function() { self.advance(); };

      this.timer = setTimeout(proceed, this.TIMEOUTS[this.pos]);
    }
  }
};


// flash video
var PSSHVideo = function(containerId, flv, options, slideshow) {
  this.containerId = containerId;

  this.stage       = $('.stage');
  this.container   = $('#' + containerId);

  this.flv         = flv;
  this.slideshow   = slideshow;

  var self = this;
  $('.skip').click(function() {
    try { self.pause(); } catch (e) { }; return false; });
  $('.replay').click(function() {
    try { self.init(); } catch (e) { }; return false; });

  this.options = options || { };

  this.wasPlayed() ? this.onComplete() : this.init();
};

PSSHVideo.prototype = {
  videoSWF : "/wp-content/themes/pssh/flash/video/FAVideo",
  //videoSWF : "http://static.pugetsoundstartshere.org.s3.amazonaws.com/flash/FAVideo",

  init: function() {
    this.stage.addClass('stage-playing');

    var self = this;

    this.options.swfPath = this.videoSWF;

    this.videoFLV = $.isArray(this.flv) ?
                      this.flv[Math.floor(Math.random()*this.flv.length)] :
                      this.flv;

    this.player = new FAVideo(this.containerId, this.videoFLV,
                              this.options.width, this.options.height, this.options);

    this.player.addEventListener("init",        this, this.onInit);
    this.player.addEventListener("ready",       this, this.onReady);
    this.player.addEventListener("stateChange", this, this.onStateChange);
    this.player.addEventListener("complete",    this, this.onComplete);

    if (this.slideshow) { this.slideshow.rewind(); }
  },

  configure: function() {
    if (this.options.videoAlign) {
      this.player.setVideoAlign(this.options.videoAlign);
    }
    if (this.options.videoScaleMode) {
      this.player.setVideoScaleMode(this.options.videoScaleMode);
    }
    if (this.options.bufferTime) {
      this.player.setBufferTime(this.options.bufferTime);
    }
  },

  play: function() {
    this.player.play(this.videoFLV);
  },

  pause: function() {
    this.player.stop();
  },

  stop: function() {
    if (this.player) {
      //this.player.stop();

      this.player.removeEventListener("init",        this, this.onInit);
      this.player.removeEventListener("ready",       this, this.onReady);
      this.player.removeEventListener("stateChange", this, this.onStateChange);
      this.player.removeEventListener("complete",    this, this.onComplete);
    };

    this.onComplete();
  },

  onInit: function() {
    //console.log("init");
  },

  onReady: function() {
    //console.log("ready");

    if (this.options.playDelay) {
      var self = this; 
      var playLater = function() { 
        self.play(); 
      };
      if (this.playTimer) { 
        clearTimeout(this.playTimer); 
      }
      this.playTimer = setTimeout(playLater, this.options.playDelay * 1000);
    }
  },

  onStateChange: function(event) {
    //console.log("stateChange: " + event.state);

    if (event.state == "playing") {
      this.onPlay();
    } else if (event.state == "paused") {
      this.onPause();
    }
  },

  onPlay: function() {
    if (!this.playing) {
      this.playing = true;

      this.markPlayed();

      if (this.slideshow) { this.slideshow.play(); }

      var self = this;
      setTimeout(function() { self.configure(); },    1);
      setTimeout(function() { self.configure(); }, 1000);
      setTimeout(function() { self.configure(); }, 2000);
      setTimeout(function() { self.configure(); }, 4000);
    }
  },

  onPause: function() {
    if (this.playing) {
      this.stop();
    }
  },

  onComplete: function() {
    //console.log("complete");

    this.playing = false;

    //this.container.empty();
    this.stage.removeClass('stage-playing');

    if (this.slideshow) { this.slideshow.end(); }
  },

  wasPlayed: function() {
    return $.cookie(this.containerId);
  },

  markPlayed: function() {
    var now = new Date();
    $.cookie(this.containerId, '' + now.getTime(), { path : '/', expires : 7 });
  }
};


// google map
var PSSHMap = function(container, places, options) {
  if (!GBrowserIsCompatible()) return;

  this.map      = new GMap2(container);
  this.geocoder = new GClientGeocoder();

  this.options  = { };
  $.extend(this.options, this.DEFAULT_OPTIONS);
  $.extend(this.options, options);

  this.map.setCenter(new GLatLng(this.options.lat, this.options.lon), this.options.zoom);

  //this.map.setUIToDefault();
  this.map.addControl(new GLargeMapControl());

  this.addPlaces(places);

  $(window).unload(GUnload);
};

PSSHMap.prototype = {
  DEFAULT_OPTIONS : {
    lat  :    47.9267,
    lon  :  -122.6333,
    zoom :          8
  },

  addPlaces: function(places) {
    for (var i = 0; i < places.length; i++) {
      this.addPlace(places[i], i);
    }
  },

  addPlace: function(place, pos) {
    if (place.lat && place.lon) {
      this.addPoint(place, new GLatLng(place.lat, place.lon));
    } else {
      var self = this;

      var addIt = function(point) {
        self.addPoint(place, point);
      }
      var geocodePlace = function() {
        self.geocoder.getLatLng(place.address, placePoint);
      };

      setTimeout(geocodePlace, pos * 300);
    }
  },

  addPoint: function(place, point) {
    if (!point) {
      //console.log(place.address + " not found!");
      return;
    }

    var marker = new GMarker(point);

    var self = this;

    if (place.detail_url) {
      GEvent.addListener(marker, 'click', function() {
        window.open(place.detail_url, '_self');
      });
    }
	else
	{
		GEvent.addListener(marker, 'click', function() {
		  if (marker.openExtInfoWindow) {
			marker.openExtInfoWindow(
			  self.map, "jurisdiction_label", place.marker_html
			); 
		  } else {
			marker.openInfoWindowHtml(place.marker_html, {maxWidth:200});
		  }
		});
	}

    this.map.addOverlay(marker);
  }
};
