/*--------------------------------------	init.js */

/*--------------------------------------	table of contents */

/*	
	*document ready
	*javascript-enabled
	*pdf icon
	*wrap asset listing pagination in spans
	*menu selected fix
	*balance heights
	*image gallery banner
	*image gallery
	*utilities
	*bookmark
	*textResize
	*print preview
	*home page functionality
		*information for tabs
		*expand collapse panels
        *ensure equal height of panels
	*inside page functionality
		*expand collapse panels
	*external links to open new window 
*/

/*--------------------------------------	*document ready */

	$(document).ready(function() {

/*--------------------------------------	*javascript-enabled */

		$('body').addClass("javascript-enabled").removeClass("javascript-disabled");

/*--------------------------------------	*pdf icon */

		$("#content-main a[href$='.pdf']")
			.not("#content-main #search-results a[href$='.pdf']")
			.before('<em class="pdf">pdf document</em>');

/*--------------------------------------	*wrap asset listing pagination in spans */

		if ($('.pagination.news .page-listing').length) {
			$('.pagination.news .page-listing').html($('.pagination.news .page-listing').html()
			.replace(/(1 )/g, '<b>$1</b>')
			.replace(/(2 )/g, '<b>$1</b>')
			.replace(/(3 )/g, '<b>$1</b>')
			.replace(/(4 )/g, '<b>$1</b>')
			.replace(/(5 )/g, '<b>$1</b>')
			.replace(/(6 )/g, '<b>$1</b>')
			.replace(/(7 )/g, '<b>$1</b>')
			.replace(/(8 )/g, '<b>$1</b>')
			.replace(/(9 )/g, '<b>$1</b>')
			.replace(/(10 )/g, '<b>$1</b>'));
		}

/*--------------------------------------	*menu selected fix */

		// Street & Place names wiki

		if($('#street-and-place-wiki').length) {

			$('#navigation-secondary li a:contains("Street")').parent().parent().addClass('selected');			

		};

		// Parks & Sportsgrounds

		if($('#recreational-facilities.parks-and-sportsgrounds').length){

			$('#navigation-secondary li a:contains("Sportsgrounds")').parent().parent().addClass('selected');

		}

		// All Events

		if($('#events.all-events').length){

			$('#navigation-secondary li a:contains("All Events")').parent().parent().addClass('selected');

		}

/*--------------------------------------	*balance heights */	

		function equalHeight(group) {

			tallest = 0;

			group.each(function() {

				thisHeight = $(this).height();				

				if(thisHeight > tallest) {

				tallest = thisHeight;

				}

		});

		group.height(tallest);

		}

		equalHeight($('.balance-height'));

/*--------------------------------------	*image gallery banner */	

		// Carousel functionality for banner image

		if ($('#mycarousel').length) {

		jQuery("#mycarousel").removeClass("javascript-disabled").addClass("jcarousel-skin").jcarousel({

			scroll: 1

		});

		}		
		
/*--------------------------------------	*image gallery banner v2 */		
		
		if ( $('#mycarousel-v2').length	) {
			
			function afterAnimation(carousel){
			
				if(carousel.autoStopped){
					carousel.startAuto()
	
				}
			}

			function paginationUpdate(carousel){

				var firstItem = carousel.first;
				var lastItem = carousel.last;
				var caroselSize = carousel.size();

				if ( caroselSize == 1) {
				
					$('.controls-background, .jcarousel-prev-horizontal, .jcarousel-next-horizontal').hide();
				
				} else {
				
					// Enable or Disable Next Prev buttons
	
					if (firstItem == 1){
	
						$('.jcarousel-prev-horizontal')
							.hide();
	
					} else {
	
						$('.jcarousel-prev-horizontal')
							.show();
	
					}
	
					if (lastItem == caroselSize){
	
						$('.jcarousel-next-horizontal')
							.hide();
	
					} else {
	
						$('.jcarousel-next-horizontal')
							.show();
	
					}	
				
				}
			
			}	

			jQuery("#mycarousel-v2")
				.removeClass("javascript-disabled")
				.addClass("jcarousel-skin")
				.jcarousel({
					scroll: 1,
					auto: 6,
					wrap: 'last',
					/*
					initCallback: function(jc, state) {
	
							  if (state == 'init') {
								jc.startAutoOrig = jc.startAuto;
								jc.startAuto = function() {
								  if (!jc.paused) {
									jc.startAutoOrig();
								  }
								}
								jc.pause = function() {
								  jc.paused = true;
								  jc.stopAuto();
								};
								jc.play = function() {
								  jc.paused = false;
								  jc.startAuto();
								};
								$('li.jcarousel-item').mouseover(function() {
									jc.pause();
								});
								$('li.jcarousel-item').mouseout(function() {
									jc.play();
								});
							  }
							  jc.play();
							},
					*/
					itemLoadCallback: paginationUpdate,
					itemLastInCallback:{
							onAfterAnimation: afterAnimation
				}

	
			});


		}			
		

/*--------------------------------------	*image gallery */

		// Add sorting functionality, collapse table view and add expand collapse links
        if ($('#image-gallery table').length) {
		$('#image-gallery table')
			.tablesorter()
			.addClass('table-sort')
			.find('tr td:nth-child(1)')
			.prepend('<a class="show-hide" href="#">Expand Collapse</a>')
			.find('div')
			.addClass('show');													

		// Table expand and collapse functionality

		$('.show-hide').toggle(
		  function () {
		   $(this).removeClass('show').addClass('hide').siblings('div').removeClass('show').addClass('hide');
		  },
		  function () {
			$(this).removeClass('hide').addClass('show').siblings('div').removeClass('hide').addClass('show');
		  }
		);

		// Carousel functionality for image details page
		if ($('#carousel').length) {
			$('#carousel').jcarousel();

			//Lightbox for image details page
			$('a[rel="image-gallery-details"]').colorbox({
				transition: "fade"
			});
		}
		}

/*--------------------------------------	*utilities */

		$('#utilities ul')

		.prepend(

			'<li id="bookmark"><a title="Bookmark name"  href="#" class="jQueryBookmark">Bookmark</a></li>' +		

			'<li id="decrease"><a title="Decrease text size" href="#">Decrease</a></li>' +

			'<li id="increase"><a title="Increase text size" href="#">Increase</a></li>'

		)

		.append(

			'<li id="print"><a title="Print preview this page" id="link-print-preview" href="#">Print this page</a></li>'

		);	

/*--------------------------------------	*bookmark */

		// Remove bookmark link if not supported

		if ($.browser.safari) {

			$('#utilities #bookmark').remove();

		}

		// Add to bookmarks on click

		$("a.jQueryBookmark").click(function(e){

			e.preventDefault(); // this will prevent the anchor tag from going the user off to the link

			var bookmarkUrl = this.href;

			var bookmarkTitle = this.title;

			if (window.sidebar) { // For Mozilla Firefox Bookmark
				window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
			} 

			else if( window.external || document.all) { // For IE Favorite
				window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
			} 

			else if(window.opera) { // For Opera Browsers
				$("a.jQueryBookmark").attr("href",bookmarkUrl);
				$("a.jQueryBookmark").attr("title",bookmarkTitle);
				$("a.jQueryBookmark").attr("rel","sidebar");
			} 

			else { // for other browsers which does not support
				 //alert('Your browser does not support this bookmark action');
				 return false;
			}
		});

/*--------------------------------------	*textResize */

		$("#content-main")

			.textResize(

				$("#decrease"), 

				$("#increase"), 

				$('<a href="#"></a>'),

				{baseFontSize: "1em", maxZoom: .5, steps: 3}

			);

			$("#content-main")

			.bind('textResized', function(){

				$('.balance-height').css('height', 'auto');

				equalHeight($('.balance-height'));				

			});	

/*--------------------------------------	*print preview */

		$('a#link-print-preview').printPreview();	

/*--------------------------------------	*home page functionality */	

		if ($('body.home').length) {

/* *information for tabs */

			$("#information-for-menu").tabs("#information-for-items > div");

/* *expand collapse panels */

			$('#panel-i-want-to h2').html(

				'<a title="Collapse this panel" href="#">' +

				$('#panel-i-want-to h2').html() +

				'</a>'

			);

			$('#panel-find-out-about h2').html(

				'<a title="Collapse this panel" href="#">' +

				$('#panel-find-out-about h2').html() +

				'</a>'

			);

			$("div.panel h2 a").toggle(function () { 

				$(this)
					.parent()
					.parent()
					.addClass("expand-collapse")
					.find('a')
					.attr('title', 'Expand this panel'); 

				}, function () {

				$(this)
					.parent()
					.parent()
					.removeClass("expand-collapse")
					.find('a')
					.attr('title', 'Collapse this panel'); 

			});

            
         /* *ensure equal height of panels */
            var image_carousel_height = $("#panel-image-carousel").outerHeight(true);
            var quick_links_outer_height = $("#panel-quick-links").outerHeight(true);
            var quick_links_inner_height = $("#panel-quick-links").height();
            var feature_area_height = $("#panel-feature-area").outerHeight(true);
            var left_column_height = image_carousel_height + quick_links_outer_height;
            if (left_column_height > feature_area_height) {    
                $("#panel-feature-area").height(left_column_height);
            }
            else if (left_column_height < feature_area_height) {
                var new_quick_links_inner_height = feature_area_height - image_carousel_height - (quick_links_outer_height - quick_links_inner_height);
                var quick_links_height_difference = new_quick_links_inner_height - quick_links_inner_height;
                var new_quick_links_content_height = $("#panel-quick-links div.content").height() + quick_links_height_difference;
                $("#panel-quick-links div.content").height(new_quick_links_content_height);
            }
		}; 
        
    

/*--------------------------------------	*inside page functionality */	

/* *expand collapse panels */

		if ($('body.inside').length) {

			$('#panel-i-want-to h2').html(

				'<a title="Collapse I want to..." href="#">' +

				$('#panel-i-want-to h2').html() +

				'</a>'

			);

			$('#panel-find-out-about h2').html(

				'<a title="Collapse Find out about:" href="#">' +

				$('#panel-find-out-about h2').html() +

				'</a>'

			);

			$("div.panel h2 a").toggle(function () { 

				$(this).parent().parent().addClass("expand-collapse"); 

				}, function () {

				$(this).parent().parent().removeClass("expand-collapse");

			});

		}	

/* *empty uls */	

		$("#panel-navigation-secondary ul").each(

		function() {
		  var elem = $(this);
		  if (elem.children().length == 0) {
			elem.remove();
		  }
		}
	  );

/*--------------------------------------	*open add social + news items wysiwyg */		

		if( ($('.add-your-event-item').length)){

			$('#calendar_event_single_0_1064_switch_option_div > div')
				.trigger('click');

				//.css('display', 'none')
				//.find('iframe')

			//$('#htmlarea iframe')
				//.css('height', '300px');

			$('html, body').animate({scrollTop:0}, 'slow');

		}		

/*--------------------------------------	*external links to open new window */	

	$('a[href*="external-links"]')
		.attr('target', '_blank');

/*--------------------------------------	*Pagination limiter */	


	// only kicks in if more then 10 page link
	if($(".pagination .page-listing a").length>10){
		$(".pagination .page-listing a").each(function(index){
			// sets how many page link to display
			var offset=5; 
			// initialise variable
			var pageNumber=parseInt($(this).text());
			var pageNumberActive=parseInt($(".pagination .page-listing b").text());
			var pageNumberMin=pageNumberActive-offset;
			var pageNumberMax=pageNumberActive+offset;
			var pageNumberCount=$(".pagination .page-listing a").length+1;

			// range adjustment
			if(pageNumberMin<0){
				pageNumberMin=0;
				pageNumberMax=10;
			}
			if(pageNumberMax>pageNumberCount){
				pageNumberMin=pageNumberCount-10;
				pageNumberMax=pageNumberCount;
			}

			// process page link
			if(pageNumber<pageNumberMin||pageNumber>pageNumberMax){
				// mark page link to remove
				$(this).addClass("remove-page");
			}
		});
		// remove marked pages
		$(".pagination .page-listing .remove-page").remove();
	}

});
