var core = {};
// events
core.events = {};
core.events.Count = 3;
core.events.Current = 0;	/* position of the <li> that is currently shown: Zero-Based */
core.events.ImagesLoaded = 0;
core.events.Day = 1;
core.events.Seconds = 1;	// delay until next event shown
core.events.Width = 800;
core.events.BgPosX1 = 0;
core.events.BgPosX2 = -266;
core.events.BgPosX3 = -532;
core.events.BgPosY = 0;
// animation
core.anim = {};
core.anim.rightToLeft = 0;
core.anim.leftToRight = 1;
// day
core.weekday = {};
core.weekday.Sunday = 0;
core.weekday.Monday = 1;
core.weekday.Tuesday = 2;
core.weekday.Wednesday = 3;
core.weekday.Thursday = 4;
core.weekday.Friday = 5;
core.weekday.Saturday = 6;
// background
core.bg = {};
core.bg.Count = 6;
core.bg.ImagesLoaded = 0;
core.bg.Current = 0;
core.bg.Seconds = 1;
core.bg.Prefix = 'dots';
core.bg.interval = null;
// Google Maps
core.googleMaps = {};
core.googleMaps.latitude = 52.4712561;
core.googleMaps.longitude = -1.8942281;
core.googleMaps.zoom = 16;
core.googleMaps.pinTitle = 'The Core Club';
core.googleMaps.dialogTitle = 'The Core Club - How to find us';
core.googleMaps.dialogHeight = 400;
core.googleMaps.dialogWidth = 600;
// RP
core.googleMaps.Key = 'ABQIAAAAwuf64pJ2y5voFL99AnbGnRQwNnO23fXhWDaCypipfqqNmX_ghhQUk0sDAy2ZZa0cDT1-b7teCw01gQ';
//core.googleMaps.Key = 'ABQIAAAAgUB7wd3s3RRB0DxCyHHZ1xSDxytjCPU_uc0iUZrdC2YBUa5Q1BQWZsjRWETDk6tApObIewh93_jI4A';


core.startup = function(){
	core.initImages();
};

core.initImages = function() {
	// events
	for (var x = 1; x <= core.events.Count; x++) {
		$('<img />').load(function() {
			core.events.ImagesLoaded++;
			if (core.events.ImagesLoaded == core.events.Count) {
				core.initEvent();
			}
		}).attr('src', x + '.jpg');
	}
	// backgrounds
	for (var y = 1; y <= core.bg.Count; y++) {
		$('<img />').load(function() {
			core.bg.ImagesLoaded++;
			if (core.bg.ImagesLoaded == core.bg.Count) {
				core.initBgSwap();
			}
		}).attr('src', core.bg.Prefix + y + '.jpg');
	}
};

core.initEvent = function() {
	var d = new Date();
	switch (d.getDay()) {
		case core.weekday.Sunday:
		case core.weekday.Monday:
			core.events.Day = 1; break;	// Monday
		case core.weekday.Tuesday:
		case core.weekday.Wednesday:
		case core.weekday.Thursday:
		case core.weekday.Friday:
			core.events.Day = 3; break; // Friday
		case core.weekday.Saturday:
			core.events.Day = 3; break; // Saturday
	}
	core.initMouseOver();
	core.interval = setInterval('core.showTodaysEvent()', (1000 * core.events.Seconds));
};

core.showTodaysEvent = function() {
	clearInterval(core.interval);
	$('#menu > li')
		.removeClass('bg0 bg1 bg2 bg3')
		.addClass('bg' + core.events.Day);
	core.events.Current = (core.events.Day - 1);
	// display days
	$('#bg' + core.events.Day).addClass('active');
	$('ul.menu > li > a').show();
	//
	core.showMapIcon();
};

