function popup_init() {
	popup_init.popPars = { arrowKey:false, rel:'nofollow' };
	$(document).bind('cbox_complete', function(){
		$('#cboxLoadedContent a[rel=popup]').colorbox(popup_init.popPars);
		$('#cboxLoadedContent form').smoothForm({
			success: function(html){
				var par = {};
				for(var i in popup_init.popPars)
					par[i] = popup_init.popPars[i];
				par.html = html;
				$.colorbox(par);
			}
		});
		Cufon.refresh();
		$('#cboxLoadedContent input:first').focus();
	});
	$('a[rel=popup]').colorbox(popup_init.popPars);

	$('a[rel=ipopup]').click(function(){
		var par = {};
		for(var i in popup_init.popPars)
			par[i] = popup_init.popPars[i];
		par.html = '<div style="width:800px; height:500px"><br><iframe src="' + this.href + '" frameborder="0" border="0" style="width:100%; height:100%"></iframe></div>';
		$.colorbox(par);
		return false;
	});
}

function tabbox_init() {
	$('.tab_box').each(function(){
		var box = $(this);
		var tabs = Math.max($('._head', box).length, $('._tab', box).length);
		$('._head', box).width($('._head', box).parent().width() / tabs - 17);
		$('._head ._ttl', box).click(function(){
			var i = $('._head ._ttl', box).removeClass('_sel').index(this);
			$(this).addClass('_sel');
			$('._tab', box).hide().eq(i).show();
			Cufon.refresh();
		}).mouseleave(function(){
			window.setTimeout(function(){
				Cufon.refresh('.tab_box ._head ._ttl._sel');
			}, 10);
		}).eq(0).click();
	});
}

function slidebox_init() {
	$('.slide_box').each(function(){
		var ch = $(this).children();
		var ew = ch.hide().eq(0).show().outerWidth();
		$(this).append('<div class="cb"></div>');
		var h = $(this).height();
		$(this).height(h).css('position', 'relative');
		var ec = Math.floor($(this).width() / ew);
		var pg = 0, pc = Math.ceil(ch.length / ec);
		$('<div class="_prev"></div>').appendTo(this).click(function(){
			if(--pg < 0) pg = 0;
			ch.hide();
			for(var i = pg * ec; i < (pg + 1) * ec && ch[i]; i++)
				$(ch[i]).fadeIn();
		}).css('top', h / 2 - 20).click();
		$('<div class="_next"></div>').appendTo(this).click(function(){
			if(++pg >= pc) pg = pc - 1;
			ch.hide();
			for(var i = pg * ec; i < (pg + 1) * ec && ch[i]; i++)
				$(ch[i]).fadeIn();
		}).css('top', h / 2 - 20);
	});
}

function input_init() {
	$(':input[data-label]').focus(function(){
		if($(this).val() == $(this).attr('data-label'))
			$(this).val('');
	}).blur(function(){
		if($(this).val() == '')
			$(this).val($(this).attr('data-label'));
	}).blur();
	
	$('form').submit(function(){
		$(':input[data-label]', this).each(function(){
			if($(this).val() == $(this).attr('data-label'))
				$(this).val('');
		});
	});
}

function slideshow_init() {
	$('.slideshow').each(function(){
		var b = $('.box', this);
		var t = $('.thumbs', this);
		var cur = 0, cnt = Math.floor(t.height() / $('.box_thumb', t).height()) * Math.floor(t.width() / $('.box_thumb', t).width()), max = $('.box_thumb', t).length;
		if(max > cnt)
			$('._prev, ._next', this).show();
		$('._prev', this).click(function(){
			if(cur > 0 && $('.box_thumb', t).eq(cur - cnt).position()) cur -= cnt;
			t.scrollTop($('.box_thumb', t).eq(cur).position().top - $('.box_thumb', t).eq(0).position().top);
		});
		$('._next', this).click(function(){
			if(cur < max && $('.box_thumb', t).eq(cur + cnt).position()) cur += cnt;
			t.scrollTop($('.box_thumb', t).eq(cur).position().top - $('.box_thumb', t).eq(0).position().top);
		});
		$('a', t).click(function(){
			if($(this).attr('href').match(/\.(flv|mp4)/)) {
				b.html('<div id="flash_player"></div>');
				swfobject.embedSWF('plugins/videos/player.swf', 'flash_player', b.width(), b.height(), "8.0.0", "", {
					file: $(this).attr('href'),
					image: $('img', this).attr('src'),
					skin: WEB_ROOT + 'plugins/videos/skin.zip',
					controlbar: 'over',
					autostart: 'true'
				}, {
					allowfullscreen: 'true',
					wmode: 'transparent',
					allowscriptaccess: 'always'
				}, {
				});
			} else {
				var img = new Image();
				$(img).load(function(){
					b.fadeTo('fast', 0.01, function(){
						var r = Math.max(img.width / b.width(), img.height / b.height())
						b.html('<img src="' + img.src + '" alt="" width="' + Math.round(img.width / r) + '" height="' + Math.round(img.height / r) + '">');
						b.fadeTo('fast', 1.0);
					});
				});
				img.src = $(this).attr('href');
			}
			return false;
		}).eq(0).click();
	});
}

function viewcart_init() {
	$('#view_cart').css('cursor', 'pointer').click(function(){
		var p = $(this).offset();
		$('#cart_popup').css({
			top: (p.top + $(this).height() + 7) + 'px',
			left: (p.left - 250) + 'px',
			zIndex: 1000
		}).slideToggle();
		$('#cart_popup #close').click(function(){
			$('#cart_popup').slideUp();
			return false;
		});
		return false;
	});
}

$(function(){
	$('.pngfix').ifixpng();
	$('.pngfix_str').ifixpng();
	
	$('#menu a._sel').mouseleave(function(){
		window.setTimeout(function(){
			Cufon.refresh('#menu a._sel');
		}, 10);
	});
	
	popup_init();
	
	slidebox_init();
	
	tabbox_init();
	
	input_init();
	
	slideshow_init();
	
	viewcart_init();
});

