﻿// Custom javascript for Craft-Bilt Betterliving CMS

//Utility function to preload images
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

jQuery.preloadImageArr = function(imgArr)
{
	for(i = 0; i<imgArr.length; i++)
	{
		jQuery("<img>").attr("src", imgArr[i]);
	}
}

function reposition_logolink() {
	offLeft = $('#logo').offset().left;
	offLeft += 20;
	$('#logo_click').css({ left: offLeft+'px' });
}

//top nav
function topInit() {
	$('#topNav li').not('.selected').hover(
		function() { $('img',this).attr('src', $('img',this).attr('src').replace('.gif','-s.gif')); },
		function() { $('img',this).attr('src', $('img',this).attr('src').replace('-s.gif','.gif')); }
	);
	
	//preload topnav rollover images
	var imgStr = "";
	$('img', '#topNav').each( function() {
		imgStr += ',' + $(this).attr('src').replace('.gif', '-s.gif');
	} );
	
	if(imgStr.length > 0) {
		imgArr = imgStr.substr(1).split(',');
		
		$.preloadImageArr(imgArr);
	}
}

//image rollovers
jQuery.fn.swapImg = function(imgDiff) {
	return $(this).each( function() {
		var imgSrc = $(this).attr('src');
		var imgExt = imgSrc.substr(imgSrc.lastIndexOf('.'));
		
		$(this).hover(
			function() { 
				$(this).attr('src', imgSrc.replace(imgExt, imgDiff + imgExt));
			},
			function() {
				$(this).attr('src', imgSrc.replace(imgDiff + imgExt, imgExt));
			}
		);
		
		//do image preload
		$.preloadImages(imgSrc, imgSrc.replace(imgExt, imgDiff + imgExt));
	} );
}

//expanding/collapsing list
function getExpList() {
	expList = $('ul.expList');
	$('li', expList).each( function() {
		$('a[href*=expandCollapse]', this)
			.wrap('<div class="lnkDiv"></div>')
			.click(function() {
				expDiv = $(this).parent().siblings('.expDiv');
				$(expDiv).toggle('slow');
				return false;
			} );	
		$('p', this).wrapAll('<div class="expDiv"></div>');
	} );
}

function expandCollapse(number) {
	me = $('div#subheadingLink' + number);
	expDiv = $('.expDiv', me.parent());
	
	$(expDiv).toggle('slow');
}

/* javascript for doing dynamic image-replacement */
function imageParms(sText, sForeColor, sBackgroundColor, sWidth, sAlign, sFontSize, sFont, sHasLink, sLink) {
	this.text = sText;
	this.foreColor = sForeColor;
	this.backColor = sBackgroundColor;
	this.width = sWidth;
	this.align = sAlign;
	this.fontSize = sFontSize;
	this.font = sFont;
	this.hasLink = sHasLink;
	this.link = sLink;
	
	this.query = "t=" + escape(sText) + "&w=" + sWidth + "&fc=" + urlEncode(sForeColor)
		+ "&bc=" + urlEncode(sBackgroundColor) + "&fs=" + sFontSize + "&fn=" + urlEncode(sFont) + "&align=" + sAlign;
}