core.initMouseOver = function() {
	$('#bg1,#bg2,#bg3').mouseover(function(e){

		var $this = $(this);
		/* if we hover the current one, then don't do anything */
		if ($this.parent().index() == core.events.Current) {
			return;
		}

		/* item is bg1 or bg2 or bg3, depending where we are hovering */
		var item = e.target.id;

		$('ul.menu > li > a').removeClass('active');
		$(this, 'a').addClass('active');

		if (item == 'bg1' || core.events.Current == 2) {
			/*
			if we hover the first <li> or if we come from
			the last one, then the images should move left -> right
			*/
			$('#menu > li')
				.animate({ backgroundPosition: '(-' + core.events.Width + 'px ' + core.events.BgPosY + 'px)' }, 0)
				.removeClass('bg0 bg1 bg2 bg3')
				.addClass(item);
			core.move(core.anim.leftToRight, item);
		} else {
			/*
			if we hover the first <li> or if we come
			from the last one, then the images should move
			right -> left
			*/
			$('#menu > li')
				.animate({ backgroundPosition: '(' + core.events.Width + 'px ' + core.events.BgPosY + 'px)' }, 0)
				.removeClass('bg0 bg1 bg2 bg3')
				.addClass(item);
			core.move(core.anim.rightToLeft, item);
		}

		/* change the current element */
		core.events.Current = $this.parent().index();
	});
};

core.initBgSwap = function() {
	core.bg.interval = setInterval('core.swapBg()', (1000 * core.bg.Seconds));
};

core.swapBg = function() {
	var newLogo = (core.bg.Current == core.bg.Count ? 1 : core.bg.Current + 1);
	$('body').css( { backgroundImage: 'url(' + core.bg.Prefix + newLogo + '.jpg)' });
	core.bg.Current = newLogo;
};

core.move = function(dir, item) {
    if (core.anim.leftToRight) {
        $('#bg1').parent().stop().animate({backgroundPosition:'(' + core.events.BgPosX1 + 'px ' + core.events.BgPosY + 'px)'}, 200);
        $('#bg2').parent().stop().animate({backgroundPosition:'(' + core.events.BgPosX2 + 'px ' + core.events.BgPosY + 'px)'}, 300);
        $('#bg3').parent().stop().animate({backgroundPosition:'(' + core.events.BgPosX3 + 'px ' + core.events.BgPosY + 'px)'}, 400, function(){
            $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
        });
    } else {
        $('#bg1').parent().stop().animate({backgroundPosition:'(' + core.events.BgPosX1 + 'px ' + core.events.BgPosY + 'px)'}, 400, function(){
            $('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
        });
        $('#bg2').parent().stop().animate({backgroundPosition:'(' + core.events.BgPosX2 + 'px ' + core.events.BgPosY + 'px)'}, 300);
        $('#bg3').parent().stop().animate({backgroundPosition:'(' + core.events.BgPosX3 + 'px ' + core.events.BgPosY + 'px)'}, 200);
    }
};

core.showMapIcon = function() {
	$('#googlemaps').show();
};

core.showMap = function() {
	$('#mapCanvas').dialog({
		autoOpen: false,
		closeOnEscape: true,
		draggable: true,
		resizable: false,
		modal: true,
		height: core.googleMaps.dialogHeight,
  		width: core.googleMaps.dialogWidth,
  		title: core.googleMaps.dialogTitle
	});
	$('#mapCanvas').parent().children('.ui-dialog-titlebar').css({
		padding: 0,
		backgroundImage: 'none'
	});
	$('#ui-dialog-title-mapCanvas').css({
		fontWeight: 'normal',
		fontSize: '12px',
		paddingLeft: '10px'
	});
	
	core.googleMaps.LatLng = new google.maps.LatLng(core.googleMaps.latitude, core.googleMaps.longitude);
	$('#mapCanvas').gmap({
		'center': core.googleMaps.LatLng,
		scrollwheel: false,
		zoom: core.googleMaps.zoom,
		'callback': function () {
            $('#mapCanvas').gmap('addMarker', {
            	position: new google.maps.LatLng(core.googleMaps.latitude, core.googleMaps.longitude),
            	visible: true,
            	title: core.googleMaps.pinTitle
            });
        }
    });
	$('#mapCanvas').dialog('open');
};


$(document).ready(function(){ core.startup(); });
