/*
	functions.dom.js
	DOM Interactivity Functions
	Created: 1.26.08
	Creator: Matt Kircher, IDa Creative
*/

function translateEmails(){
	$('span.email').each(function(){
		var spt = $(this);
		var at = / at /;
		var dot = / dot /g;
		var addr = $(spt).text().replace(at,"@").replace(dot,".");
		
		var t = $(spt).attr('title');
		
		var subject = t.substring(t.indexOf('|')+1);
		fulladdr = ($.trim(subject) != "")?addr+'?subject='+subject:addr;
		
		var title = "Send an email";
		var altContent = t.substring(0, t.indexOf('|'));
		var content = ($.trim(altContent) != "")?altContent:addr
		
		$(spt).after('<a href="mailto:'+fulladdr+'" title="'+title+'">'+ content +'</a>')
		.hover(function(){window.status="Send an email!";}, function(){window.status="";});
		$(spt).remove();
	});
}

function setupPage(){
	
	//thumbnails
	$('.thumbnails a').lightBox();
	
	//bgs
	if($('#masthead-content[title="artists"]').length){ $('#content').css({ backgroundImage:'url(images/content_top_artists_bg.jpg)' }); }
	
	//Fix IE6 crap
	$(document).applyIE6FlickerFix();
	$('img[@src$=png]').pngfix();
	
	//index
	if($('.index').length){
		$('#flash-content').bind('click', function(){
			window.location.href = "home.php";
		});
	}
	
	//nav effects
	if($('#main-nav').length){
		$('#main-nav').append('<div id="main-nav-bg"></div>').find('div')
		.width($('#main-nav ul').width())
		.height($('#main-nav ul').height())
		.css('top', eval(-1*$('#main-nav ul').height())+'px')
		
		$('#main-nav div').css('opacity','0.25').slideDown(500, function(){
			$('#main-nav ul').fadeTo(500, 1, function(){
				loadFlashMasthead();
			});
		});
	}
	
	//arrowed link style
	$('.arrowed').append(' <b>&rsaquo;</b>');
	
	//preamble conditional style
	if($('#preamble p').length){
		$('#preamble').css({marginBottom:'15px', borderBottom:'1px solid #85582F'});
	}
	
	//preamble 1st header top-align
	if(BrowserDetect.browser != "Explorer"){
		$('.subpage #preamble').find(':header:eq(0)').css({marginTop:'0px !important', position:'relative', top:'-5px'});
	}
	
	//home-news
	$('#home-news div:nth-child(4n), #home-news div:last:not(:nth-child(4n))').addClass('end_news').next().css('clear','left');
	$('#home-news').append('<span class="clear_block"></span>');
	$('#home-news p')
	.css({opacity:'.5', cursor:'pointer'})
	.hover(
		function(){ $(this).animate( { opacity:'1' }, 250); },
		function(){ $(this).animate( { opacity:'.5' }, 250); }
	)
	.bind('click', function(){
		document.location.href="news.php";
	});
	
	//hours
	$('#hours_locations_listing hr:last').remove();
	
	//guest artist info
	$('#guest-artist-info hr:last').remove();
}

function loadFlashMasthead(){
	var so = new SWFObject("media/flash/mastheads/"+$('#masthead-content').attr('title')+".swf", "bg_masthead", "800", "290", "8", "#030303");
	so.addParam("quality", "high");
	so.addParam("allowScriptAccess", "sameDomain");
	so.addParam("scale", "noscale");
	so.addParam("wmode", "transparent");
	so.write("masthead");
	
	$('#masthead-content').attr('class', $('#masthead-content').attr('title'));
}

function setupHeights(){
	//setup content area heights
	$('#content').get(0).style.height = eval($('#main-content').get(0).offsetHeight)+"px";
	$('#sub-content').get(0).style.height = eval($('#content').get(0).offsetHeight)+"px";
		
	$(document).bind('resize', setupHeights);
}

