﻿$(document).ready(function() {
	$('ul.sub_menu').hide();
	var decorative_bottom = '<div class="bottom_left"><div class="bottom_right">&nbsp;</div>&nbsp;</div>';
  $('.featurette:last-child').after(decorative_bottom);
  $('ul#case_studies li').append(decorative_bottom);
  $('div.related, #col_3 .box, #col_1 .box').append(decorative_bottom);
  
  
  if(typeof sIFR == "function"){
    sIFR.replaceElement(".mast ul li h3", named({sFlashSrc: "/flash/conduit_itc_bold.swf", sColor: "#ffffff", sBgColor: "#99E3F9", sWmode: "transparent"}));
    sIFR.replaceElement(".medium h3", named({sFlashSrc: "/flash/conduit_itc_bold.swf", sColor: "#ffffff", sBgColor: "#99E3F9", sWmode: "transparent"}));
    sIFR.replaceElement(".mast h3", named({sFlashSrc: "/flash/conduit_itc_light.swf", sColor: "#ffffff", sBgColor: "#99E3F9", sWmode: "transparent"}));
    sIFR.replaceElement("#front h2", named({sFlashSrc: "/flash/conduit_itc_light.swf", sColor: "#ffffff", sBgColor: "#99E3F9", sWmode: "transparent"}));
  };
  /* FAQ Slider */
  $('#faq_question input, #faq_question textarea,#faq_question label').hide();
  $('#faq_question legend').click(function(){
    $(this).toggleClass('open');
    $('#faq_question input, #faq_question textarea,#faq_question label').slideToggle(100);
  });
  $("tr:nth-child(odd)").addClass("odd");
  $('div#login_form').hide().parent().find('a').click(function(){
    $('div#login_form').slideToggle(100);
    return false;
  });

  
  /* Solutions Submenu slider */
  
  $('ul#primary_nav > li:first > a').click(function(){
    $('ul.sub_menu').slideToggle(50);
    return false;
  });
  
	$('ul#primary_nav > li:first > a').mouseover(function(){
		$('ul.sub_menu').slideDown(100);
	});
	$('ul#primary_nav > li:not(:first) > a').mouseover(function(){
		$('ul.sub_menu').slideUp(300);
	});
	$('ul#primary_nav').mouseout(function() {
		if (this.movement) clearTimeout(this.movement);
		this.movement = setTimeout("$('ul.sub_menu').slideUp(300)", 5000);
	});
	animateBanner($('div#front'), 2, 7500);
  
  
  $('img').ifixpng();
  
  /* Validation of forms */
  
  
  var lang = $('meta[name="DC.language"]').attr('content');
  var required, valid_mail, valid_url, valid_number;
  if (lang === 'da') {
    required = 'Dette felt skal udfyldes!';
    valid_mail = 'Indtast en gyldig email-adresse!';
    valid_url = 'Indtast en gyldig URL-adresse!';
    valid_number = 'Indtast et fire-cifret postnummer!'
  }
  else {
    required = 'This field is required!';
    valid_mail = 'Please enter a valid email address!';
    valid_url = 'Please enter a valid URL!';
  }
  if ($('#mainform')[0] != null) {
      $('#mainform').validate({
        rules: {
            question:   'required',
            email:      {
                required: true,
                minLength: 5,
                email: true
            },
            name: 'required',
            organisation: 'required',
            url: {
                required:true,
                url:true
            }, 
            country: 'required',
            /*Intranet signup*/
            fullName: 'required',
            e_mail: {
            	email: true
            },
            address1: 'required',
            postcode: {
            	required: true,
            	number: true
            },
            city: 'required'

        },
        messages: {
            question: required,
            email: {
                required: required,
                minLength: valid_mail,
                email: valid_mail
                },
           name: required,
           organisation: required,
           country: required,
           url: {
                required: required,
                url: valid_url
	           },
           fullName: required,
           address1: required,
           e_mail: {
                required: required,
                email: valid_mail
                },
           postcode: {
           		required: required,
           		number: valid_number
           },
           city: required
        }
    });
    if (lang === 'en') {
    	replaceText('wrapper', [ 
			['(fav|col|behavi|endeav)our','$1or','gi'], 
			['(comprom|categor|custom|personal|organ|priorit|summar|local|international|special|real|recogn|central|public|optim|standard)is(e(d)?|ing|ation)', '$1iz$2', 'ig'],
			['SiteImprove', 'Siteimprove', 'g']
		]);
    } else {
    	replaceText('wrapper', [['SiteImprove', 'Siteimprove', 'g']]);
    }
  }
	$('#postcode').keyup(function() {
		if ($(this).val().length === 4) {
			$('#city').val(postcodes().pc[$('#postcode').val()]);
		} else {
			$('#city').val('');
		};
	}).blur(function() {
		$('#city').val(postcodes().pc[$('#postcode').val()]);
	});
	
  //External links open in a new window
  $('a.external').append(' <span>(opens a new window)</span>').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
  
	//Google Analytics event tracking.
	if (typeof(pageTracker) === 'object') {
		$('a[href^="#"]').click(function() {
			pageTracker._trackEvent('Page navigation', 'Skip', $(this).text());
		});
		$('#box_news a[href="/About_Us/News.aspx?xml=1"]').click(function() {
			pageTracker._trackEvent('Non-HTML content', 'Subscribe', 'RSS download');
		});
		$('.w3badge a').click(function() {
			pageTracker._trackEvent('Non-HTML content', 'Validate', document.location.href);
		});
	};
  
  
  $('.accordion li').each(function(i) {
    var $$ = $(this),
        desc = $$.find('.desc');
    if (i != 0) {
      desc.hide();
    } else {
      $$.find('h3 a').addClass('active');
    }
    $$.find('h3 a').click(function() { 
      if ($(this).hasClass('active')) {        
        $(this).removeClass('active');
        desc.slideUp(250);
      } else {
        $('.accordion .active').removeClass('active').parents('li').find('.desc').slideUp(250);
        desc.slideDown(250);
        $(this).addClass('active');
      }
      return false;
    });    
    
  });
  
});

