var languageSwitch = {
	texts : {
		after : "(change)",
		// before : "Select language:"
		before : ""
	},
	id : "language-switch",
	classes : {
		active : "active",
		label : "language-switch-label",
		menu : "language-switch-menu",
		hide : "language-switch-hide",
		after : "language-switch-after",
		before : "language-switch-before"
	},
	init : function() {
		var el = document.getElementById(this.id);
		var act = null;
		var close = null;
		if(el) {
			var a = el.getElementsByTagName('a');
			for(var i = 0; a[i]; i++)
			if(a[i].className.search(new RegExp(this.classes.active)) != -1) {
				act = a[i].cloneNode(a[i]);
				break
			}
			if(act == null)
				act = a[0].cloneNode(a[0]);
			act.onclick = function() {
				this.blur();
				return false
			}
			var d1 = document.createElement("div");
			var d2 = document.createElement("span");
			d2.className = languageSwitch.classes.label;

			var d0 = document.createElement("span");
			d0.className = this.classes.before;
			if(this.texts.before && this.texts.before != "") {
				d0.appendChild(document.createTextNode(this.texts.before));
			}
			d1.appendChild(d0);

			d1.appendChild(d2);

			var cont = document.createElement("div");
			cont.id = el.id;
			cont.appendChild(d1);

			el.id = "";
			el.parentNode.insertBefore(cont, el);

			d2.appendChild(act);
			var d3 = document.createElement("span");
			d3.className = this.classes.after;
			if(this.texts.after && this.texts.after != "") {
				d3.appendChild(document.createTextNode(this.texts.after));
			}
			act.appendChild(d3);
			d1.appendChild(el);
			d1.className = this.classes.hide;
			el.className = this.classes.menu;
			var fce = function() {
				d1.className = languageSwitch.classes.hide;
			}
			var fceclose = function() {
				close = setTimeout(fce, 400)
			};
			var fcestop = function() {clearTimeout(close)
			};
			d2.onclick = function() {
				d1.className = '';
				fcestop();
			}
			d2.onmouseout = fceclose;
			d2.onmouseover = fcestop;
			el.onmouseout = fceclose;
			el.onmouseover = fcestop;

			var mw = 0;
			el.style.display = 'block';
			for(var i = 0; a[i]; i++) {
				if(mw < a[i].offsetWidth)
					mw = a[i].offsetWidth;
			}
			el.style.width = mw + 'px';
			el.style.display = '';
		}
	}
}

$(document).ready(function() {

	// banner slideshow on homepage
	$('#slideshow').tabs({
		fx : {
			opacity : 'toggle'
		}
	}).tabs('rotate', 8000, true);

	// fancybox
	if(jQuery().fancybox) {
		$('a.fancybox').fancybox({
			'titleShow' : false,
			'transitionIn' : 'elastic',
			'transitionOut' : 'elastic'
		});
	};

	// sliding submenu on hover with delay
	var displayTimeout = 0;
	$('#menu li, #sub-menu li, #user-menu-wrap').hover(function() {
		if(displayTimeout != 0) {
			clearTimeout(displayTimeout);
		};
		var thisElement = this;
		displayTimeout = setTimeout(function() {
			displayTimeout = 0;
			$(thisElement).find('ul').slideDown('fast');
		}, 400);
	}, function() {
		if(displayTimeout != 0) {
			clearTimeout(displayTimeout);
		};
		$(this).find('ul').slideUp('fast');
	});

	// sliding help section menu on hover
	$('.help-products li').hover(function() {
		$(this).find('ul').show('slide', {
			direction : 'right'
		}, 'fast');
	}, function() {
		$(this).find('ul').hide('slide', {
			direction : 'right'
		}, 'fast');
	});

	// tabs on product pages
	$('#tabs').tabs();
	if($.browser.msie && $.browser.version.substr(0, 1) < 8) {
		$('#product-tabs').css('height', '355px');
		$('#product-tabs').wrap('<div style="height: 385px;"></div>');
	};
	$('#product-tabs .inner').css('height', '240px');
	$('.ui-tabs .ui-tabs-panel').css('height', '320px');

	// set background color for odd rows (IE < 9)
	if($.browser.msie && $.browser.version.substr(0, 1) < 9) {
		$('#product-tabs tr:nth-child(odd)').css('background-color', '#EEE');
	};

	// rotate text in price tag on product pages
	if(jQuery().transform) {
		$('.price-tag p').transform({
			rotate : '-6deg'
		});
	};

	// show rating articles box
	$('.rate-this-article').click(function() {
		var rate = $('#rate-this-article').is(':hidden');
		if (rate) {
			$('#rate-this-article').fadeIn('fast');
		} else {
			$('#rate-this-article').fadeOut('fast');
		};
		return false;
	});

	// hide rating articles box when clicked outside
	var mouseInside = false;
	$('#rate-this-article').hover(function() {
		mouseInside = true;
	}, function() {
		mouseInside = false;
	});
	$('html').click(function() {
		if(!mouseInside)
			$('#rate-this-article:visible').fadeOut('fast');
	});

	// rating articles - toggle gray & yellow stars on hover
	$('#rate-this-article a').hover(function() {
		var counter = $(this).index();
		var position = (($(this).find('span').width() + 20) / 2).toFixed(0);
		$(this).find('span').css('margin-left', '-' + position + 'px');
		$('#rate-this-article a').each(function() {
			if($(this).index() <= counter)
				$(this).addClass('yellow-star');
		});
	}, function() {
		$('#rate-this-article a').removeClass('yellow-star');
	});
	$('#rate-this-article a').click(function() {
		var form = document.createElement('form');
		form.method = 'post';
		form.action = this.protocol + '//' + this.hostname + this.pathname;
		var input = document.createElement('input');
		input.type = 'hidden';
		input.name = 'ratValue';
		input.value = $(this).index() + 1;
		form.appendChild(input);
		document.body.appendChild(form);
		form.submit();
		return false;
	});

	// articles - insert clearing div before gray box
	$('.article .graybox').before('<div class="clear" />');
	$('.article .clear').prev('p').css('margin-bottom', 0);

	// articles - gray tooltip on hover except anchors with titles starting "go to"
	if(jQuery().tooltip) {
		$('.article a[title], #read-more').not('[title^="go to"]').tooltip({
			effect : 'slide',
			position : 'top center'
		}).dynamic();
	};
	$('#read-more').click(function() {
		return false;
	});

	// send article to a friend box
	if(jQuery().fancybox) {
		$("#send-to-friend-trigger").fancybox();
	};

	// promotion banners - select text in textarea
	$('.banner-code textarea').focus(function() {
		$(this).select();
	});
	$('.select-all').click(function() {
		$(this).parent().prev('textarea').select();
		return false;
	});

	// promotion banners - show & hide sections
	$('.banners').hide();
	$('#banners-1').show();
	$('#promote ul li a').click(function() {
		var bannerSection = $(this).attr('href');
		var minHeight = $('#promote').height();
		// $('#promote').css('min-height', minHeight + 'px');
		$('.banners:visible').fadeOut('fast', function() {
			$(bannerSection).fadeIn('fast');
		});
		return false;
	});

	// jcarousel
	if(jQuery().jcarousel) {
		$('.jcarousel').jcarousel();
	};

});

