function showPopup(e){
	$('.purple_popup').hide();

	var popup = e.children('.purple_popup');
	
	popup.css({display:"block", opacity:0, zIndex:200}).parent().css({zIndex:100});
	
	var overRight = (e.parent().width() - 15) - (popup.width() + e.position().left);	
	var offsetLeft = overRight <= 0 ? e.position().left + overRight : e.position().left + 3;
	var offsetTop = e.position().top + e.height() + 5;
	
	popup.css({top: offsetTop + "px", left: offsetLeft + "px"}).animate({opacity:1},150);
};

function hidePopup(e){
	var popup = e.children('.purple_popup');
	popup.hide().css({zIndex:1}).parent().css({zIndex:1});
};

$(function(){
	// thumbs and showcase
	$('#prod_thumbs a').hover(function(){
		$('#prod_thumbs a.current').removeClass('current');
		$(this).addClass('current');
		var file = $(this).attr('href');
		var current = $('#prod_show .current');
		var target = $('#prod_show img[src=' + file + ']');
		var title = $(this).attr('title');
		if( !target.hasClass('current') ){
			$('#prod_show').removeClass('nopng');
			if( target.length == 1 ){
				target.css({zIndex:9}).show().addClass('current');
				current.removeClass('current').css({zIndex:1}).hide();
			} else {
				var image = new Image();
				$(image).load(function(){
					$(image).css({position:"absolute", left:"0", top:"0", display:"none"}).addClass('pngfix');
					$(image).appendTo('#prod_show');
					$(image).css({zIndex:9}).show().addClass('current');
					current.removeClass('current').css({zIndex:1}).hide();
				});
				$(image).attr('src',file);
			}
			if( file.match(/\.jpg$/) ){
				$('#prod_show').addClass('nopng');
			}
		}
		
	},
	function(){});
	$('#prod_thumbs a').click(function(){ return false; });
	
	// tabs
	$('#prod_tabs a').click(function(){
		var target = $(this).attr('href').replace(/#/,'');
		var current = $('.prod_tab:visible').attr('id');
		if( current != target ){
			$('#prod_tabs a').removeClass('current');
			$(this).addClass('current');
			$('#' + current).hide();
			$('#' + target).show();
			Cufon.refresh();
		};
		return false;
	});
	
	// tooltips
	$('.hoverable li a').hover(function(){
		var offset = parseInt($(this).width() + 14);
		$(this).next('.tip').css({left:offset + "px"}).fadeIn();
		$(this).parent().css({zIndex:10});
	},
	function(){
		$(this).next('.tip').hide();
		$(this).parent().css({zIndex:1});
	});
	$('.hoverable li a').click(function(){ return false; });
	
	// gallery
	// image hover effect
	var highlighted = false;
	var timeout;
	
	$('.glow_popup img').bind('mouseover',function(event){		
		var container = $(this).parent().parent('.glow_popup');
		showPopup( container );
		
		clearTimeout(timeout);
		if( !highlighted ){
			highlighted = true;
			container.addClass('highlighted');
			$('.glow_popup:not(".highlighted")').animate({opacity:.4},150);
			
		} else {
			$('.highlighted').removeClass('highlighted').animate({opacity:.4}, 150);
			container.animate({opacity:1},150).addClass('highlighted');
		}
	});
	
	$('.glow_popup img').bind('mouseout', function(event){
		//event.stopPropagation();
		var container = $(this).parent().parent('.glow_popup');
		hidePopup( container );
		timeout = setTimeout(function(){
			highlighted = false;
			$('.glow_popup').animate({opacity:1},150).removeClass('highlighted');
		},250);
	});
	
	// hairstyles lightbox
	$('#gallery_thumbs a').PHPLightBox();
	
	// offer lightbox
	$('#excl_offer a').PHPLightBox();
});