/**
 * 
 * These cookie functions were taken from PPK
 * http://www.quirksmode.org/js/cookies.html
 * 
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function adjustJpJa(pl) {
	if (pl=='jp') {
		pl = 'ja' ;
	} else 
	if (pl=='ja' || pl=='en') {
		//NOP
	} else {
		pl = '';
	}
return pl;
}

/*
 * Language selection
 */
var defaultLang = 'en';

var regex = /[\?&]lang=([a-z][a-z])/;
var lang = window.location.search.toString();
var res_exp = regex.exec(lang);
var pl = res_exp ? res_exp[1] : '';
pl = adjustJpJa(pl);

var plc = readCookie('pl');

if (pl) {
	createCookie('pl', pl, 365); // expires in 1 year
	if (pl != plc) {
		window.location.reload(true);
	}
} else {
	pl = adjustJpJa(plc);
	if (pl == '' || pl == null || pl == undefined) {
		// detect browser language
		lang = $.browser.msie ? navigator.userLanguage : window.navigator.language;
		pl = (/ja\-\w+/.test(lang)) ? 'ja' : defaultLang;
		createCookie('pl', pl, 365); // expires in 1 year
		window.location.reload(true);
	} else {
		createCookie('pl', pl, 365); // expires in 1 year
	}
}
$('html').addClass('selected-lang-' + pl);


/*
 * LEGO Cuusoo global
 */
jQuery(function($) {
	// simple sliding menu
	var timer = 0;
	$('#sidebar').find('.explas').hide().parent().hover(function() { 
		var self = $(this);
		window.clearTimeout(timer);
		timer = window.setTimeout(function() {
			self.find('.explas:hidden').slideDown();
		}, 400);
	}, function() { 
		window.clearTimeout(timer);
		$(this).find('.explas:visible').slideUp(); 
	});
	
	// FAQ
	var hash = '';
	$('.aframe').hide();
	$('.qframe').css({cursor: 'pointer'}).click(function() {
		$(this).next().toggle('slow');
	});
	
	// detect hash and expand target faq item
	if ($('.aframe').length) {
		window.setInterval(function() {
			if (window.location.hash.substr(1) !== hash) {
				hash = window.location.hash.substr(1);
				
				if (hash == '__all') {
					$('.aframe').show();
				}
				
				var langHash = '';
				if (pl=='ja' || pl=='jp') {
					langHash = 'j_' + hash;
				} else {
					langHash = 'e_' + hash;
				}
				var target = $('a[name="' + langHash + '"]');
				var aframe = target.next().next();
				
				scrollTo(0, target.offset().top);
				aframe.show('slow');
			}
		}, 100);
	}
	
	// gallery
	$('.gallery-nav li div').hide();
	$('.gallery-nav li').bind('mouseover', function() {
		$(this).closest('.gallery').find('.gallery-window img').attr('src', $('a img', this).attr('src'));
		if ($('#item-description').length) {
			$('#item-description').html($('div', this).html());
		}
	});

});