function urlEncode (sText) {
	sText = sText.replace(/%\d{0}/g, '%25');	
	sText = sText.replace(/\+/g, '%2B');		
	sText = sText.replace(/&amp;/g, '%26');
	sText = sText.replace(/&\w{0}#{0}/g, '%26');
	sText = sText.replace(/\"/g, '%22');
	sText = sText.replace(/\#/g, '%23');

    return sText;
}

function replaceElements() {
	
	replaceHeadings();
	replaceSubheadings();
	replaceOtherHeadings();
}

function replaceHeadings() {
	$('h2.pageheading', 'div#pageTitle').each( function(i) {
		title = $(this).text().toUpperCase();
		parms = new imageParms(title, "#003C5A", "transparent", 688, "left", 32, "/includes/eagle_book.ttf");
		bgImage = '/images/heading_bg.gif';
		parms.query += "&bgimage=" + urlEncode(bgImage) + "&height=" + 47 + "&marginTop=" + 8 + "&marginLeft=" + 15;		
		if(title.length)
			loadImage($(this), parms);
	} );
	$('h2.pageheading', 'div.pageSubTitle').each( function(i) {
		title = $(this).text().toUpperCase();
		parms = new imageParms(title, "#003C5A", "transparent", 688, "left", 32, "/includes/eagle_book.ttf");
		bgImage = '/images/subheading_bg.gif';
		parms.query += "&bgimage=" + urlEncode(bgImage) + "&height=" + 47 + "&marginTop=" + 8 + "&marginLeft=" + 15;		
		loadImage($(this), parms);
	} );
	
}

function replaceSubheadings() {
	$('h3.subheading').each( function(i) {
		title = $(this).text().toUpperCase();
		width = 630;
		if($(this).width() > 0) { width = $(this).width(); }
		
		if($(this).attr('class').indexOf('shorter') > -1) { width = 450; }
		
		parms = new imageParms(title, "#0071BB", "#fff", width, "left", 18, "/includes/avenir_roman.ttf");
		
		if(title.length)
			loadImage($(this), parms);
	} );
}

function replaceOtherHeadings() {
	$('h4.subheading').each( function(i) {
		title = $(this).text();
		width = 630;
		
		if($(this).width() > 0) { width = $(this).width(); }
		
		parms = new imageParms(title, "#0071BB", "#fff", width, "left", 15, "/includes/avenir_roman.ttf");
		
		if(title.length)
			loadImage($(this), parms);
	} );
	$('a', 'div.subheading').add('a.listitemheading', 'div.lnkDiv').each( function(i) {
		title = $(this).text().toUpperCase();
		width = 275;
		fontSize = 16;
		
		if($(this).attr('class') && $(this).attr('class').indexOf('listitemheading') > -1) {
			width = 648;
			fontSize = 14;
		}
		
		parms = new imageParms(title, "#0071BB", "#fff", width, "left", fontSize, "/includes/avenir_roman.ttf", true, $(this).attr('href'));
		
		if(title.length)
			loadImage($(this), parms);
	} );
	$('span.listitemheading').each( function(i) {
		title = $(this).text();
		parms = new imageParms(title, "#0071BB", "#fff", 630, "left", 14, "/includes/avenir_roman.ttf");
		
		if(title.length)
			loadImage($(this), parms);
	} );
}

function replaceInvalidChars(input) {
	invalid = new Array('\\/', '\\\\', '\\:', '\\?', '\\*', '\\"', "\\'", '\\<', '\\>', '\\|');
	for(x in invalid) {
		rx = new RegExp(invalid[x], 'g');
		input = input.replace(rx, '');
	}
	
	return input.replace(/ /g, "_");
}

function loadImage(elem, parms) {
	
	var imgUrl = "/ImageHandler.ashx?" + parms.query;
	imgName = location.pathname.lastIndexOf('/') > 0 ? location.pathname.substr(location.pathname.lastIndexOf('/') + 1) : 'Default.aspx';
	imgName += "-" + $(elem).get(0).tagName + "-" + replaceInvalidChars(parms.text) + ".gif";
	imgUrl += "&imgName=" + imgName.replace("&","");
	
	if(parms.hasLink) {
		$(elem).replaceWith("<a href=\"" + parms.link + "\"><img src='" + imgUrl + "' alt='" + parms.text + "' /></a>");
	}
	else {
		if($(elem).is('h3')) {
			$(elem).empty();
			$(elem).append("<img src='" + imgUrl + "' alt='" + parms.text + "' />");
		}
		else {
			$(elem).replaceWith("<img src='" + imgUrl + "' alt='" + parms.text + "' />");
		}
	}
}

//utility function to load swf objects (namely for flash-text replacement)
function loadSwfObject(elem, swfFile, parms, parentId, width, height) {
	var so = new SWFObject(swfFile + parms, parentId + '_flash', width, height, '7', '#ffffff');
	so.addParam('wmode', 'transparent');
	
	$(elem).wrap('<div id="' + parentId + '"></div>');
	
	so.write(parentId);
	
	return $('div#'+parentId);
}
