/* ---------------------------------------------------------
	more accessible "hover" function
	<http://groups.google.com/group/jquery-en/browse_thread/thread/3042924a08d3fa53>
--------------------------------------------------------- */

jQuery.fn.extend({ 
	hover: function(fnOver, fnOut) { 
		return this.bind('mouseover focus', fnOver).bind('mouseout blur', fnOut); 
	}
});



/* ---------------------------------------------------------
	drop-down navigation
--------------------------------------------------------- */

$(document).ready(function(){
	
	// position div now that JS is active and hide it
	$('#nav-main li ul').hide();
	$('#nav-main li ul').css({ 'left':'auto' });

	// when link is hovered over…
	$('#nav-main li').hoverIntent({
		sensitivity: 1,
		interval: 100,
		over: showNav,
		timeout: 300,
		out: hideNav
	});
});

function showNav() { $(this).find('ul').animate({height: 'show', opacity: 'show'}, 300);}
function hideNav() { $(this).find('ul').animate({height: 'hide', opacity: 'hide'}, 300);}

// popup the store window for now…
$( document ).ready( function() {
	$('#nav-store a').click(function() {
		var features = 'width=850,resizable=1,scrollbars=1,location=0';
		newwindow=window.open(this.href, 'Modern West merchandise', features);
		return false;
	});
});



/* ---------------------------------------------------------
	video players
--------------------------------------------------------- */

// home page
$(document).ready(function(){
	if ($('#home #video-player').length){
		var $this = $('#home #video-player');
		var file = $this.find('a').attr('href');
		var image = $this.find('img').attr('src');
		
		var flashvars = {
			file: file,
			skin: '/themes/site_themes/default/flash/modieus.swf',
			image: image,
			id: 'flv-player'
		};
		var params = {
			allowscriptaccess: 'always',
			bgcolor: '#000000',
			wmode: 'transparent'
		};
		var attributes = {
			id: 'flv-player',
			name: 'flv-player'
		};

		swfobject.embedSWF('/themes/site_themes/default/flash/jwplayer.swf', 'player-flv', '345', '225', '9.0.0', false, flashvars, params, attributes);
	}
});


// video page
$(document).ready(function(){
	if ($('#videos #video-player').length){
		var $this = $('#videos #video-player');
		var file = $this.find('a').attr('href');
		var image = $this.find('img').attr('src');
		
		var flashvars = {
			file: file,
			skin: '/themes/site_themes/default/flash/modieus.swf',
			image: image,
			id: 'flv-player'
		};
		var params = {
			allowscriptaccess: 'always',
			bgcolor: '#000000',
			wmode: 'transparent'
		};
		var attributes = {
			id: 'flv-player',
			name: 'flv-player'
		};

		swfobject.embedSWF('/themes/site_themes/default/flash/jwplayer.swf', 'player-flv', '580', '358', '9.0.0', false, flashvars, params, attributes);
	}
});



/* ---------------------------------------------------------
	music player
--------------------------------------------------------- */

// home page
$(document).ready(function(){
	if ($('#home #music-player').length){
		var $this = $('#home #music-player');
		var file = $this.find('a').attr('href');
		var title = $this.text();
		
		var flashvars = {
			file: file,
			skin: '/themes/site_themes/default/flash/modieus.swf',
			id: 'mp3-player'
		};
		var params = {
			allowscriptaccess: 'always',
			bgcolor: '#000000',
			wmode: 'transparent'
		};
		var attributes = {
			id: 'mp3-player',
			name: 'mp3-player'
		};

		swfobject.embedSWF('/themes/site_themes/default/flash/jwplayer.swf', 'player-mp3', '345', '30', '9.0.0', false, flashvars, params, attributes);
		$this.append('<p class="caption"><span>Now Playing:</span> ' + title + '</p>');
	}
});


// music page
$(document).ready(function(){
	if ($('#mp3s #music-player').length) {
		var $this = $('#mp3s #music-player');
		var file = $this.find('a').attr('href');

		var flashvars = {
			file: file,
			skin: '/themes/site_themes/default/flash/modieus.swf',
			id: 'mp3-player'
		};
		var params = {
			allowscriptaccess: 'always',
			bgcolor: '#000000',
			wmode: 'transparent'
		};
		var attributes = {
			id: 'mp3-player',
			name: 'mp3-player'
		};
			
		swfobject.embedSWF('/themes/site_themes/default/flash/jwplayer.swf', 'player-mp3', '345', '30', '9.0.0', false, flashvars, params, attributes);
	}
});



/* ---------------------------------------------------------
	mp3 playlist
--------------------------------------------------------- */

