var t_imgtiles;
var t_imgtilesCheck;
var t_imgTilesAll;
var b_imgTilesAll = false;
var log_enabled;
var log;

$k = jQuery.noConflict();
$k(document).ready(
function()
{
	log_enabled = true;
	
	if($k('#kids-mnu').length > 0)
		setTimeout('kidsblock(0)', 500);
	
	init();
});

function init()
{
	$k('.home h1#title').animate({
		opacity: 1 }, {
		duration: 2500
	});
	
	if(!$k.isArray(log))
		log = new Array();
	
	$k('a[rel="gallery"]').fancybox({
		speedIn: 1000
	});
	
	$k('#imgtiles > li > a').fancybox({
		speedIn: 1000
	});
	
	$k('ul#imgtiles').animate({
		opacity: 1 }, {
		duration: 1500, 
		queue: false, 
		complete:
			function() 
			{
				imgtiles();
			
				$k('#imgtiles > li > img:not(.enlarged)').each(function(index) {
					$k(this).load(function() {
						$k(this).css({
							display: 'block',
							opacity: 0
						}).animate({
							opacity: 1 }, {
							duration: 2000, 
							queue: false
						});
					})
				});

				if(!b_imgTilesAll) {
					t_imgTilesAll = setTimeout('showAllImgtiles();', 3000);
					b_imgTilesAll = true;
				}
				
				if(!t_imgtiles)
					t_imgtiles = setInterval('imgtile_getRandom();', 5000);
			}
	});

	$k('ul#gallery li img').each(function() {
		$k(this).load(function() {
			$k(this).fadeIn(2000);
		});
	});
	
	$k('ul#gallery li img').hover(
	function() {
		$k(this).animate({
			marginTop: 4,
			marginLeft: 4,
			width: 172, 
			heigh: 112
		}, {
			duration: 75, 
			queue: false
		});
	}, 
	function() {
		$k(this).animate({
			marginTop: 0,
			marginLeft: 0,
			width: 170, 
			heigh: 108
		}, {
			duration: 75, 
			queue: false
		});
	});
	
	$k('li.mnu-scroll').click(function() {
		var target = $k($k(this).attr('data-target'));
		debug(target);
		
		$k(document).scrollTo(
			target, 
			700, { 
				offset: -25
			});
	});

	$k('span.top').click(function() {
		$k(document).scrollTo(
			$k('body'), 
			700);
	});

	var uri_segments = (document.location.href).split('/');
	for(var i=0; i<uri_segments.length; i++) {
		var segment = uri_segments[i];
		switch(segment) {
			case 'kids1':
			case 'kids2':
			case 'kids3':
			case 'kids4':
				var mnu_item = $k('#m-mnu > ul > li:last > a');
				var href = $k(mnu_item).attr('href');
				$k(mnu_item).attr('href', href+'?ref='+segment);
				break;
		}
	}
	
	$k('.day-contents .block').hover(function(e) {
		var hc 	= $k(this).attr('data-target');
		var day = $k(this).parents('.day');
		var arr = $k('#arrow');
		$k('.hc').hide();
		
		$k(arr).css({
			top: day.position().top+25, 
			left: day.position().left+111
		}).show();
		
		$k(hc).css({
			top: day.position().top+14, 
			left: day.position().left+115
		}).show();
	}, function() {
		$k('.hc, #arrow').hide();
	});
}

function showAllImgtiles() 
{
	$k('#imgtiles li, #imgtiles li img:not(.enlarged)').fadeIn(3000);
}

function kidsblock(index, onlyme)
{
	var li 	= $k('#kids-mnu > li')[index];
	var img = $k(li).find('img');
	var src = $k(li).find('.image').attr('data-src');
	
	if(src != undefined)
	{
		var src_imm = src.replace('.gif', '_imm.gif');
		
		$k(li).find('img').mouseover(function() {
			kidsblock(index, true);
		});
		
		if(!onlyme)
			$k(img).attr('src', src);
		else
			$k(img).attr('src', src_imm);
		
		if(index<5 && !onlyme) {
			setTimeout('kidsblock('+(index+1)+');', 0);
		}
	}
}

function imgtile_getRandom()
{
	var images = new Array();
	
	$k('#imgtiles > li').each(
	function() {
		images[images.length] = $k(this).attr('data-large');
	});

	$k.ajax({
		url: '/index.php?id=2',
		type: 'POST', 
		dataType: 'json',
		data: { 
			'type': 'json', 
			'existing': images
			},
		success:
			function(data) {
				if(data) {
					if(data.image)
					{
						var rand = Math.floor(Math.random()*11) - 1;
							
						$k('#imgtiles > li.tile_'+rand).animate({
							opacity: 0 }, {
							duration: 1000, 
							queue: false, 
							complete: function() {

								$k(this).html(data.image);
								
								$k('#imgtiles > li > a').fancybox({
									speedIn: 1000
								});
								
								$k(this).find('img:not(.enlarged)').show();
								$k(this).delay(500).animate({
									opacity: 1 }, {
									duration: 2500, 
									queue: false
								});
							}
						})
					}
				}
			},
		error:
			function(data) {
				if(data.responseText)
					debug(data.responseText);
			}
	});

	$k('#imgtiles > li').delay(4000).fadeIn(1000);
}

function imgtiles()
{
	$k('#imgtiles > li').hover(
	function() 
	{
		var enlarged = $k(this).find('img.enlarged');
		
		enlarged.css({
			top: $k(this).position().top - 13, 
			left: $k(this).position().left - 8
		}).show();
	}, 
	function()
	{
		$k(this).find('img.enlarged').hide();
	});
}

function debug(msg)
{
	if(log_enabled)
	{
		if(window.console)
			window.console.log(msg);
	}
}

