/* Changelog
 *	27/11/09
 *		UB - set title case
 *
 *  04/12/09
 *		UB - override COA_Advert to display X ads instead of default in netcat template
 *
 *  10/02/10
 *		UB - move the loadscript function in here
 */

/***********************************************************
* Global Data
***********************************************************/
var g_coa_calendar = null;
var _SITE_DATA = {
	'url_add_event' : '/add-an-event.html',
	'_REDIRECTION_URL_RULES' : [ { 'pat' : /\/wo.html$/gi, 'url' : '/action/EventHighlights?pc=WHATSON' } ],
	'html_footer_2' : '<div class="inner">' +
	'<div class="foot-block" id="foot-block-1">' +
		'<h2 id="head-foot-did-you-know"></h2><p>Adelaide is often known as the &ldquo;City of Churches.&rdquo; It is believed that for every church that was built in the city, a public house was also built.</p>' +
		'<h2 id="head-foot-about"></h2><p>Official guide to Adelaide! Upcoming events and guide to local sights, entertainment and recreation in the City.</p>' +
	'</div>' +
	'<div class="foot-block info" id="foot-block-2"><h2 id="head-foot-information-centre"></h2><p>Corner of Rundle Mall and James Place<br />Phone: (08) 8203 7611<br />Email: <a href="mailto:visitor@adelaidecitycouncil.com?subject=Site%20Enquiry">visitor@adelaidecitycouncil.com</a></div>' +
	'<ul id="website_list">' +
	'<li class="heading" id="head-foot-interesting-websites"></li>' +
	'<li><a href="http://www.southaustralia.com">Tourism SA</a></li>' +
	'<li><a href="http://www.adelaidefestivalcentre.com.au">Adelaide Festival Centre</a></li>' +
	'<li><a href="http://www.adelaidecentralmarket.com.au">Central Market</a></li>' +
	'<li><a href="http://www.rundlemall.com">Rundle Mall</a></li>' +
	'<li><a href="http://www.heapsgoodsa.com.au">Heaps Good</a><div class="icon heart"></div></li>' +
	'<li><a href="http://zoossa.com.au">Zoos SA</a></li>' +
	'<li><a href="http://www.botanicgardens.sa.gov.au">Botanic Gardens</a></li>' +
	'</ul>' +
	'<div class="foot-map"><div class="foot-map-inner"><div class="foot-hit" id="icon-foot-hit"></div></div></div></div><div style="clear: both"></div>',
	'html_sub_footer' : '<li class="logo-acc" onclick="' + "javascript:location.href='http://adelaidecitycouncil.com';"+ '"></li>',
	'site_plugins' : {
		'whatson' : '/ADCCBRANDWR/StaticAssets/javascript/coa-section-whatson-0610.js',
		'plugin-coa-webcam' : '/ADCCBRANDWR/StaticAssets/javascript/plugin-coa-webcam.js',
		'plugin_slideshow' : '/ADCCBRANDWR/StaticAssets/javascript/plugin-slideshow.js',
		'plugin_google_map' : '/ADCCBRANDWR/StaticAssets/google-maps/js/google-maps.js'
	}
};

var load_css = function(s){ jQuery('head').append('<link rel="stylesheet" href="' + s + '" />'); }

if (typeof ncBPFramework == "undefined"){
	jQuery('body').ready(
		function(){
			_rundle_lantern_style();
			_event_calendar_embed();
			_social_box();
		}
	);
}

/***********************************************************************
* Global code 
***********************************************************************/  

/***********************************************************************
* New COA advert code replacement for June 2010 
***********************************************************************/  

/***********************************************************************
* reposition footer 
***********************************************************************/  
var g_autopos_last_new_h = 0;
var autopos_footer  = function(footer_id, footer_h){
	var new_h = 0;
	try {
		var body_h = jQuery('body').attr('offsetHeight');
		var s = '#' + footer_id;
		var footer_y = jQuery(s).offset().top;
		var h = body_h - footer_y;
		new_h = ((footer_y + footer_h) < body_h) ? h : footer_h;		
		if (new_h != g_autopos_last_new_h){
			jQuery(s).css('height', ((new_h).toString() + 'px'));
			g_autopos_last_body_h = new_h;
		}
	} catch (ex){ /* dummy */ } 
	return new_h;
}