$(document).ready(function(){

	// hide purchase links
	$('#playlist td.mp3-options').hide();
	$('#playlist .mp3-info').hide();

	// show info
	$('#playlist .mp3-extras a.toggle').click(function(){
		$(this).parent().parent().find('.mp3-info').toggle();
		return false;
	});
	
	$('#playlist td.mp3-title a').click(function(){
		
		// get new mp3 info
		var new_src = $(this).attr('href');
		var new_title = $(this).text();
		var new_download = $(this).parent().parent().find('.mp3-options .download').attr('href');
		var new_tunes = $(this).parent().parent().find('.mp3-options .itunes').attr('href');
		var new_amazon = $(this).parent().parent().find('.mp3-options .amazon').attr('href');

		var player = document.getElementById('mp3-player');

		// load and play new mp3
		player.sendEvent('LOAD', { file: new_src, type: 'sound' } );
		player.sendEvent('PLAY');
		
		// swap title and load new purchase buttons
		$('#current-mp3 h3').text(new_title);
		$('#current-mp3 .mp3-options').empty();
		if (new_download) {
			$('#current-mp3 .mp3-options').append('<a class="download" href="' + new_download + '"><img src="/themes/site_themes/default/images/buttons/download.png" width="125" height="20" alt="download" /></a>');
		}
		if (new_tunes) {
			$('#current-mp3 .mp3-options').append('<a class="itunes" href="' + new_tunes + '"><img src="/themes/site_themes/default/images/buttons/itunes.png" width="92" height="20" alt="itunes" /></a>');
		}
		if (new_amazon) {
			$('#current-mp3 .mp3-options').append('<a class="amazon" href="' + new_amazon + '"><img src="/themes/site_themes/default/images/buttons/amazon.png" width="103" height="20" alt="amazon" /></a>');
		}

		return false;
	});
});



/* ---------------------------------------------------------
	mailing list
--------------------------------------------------------- */

$(document).ready(function(){
	
	// preload wait.gif
	var wait_img = $('<img />').attr({
		className: 'wait',
		src: '/themes/site_themes/default/images/wait.gif',
		width: '20',
		height: '20',
		alt: 'submitting form…'
	});

	// hide submit button
	$('#mailing-list input[type=submit]').hide();
	
	// hell, hide the Search submit button too while we're at it
	$('#search input[type=submit]').hide();
	
	// bind ajaxForm to mailing list
	$('#mailinglist_form').ajaxForm( {
		target: '.form-response',
		beforeSubmit: function() {
			
			// simple form validatation
			var e = $('#mailinglist_form input[type="text"]');
			
			// if email is submitted empty
			if (e.val() == '' || e.val() == e.attr('title')) {
				e.focus();
				e.parent().next('.form-status').html('<p class="required">Please enter a valid email address.</p>');
				return false;
			}
		
			// check email address
			str = e.val();
			var s = $.trim(str);
			var at = "@";
			var dot = ".";
			var lat = s.indexOf(at);
			var lstr = s.length;
			var ldot = s.indexOf(dot);

			if (s.indexOf(at)==-1 ||
				(s.indexOf(at)==-1 || s.indexOf(at)==0 || s.indexOf(at)==lstr) ||
				(s.indexOf(dot)==-1 || s.indexOf(dot)==0 || s.indexOf(dot)==lstr) ||
				(s.indexOf(at,(lat+1))!=-1) ||
				(s.substring(lat-1,lat)==dot || s.substring(lat+1,lat+2)==dot) ||
				(s.indexOf(dot,(lat+2))==-1) ||
				(s.indexOf(" ")!=-1)) {

				e.focus();
				e.parent().next('.form-status').html('<p class="required">Please enter a valid email address.</p>');
				return false;
			}
			
			// send form
			$('#mailinglist_form input').hide();
			$('#mailinglist_form .form-status').html(wait_img);
		},
		success:function() {
			var $this = $('#mailinglist_form .form-status');
			$this.hide();
			$this.html('<p>Thank you! You’ll receive a confirmation email shortly.</p>');
			$this.fadeIn(500).delay(5000,function(){
				$('#mailing-list').fadeOut(500);
			});
		}
	});
});



/* ---------------------------------------------------------
	form input default text
--------------------------------------------------------- */

$(document).ready(function() {
	$('input[type=text],textarea').each(function(){
		var input = $(this);
		var title = input.attr('title');
		input.addClass('blur').attr('value',title);
		input.focus(function() {
			if (input.attr('value') == title) {
				$(this).attr('value','');
			}
			$(this).removeClass('blur');
		});
		input.blur(function() {
			if (input.attr('value') == '') {
				$(this).attr('value',title)
					.addClass('blur')
					
					// reset any form warnings
					.parent().next('.form-status').html('');
			}
		});
	});
})



