﻿// General container for site-wide scripts
var sitefunctions = {
	//
	//     JQuery Text resize + Cookie recall
	//     by Jonny Kamaly
	//     based on script written by Faisal and Homar
	//     modified by Nicolaj Kirkgaard Nielsen 
	//
	
	language: "da",
	
	textresize : function(){
		// Add buttons to menu
		//$('#toolbar-list li:eq(2)').after('<li class="text-size"><a href="" class="minus" id="font-size-down" title="Formindsk skriftstÃ¸rrelsen">-</a><a href="" class="plus" id="font-size-up" title="ForstÃ¸r skriftstÃ¸rrelsen">+</a> Tekst</li>');		
		$('#text-size').show();
		
		// Define cookie
		var $cookie_name = "EsbjergKommune-FontSize";
		var originalFontSize = $("html").css("font-size");
		//var sizes = [originalFontSize*0.8,originalFontSize,originalFontSize*1.2];

		// if exists load saved value, otherwise store it
		if($.cookie($cookie_name)) {
			var $getSize = $.cookie($cookie_name);
			$("html").css({fontSize : $getSize + ($getSize.indexOf("px")!=-1 ? "" : "px")}); // IE fix for double "pxpx" error
		} else {
			$.cookie($cookie_name, originalFontSize);
		}
		// reset link
		/*
		$(".something").bind("click", function() {
			$("html").css("font-size", originalFontSize);
			$.cookie($cookie_name, originalFontSize);
		});*/
		$("#font-size-down").bind("click", function() {
		  var currentFontSize = $("html").css("font-size");
		  var currentFontSizeNum = parseFloat(currentFontSize, 10);
		  var newFontSize = currentFontSizeNum-2;
		  newFontSize = newFontSize < 16 ? 16 : newFontSize;
		  if (newFontSize >= 16) {
			$("html").css("font-size", newFontSize);
			$.cookie($cookie_name, newFontSize);
			// Redraw frontpage columns
			$('#front-self-service,#front-tips-tricks,#front-news,#front-calendar').find('.bd').css('height','auto').end().equalCols();
		  }
		  return false;
		});
		// text "+" link
		$("#font-size-up").bind("click", function() {
			var currentFontSize = $("html").css("font-size");
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum+2;
			newFontSize = newFontSize > 20 ? 20 : newFontSize;
			if (newFontSize <= 20) {
				$("html").css("font-size", newFontSize);
				$.cookie($cookie_name, newFontSize);
				// Redraw frontpage columns
				$('#front-self-service,#front-tips-tricks,#front-news,#front-calendar').find('.bd').css('height','auto').end().equalCols();
			}
			return false;	
		});
	}, 
	// Clear input fields, use in .each() loop input fields
	clearInput : function() {
		var $this = $(this);
		$this.data( "txt", $.trim($this.val()) );
		$this.focus(function(){
				if ( $.trim($(this).val()) === $(this).data("txt") ) {
					$(this).val("");
				}
			}).blur(function(){
				if ( $.trim($(this).val()) === "" && !$(this).hasClass("once") ) {
					$(this).val( $(this).data("txt") );
				}
			});
	}, 
	// Add icons to file lists based on file type, us in .each() loop on a elements		
	addIcons : function () {
		// Define file types and class names
		var fileReg = [ {reg: /\.(pdf)/i, name: 'pdf'}, {reg: /\.(rtf|txt)/i, name: 'txt'}, /*{reg: /\.(doc|docx)/i, name: 'doc'}, {reg: /\.(xls|xlsx)/i, name: 'xls'},*/ {reg: /\.(ppt|pptx)/i, name: 'ppt'}, {reg: /\.(rar|zip|tar\.gz)/i, name: 'zip'}, {reg: /\.(jpg|jpeg|gif|png|bmp|tif|tiff)/i, name: 'img'} ];
		
		var $this = $(this);
		var aText = $this.attr('href');
		var result;
		// Add the icons via classnames, change title to reflect filetype for accessibility
		for (var i=0, fileRegLength=fileReg.length;i<fileRegLength;i++) {
			result = fileReg[i].reg.exec(aText);
			if (!!result) { break; }
		}
		if (!!result) {
			$this.addClass(fileReg[i].name)
				.attr('title', $this.text()+" ("+result[1]+")" );
		}
	},
	// Show translate function with preemptive warning
	showTranslate : function () {
		
		var message = { 
			'da': 'Oversættelse af denne side er leveret af Google Translate. Esbjerg Kommune står derfor ikke til ansvar for korrektheden af oversat indhold.', 
			'de': 'Übersetzungen dieser Seite werden von Google Translate zur Verfügung gestellt. Esbjerg Kommune haftet nicht für die Richtigkeit der übersetzten Inhalte', 
			'en': 'Translations on this page is provied by Google Translate. Esbjerg Municipality is not liable for the accuracy of translated content.'};
		var doTranslate = window.confirm(message[sitefunctions.language]);
		if (doTranslate) {
			if (typeof google != "undefined" && typeof google.translate != "undefined") {
				new google.translate.TranslateElement({ pageLanguage: sitefunctions.language }, 'google_translate_element').showBanner();
			}
		}			
		return false;
	}
};
 
(function($) {
	
	// jQuery ready function
	$(function() {
		// Enable js differentiated CSS
		$('body').removeClass('no-js');
			
		// Run text resize function
		sitefunctions.textresize();	
		
		// Add print functionality in toolbar
		//$('<a href="" title="Udskriv denne side">Udskriv</a>').click(function() { window.print(); return false; }).wrap('<li class="print"/>').parent().prependTo('#toolbar-list');
		$('#print-this').show().find('a').click(function() { window.print(); return false; });
		
		// Add Google translate functionality
		$('#g-translate').show().find('a').click(sitefunctions.showTranslate);
		
		// Add icons to file lists based on file type			
		$('.file-list a').each(sitefunctions.addIcons);

		// Equalize heights of frontpage boxes
		$('#front-self-service,#front-tips-tricks,#front-news,#front-calendar').equalCols();
		$('#frontbox-a,#frontbox-b').equalCols();
		
		// News rotator for front page
		$('#NewsRotator').innerfade({speed: 1000, timeout: 10000, type: 'sequence',containerheight: '200px'});
		
		// Textfield clearing of default values
		$('input.clear').each(sitefunctions.clearInput);
		
		// Top Search autocomplete
		$('#topSearch').autocomplete({
			serviceUrl:'http://autocomplete.searchimprove.com/autocomplete/autocomplete.aspx?callback=?',
			autoSubmit:true,
			minChars:2,
			width:124,
			maxHeight:120,
			deferRequestBy:200,
			highlight:true,
			params: {account_id:273521,package_id:1000796755},
			submitButtonName: '#topSearchSubmit',
			submitAction: function(elm) {;}
		});
		$('#search').autocomplete({
			serviceUrl:'http://autocomplete.searchimprove.com/autocomplete/autocomplete.aspx?callback=?',
			autoSubmit:true,
			minChars:2,
			width:147,
			maxHeight:120,
			deferRequestBy:200,
			highlight:true,
			params: {account_id:273521,package_id:1000796755},
			submitButtonName: '#topSearchSubmit',
			submitAction: function(elm) {;}
		});
	});

})(jQuery);