function animateBanner (elm, repeats, duration) {
	if (!document.getElementsByClassName) return false; //Not pretty... I don't want this animation to happen on any machine but the most powerful, running the newest browsers, so I ask this assuming that users who run FF#, Chrome, Safari & Opera get decent performance.
	$(elm).css({
		backgroundPosition: '-500px 0px'
	}).animate(
		{backgroundPosition: '500px 0px'}, 
		duration
	).animate(
		{backgroundPosition: '-500px 0px'},
		duration + 1000, 
		function(){
			if (repeats > 0) {
				repeats--;
				animateBanner(elm, repeats, duration);
			}
		}
	)
}
function replaceText (elementID, values) {
	values = values || [];
	if (!document.getElementById) return;
	elementID = elementID || 'wrapper';
	var els = document.getElementById(elementID).getElementsByTagName('*');
	var noOfReplacements = 0;
	var match = false;
	/*var values = [ // in the form [expression, replacement, flags (i: case insensitive; g: global)]
		['(fav|col)our','$1or','gi'], 
		['(comprom|categor|custom|personal|organ|priorit|summar)is(e(d)?|ing)', '$1iz$2', 'ig'],
		['SiteImprove', 'Siteimprove', 'g']
	];*/
	for (var i=0; i < els.length; i++) {
		var ch = els[i].childNodes;
		for (var k=0; k < ch.length; k++) {
			if (ch[k].nodeType !== 3) {  //If this is not a text node, try the next node.
				continue;
			} else {
				var val = ch[k].nodeValue;
				for (var j=0; j < values.length; j++) {
					reg = new RegExp(values[j][0], values[j][2]);
					reg.test(val) ? match = true : match = false;
					if (match === true) {
						val = val.replace(reg, values[j][1]);
						noOfReplacements++;
					} else {
						continue;
					};
				};
				ch[k].nodeValue = val;
			}
		};
	};
};