/* ---------------------------------------------------------
	comment form
--------------------------------------------------------- */

$(document).ready(function(){

	// preload wait.gif
	var wait_img = $('<img />').attr({
		className: 'wait',
		src: '/themes/site_themes/default/images/wait.gif',
		width: '20',
		height: '20',
		alt: 'submitting form…'
	});
	
	// set up comment form
	$('#comment_form').hide();
	$('#comment_form').before( '<p id="comment-toggle"><span><a href="#">Leave a Comment</a></span></p>' );
	
	// toggle comment form
	$('#comment-toggle a').click(function(){
		if($(this).parent().attr('class') == 'toggled') {
			$('#comment_form').slideUp(500);
			$(this).html( 'Leave a Comment' );
			$(this).parent().removeClass('toggled');
			return false;
		} else {
			$('#comment_form').slideDown(500);
			$(this).html( 'Hide comment form' );
			$(this).parent().addClass('toggled');
			return false;
		}
	});
	
	// check URL
	var url = jQuery.url.attr('anchor');
	if (url == 'comment-toggle') {
		$('#comment_form').show();
		$('#comment-toggle a').html( 'Hide comment form' );
		$('#comment-toggle a').parent().addClass('toggled');
	}
	
	// comment administration
	$('.comment-admin a').click(function(){
		if ($(this).hasClass('closelink')) {
			if (!confirm('Are you sure you want to close this comment?')) return false;
		} else if ($(this).hasClass('deletelink')) {
			if (!confirm('Are you sure you want to delete this comment?')) return false;
		}
		var href = $(this).attr('href');
		$(this).parent().load(href);
		return false;
	});

	// show hidden preview div
	$('#comment-preview').show();

	// preview text
	$('#comment-message').keyup(function(){
		var message_text = $('#comment-message').val();

		message_text = message_text.replace(/\n/g, '<br />'); 
		$('#comment-preview div').html( message_text );
	});

	// reply button	
	$('.comment-reply').click(function() {
		var author = $(this).parents('div.comment div').find('.comment-name span').text();
		var authlink = $(this).parents('div.comment div').find('.comment-date').attr('href');
		var text = $('textarea#comment-message').attr('value');
		if ($('#comment_form:hidden').length == 1) {
			$('#comment-toggle a').trigger('click');
		}
		$('textarea#comment-message').focus().attr('value',text+'<a href="'+authlink+'">@'+author+'</a>: ');
	});
	
	// validate comment form
	$('#comment_form').submit(function() {
	
		// clear warnings
		$(this).find('input,textarea').next('.required').remove();
	
		// if fields are empty
		$(this).find('#comment-name, #comment-email, #comment-message').each(function(){
			if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				if ($(this).attr('id') == 'comment-name') {
					$(this).after('<p class="required">Please enter your name.</p>');
				}
				if ($(this).attr('id') == 'comment-email') {
					$(this).after('<p class="required">Please enter an email address.</p>');
				}
				if ($(this).attr('id') == 'comment-message') {
					$(this).after('<p class="required">Please enter your comment.</p>');
				}
				
			}
		});
		
		// check email address
		e = $(this).find('#comment-email');
		str = e.val();
		if (str != '') {
			var s = $.trim(str);
			var at = "@";
			var dot = ".";
			var lat = s.indexOf(at);
			var lstr = s.length;
			var ldot = s.indexOf(dot);
	
			if (s.indexOf(at)==-1 ||
				(s.indexOf(at)==-1 || s.indexOf(at)==0 || s.indexOf(at)==lstr) ||
				(s.indexOf(dot)==-1 || s.indexOf(dot)==0 || s.indexOf(dot)==lstr) ||
				(s.indexOf(at,(lat+1))!=-1) ||
				(s.substring(lat-1,lat)==dot || s.substring(lat+1,lat+2)==dot) ||
				(s.indexOf(dot,(lat+2))==-1) ||
				(s.indexOf(" ")!=-1)) {
	
				e.after('<p class="required">Please enter a valid email address.</p>');
			}
		}
		
		// check for warnings
		if ($(this).find('input,textarea').next('.required').length > 0) {
			return false;
		} else {
			$(this).find('input[type=image]').replaceWith(wait_img);
		}
    });	
});



/* ---------------------------------------------------------
	video/photo gallery scrolling
--------------------------------------------------------- */

