if (typeof console == "undefined"){
	 var console = { 'log' : function() { } };
}

/**********************************************************************
*
* Adelaide City Council
*
* Global site script.. Please put ahead of all custom scripts
*
**********************************************************************/
/**********************************************************************
* Global data
* Keep global javascript values in here
* Most of these may become redundant when used with expression
* engine.
*
* To override add a data-* attribute of the relevant DOM element
*
* eg. <div id="obj" data-maps_api_key="new-api-key"></div>
*
***********************************************************************/
G_COA_VARS = {};
var IS_ANIMATING = false;

/**********************************************************************
* An array of flags for globally accessible simple values
*
* eg. G_COA_FLAGS['_big_map_loaded'] = true;
*
**********************************************************************/
var G_COA_FLAGS = {};

/**********************************************************************
* Global scripts
* Keep global javascript functions and objects here
*
* If scripting is only relevant to a section or a template, please
* put them into their respective script files and not in here
*
***********************************************************************/
jQuery.fn.coa_page_init = function(){ return this; }
jQuery(document).ready(
function($){
var tier_min = 2;
var tier_max = 3;
/**********************************************************************
* list of references to nav link elements
***********************************************************************/

//main_init();
var main_init_2 = function(){
var ARCHIVE_DATA = [];
var gal_mouseover = false;
var info_mouseover = false;

$('.pic-wrap').css('position', 'relative').css('overflow', 'hidden');
if ($('#prog-update img.pic2').length == 0){ $('#prog-update img.pic').before('<img class="pic2" />'); }
$('#prog-update img.pic2').css('position', 'absolute').css('left', '-460px').css('top', '0px');
if ($('.pic-wrap .next').length == 0){
	$('.pic-wrap').append('<div class="prev"></div><div class="next"></div>');
}

jQuery('.next, .prev').hide();

var current_n = 0;
jQuery('.info-wrap').bind('mouseover', function(){
	info_mouseover = true;
});
jQuery('.info-wrap').bind('mouseout', function(){
	info_mouseover = false;
});
jQuery('.pic-wrap').bind('mouseover', function(){
	gal_mouseover = true;
	if (current_n > 0){ jQuery('.next', this).show(); } else { jQuery('.next', this).hide(); }
	if (current_n < (ARCHIVE_DATA.length - 1)){ jQuery('.prev', this).show(); } else { jQuery('.prev', this).hide(); }
});
jQuery('.pic-wrap').bind('mouseout', function(){
	gal_mouseover = false;
	jQuery('.next, .prev').hide();
});
/* reset positions */

var direction = -1; /* -1 back 1 forward */

var set_item = function(index){
	if (ARCHIVE_DATA.length > 0){
		if (index >= 0 && index < ARCHIVE_DATA.length){
			var b = ARCHIVE_DATA[index];


b.click();
jQuery('.prev,.next').hide();
	/*		var src = $(b).attr('data-img');
			var cmnt = $(b).attr('data-comment');
			var head = b.html();

			$('#prog-update img.pic').attr('src', src);
			$('#prog-update .caption').text(cmnt);
			$('#prog-update h5').text(head);

			current_n = index;*/
		}
	}
}
$('.next').click(function(){
	if (ARCHIVE_DATA.length > 0){
		if ((current_n - 1) < 0){
			current_n = (ARCHIVE_DATA.length - 1);	
			ARCHIVE_DATA[current_n].click();
			console.log(current_n + ' changed');
		} else {
			ARCHIVE_DATA[current_n - 1].click();
			console.log(current_n);
		}
		direction = -1;
		if (gal_mouseover){
			if (current_n > 0){ jQuery('.next').show(); } else { jQuery('.next').hide(); }
			if (current_n < (ARCHIVE_DATA.length - 1)){ jQuery('.prev').show(); } else { jQuery('.prev').hide(); }
		} else {
			jQuery('.next, .prev').hide();
		}
	}
});
$('.prev').click(function(){
	if (ARCHIVE_DATA.length > 0){
		if ((current_n + 1) >= ARCHIVE_DATA.length){
			current_n = 0; 
			ARCHIVE_DATA[current_n].click();
			console.log(current_n);
		} else {
			ARCHIVE_DATA[current_n + 1].click();
			console.log(current_n);
		}
		if (gal_mouseover){
			if (current_n > 0){ jQuery('.next').show(); } else { jQuery('.next').hide(); }
			if (current_n < (ARCHIVE_DATA.length - 1)){ jQuery('.prev').show(); } else { jQuery('.prev').hide(); }
		} else {
			jQuery('.next, .prev').hide();
		}
	}
});

$('body').append('<div style="display: none !important;" id="preload_area"></div>');

var global_timer = null;
var timer_interval = 5000;
var guid = 0;
var add_link = function(t, n){
	$('#archive-links').append(
		'<li><span data-n="' + n.toString() + '" data-rel="' + n.toString() + '">' + t + '</span></li>'
	);
}

$('#archives > span').each(
function(){
var src = $(this).attr('data-img');
//var cmnt = $(this).attr('data-comment');
var cmnt = $('.comment', this).html();
var tl = $('.title', this).html();
console.log(cmnt);
if ($('span.caption', this).length > 0){
	cmnt = $('span.caption', this).html();
}

if (guid == 0){
	$('#prog-update img.pic2').attr('src', src);
	$('#prog-update .caption').html(cmnt);
	$('#prog-update h5').text(tl);
}

add_link(tl, guid);

var b = $('#archive-links span[data-rel="' + guid + '"]');

/* preload */
$('#preload_area').append('<img src="' + src + '" style="display: none !important;" />');
var c_b = guid;
if (b.length > 0){
	b.attr('data-n', guid.toString());
	ARCHIVE_DATA[guid] = b;
	b.click(function(){
		//if (current_n == c_b) { console.log('current_n == c_b'); return; }
		if (IS_ANIMATING){ console.log('is animating');  return; }
		IS_ANIMATING = true;
		if (current_n < c_b){ direction = 1; } else { direction = -1; }
		//console.log(current_n.toString() + ', ' + c_b.toString());
		current_n = c_b;

		var head = $(this).html();

		$('#prog-update img.pic').css('position', 'absolute').css('left', '0px').css('top', '0px');
		var q = (direction > 0) ? '-460px'  : '460px';
		$('#prog-update img.pic2').css('position', 'absolute').css('left', q).css('top', '0px');
		$('#prog-update img.pic2').bind('load', function(){
			var p2 = $(this);
			p2.stop().animate({ left: '0px' }, 'medium', function(){ $(this).unbind('load'); });
			var q = (direction > 0) ? '460px'  : '-460px';
			$('#prog-update img.pic').stop().animate({ left: q }, 'medium', function(){
				/* perform swap here */
				p2.removeClass('pic2').addClass('pic');
				$(this).remove();
				$('#prog-update img.pic').before('<img class="pic2" />');
				var q = (direction > 0) ? '460px'  : '-460px';
				$('#prog-update img.pic2').css('position', 'absolute').css('left', q);
				IS_ANIMATING = false;
			});
		});
		$('#prog-update img.pic2').attr('src', src);
		$('#prog-update .caption').html(cmnt);
		$('#prog-update h5').text(head);
	});
	guid++;
	}
}); /* each archive */
//set_item(0);
current_n = 0;

global_timer = setInterval(
	function(){
		if (!gal_mouseover && !info_mouseover){
			$('.next').trigger('click');
		} else {
			console.log('is mouse over');
		}
	},
	timer_interval
);
}
ANIMATING = false;
var init_widgets = function(){
	var n2 = 0;
	jQuery('h2[data-widget-type=button]').each(function(){
		var n = jQuery(this).next();
		n.hide();

		var name = '#a' + n2.toString(); 

		jQuery(this).css('cursor', 'pointer');
		//jQuery(this).addClass('open-state');

		if (jQuery(this).hasClass('on-state')){ n.slideToggle('fast',function(){ }); }

		jQuery(this).click(function(){
			if (ANIMATING) return;
			ANIMATING = true;
			if (jQuery(this).hasClass('on-state')){
				jQuery(this).removeClass('on-state');
			} else {
				jQuery(this).addClass('on-state');
			}
			n.slideToggle('fast',function(){ 
				ANIMATING = false;
			}); 
		});
		++n2;
	});
	n2 = 0;
	jQuery('h2[data-widget-type=button]').each(function(){
		var n = jQuery(this).next();
		jQuery(this).before('<a name="a' + n2.toString() +  '" style="height: 0px !important; display: none !important;" ></a>');
		n2++;
	});
}

init_widgets();
main_init_2();

var main_init_3 = function(){
	$('.nav.tier-2 .level-1').each(function(){
		$(this).bind('mouseover',function(){
			$('.nav.tier-2 .level-1 > ul').css('display', 'none');
			$('ul', this).css('display', 'block');
		});
		$(this).bind('mouseleave',function(){
			$('.nav.tier-2 .level-1 > ul').css('display', 'none');
		});
	});

	$('.crumb-wrap .last').each(function(){
		var mw = 90;
		$(this).css('position', 'relative');
		$(this).css('left', Math.floor(-($(this).width() - mw)/2));
	});
}

main_init_3();
/**********************************************************************
* Configure internally referenced pages
**********************************************************************/
});


