jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", parseInt( $('body').width() - this.width() ) / parseInt(2+$('body').scrollLeft()) + "px");
    return this;
}

$(document).ready(function() {
/*convert content h1/h2 to header*/
var h1 = $('#pagecontent').find('h1:first').text();$('#banner').find('h1').text(h1);$('#pagecontent').find('h1:first').remove();	
$('div#nav ul li:has(ul)').each(function() {$(this).append('<span>&#9656;</span>');$(this).hover(function() {$(this).find('ul').show();$(this).css({'color':'#ffffff','background':'#016098'});},function() {$(this).find('ul').hide();$(this).css({'color':'#ffffff','background':'#cb0a1d'});});});
/*change text in search button*/
$('#searchsubmit').val('Go');	

/*gallery*/
$('td.main_thumb_holder').each(function() {
$('<p class="morepics">More product images <em>(click to enlarge)</em></p>').prependTo($(this));


});
/*image zoom*/
var pz = 1;
$('.prodzoom').each(function() {
//preload the images
var linkto = $(this).attr('href');
var alt = $(this).attr('alt');
var title = $(this).attr('title');
var newimg = "pz_"+pz;
$(this).attr('id','zi_'+pz);
$('<div class="gallpopup" id="'+newimg+'"><img src="'+linkto+'" alt="'+alt+'" /><p class="close">CLOSE X</p></div>').appendTo('body');	
pz++;	
//click zoom thingy function

$(this).click(function(e) {
e.preventDefault();
$('.gallpopup:visible').fadeOut(300);
var tid = $(this).attr('id');
var idnum = tid.match(/\d{1,3}/);
var punum = "pz_"+idnum;
$('#'+punum).center().fadeIn(300);
});
});

$('div.gallpopup').hover(function() {$(this).find('p.close').fadeIn(300);},function() {$(this).find('p.close').fadeOut(300);});
$('p.close').click(function() {
$(this).parents('.gallpopup').fadeOut(300);$(this).hide();	
	
});

/*colour chart*/
$('ul#colourchart > li').hover(function() {var bgimg = $(this).css('background-image'); var ctext = $(this).html(); $('div#colour_sample').css('background-image',bgimg); $('div#colour_sample > h1').css('opacity','0.5'); $('div#colour_sample > h1').html(ctext);}, function() {$('div#colour_sample').css('background-image','none'); $('div#colour_sample > h1').html(''); });//end function

/*all the clever shit now*/
$('<div id="container"><p class="fabclose">Click To Close</p></div>').appendTo('body');

$('div.entry').each(function() {$(this).find('ul li').each(function() { var t = $(this).text(); var pat = /(RAD[i]*[A-Z0-9\s]+)/; if (t.match(pat)) { var o = $.trim(RegExp.$1); var id = o.replace(/\s/g,'_'); var rep = new RegExp(o); $(this).html($(this).text().replace(rep,'<span class="loadData" id="'+id+'">'+o+'</span>')); } }); });

$(".loadData").click(function() {var linkto = $(this).attr('id'); $("#container").prepend('<div id="fabric"></div>') .children("#fabric").load("/rs2011/fabrics div#"+linkto, function() { /*replace the image url so the bastard loads*/ var s = $('#fabric').contents().find('img:first').attr('src'); var l = "http://dev.tndnet.co.uk/rs2011/"; var newsrc = l+s; $('#fabric').contents().find('img:first').attr('src',newsrc); $("#container").center().fadeIn(300);});});

$('#container').live('click',function() {
$(this).fadeOut(300,function() {$(this).find('#fabric').remove();});	
});

});