/***********************************************************************
* New COA advert class for June 2010 
***********************************************************************/  
var COA_Adverts = function(){
	this.config = { };

	this.ajax_init = function(){
		var sender = this;
		jQuery.ajax(
			{
			'url' : sender.config['data_src_url'],
			'dataType' : 'xml',
			'success' : function(data){	
				jQuery('.content-block.ad').remove();
				var rows = [];
				var proc_items = function(p, depth){
					var _c = jQuery(p).find('item'); 
					if (depth > 0){
						if (jQuery(p).find('sub_item').length){
							_c = jQuery(p).find('sub_item');
							(function(){
								var r = [];
								_c.each(function(){
									var t = jQuery(this).find('title'); 
									var u = jQuery(this).find('link_url'); 
									var img = jQuery(this).find('image_url'); 
									r[r.length] = {
										'title' : t.text(),
										'url' : u.text(),
										'img' : img.text()
									};
								});
								rows[rows.length] = r;
							})();
						} else if (_c.length == 0) { 
							try {
								var t = jQuery(p).find('title'); 
								var u = jQuery(p).find('link_url'); 
								var img = jQuery(p).find('image_url'); 
						
								rows[rows.length] = [{
									'title' : t.text(),
									'url' : u.text(),
									'img' : img.text()
								}]
							} catch (ex) {
							}
						}
					} else {
						jQuery(_c).each(function(){
							proc_items(this, depth + 1);
						});
					}
				}
				proc_items(data, 0);
				for (var i = 0; i < rows.length; ++i){
					(function(){
						var h = '<div class="cblk-ad">';
						for (var j = 0 ; j < rows[i].length; ++j){
							h +='<a title="' + rows[i][j].title + '" href="' + rows[i][j].url + '"><img class="img-' + j.toString() + '" src="' + rows[i][j].img + '" /></a>';
						}
						h += '<div style="clear: both"></div></div>';
						jQuery('.column-c').append(h);
					})();
				}
				autopos_footer('footer-sub', 40);
			},
			'error' : function(e){ }
			}
		);
	}

	this.init = function(){
		this.ajax_init();
	}

	if (arguments.length > 0){
		var _config = arguments[0];
		if (_config != null && typeof _config != 'undefined'){
			for (var k in _config){ this.config[k] = _config[k]; }
		}
	}

	this.init();
}

var __main_menu_init = function(){ $('#main-menu').jdMenu( { onShow: loadMenu, onHide: onHideMenu }); }; function loadMenu(){  } function onHideMenu(){ $(this).css('display', 'none'); } function onHideCheckMenu() { return !$(this).parent().is('.LOCKED'); } 

/*var __preload_main_menu_ids = ['whatson', 'entertainment', 'attractions', 'recreation', 'accommodation', 'getaround', 'study']; var __preload_main_menu_baseurl = '../assets/menus'; for (var i =0 ;i < __preload_main_menu_ids.length; ++i){ (new Image()).src = __preload_main_menu_baseurl + '/' + 'nav-' + __preload_main_menu_ids[i] + '-on.gif'; }  */
 
