Cufon.replace('h1', {
	fontFamily: 'Copperplate Gothic Light'
});
Cufon.replace('h4', {
	fontFamily: 'Candara',
	fontStretch: '105%',
	letterSpacing: '-1px',
	hover: true
});
Cufon.replace('h5', {
	fontFamily: 'Candara',
	fontStretch: '95%',
	hover: true
});


function emailFocus(el) {
	if (el.value == 'Enter email address') {
		el.value='';
	}
	el.style.color = '#333333';
}
function emailBlur(el) {
	if (el.value == '') {
		el.value='Enter email address';
		el.style.color = '#888888';
	}
}


var request = null;
function signUp(fm) {
	if (request) {
		request.abort();
	}
	request = doGetRequest('/ajax/signup.php?email=' + encodeURIComponent(fm.email.value), signedUp);
	
	return false;
}
function signedUp(res) {
	if (res == 'ok') {
		alert('Thank you. You will now receive our newsletters with news and special offers.');
		document.fm.email.value = '';
		emailBlur(document.fm.email);
	} else if (res == 'invalid_email') {
		alert('That doesn\'t appear to be a valid email address, please check it and try again.');
	} else {
		alert('An error occurred, please try again.');
	}
}

function doGetRequest(url, callback) {
	var req;
		
	// Set up XMLHttpRequest object
	try {
		// Firefox, Opera 8.0+, Safari
		req = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				return true;
			}
		}
	}
	req.open("GET", url, true);
	if (callback) {
		req.onreadystatechange = function() {
			handleResponse(req, callback)
		};
	}
	req.send(null);
	
	return req;
}
function handleResponse(req, callback) {
	if (req.readyState != 4 || req.status != 200 ) {
		return;
	}
	callback(req.responseText);
}