$(document).ready(function(){

	// remove empty containers
	$('#album-index .thumbnails').each(function() { 
		if($(this).children().length < 1) $(this).remove() 
	});

	// count videos
	var album_pos = 1;
	var album_count = $('#album-index .thumbnails').size();
	
	if (album_count > 1) {

		// html/css
		$('#album-index')
			.wrapInner('<div id="album-container"></div>')
			.wrapInner('<div id="album-wrapper"></div>');

		$('#album-index .thumbnails').css({ 'width':'580px', 'float':'left', 'margin-right':'1.8em' });
		
		// show navigation
		$('#photo-albums #album-index').append('<p id="pagination"><a class="prev" href="#">« Previous</a>  <a class="next" href="#">Next »</a></p>');
		$('#photo-album #album-index').append('<p id="pagination"><a class="prev" href="#">« Newer Photos</a>  <a class="next" href="#">Older Photos »</a></p>');
		$('#video-archive #album-index').append('<p id="pagination"><a class="prev" href="#">« Newer Videos</a>  <a class="next" href="#">Older Videos »</a></p>');
		
		
		$('#album-index #pagination .prev').hide();
		
		// serialScroll plugin
		$('#album-wrapper').serialScroll({
			items:'div.thumbnails',
			prev:'#album-index #pagination .prev',
			next:'#album-index #pagination .next',
			offset:0,
			step:1,
			start:0,
			duration:700,
			force:false,
			stop:true,
			lock:true,
			cycle:false,
			
			// adjust nav
			onAfter:function(){
				if (album_pos >= album_count) {
					$('#album-index #pagination .next').hide();
				} else {
					$('#album-index #pagination .next').show();
				}
				
				if (album_pos <= 1) {
					$('#album-index #pagination .prev').hide();
				} else {
					$('#album-index #pagination .prev').show();
				}
			}
		});
		
		$('#album-index #pagination .next').click(function(){
			album_pos = album_pos + 1;
			if (album_pos >= album_count) { album_pos = album_count; }
		});
		$('#album-index #pagination .prev').click(function(){
			album_pos = album_pos - 1;
			if (album_pos <= 1) { album_pos = 1; }
		});
	}
});



/* ---------------------------------------------------------
	photo pagination
--------------------------------------------------------- */

$(document).ready(function(){
	
	// grab links and place over photo
	var links = $('#photos #pagination').not('#photo-albums #pagination, #photo-album #pagination').html();
	$('#photos #pagination').not('#photo-albums #pagination, #photo-album #pagination').remove();
	$('#photos div.photo').append(links);
	
	// set height and width of img container
	var width = $('#photos div.photo img').width() + 6;
	var height = $('#photos div.photo img').height() + 6;
	$('#photos div.photo').css({'width' : width, 'height' : height});
	
	// set link width and height
	var link_width = width / 2;
	var link_height = height - 6;
	$('#photos div.photo a').css({'width' : link_width, 'height' : link_height});
	
	// hide links and show when hovered over
	$('#photos div.photo a').css('opacity','0').hover(
		function () {
			if (jQuery.support.opacity == true) {
				$(this).animate({opacity: '1'}, 300);
			} else {
				$(this).animate({opacity: '.7'}, 300);
			}
			
		}, 
		function () {
			$(this).animate({opacity: '0'}, 300);
		}
	);
});



/* ---------------------------------------------------------
	hoverIntent
	<http://cherne.net/brian/resources/jquery.hoverIntent.html>
--------------------------------------------------------- */

(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);



/* ---------------------------------------------------------
	delayHTTPoverhead
	<http://www.wait-till-i.com/index.php?p=465>
--------------------------------------------------------- */

delayHTTPoverhead = function(){
	var parentID = '';
	var avtClass = '';
	function ri(){var img=parentID!==''?document.getElementById(parentID).getElementsByTagName('img'):document.getElementsByTagName('img');var rep=/.*#/;for(var i=0;img[i];i++){var src=img[i].src;if(src.indexOf('#')!=-1&&img[i].className.indexOf(avtClass)!==-1){img[i].src = src.replace(rep,'');};};}var o=window.onload;if(typeof window.onload!='function'){window.onload=ri;}else{window.onload = function(){if(o){o();};ri();};};
}();



/* ---------------------------------------------------------
	google analytics
--------------------------------------------------------- */

$.gaTracker(
	'UA-7965580-8',
	{
		external:	'/external/',
		mailto:		'/mailto/',
		download:	'/downloads/',
		extensions:
		[
			'pdf','doc','xls','csv','jpg','gif', 'mp3',
			'swf','txt','ppt','zip','gz','dmg','xml'		
		]
	}
);