// fix ie6 background flicker problem.
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

$(document).ready(function() {
	if (window.console == undefined) { window.console = {log:function(){}}; }

	$("#head ul.menu>li>a").each(function() {
	    var $img  = $('<img />');
	    var src1 = '/shop/subshop/metafiles/4801/' + $(this).html().toLowerCase() + '.gif';
	    var src2 = '/shop/subshop/metafiles/4801/' + $(this).html().toLowerCase() + '-over.gif';

	    var i = new Image(); i.src = src2;

	    if (jQuery.className.has(this, 'active')) {
	        $img
	        .attr('border', 0)
	        .attr('src', src2);

	        jQuery.className.add($img[0], 'active');
	    } else {
	        $img
	        .attr('border', 0)
	        .attr('src', src1);
	    }

	    if ($(this).next().size() == 0) {
	        var href = $(this).attr('href');
	        $img
	        .css('cursor', 'pointer')
	        .bind('click', function() {
	            location.href = href;
	        });
	    }

	    $(this).before($img);
	    $img.after('<br />'); // for IE
	    $(this).remove();
	});

	$("#head ul.menu>li").hover(function() {
	    $("#head ul.menu>li>ul").hide();

	    $('img', this).each(function() {
	        if (!jQuery.className.has(this, 'active')) {
	            var src = $(this).attr('src').replace(/\.gif/g, '-over.gif');
	            $(this).attr('src', src);
	        }
	    });

	    $('ul', this).show();

	}, function() {
	    $('img', this).each(function() {
	        if (!jQuery.className.has(this, 'active')) {
	            var src = $(this).attr('src').replace(/\-over\.gif/g, '.gif');
	            $(this).attr('src', src);
	        }
	    });

	    $('ul', this).hide();
	});


	if (!jQuery.browser.mozilla) {
		$("#banner").corner("bottom");
		$("div.box").corner();
		$("div.piqtitle").corner("bottom");
		$("div.innerbox").corner('top bottom', 'corner-box-');
	}
});

$.fn.corner = function(c, i) {
	return this.each(function() {
		var opts = {
			tl: /top|tl/.test(c),    tr: /top|tr/.test(c),
			bl: /bottom|bl/.test(c), br: /bottom|br/.test(c)
		};

		if (i == undefined || !i) {
		    i = 'corner-';
		}

		if (!opts.tl && !opts.tr && !opts.bl && !opts.br)
			opts = {tl:1, tr:1, bl:1, br:1};

		var html = $(this).html();

		var tl = opts.tl ? '<img src="/shop/subshop/metafiles/4801/'+i+'tl.gif" border=0" />' : '';
		var tr = opts.tr ? '<img src="/shop/subshop/metafiles/4801/'+i+'tr.gif" border=0" />' : '';
		var bl = opts.bl ? '<img src="/shop/subshop/metafiles/4801/'+i+'bl.gif" border=0" />' : '';
		var br = opts.br ? '<img src="/shop/subshop/metafiles/4801/'+i+'br.gif" border=0" />' : '';

		var width = $(this).width() - toInt($(this).css('paddingLeft')) - toInt($(this).css('paddingRight'));

		html = '<table border="0" cellspacing="0" cellpadding="0"><tr><td align="left" valign="top" style="width:'+toInt($(this).css('paddingLeft'))+'px; height:'+toInt($(this).css('paddingTop'))+'px">'+tl+'</td><td></td><td align="right" valign="top" width="'+toInt($(this).css('paddingRight'))+'" height="'+toInt($(this).css('paddingTop'))+'">'+tr+'</td></tr><tr><td></td><td style="width:'+toInt(width)+'px">'+html+'</td><td></td></tr><tr><td align="left" valign="bottom" width="'+toInt($(this).css('paddingLeft'))+'" height="'+toInt($(this).css('paddingBottom'))+'">'+bl+'</td><td></td><td align="right" valign="bottom" width="'+toInt($(this).css('paddingRight'))+'" height="'+toInt($(this).css('paddingBottom'))+'">'+br+'</td></tr></table>';

		$(this).css('padding', 0);

		$(this).html(html);
	});
};

$.fn.shadow = function(c) {
	$(this).each(function() {
	    var $el = $(this);

		var width   = $el.width();
		var height  = $el.height();
		var display = $el.css('display');
		var top     = $el.css('top');

		console.log(width + ':' + height + ':' + display + ':' + top);

		$el
		.before('<div class="shadow"></div>')
		.prev()
		.css({
			'zIndex': '-1',
			'background': '#000',
			'position': 'absolute',
			//'display': display,
			'top': top,
			'width': width + 20 + 'px',
			'height': height + 20 + 'px'
		});
	});
};

var toInt = function(n) {
	n = parseInt(n);
	return isNaN(n) ? 0 : n;
};

var uniConfirm = function(txt)
{
    return confirm(entity2unicode(txt));
};

var uniAlert = function(txt)
{
    return alert(entity2unicode(txt));
};

var entity2unicode = function(txt) {
    txt = txt.replace(/&auml;/g, String.fromCharCode(228));
    txt = txt.replace(/&ouml;/g, String.fromCharCode(246));
    txt = txt.replace(/&uuml;/g, String.fromCharCode(252));
    txt = txt.replace(/&szlig;/g, String.fromCharCode(223));
    txt = txt.replace(/&Auml;/g, String.fromCharCode(196));
    txt = txt.replace(/&Ouml;/g, String.fromCharCode(214));
    txt = txt.replace(/&Uuml;/g, String.fromCharCode(220));

    var reg = txt.split(/&#(\d+);/i);
    for (var i = 1; i < reg.length; i+=2) {
        reg[i] = String.fromCharCode(parseInt(reg[i]));
    }
    return reg.join('');
};

var truncate = function(str, length, suffix) {
    if (length == undefined) {
        length = 80;
    }

    if (str.length <= length) {
        return str;
    }

    if (suffix == undefined) {
        suffix = '...';
    }

    return str.replace(/\s+?(\S+)?$/g, '', str.substr(0, length+1)) + suffix;
};