String.prototype.trim = function (what) {
	if (typeof what == 'undefined') {
		return this.replace(/^\s*/,"").replace(/\s*$/,"");
	} else {
		var reg1 = new RegExp('^'+what+'*');
		var reg2 = new RegExp(what+'*$');
		return this.replace(reg1,"").replace(reg2,"");
	}
}
String.prototype.escapeHTML = function () {
	return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

function openShareWin(url) {
	var d = new Date();
	window.open(url, 'sharewin'+d.getTime(), 'width=755,height=400,resizable=yes,scrollbars=yes,toolbar=no,location=yes,directories=no,status=no,menubar=no,copyhistory=no');
}

function showShareMore() {
	document.getElementById('sharemore').style.display = 'block';
	document.getElementById('sharemore').focus();
}
function hideShareMore() {
	document.getElementById('sharemore').style.display = 'none';
}


var brokenIE = ($.browser.msie && parseInt($.browser.version) <= 6);
function addToBasket(fm, quickorder) {
	if (brokenIE) {
		return true;
	}
	
	if (!quickorder) {
		var prod = fm.prod.value;
		var opt = fm.opt.value;
		var qty = fm.qty.value;
		if (_admin == '1') {
			qty = parseFloat(qty);
		} else {
			qty = parseInt(qty);
		}
		if (prod == '') {
			alert('Please choose the design');
			return false;
		}
		if (isNaN(qty) || qty <= 0) {
			alert('Please enter the quantity');
			return false;
		}
		var data = {prod: prod, opt: opt, qty: qty};
	} else {
		var data = new Object();
		for (var i = 0; i < fm.length; i++) {
			data[fm[i].name] = fm[i].value;
			if (fm[i].name.substr(0,4) == 'qty_') {
				fm[i].value = '';
			}
		}
	}
	$.ajax({
		type: "POST",
		url: '/ajax/add_to_basket.php',
		data: data,
		success: addedToBasket,
		cache: false
	});
	return false;
}

function addedToBasket(res) {
	if (!res.match(/^var res = new Object\(\);/)) {
		alert('An error occurred, please try again.');
		return;
	}
	eval(res);
	if (res.err) {
		location.href = '/basket.php?add_x=1&prod=' + encodeURIComponent(res.prod) + '&opt=' + encodeURIComponent(res.opt) + '&qty=' + encodeURIComponent(res.qty);
	} else {
		$('#basketsum').remove();
		$('#logo').parent().after(res.basketsum);
		$('#minibasket').html(res.minibasket);
		$('#minibasketcont').show(600);
	}
}


function imgAlt2Tip() {
	$('img.alt2tip').mouseover(showTip).mouseout(hideTip);
}
function showTip(e) {
	var img = e.target;
	var a = $(img).parent();
	// Need to add wrapper?
	if (!a.parent().hasClass('imgwrap')) {
		a.wrap('<div class=imgwrap style="width:'+(img.width+2)+'px;" />');
	}
	
	var html = '<div class=imgtip><div class=innertip>'+img.alt.escapeHTML()+'</div></div>';
	a.before(html);
}
function hideTip(e) {
	$('div.imgtip').remove();
}
$(imgAlt2Tip);

function changeImage(imgid) {
	$('#imgchanger').remove();
	$('body').append('<iframe id=imgchanger style="position: fixed; top: 23px; left: 50%; width: 525px; height: 640px; margin-left: -275px; border: 3px solid #946474; background: #f1f6f6;" src="/?change='+imgid+'"></iframe>');
}


var debug_init = new Array();
/* Outputs a string in top left of browser window */
/* Optionally <line> specifies a line offset */
function debugOut(str, line) {
	line = (line == null) ? 0 : line;
	if (!debug_init[line]) {
		var el = document.createElement('div'); 
		el.id = 'debug_div_'+line;     
		with(el.style) {
			if (document.all) {
				position = 'absolute';
			} else {
				position = 'fixed';
			}
			top = (5+40*line)+'px';
			left = '5px';
			backgroundColor = '#FF0000';
			color = '#FFFFFF';
			padding = '5px';
			border = 'solid 1px #000000';
		}
		el.innerHTML = '&nbsp;';
		document.body.appendChild(el); 
		debug_init[line] = true;
	} else {
		var el = document.getElementById('debug_div_'+line);
	}
	el.innerHTML = str;
}

var debug_init2 = false;
/* Outputs a scrolling debug window in top left of browser window */
function debugOut2(str) {
	if (!debug_init2) {
		var el = document.createElement('div'); 
		el.id = 'debug_div';     
		with(el.style) {
			if (document.all) {
				position = 'absolute';
			} else {
				position = 'fixed';
			}
			top = '5px';
			left = '5px';
			height = '500px';
			overflow = 'auto';
			backgroundColor = '#FF0000';
			color = '#FFFFFF';
			padding = '5px';
			paddingRight = '25px';
			border = 'solid 1px #000000';
		}
		el.innerHTML = '&nbsp;';
		document.body.appendChild(el);
		debug_init2 = true;
	} else {
		var el = document.getElementById('debug_div');
	}
	if (el.innerHTML != '') {
		el.innerHTML += '<br>'+str;
	} else {
		el.innerHTML += str;
	}
	el.scrollTop = 5000;
}


/* Gallery stuff */
function showPic(el, caption) {
	document.getElementById('galleryzoom').src = el.href;
	document.getElementById('gallerycaption').innerHTML = caption.replace(/  /g, '&nbsp; ');
	return false;	
}
function moveUp(el) {
	var div = $(el).parent();
	var prev = div.prev();
	if (prev.attr('id') == 'edgallery') {
		var prev_html = prev.html();
		prev.html(div.html());
		div.html(prev_html);
		
	}
}
function moveDown(el) {
	var div = $(el).parent();
	var next = div.next();
	if ($(el).parent().next().attr('id') == 'edgallery') {
		var next_html = next.html();
		next.html(div.html());
		div.html(next_html);
	}
}
function removeItem(el) {
	$(el).parent().remove();
}

