/** Carousels **/
$('.carousel').nivoSlider({
	effect:	'random',
	slices: 10,
	animSpeed: 800,
	pauseTime: 5000,
	directionNav: true,
	directionNavHide: false,
	controlNav: false,
	pauseOnHover: true,
	manualAdvance: false, 
	captionOpacity: 0,
	afterChange: function(){
		$(this).find('.status').text(
			($(this).data('nivo:vars').currentSlide+1)
			+'/' + $(this).data('nivo:vars').totalSlides
		);
	}
}).find('.nivo-prevNav').after('<span class="status">1/'+$('.carousel img').length);

/** Directions **/
var dirDisplay;
var dirService;
var geocoder;
var map;
var GoodsMap = {
	store: 			'220 Main Street North, Kewanee, IL 61443',
	infoHTML: 		'<address id="info">200 Main Street North<br/>Kewanee, IL 61443<br/>309.852.5656</address>',
	fromDefault:	'City, State or Zip',
	initialize: 	(function(){
		dirService 	= new google.maps.DirectionsService();
		dirDisplay 	= new google.maps.DirectionsRenderer();
		geocoder	= new google.maps.Geocoder();
		geocoder.geocode({'address': this.store}, function(results, status){
			if(status != google.maps.GeocoderStatus.OK) return;
			map = new google.maps.Map(document.getElementById("map_canvas"), {
				zoom: 8,
				center: results[0].geometry.location,
				disableDefaultUI: true,								
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});
			var marker = new google.maps.Marker({
				map: map, 
				position: results[0].geometry.location
			});
			var infowindow = new google.maps.InfoWindow({
				content: GoodsMap.infoHTML
			});			
			infowindow.open(map,marker);		
		});
	})
};
$(function(){
	if(!$('body.directions').length) return;	
	$('#content h1').after('<div id="map"/>').next()
		.append('<div id="map_canvas"/>').find('#map_canvas')
		.append('<div class="loading">Map Loading</div>').parent()
		.append('<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&callback=GoodsMap.initialize"/>')
		.find('#start').val(GoodsMap.fromDefault).next().click(GoodsMap.initialize);
});


/** Media **/
vswf = HTPATH + '/style/swf/video_player.swf?video=';
aswf = HTPATH + '/style/swf/audio_player.swf?mp3=';
$('.media ul.video').each(function(){
	$(this).prev().wrap('<div/>').end().appendTo($(this).prev())
		.before('<div class="nav"/>')
		.parent().attr('class', 'media video')
			.before('<div id="vholder"></div>');
	for(i=1; i<=Math.ceil($(this).find('li').length / 3); i++)
		$(this).prev().append('<a>'+i+'</a>');
	$(this).prev().find('a').click(function(){
		idx = (parseInt($(this).text())-1) * 3;
		$(this).parent().next().find('li').hide();
		for(i=idx; i<idx+3; i++) 
			$(this).parent().next().find('li:eq('+i+')').fadeIn();
	}).filter(':first').click();	
	$(this).find('a').each(function(){
		$(this).attr('rel', $(this).attr('href')).removeAttr('href').click(function(){
			$('#aholder').empty().hide();
			$(this).closest('ul').find('a').removeAttr('class').filter(this).addClass('selected');
			$('#vholder').empty().append('<div id="vplayer"/>').show();
			$('#vplayer').flash({
				swf: vswf+$(this).attr('rel'),
				width: 286,
				height: 223,
				params: {menu: 'false', wmode: 'transparent'}
			});
		});
	});	
	$(this).find('a:first').click();
});
$('.media ul.audio').each(function(){
	$(this).prev().wrap('<div/>').end().appendTo($(this).prev())
		.before('<div class="nav"/>')
		.parent().attr('class', 'media audio')
			.before('<div id="aholder"></div>');
	for(i=1; i<=Math.ceil($(this).find('li').length / 6); i++)
		$(this).prev().append('<a>'+i+'</a>');
	$(this).prev().find('a').click(function(){
		idx = (parseInt($(this).text())-1) * 6;
		$(this).parent().next().find('li').hide();
		for(i=idx; i<idx+6; i++) 
			$(this).parent().next().find('li:eq('+i+')').fadeIn();
	}).filter(':first').click();
	$(this).find('a').each(function(){
		$(this).attr('rel', $(this).attr('href')).removeAttr('href').click(function(){
			$('#vholder').empty().hide();
			$(this).closest('ul').find('a').removeAttr('class').filter(this).addClass('selected');
			$('#aholder').empty().append('<div id="aplayer"/>').show();
			$('#aplayer').flash({
				swf: aswf+$(this).attr('rel'),
				width: 356,
				height: 94,
				params: {menu: 'false', wmode: 'transparent'}
			});			
		});
	});		
});