/* DIRECTIONS */
function setupDirections(){
	if($('#directions').length){
		
		$('#directions_location_search options:eq(0)').select();
		$('#directions_from_search options:eq(0)').select();
		$('#directions div, #directions h3, #directions h4').not('#directions_warp').hide();
		
		if(BrowserDetect.browser != "Explorer"){ $('#directions div:not(#directions_warp)').css({ marginBottom:'30px;' }); }
		
		//print links
		$('#directions div:not(#directions_warp)').append('<p class="print_link"><a href="#">Print Directions</a></p>');
		$('#directions p.print_link').bind('click', function(){
			var p = $('<div></div>');
			$('<h1>Body Graphics</h1>').appendTo(p);
			$('#directions').clone().appendTo(p);
			$(p).find('#directions_warp, p.print_link').remove();
			$.jPrintArea($(p)); return false;
		});
		
		//onchanges
		$('#directions_location_search').bind('change', function(){
			var c = $(this).find('option:selected').attr('value');
			$('#directions div, #directions h3, #directions h4').not('#directions_warp').hide();
			
			var c2 = $('#directions_from_search').find('option:selected').attr('value');
			
			if($.trim(c2) == ""){
				$('#directions .'+c).show().children().show();
			} else {
				if($.trim(c) == ""){
					$('#directions h3').show();
					$('#directions div').each(function(){
						 if($(this).hasClass(c2)){
							 $(this).show().children().show();
						 }
					});
				} else {
					$('#directions h3.'+c).show();
					$('#directions .'+c).each(function(){
						 if($(this).hasClass(c2)){
							 $(this).show().children().show();
						 }
					});
				}
			}
		});		
		$('#directions_from_search').bind('change', function(){
			var c = $(this).find('option:selected').attr('value');
			$('#directions div, #directions h3, #directions h4').not('#directions_warp').hide();
			
			var c2 = $('#directions_location_search').find('option:selected').attr('value');
			
			if($.trim(c2) == ""){
				$('#directions h3').show();
				$('#directions .'+c).show().children().show();
			} else {
				
				if($.trim(c) == ""){
					$('#directions .'+c2).show().children().show();
				} else {
					$('#directions h3.'+c2).show();
					$('#directions .'+c).each(function(){
						 if($(this).hasClass(c2)){
							 $(this).show().children().show();
						 }
					});
				}
			}
		});
	}
}

/* FAQ */
function setupFaq(){
	if($('#faq').length){
		var idx = 1;
		
		$('#right-content').append('<div id="section-jump" class="module"><h4>Quick Jump</h4><ul></ul></div>');
		
		$('#faq h3').each(function(){
			var id = 'faq'+idx;
			$(this).attr('id', id);
			$('#section-jump ul').append('<li><a href="#'+id+'">'+$(this).text()+'</a></li>');
			idx++;
		});
		
		$('#faq ul').after('<p class="backtotop"><a href="#section-jump">Back to top</a></p><hr />');
		$('#faq hr:last').remove();
	}
}

/* AFTERCARE */
function setupAfterCare(){
	if($('#aftercare').length){
		var idx = 1;
		
		$('#right-content').append('<div id="section-jump" class="module"><h4>Quick Jump</h4><ul></ul></div>');
		
		$('#aftercare h4').each(function(){
			var id = 'aftercare'+idx;
			$(this).attr('id', id);
			$('#section-jump ul').append('<li><a href="#'+id+'">'+$(this).text()+'</a></li>');
			idx++;
		});
		
		$('#aftercare h4:gt(0)').before('<p class="backtotop"><a href="#section-jump">Back to top</a></p><hr />');
		$('#aftercare').append('<p class="backtotop"><a href="#section-jump">Back to top</a></p>');
	}
}

/* VISITING */
function setupVisiting(){
	if($('#visiting-requirements').length){
		var idx = 1;
		
		$('#right-content').append('<div id="section-jump" class="module"><h4>Info for:</h4><ul></ul></div>');
		
		$('#visiting-requirements h3').each(function(){
			var id = 'visit-req'+idx;
			$(this).attr('id', id);
			$('#section-jump ul').append('<li><a href="#'+id+'">'+$(this).text()+'</a></li>');
			idx++;
		});
		
		$('#visiting-requirements h3:gt(0)').before('<p class="backtotop"><a href="#section-jump">Back to top</a></p><hr />');
		$('#visiting-requirements').append('<p class="backtotop"><a href="#section-jump">Back to top</a></p>');
	}	
}

/* EVENTS */
function setupEvents(){
	if($('#events').length){
		var idx = 1;
		
		$('#right-content').append('<div id="section-jump" class="module"><h4>Events:</h4><ul></ul></div>');
		
		$('#events h3').each(function(){
			var id = 'event'+idx;
			$(this).attr('id', id);
			$('#section-jump ul').append('<li><a href="#'+id+'">'+$(this).text()+'</a></li>');
			idx++;
		});
	}	
}

//DOM loaded
$(document).ready(function(){
	BrowserDetect.init();
	setupPage();
});

