if (typeof window.console == "undefined") console = {};
if (typeof console['log'] == "undefined") console['log'] = function(){};
if (typeof console['error'] == "undefined") console['error'] = function(){};
if (typeof console['info'] == "undefined") console['info'] = function(){};
if (typeof console['warn'] == "undefined") console['warn'] = 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 = {};

/**********************************************************************
* An array of flags for globally accessible simple values 
*
*	eg. G_COA_FLAGS['_big_map_loaded'] = true; 
* 	
**********************************************************************/
var G_COA_FLAGS = {};
var LAST_HASH = null;

var G_GOOGLE_MAP = null;


/**********************************************************************
* Associated array callbacks for links, key is matched with link tag's 
* href attribute
*
* Mostly use this for adding dynamic content to sub pages only when
* they are loaded
**********************************************************************/
var G_LINK_CALLBACKS = {
	'#permanent-reserved-parking-form' : function(){
		$(document).ready(
			function($){
				var u = 'https://cityofadelaide.wufoo.com/embed/z7x2m9/';
				var f = '#permanent-reserved-parking-form';
				var s = '<iframe border="0" width="600" scrolling="noscroll" frameborder="0" class="form-frame" src="' + u + '">Iframes must be enabled to view this form</iframe>';
				$('#sub-pages .sub-page').css('display', 'none');	
				$('#sub-pages ' + f).css('display', 'block');	
				if ($(f + ' iframe').length == 0){
					$(f + ' .column-b').append(s);
				}
			}
		);
	},
	'#home' : function(){
		if (typeof G_GOOGLE_MAP != "undefined"){
			google.maps.event.trigger(G_GOOGLE_MAP, 'resize');
		}
	}
};


/**********************************************************************
* 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 
*
***********************************************************************/
$.fn.coa_page_init = function(){ return this; }


var isotope_links = {};
jQuery(document).ready(
	function($){
		var isiPad = navigator.userAgent.match(/iPad/i) != null;
		if (isiPad){
			console.log('Running on ipad');
			jQuery('body').css('width', '1024px');
		}

		var tier_min = 2;
		var tier_max = 3;

		/**********************************************************************
		* list of references to nav link elements 
		***********************************************************************/
		var isotope_nav_links = {};	

		/**********************************************************************
		* initialize isotope layout engine 
		***********************************************************************/
		var init_isotope = function(){
			$('#sub-pages .sub-page.home').css('display', 'block');	
		}


		/**********************************************************************
		* clear highlighte menu items by tier 
		***********************************************************************/
		var unhighlight_menus = function(i){
			$('.nav.tier-' + i.toString() + ' li').removeClass('highlight');
		}

		var main_init = function(){
			var __f = function(sender){
				var f = $(sender).attr('data-filter');
				if ((typeof f != "undefined") && (f != null)){
					$(sender).bind('click', function(){
						unhighlight_menus(tier_min); //remove highlights
						unhighlight_menus(tier_max); //remove highlights
						$('#sub-pages .sub-page').css('display', 'none');	
						$('#sub-pages ' + f).css('display', 'block');	
					});
				}
			}
			__f($('#home-button'));

			$('#sub-pages').css('display', 'block');

			for (var i = tier_min; i <= tier_max; ++i){
				(function(){
					$('.isotope-filter-nav-group').each(function(){ 
						$('a', this).each(function(){ 
							var t = this;
							var r = $(t).attr('href');
							var f = $(t).attr('data-filter');

							var _f = function(_t){
								/* load subpage */
								if (typeof f != "undefined" && f != null){ 
									$('#sub-pages .sub-page').css('display', 'none');	
									$('#sub-pages ' + f).css('display', 'block');	
								}

								/* reset menus */
								unhighlight_menus(tier_min); //remove highlights
								unhighlight_menus(tier_max); //remove highlights

								var ref = r.split('/');
								var p = $(_t).parent();
								if (ref.length > 1){
									var q = $('.nav li a[href="' + ref[0] + '"]');
									if (q.length > 0){
										console.log('Found menu ' + ref[0]);
										pq = q.parent();
										pq.addClass('highlight');
									}
									q = $('.nav li a[href="' + r + '"]');
									pq = q.parent();
									pq.addClass('highlight');
								}
								p.addClass('highlight');
							}
							console.log('Binding ' + r);
							isotope_links[r] = function(){ _f(t); }; 
						});
					});
				})();
			}

			if (!/#showall/gim.test(location.href)){
				init_isotope();
				$('#home-button').trigger('click');
			}
		}

		/**********************************************************************
		* Configure internally referenced pages
		**********************************************************************/
		var init_internal_links = function(){
			$('a.isotope-internal-link, a.internal-link').each(
				function(){
					var r = $(this).attr('href');	

					/* see if we can associate it with an existing link */
					if ((r != null && r.length > 0) && (typeof isotope_links[r] != "undefined")){
						$(this).bind('click', 
							function(){
								isotope_links[r]();
								//return false; do not return false so hash can be set
							}
						);
					}

					/* see if we can associate it with a callback */
					if ((r != null && r.length > 0) && (typeof G_LINK_CALLBACKS[r] != "undefined")){
						$(this).bind('click', 
							function(){
								G_LINK_CALLBACKS[r]();
							}
						);
					}
				}
			);
		}

		main_init();
		init_internal_links();

		/**********************************************************************
		* Initialize Takayuki Miwa's history plugin 
		**********************************************************************/
		$.history.init(function(hash){
			if (hash == LAST_HASH) { console.log('Same hash, returning'); return; }
			if (hash == "" || hash == "home"){	
				$('#home-button').trigger('click');
			} else {
				if (typeof isotope_links['#' + hash] == "function"){ isotope_links['#' + hash](); }
				if (typeof G_LINK_CALLBACKS != "undefined" && typeof G_LINK_CALLBACKS['#' + hash] == "function"){ G_LINK_CALLBACKS['#' + hash](); }
			}
			console.log("hash: " + hash);
			LAST_HASH = hash;
		});

	}
);


