function loadPlugins(el){
    
	// share widget
	$('.share_popup', el).each(function(){
		var a = $(this);

		// TODO: get/post instead of load, or dynamically insert page url (less flexible for multiple share buttons on same page...)
		// TODO: adjust left/right depending on space available -- not needed if we keep link to left of container

		a.click(function(){
			if( a.next('.popup').length ){
				// popup exists
				if( a.next('.popup').children('.popup_bdr').is(':visible') ){
					// popup already shown, hide it
					a.next('.popup').hide();
				} else {
					// popup hidden, show it
					a.next('.popup').fadeIn();
				}
			} else {
				// popup doesn't exist, create and load it
				var href = a.attr('href'),
					left = a.position().left,
					top = a.position().top,
					height = a.outerHeight(),
					close = $('<a></a>').addClass('btn_close').text('Close'),
					popup = $('<div></div>').addClass('popup').css({
								position:"absolute",
								left:left,
								top:top
							}).hide().insertAfter(a).load(href, function(){
								$(this).fadeIn().children('.popup_bdr').append(close);
								$('form', this).live("submit", function(){
									var data = $(this).serialize();
									$.ajax({
										data: data, 
										url: $(this).attr('action'),
										type: 'post',
										success: function(data){
											popup.closest('.popup').html(data).children('.popup_bdr').append(close);
											var textareaVal = popup.find('textarea').val();
											loadPlugins(popup);
										}
									});
									return false;
								});
								loadPlugins($(this));
							});
					$('a.btn_close').live("click", function(){
						$(this).closest('.popup').hide();
						return false;
					})
			}
			return false;
		});
		
	});
	
	// tabbed content
	$('.tabs_wrap', el).each(function(){

		// init
		var tabNav = $('.tab_nav', this),
			tabs = $('.tabs', this),
			tabsWidth = tabs.outerWidth(),
			tabsCount = tabs.find('.tab').length,
			tabsScroll = $('.tabs_scroll', tabs),
			current = $('.current', tabsScroll),
			startHeight = current.outerHeight();

		tabs.css({
			position:"relative",
			width:tabsWidth,
			height:startHeight,
			overflow:"hidden"
		});
		tabsScroll.css({
			width:parseInt(tabsCount*tabsWidth),
			position:"absolute",
			left:0,
			top:0
		}).find('.tab').css({
			float:"left",
			width:tabsWidth
		}).show();

		$('a', tabNav).click(function(){
			var target = $(this).attr('href'),
				targetEq = $(target).index(),
				offset = targetEq*tabsWidth,
				targetHeight = $(target).outerHeight();

			$(this).css({outline:"none"});
			tabs.animate({height:targetHeight}, 250);
			tabsScroll.animate({left:-offset}, 500);
			$('a', tabNav).removeClass('current');
			$(this).addClass('current');
			return false;

		});
		
		tabs.height(tabsScroll.outerHeight());

	});

	// collapsible dl
	$('.collapsible', el).each(function(){
		$('dd', this).hide();
		$('dt', this).css({cursor:"pointer"}).click(function(){
			if( $(this).next('dd').is(':visible') ){
				$(this).next('dd').slideUp(150);
			} else {
				$(this).siblings('dd').slideUp(150);
				$(this).next('dd').slideDown(150);
			}
		})
	});
	
	// rate it
	$('.rateit', el).rateit();
	$('.rateit', el).bind('rated', function (event, value) { $('#id_rating').val(value*2); });
	
	// nice scrollbars
	$('.scroll-pane', el).jScrollPane({showArrows: true, arrowButtonSpeed: 100, verticalDragMaxHeight: 23, horizontalDragMaxWidth: 23});
	
	// showcase
	$('#showcase', el).each(function(){
		if( $(this).children('#imgs').length ){
			var showcase = $(this),
				nav = $('ul', showcase),
				imgs = $('#imgs', showcase);

			nav.find(':first-child a').addClass('current');
			imgs.height(imgs.height()).find('img').css({position:"absolute", left:0, top:0, zIndex:10}).addClass('current');

			$('a', nav).click(function(){

				function fadeImg(img){
					img.siblings().animate({opacity:0}, 250).css({zIndex:1});
					img.css({opacity:0, zIndex:11}).animate({opacity:1}, 250, function(){
						$(this).css({zIndex:10});
					});
				}

				if( ! $(this).is('.current') ){
					$(this).css({outline:"none"});
					var src = $(this).attr('href'),
						thumb = $(this);
					thumb.addClass('current').parent('li').siblings('li').find('a').removeClass('current');

					// is image loaded?
					if( $('[src="' + src + '"]', imgs).length ){
						// loaded, fade it in
						fadeImg( $('[src="' + src + '"]', imgs) );
					} else {
						// not loaded
						var img = $('<img/>').attr('src', src).addClass('current').css({position:"absolute", left:0, top:0, opacity:0}).insertAfter('#imgs img:last-child');
						fadeImg(img);
					}
				}
				return false;
			});
		}
	});
	
}

loadPlugins($('body'));