/***********************************************************************
* Start netcat safe block
***********************************************************************/  
 if (typeof ncBPFramework == "undefined" && self == top){
	 var _redirect_rules_exec = function(){
		for (var i = 0 ; i < _SITE_DATA._REDIRECTION_URL_RULES.length; ++i){
			if (_SITE_DATA._REDIRECTION_URL_RULES[i].pat.test(location.href)){
				location.href = _SITE_DATA._REDIRECTION_URL_RULES[i].url;
				break;
			}
		}
	 }
	 _redirect_rules_exec();
 
	var _social_box = function(){
		var social_list = {
			'webcam' : { 'url' : '/attractions/webcams.html', 'title' : 'Webcams' },
			'facebook' : { 'url' : 'http://www.facebook.com/pages/City-of-Adelaide/179523605276', 'title' : 'Facebook' },
			'twitter' : { 'url' : 'http://twitter.com/cityofadelaide' , 'title' : 'Twitter'},
			'flickr' : { 'url' : 'http://www.flickr.com/photos/cityofadelaide', 'title' : 'Flickr' }
		};
		if (jQuery('#btn-webcam-feed').length == 0){ 
			jQuery('.column-c').append('<div id="btn-webcam-feed"></div>');
		}
		if (jQuery('#btn-webcam-feed').length > 0){ 
			var tmp = jQuery('#weather-feed .temp').text();
			tmp = parseInt(tmp);
			jQuery('#weather-feed .temp').html(tmp.toString() + '<b>&#176;</b>');
			jQuery('#btn-webcam-feed').html('<ul class="soc-list"></ul>'); 
			for (var k in social_list){ 
				var s = '<img src="/ADCCBRANDWR/StaticAssets/images/btn_soc_' + k.toString()+ '.gif" />';
				var u = social_list[k]['url'];
				var t = social_list[k]['title'];
				if (typeof u != "undefined" && u != null){
					s = '<a href="' + u + '" ' + ((typeof t != "undefined") ? 'title="' + t + '"' : '')+ '>' + s + '</a>';	
				}
				jQuery('#btn-webcam-feed ul').append('<li id="btn-soc-' + k.toString() + '">' + s + '</li>');
			}
			jQuery('#btn-webcam-feed ul').append('<li style="clear: both"></li>');
		}
	}

	var _event_calendar_embed = function(){
		if (typeof ncBPFramework == "undefined"){
			jQuery('#event_calendar_container').ready(
				function(){
					if(jQuery('#event_calendar_container').length || jQuery('#event_calendar_iframe').length){
						jQuery('head').append('<link rel="stylesheet" type="text/css" href="/ADCCBRANDWR/StaticAssets/css/event_calendar.css" />');
						if (jQuery('#event_calendar_iframe').length){
							jQuery('#event_calendar_iframe').attr('scrolling', 'no');
						}
					}
				}
			);
		}
	}
 
	var _rundle_lantern_style = function(){
		if (typeof ncBPFramework == "undefined"){
			jQuery('#iframe-rundle-lantern').ready(
				function() {
					if (jQuery('#iframe-rundle-lantern').length){
						jQuery('#iframe-rundle-lantern').css({'margin-top' : '-10px', 'margin-left': '-5px', 'width': '1008px'});
						jQuery('body').append('<div class="iframe-rundle-lantern-fullscreen-bg"></div>');
						jQuery('.iframe-rundle-lantern-fullscreen-bg').ready(
							function(){
								jQuery('.main-wrapper').css('background-color', '#000');
								jQuery('.iframe-rundle-lantern-fullscreen-bg').css({ 'position': 'absolute', 'border' : '0px solid #000', 'width' : '100%', 'height' : '100%', 'background-color': '#000' });
								jQuery('body').css('background-color', '#000');
								jQuery('#footer-main').css('background', '#191919').css('width', '1008px').css('margin-top', '-5px');
								jQuery('#footer-main .nav li a').css('color', 'silver');
								jQuery('#footer-sub .nav li a').css('color', 'silver').css('border-left', '0px solid black');
							}
						);
					}
				}
			);
		}
	}

	/***********************************************************
	* Load external scripts
	***********************************************************/
	jQuery(document).ready(function() {
		if (typeof _SITE_DATA['site_plugins'] != "undefined"){
			for (k in _SITE_DATA['site_plugins']){
				(function(){
				 	var j = k;
					jQuery.getScript(_SITE_DATA['site_plugins'][j], function(){});
				})();
			}
		}
	});

	var current_events_init = function(){
		var url = '/ADCCBRANDWR/StaticAssets/xml/whats_on.xml';
		jQuery.ajax(
			{
				'url' : url,
				'dataType' : 'xml',
				'success' : function(data){
					var f = 0;
					jQuery(data).find('item').each(
						function(){
							try { 
								var title = jQuery(this).find('title');	
								var url = jQuery(this).find('url');	
								var cat = jQuery(this).find('category');
								var icon = jQuery(this).find('icon');
								var s = '<div class="item ' + ((++f == 1) ? 'first' : '') + '">' + '<div class="icon ' + ((icon.length > 0) ? icon.text() : '')+ '"></div><a href="' + ((url.length > 0) ? url.text() : '') + '"><span class="text">' + title.text() + '</span></a>' + '</div>';
								jQuery('#event-list').append(s);
							} catch (ex){
								jQuery('#event-list').append('<div class="error">Error reading item data</div>');
							}
						}
					);
					var add_ellip = function(p){ jQuery(p).append('<div class="ellip">...</div>'); }
					jQuery('#event-list .item a').each(
						function(){
							var m = 1;
							var max_len = 24;
							var t = jQuery(this).find('.text');
							var s = t.text();
							s = s.replace(/^\s+|\s$/gim, '');
							if (s.length > max_len){
								t.attr('title', s);
								var g = s.match(/(.){24}/gim);
								s = g[0];	
								s = s.replace(/\s+$/gim, '');
								t.text(s + '...');
							}
						}
					);
					jQuery('#event-list').before('<h1 id="head-event-list"><span>Events this Week</span></h1>');
					autopos_footer('footer-sub', 40);
				},
				'error' : function(){
					jQuery('#event-list').append('<li class="error"></li>');
				}
			}
		);
	}

	/***********************************************************
	* Updates for June 2010 
	***********************************************************/
	jQuery(document).ready(
		function(){
			var footer_init = function(){
				jQuery('#footer-main').append(_SITE_DATA.html_footer_2);
				jQuery('#footer-sub ul.nav').append(_SITE_DATA.html_sub_footer);

				jQuery('#footer-main').prepend('<div class="bg-footer"></div><div class="skyline"></div>');
				jQuery('#icon-foot-hit').click(
					function(){ location.href = '/city-guide/information-centre.html'; }
				);
				jQuery('#head-foot-information-centre').click(
					function(){ location.href = '/city-guide/information-centre.html'; }
				);
				jQuery('window').resize( function(){ var nh = autopos_footer('footer-sub', 40); });
				autopos_footer('footer-sub', 40);
			}
			footer_init();


			var scripts = { 'header' : '/ADCCBRANDWR/StaticAssets/javascript/plugin-transparent-header.js?r=' + Math.random().toString() };
			for (var k in scripts){ (function(){ jQuery.getScript(scripts[k], function(){}); })(); }
			jQuery('#main-menu li.item').each( function(){ jQuery(this).prepend('<div class="edge-l"></div><div class="edge-r"></div>'); });
			var preload_images = {
				0: '/ADCCBRANDWR/StaticAssets/assets/menus/nav-all-on.png',	
				1: '/ADCCBRANDWR/StaticAssets/assets/menus/nav-all-over.png'
			};
			jQuery('body').append('<div id="preload_area"></div>');
			for (var k in preload_images){
				(function(){
					var img = new Image();
					img.src = preload_images[k];
					jQuery('#preload_area').append(img);
				})();
			}
			if (jQuery.browser.msie){
				jQuery('#main-menu').addClass('ie-menu');
				jQuery('#main-menu ul.menu-level-2').each(
					function(){
						var _p = this;
						var _widest = 0;
						jQuery('li.item a', this).each(
							function(){
								var _w = jQuery(this).text().length;
								_widest = (_widest > _w) ? _widest : _w;
							}
						);
						jQuery('li.item a', this).css('minWidth', (_widest /2 + 2).toString() + 'em');	
					}
				);
			}
			jQuery('div.header img.site-logo').remove();
			jQuery('div.header').append('<div class="site-logo" onclick="location.href=' + "'/'" + ';"></div>');
	
			(function(){
				var _c = jQuery('#main-menu #nav-recreation').clone();	
				jQuery('#main-menu #nav-recreation').remove();
				jQuery(_c).attr('id', 'nav-parklands');
				jQuery('#main-menu #nav-entertainment').before(_c);
			})();

			var calendar_init = function(){
				jQuery.getScript('/ADCCBRANDWR/StaticAssets/javascript/plugin-event-calendar.js', function(){ 
					var _t = setInterval(
						function(){
							if (typeof plugin_event_calendar != "undefined"){
								var p = new plugin_event_calendar({ 'id' : 'event-calendar'});
								jQuery('#event-calendar').before('<h1 id="head-whats-on-calendar"><span>What&rsquo;s On Calendar</span></h1>');
								g_coa_calendar = p;
								if (jQuery('#event-list').length > 0 && jQuery('#btn-add-your-event').length == 0){
									jQuery('#event-list').after('<a href="' + _SITE_DATA.url_add_event + '"><div id="btn-add-your-event"></div></a>');
								}
								clearInterval(_t);
							} 
						}, 32 
					);
				});
			}
			calendar_init();
			current_events_init();
		}
	);
}
/***********************************************************************
* End netcat safe block
***********************************************************************/  