/* styling init */
$(document).ready(function(){
	var init_table_style = function(){ $('table').each(function(){ $('tr:even', this).addClass('alt'); }); }
	var init_shadows = function(){ jQuery('#wrap-main').prepend('<div class="shadow-wrap"><div class="inner"><div class="shad-l"></div><div class="shad-r"></div></div></div>'); }


	/*********************************
	* adjust last menu item to fit - note IE will report a size even if item is hidden
	*********************************/
	var init_menu_style = function(){
		var ttw = $('.nav.tier-2').width();	

		var p = 10;

		var otw = 0;
		var ltw = 0;

		var n_items = 0;
		$('.nav.tier-2 li').each(
			function(){
				if ($(this).hasClass('last')){
					ltw += $(this).width();
				} else {
					otw += $(this).width();
				}
				++n_items;
			}
		);
		var dm = 'Nav width: ' + ttw.toString() + ' Menu item width: ' + ltw.toString() + ', Total others width: ' + otw.toString();
		if ((ltw + otw) < ttw){ 
			var _adj = parseInt((ttw - (ltw + otw)) / 2, 10);
			var adj = parseInt((ttw - (ltw + otw)) / n_items, 10);
			if (adj == 0){
				var cssw = parseInt($('.nav.tier-2 li.last').css('width'),10);
				$('.nav.tier-2 li.last').css('width', (cssw + _adj).toString() + 'px');
			} else {
				$('.nav.tier-2 li').each(
					function(){
						var cssw = parseInt($(this).css('width'),10);
						$(this).css('width', (cssw + adj).toString() + 'px');
					}
				);
			}
		}
	}
	init_menu_style();
	init_table_style();
	init_shadows();
});


var callback_maps_ready = function(){
	this.center = new google.maps.LatLng(-34.92735457489657, 138.60201358795166);
	/*this.locations = [ 
		[10,'Central Market', -34.93004621180896, 138.59810829162598],	
		[2,'Frome Street', -34.922197,138.607882],
		[1,'Gawler Place',  -34.921954,138.601747],	
		[4,'Grenfell', -34.923773,138.603142],	
		[9,'Grote Street', -34.928078,138.595838],	
		[3,'Light Street', -34.92374,138.594757],	
		[8,'Pirie Flinderes', -34.925382,138.607568],	
		[5,'Rundle', -34.923011,138.606711],	
		[11,'Sturt', -34.933011,138.597964],	
		[6,'Topham', -34.92519,138.597199],	
		[7,'Wyatt', -34.925554,138.603846]
	];*/
	this.locations = [ 
		[9,'Central Market', -34.93004621180896, 138.59810829162598],	
		[2,'Frome Street', -34.922197,138.607882],
		[1,'Gawler Place',  -34.921954,138.601747],	
		[8,'Grote Street', -34.928078,138.595838],	
		[3,'Light Street', -34.92374,138.594757],	
		[7,'Pirie Flinderes', -34.925382,138.607568],	
		[4,'Rundle', -34.923011,138.606711],	
		[5,'Topham', -34.92519,138.597199],	
		[6,'Wyatt', -34.925554,138.603846]
	];

	//[10,'Sturt', -34.933011,138.597964],	
	this.options = {
		zoom: 15,
		center: this.center,
		disableDefaultUI: true,
		disableDoubleClickZoom: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}	
	this.map = null;
	this.init = function(){
		var sender = this;

		this.map = new google.maps.Map(document.getElementById('home-map-wrap'), this.options);
		for (var i = 0 ; i < this.locations.length; ++i) {
			(function(){
				var loc = this.locations[i];
				var n = (this.locations[i][0] < 10) ? '0' + loc[0].toString() : loc[0].toString();
				var img = '/common/upark/img/markers/red' + n + '.png';	
				var m = new google.maps.Marker({
					position: new google.maps.LatLng(loc[2], loc[3]),
					map: sender.map,
					clickable: false,
					icon : img
				});
			})();
		}
		G_GOOGLE_MAP = this.map;
		google.maps.event.trigger(this.map, 'resize');
	}
	this.init();
}

/* post load plugins */
$(document).ready(function(){
	$.getScript('https://www.google.com/jsapi?sensor=false',
	function(){
		try {
			google.load('maps', '3', {'callback' : 'callback_maps_ready', 'other_params' : 'sensor=false'});
		} catch(ex){
			console.log(ex.toString());
		}
	});
});