// the home page tabs 
$(document).ready( function() {
    var activePanel = $('iron-pane'); 
    
    $('.homepage #tabs a').click( function() {
        return false;
        
    }).hover( function() {
        id = '#'+$(this).attr('id').split('-')[0]+'-pane';    

        $('.pane').hide();
        $('#tabs a').each( function() {
            $(this).removeClass('on');
        });
        $(this).addClass('on');
        
        $(id).show();
    }, function() {

    });
    
    $('.homepage .pane > a').hover(function() {
        _this = $(this);
    
        $('span', $(this)).css('text-decoration', 'underline');
        
        src = $('img', _this).attr('src');
        src = src.replace('.png', '-on.png');
        $('img', _this).attr('src', src);
        
        
    }, function() {
        $('span', $(this)).css('text-decoration', 'none');    
        
        src = $('img', _this).attr('src');
        src = src.replace('-on.png', '.png');
        $('img', _this).attr('src', src);        
    });
});

// fancybox
$('.fancybox').fancybox({
	padding:0,
	overlayOpacity:.5,
	overlayColor:'#000',
	showNavArrows:false,
	scrolling:'no',
	enableMouseNav:false,
	onComplete: function(){
		loadPlugins('#fancybox-content');
	}
});

$('.fancybox_iframe').fancybox({
	padding:0,
	overlayOpacity:.5,
	overlayColor:'#000',
	showNavArrows:false,
	scrolling:'no',
	enableMouseNav:false
});

$('#extra_info > div').hide();

$('#product_links a:not(.current)').click(function(){
	var target = $(this).attr('href');
	$(this).addClass('current').siblings().removeClass('current');
	$(target).slideDown(250, function(){
		$('body, html').stop().animate({scrollTop:$('#extra_info').position().top}, 250);
	}).siblings().slideUp();
	
	return false;
})

$('#basket-link').live('click',function(){
    if( $('#basket-widget').is(':visible') ){
		$("#basket-widget").fadeOut();
	} else {
		$.ajax({
	  		url: '/shop/basket-ajax/',
	  		success: function( data ) {
	    		$("#basket-widget").html(data).fadeIn();
	  		}
		});
	}
	
	return false;
});

$('#basket-widget #remove').live('click',function(){
	
    var post = $('#basket-widget form').serialize();

	//list = list + '&delete=Remove';

	$.ajax({
		type: "POST",
  		url: '/shop/basket-ajax/',
  		data:post,
  		success: function( data ) {
    		$("#basket-widget").html(data);
  		}
	});
		
	return false;
});

$('#bw-continue-checkout').live('click',function(){
    window.location='/shop/checkout/';
});

$('#basket-widget .continue').live('click',function(){
	$("#basket-widget").fadeOut();
	return false;
});




// TWINKLES!!11!!! YAY!!!!!1111
function twinkleStar(star){

	var fadeIn = Math.random()*500 +50,
		fadeOut = fadeIn + 200,
		stopTime = (Math.random()*2000) +2000,
		ratio = (Math.random()*35) +5,
		left = Math.random()*101,
		top = Math.random()*101;
	star.animate({opacity:1}, fadeIn, function(){
		$(this).animate({opacity:0}, fadeOut, function(){
			$(this).attr('width', ratio).attr('height', ratio).css({left:left + "%", top: top + "%"});
			setTimeout(function(){twinkleStar(star); }, stopTime);
		});
	})
}

// the home page image map js
$('area').hover( function() {
    id = $(this).attr('id');
    path = '/media/new/img/home_xc/';
    $('#map-image').attr('src',path+'product-'+id+'.jpg');
    //alert($('#heading img').attr('src'));
    $('#heading img').attr('src', path+'heading-'+id+'.png');        
}, function() {
    $('#map-image').attr('src',path+'product-off.jpg');
    $('#heading img').attr('src', path+'heading-off.png');
});

$('.xlink').click( function() {
    $.fancybox({
        'type' : 'ajax',
        'href' : '/xchange-terms/',
	    'width' : 500,
	    'height': 440,
	    'scrolling':'auto',
	    'autoDimensions': false
	}); 
	
	return false;
});

if($('div.xchange') != 'undefined'){
    //animateXchange();
}

var xchange = true;
function animateXchange(){
    img = '/media/new/img/home_xc/xchange'+(xchange?'1':'2')+".png";
    
    $('.xchange img').attr('src', img);
    
    xchange = !xchange;
    
    setTimeout(animateXchange, '2000');
}


var submitted = false;
$('#full-form').submit( function() {
    if(!submitted) {
        submitted = true;
    
        $('#buy-now-message').show();
   }
   else{
        return false;
   }
});  

var twinkle;

/*$('.twinkle').each(function(){
	var el = $(this).css({position:"relative", zIndex:20}),
		offsetLeft = el.position().left-20,
		offsetTop = el.position().top-20,
		wrapWidth = el.outerWidth()+40,
		wrapHeight = el.outerHeight()+40,
		star = $('<img/>').attr('src', '/media/new/img/bg/twinkle.png').css({position:"absolute", opacity:0, maxWidth:wrapWidth/4}),
		wrap = $('<div></div>').css({position:"absolute", left:offsetLeft, top:offsetTop, zIndex:1, width:wrapWidth, height:wrapHeight}).insertAfter(el);
	
	for( var i=0; i<3; i++ ){
		var ratio = Math.floor(Math.random()*35) +5,
			left = Math.floor(Math.random()*101),
			top = Math.floor(Math.random()*101);
		var clonedStar = star.clone().attr('width',ratio).attr('height', ratio).css({left:left + "%", top:top + "%"}).appendTo(wrap);

		twinkleStar(clonedStar);
	}
	
});*/











