/***************************
 * SITE-SPECIFIC FUNCTIONS *
 ***************************/
 function embedSlideshow(id) {
	document.write('<div id="embed_' + id + '"style="margin-bottom: 15px; text-align:center"></div>');
	var so = new SWFObject("/flash/embedded_slideshow.swf", "slideshow_" + id, "360", "270", "8", "#FFFFFF");
	so.addVariable("id", id);
	so.addParam("wmode", "transparent");
	so.write("embed_" + id);
}

function embedSidebarSlideshow(id) {
	document.write('<div id="sidebarembed_' + id + '"style="margin-bottom: 15px;"></div>');
	var xmlfile = '/ssp_director/images.php?album='+id;
	var so = new SWFObject("/flash/embedded_slideshow_165.swf", "slideshow" + id, "165", "105", "8", "#FFFFFF");
	so.addVariable("xmlfile", xmlfile);
	so.addParam("wmode", "transparent");
	so.write("sidebarembed_" + id);
}

function embedLeftSidebarSlideshow(id) {
	document.write('<div id="sidebarembed_' + id + '"style="margin-bottom: 15px;"></div>');
	var xmlfile = '/ssp_director/images.php?album='+id;
	var so = new SWFObject("/flash/embedded_slideshow_155.swf", "slideshow" + id, "155", "99", "8", "#FFFFFF");
	so.addVariable("xmlfile", xmlfile);
	so.addParam("wmode", "transparent");
	so.write("sidebarembed_" + id);
}

function bindSlideshows() {
	$('a[rel*=slideshow]').click(function(evt){
		evt.preventDefault();
		var url = $(this).attr('href');
		if ($.browser.msie) {
			openWindow(url,'width=520,height=475,resizable=yes');
		} else {
			tb_show('Slideshow Viewer',url + '?TB-iframe=true&width=500&height=455');
		}
	});
}

function bindMediaPlayer() {
	$('a[rel*=video]').click(function(evt){
		evt.preventDefault();
		var url = $(this).attr('href');
		if ($.browser.msie) {
			openWindow(url,'width=520,height=320,resizable=yes');
		} else {
			tb_show('Media Player',url + '?TB-iframe=true&width=500&height=320');
		}
	});
}

function styleQuicklinks() {
	$('.quicklinks').prepend('<div><img src="/images/quicklinks.png" alt="Quick Links" width="155" height="25" /></div>');
	$('.quicklinks').append('<div class="separate"><img src="/images/quicklinks-bottom.png" alt="" width="155" height="9" style="vertical-align: bottom" /></div>');
}
 
/*********************
 * UTILITY FUNCTIONS *
 *********************/
 
function sendMail(user,domain,tld,subject,message) {
	if (!user) user = 'info';
	if (!domain) domain = 'habitatnashville';
	if (!tld) tld = 'org';
	locationstring = 'mailto:' + user + '@' + domain + '.' + tld;
	if (subject) locationstring += '?subject=' + encodeURIComponent(subject);
	if (message) locationstring += '&body=' + encodeURIComponent(message);
	window.location = locationstring;
}

function sendExternalMail(subject,message,cc) {
	locationstring = 'mailto:';
	if (subject) locationstring += "?subject=" + encodeURIComponent(subject);
	if (message) locationstring += "&body=" + encodeURIComponent(message);
	if (cc) locationstring += "&cc=" + encodeURIComponent(cc);
	window.location = locationstring;
}

function openWindow(url,features) {
	var newWin = window.open(url,'popup',features);
	newWin.focus();
}

function closeWindow() {
	if (window.opener) {
		self.close();	
	}
}

function printPage() {
	if (window.print != null) {
		window.print();
	}
	else {
		alert("To print this page, please select Print from your browser's File menu.");
	}
}

function preload() {
	var path = '/images/';
	var images = new Array('');
	var preload = new Array('');
	var j = images.length;
	for (var i=0; i<images.length; i++) {
		preload[j] = new Image;
		preload[j++].src = path + images[i];
	}
}

function externalLinks() {
	$('a[rel*=external]').attr('target','_blank');	
}

function makeExternalLinks(){
    $('a, area').filter(function(){
        return this.hostname && this.hostname != location.hostname;
    })
	 .attr({
		target: "_blank", 
		title: "Opens in a new window",
		rel: "external"
	})
	.not('a:has(img)')
	.addClass('external');
}

function filesToBlank() {
	$('a[href$=.pdf]:not(.sf-menu a),a[href$=.PDF]').attr('target','_blank').after('&nbsp;<img src="/images/pdf.png" alt="PDF" style="vertical-align:top" />');
	$('.sf-menu a[href$=.pdf]').attr('target','_blank').append('&nbsp;<img src="/images/pdf.png" alt="PDF" style="vertical-align:middle" />');
	$('a[href$=.doc],a[href$=.DOC]').attr('target','_blank');//.append(' [DOC]');
	$('a[href$=.ppt],a[href$=.PPT]').attr('target','_blank');//.append(' [PPT]');
	$('a[href$=.pps],a[href$=.PPS]').attr('target','_blank');//.append(' [PPS]');
}

function makeRollovers(){
	$('a.rollover').each(function() {
		var img = new Image();
		var upimage = $(this).children().eq(0).attr('src');
		img.src = upimage.replace('_up','_ov');
	});
	$('a.rollover').hover(
		function(){
			var overimage = $(this).children().eq(0).attr('src').replace('_up','_ov');
			$(this).children().eq(0).attr('src',overimage);
		},
		function(){
			var upimage = $(this).children().eq(0).attr('src').replace('_ov','_up');
			$(this).children().eq(0).attr('src',upimage);
		}
	);
}

function actionLinks() {
	$('a.action').after('&nbsp;&#187;');
	$('a.return').before('&#171; ');
}

function styleBlockquotes() {
	$('blockquote .cite').prev().append('<img src="/images/close-quote.png" style="position:absolute; vertical-align:top; margin-left: 2px;" />');
}

function init() {
	makeExternalLinks();
	filesToBlank();
	makeRollovers();
	$('.sf-menu').superfish();
	$('.sf-menu li li').css('opacity','0.95');
	$('a[href^=javascript:printPage]').addClass('print');
	actionLinks();
	bindSlideshows();
	bindMediaPlayer();
	styleQuicklinks();
	styleBlockquotes();
}
