//##############################################################################
//##  DOM																	  ##
//##############################################################################
$(document).ready( externalLinks );

/*###########################################
##  Dropdown Menus						   ##
###########################################*/
/* Options */
var __global_menuContainer = '.mainNav';
/* Controls */
$(document).ready(function() {
  if ($(__global_menuContainer).exists()) {
	var lis = $(__global_menuContainer).children('li').get();
    $.each(lis, function(){
      $(this).hover(
	    function () { //on
	      $(this).addClass("sfhover")
	    }, 
	    function () { //off
	      $(this).removeClass("sfhover")
	    }
	  );
    });
  }
});

/*###########################################
##  Legacy Transparency					   ##
###########################################*/
$(document).ready(function() { $('img[src$=png]').addClass('transparent'); });

/*###########################################
##  Cufón Init							   ##
###########################################*/
Cufon.replace('#pieces h2, #promo h2, #bodyColumns .left h3, #bodyColumns .left h4, #howdoi .header .description, #stayConnected h4, #audioBooks h3, #bottom h2, #pageTitle h2, #sidebar h2, #insideColumns .left .content h1, #insideColumns .left .content h2, #insideColumns .left .content h3, #insideColumns .left .content h4', { fontFamily: 'Frutiger LT Std', hover: true });
Cufon.replace('#whatshot h2, #werecommend h2, #recentarrivals h2, #bodyColumns .right h2, #supplements h2, #insideColumns #sidebar h3, .eventDate', { fontFamily: 'Bello Script', hover: true });
Cufon.replace('#bookSearch .left h2, #navigation h2', { hover: true, 
														separate: 'none',
													    fontFamily: 'Frutiger LT Std',
													    textShadow: '2px 2px rgba(0, 0, 0, 0.3)'
													  });

/*###########################################
##  Content Sliders						   ##
###########################################*/
/* Options */
var __global_sliderObject = '#slides';
var __global_pagerObject = '#sliderNav';
var __global_photoObject = '#photo';

/* Controls */
$(function() {
  if ($(__global_sliderObject).exists()) { //main promotion slider
    $(__global_sliderObject)
      .cycle({ 
        fx:		'fade', 
        timeout:	6000, //how long to wait between slides
	    speed:	600, //how long transition takes
	    pause:	1, //stop on mouseover
        pager:	__global_pagerObject
    });
  }
  //create automatic thumbnails
  $(this).createThumbnails();
});

/* Thumbnails */
$.fn.createThumbnails = function() { 
  var pages = $(__global_pagerObject).children('a').get(); //read pagination
  $.each(pages, function(){ //loop through pages
    var thumb = $(this).html();
    thumb = thumb.replace(/[^0-9]+/g,'');
    var src = $(__global_photoObject+thumb).clone(false).removeAttr("id"); //copy parent image and isolate for thumbnail preview
    $(this).html(src); //push out thumbnail source
  });
};

/*###########################################
##  Text Resize Init					   ##
###########################################*/
$(document).ready(function() {
	$("#textSize a").textresizer({
		target: ".resizeable",
		type: "css",
		sizes: [
            // Normal. Index 0
            { "font-size" : "100%" },
 
            // Larger. Index 1
            { "font-size" : "110%" },
 
            // Largest. Index 2
            { "font-size" : "120%" }
        ],
		selectedIndex: 0
	});
});

/*###########################################
##  Link Styling						   ##
###########################################*/
$(document).ready(function() {
  $('a[href$=pdf]').addClass('pdf'); //PDF
  $('a[href$=rss/]').addClass('rss'); //RSS
});

/*###########################################
##  Image Alignment						   ##
###########################################*/
$(document).ready(function() {
  $('img[align$=left]').addClass('alignLeft').removeAttr('align');
  $('img[align$=right]').addClass('alignRight').removeAttr('align');
});

//##############################################################################
//##############################################################################
//##  Google Maps Controls													  ##
//##############################################################################
(function($) {

	function meetingMap(lat,lng,detailUrl) {
		this.pt = new google.maps.LatLng(lat, lng);
		this.detailUrl = detailUrl;
		this.map = null;
	}
	meetingMap.getDirections = function(meeting, request, panel) {
		if (!meetingMap.dirService) meetingMap.dirService =  new google.maps.DirectionsService();
		var dir =  new google.maps.DirectionsRenderer();
		dir.setMap(meeting.map);
		dir.setPanel(panel);
		meetingMap.dirService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				dir.setDirections(response);
			}
		});
	}
	meetingMap.prototype = {
		init: function(elId,options) {
			var el = $(elId);
			el.data('map', this);
			var p = el.parent();
			this.map = new google.maps.Map(el.get(0),$.extend({
				zoom: 15,
				center: this.pt,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			  },options)
			);
			var inpId = el.attr('id')+'fromLoc';
			p.append($('<div class="getdir"><form><label for="'+inpId+'">Get directions from</label> <input type="text" id="'+inpId+'" maxlength="100" /> <button>Go</button></form></div>'));
			p.parent().append($('<div class="directions" />'));
			p.find('button').click(function(e) { e.preventDefault(); $(elId).data('map').getDirections($('#'+inpId).val(),$(elId).parent().parent().find('.directions').get(0)); return false; })
			p.find('form').submit(function() { p.find('button').click(); return false; });
			return this.addMarker();
		},
		addMarker: function() {
			this.marker = new google.maps.Marker({
				position: this.pt,
				title: 'Location',
				map: this.map,
				draggable: false
			});
			return this;			
		},
		getDirections: function(to, contId) {
			var req = { destination: this.pt, origin: to +',USA', travelMode: google.maps.DirectionsTravelMode.DRIVING };
			meetingMap.getDirections(this, req, contId);
			return this;
		}
	};
	window['meetingMap'] = meetingMap;
})(jQuery);

//##############################################################################
//##############################################################################
//##  User Handler Functions												  ##
//##############################################################################
function externalLinks() {
  var exs = $('a[rel=external]');
  $.each(exs, function() {
    if ($(this).attr('href')) { $(this).attr('target','_blank'); }
  });
}
//##############################################################################
//##############################################################################
//##  Globals																  ##
//##############################################################################
jQuery.fn.exists = function(){return jQuery(this).length>0;}
//##############################################################################
//##############################################################################