var speed = $.browser.msie ? 0 : 'normal';

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);
}

var preloadImages = function (imgs, i) {
	var pics = imgs.split(',');
	
	$('<img src="' + pics[i] +'" class="load" />').appendTo('body').load(function () {
		i++;
		if (i == 2) {
			$('#footer div span').text(' / ' + (pics.length - 1));
			if (pics.length > 2) {
				api.playToggle();
			
				window.setTimeout(function () {
					api.options.slides.splice(0, 1);
					$('#supersized').find('li:first').remove();
					//$('#supersized').find('li:last').prependTo('#supersized').removeClass().addClass('slide-0');
				}, 1000);
			}
			api.goTo(2);
			$('#footer div b').html(vars.current_slide);
			vars.current_slide -= 1;
			if (pics.length <= 2) api.options.slideshow = 0;
		}
		if (pics[i] != '') preloadImages(imgs, i);
	});
};

$(function () {
	var menu = $('#menu .current-menu-item');
	$('> ul', menu).addClass('sub-menu').insertAfter($('#menu')).find('ul');
	menu.parents('ul').each(function () {
		if ($(this).attr('id') != 'menu') {
			$(this).addClass('sub-menu').insertAfter($('#menu'));
		}
	});
	
	$('#header, #sidebar, #readmore, #footer, #content').show();
		
	var content = $('#content');
	content.height($(window).height() - content.offset().top - 50);
	if (navigator.platform.indexOf("iPhone") != -1 || navigator.platform.indexOf("iPod") != -1) {
		new iScroll(document.getElementById('content'));
	}
	
	$('#header, #sidebar, #readmore, #footer, #content').hide();
	
	$(window).resize(function () {
		var content = $('#content');
		content.height($(window).height() - content.offset().top - 50);
	});
	
	window.setTimeout(function () {
		$('#header').fadeIn(1000, function () {
			$('#sidebar').fadeIn(1000, function () {
				if (readCookie('hide') != 1) $('#content').fadeIn(1000);
				$('#readmore').fadeIn(500, function () {
					$('#footer').fadeIn(500);
				});
			});
		});
	}, 1000);
	
	if (speed == 0 || window.index != 1) {
		$('#header').show();
		$('#sidebar').show();
		$('#readmore').show();
		$('#footer').show();
		if (readCookie('hide') != 1) $('#content').show();
	}
	
	$('#cover').css({opacity: 0}).show().animate({opacity: 0.7}, speed);
	
	if (readCookie('hide') != 1) {
		var prev = $('#readmore a:first');
		prev.next().stop(true).hide();
		$('#readmore a:last').removeClass('active').insertBefore(prev).hide();
		prev.stop(true).show().addClass('active');
		if ($('#map').size() && typeof showMap == 'function') showMap();
	}
	
	if (speed == 0) {
		$('#readmore a').hide().filter('.active').show();
	} else {
		$('#readmore a').css({opacity: 0}).filter('.active').css({opacity: 1});
	}
	
	
	$('#readmore a').click(function (e) {
		var target = $(this),
			prev = target.prev();
		
		if (speed == 0) {
			prev.next().stop(true).hide();
			$(this).removeClass('active').insertBefore(prev).hide();
			prev.stop(true).show().addClass('active');
		} else {
			prev.next().stop(true).animate({opacity: 0}, function () {
				$(this).removeClass('active').insertBefore(prev).hide();
			});
			prev.stop(true).show().animate({opacity: 1}, function () {
				$(this).addClass('active');
			});
		}
		
		if (prev.attr('rel') == 'hide') {
			eraseCookie('hide');
			$('#content').fadeIn(speed);
			if (typeof showMap == 'function') showMap();
		} else {
			createCookie('hide', 1);
			$('#content').fadeOut(speed);
			$('#map').html('');
		}
	});
});
