
/**
 * @author author
 */

/* ::Enable Javascript-enabled stylesheet */
var head_css = $('<link rel="stylesheet" href="/Templates/1/style/js-enabled.css" type="text/css" media="screen" />').appendTo($('head')[0]);

/* -------------------------------------- */


/* ::Global Vars */


/* -------------------------------------- */


/* ::Document Ready function Callers  */
$(document).ready(function(){
	/*jQuery.easing.def = 'easeOutBounce';*/

	/* =Initialize default Functions (Fn) */
	//FnDefault.init();
	//FnDropDownHover.init();
	//FnSlideSwitchTrigger.init();
	//FnIframeFix.init();
	//FnTargetToolTips.init();
	//FnCommentFormSubmit.init(); 	
	//FnInputFocus.init();
	//FnAccordion.init();
	FnTextResize.init();
	FnPrintBtn.init();
	FnTargetSelector.init();
	//FnEvents.init();
	FnSMtoolTips.init();
	FnDatePickers.init();
	//FnPolls.init();
	//FnLegends.init();
});
/* -------------------------------------- */



/* ::Default Functions */
FnDefault = {
	init:function() {

	}	
	
	
	/* -------------------------------------- */
}

/* ::Default Functions */
FnTargetToolTips = {
	init:function() {
		// hover handler on links in targets-bar
		$('#targets-bar a').hover(function() {
		// in
			// calculate height of tooltip to position it
			$toolTipHeight = $(this).prev().height();

			// get padding of tooltip
			$toolTipPaddingTop = $(this).prev().css('padding-top');
			$toolTipPaddingBottom = $(this).prev().css('padding-bottom');			

			$startIndexPx = $toolTipPaddingTop.indexOf("px");
			$toolTipPaddingTop = parseInt($toolTipPaddingTop.substr(0,$startIndexPx));
			
			// padding-bottom
			$startIndexPx2 = $toolTipPaddingBottom.indexOf("px");
			$toolTipPaddingBottom = parseInt($toolTipPaddingBottom.substr(0,$startIndexPx2));
								
			// set margin-top to negative
			$toolTipMarginTop = -($toolTipHeight + $toolTipPaddingTop + $toolTipPaddingBottom + 3) + 'px';
			//alert($toolTipMarginTop);
			$(this).prev().css('margin-top', $toolTipMarginTop);
			$(this).prev().addClass('show');			
		}, function() {
		// out
		$(this).prev().removeClass('show');		
		});
	} 
	
	/* -------------------------------------- */
}


/* ::Dropdownmenu */
/**
 * @description function looks for the submenu's in the html and moves it into the main navigation after the A element.
 */
 
FnDropDownHover = {
	hideTimer: null,
	activeList: null,
	mainNavHovered: false,
	showItem:null,
	showItemInt:null,
	init:function() {
		$('.grid-4').hide();
		// get and place submenus
		$('#main-navigation li:not(.no-dropdown) a').each(function() {
			// add classname for detection of mainmenu-links
			$(this).addClass('mm-item');
			
			// get id
			thisItemId = $(this).attr('id');
			// get submenus
			$('#' + thisItemId + '-container')
				//.hide()
				.insertAfter(this)
				
			this.list = $('#' + thisItemId + '-container');			
		});

	
	
	// when hover over header above menu
		$('#main-navigation').hover(function(){
			//	
		}, function(){
			FnDropDownHover.mainNavHovered = false;
		})
		
		// when hover over links in menu that do not have a dropdown
		$('#main-navigation li.no-dropdown').hover(function() {
			// close any open submenu's
			FnDropDownHover.hide();
			FnDropDownHover.mainNavHovered = true;
		},
		function() {
			return false;
		});
		/*$('#main-navigation li.drop-down a').click(function(event) {
			// prevent link click default action
			
			// example links to templates
			// if link is about
			if ($(this).attr('id')== 'mn-why') {
			    event.preventDefault();
				//window.location.href = 'why-sg.php';
			} else
			if ($(this).attr('id')== 'mn-industries')
			 {
			 	event.preventDefault();
				//window.location.href = 'industries.php';
			}  else
			if ($(this).attr('id')== 'mn-faqs')
			 {
			 	event.preventDefault();
				//window.location.href = 'faqs.php';
			} else
			if ($(this).attr('id')== 'mn-interactives')
			 {
			 	event.preventDefault();
				//window.location.href = 'interactives.php';
			} else
			if ($(this).attr('id')== 'mn-news')
			 {
			 	event.preventDefault();
				//window.location.href = 'newsroom.php';
			} else 
			if ($(this).attr('id')== 'mn-events')
			 {
			 	event.preventDefault();
				//window.location.href = 'events.php';
			} else 
			if ($(this).attr('id')== 'mn-jobs')
			 {
			 	event.preventDefault();
				//window.location.href = 'jobs.php';
			} else {
				return true;
			}
		});*/
		
		// hover over links with dropdown
			$('#main-navigation li.drop-down a.mm-item').hover(function() {
				FnDropDownHover.showItem = $(this);
				FnDropDownHover.showItemInt = this;
				if (FnDropDownHover.mainNavHovered) 				
					FnDropDownHover.show();
				else {
					FnDropDownHover.showTimer = setTimeout(function(){
						FnDropDownHover.show()
					}, 250);
				}
				FnDropDownHover.mainNavHovered = true;
			},
			function() {
				clearTimeout(FnDropDownHover.showTimer);
				FnDropDownHover.hideTimer = setTimeout(function(){ FnDropDownHover.hide(); }, 1000);
			});
		
			// Remove submenu ater mouseout of submenu by removing the 'open' class on the parent of the hovered link (LI).
/* 			var hideTimer = false;  */

			$('#main-navigation .submenu-container').hover(function() {
				//alert('fdfds')
				clearTimeout(FnDropDownHover.hideTimer);
			},
			function() {
				FnDropDownHover.hideTimer = setTimeout(function(){ FnDropDownHover.hide(); },1000);

			});

		},
		hide:function(){
			if (FnDropDownHover.activeList != null) {
				FnDropDownHover.activeList.parent().removeClass('open');
				FnDropDownHover.activeList.prev().removeClass('hover');
				/*FnDropDownHover.activeList.hide();slideUp({
					duration:500, 
					easing:'easeOutExpo', 
					complete:function(){
						//
					}
				});*/
			}
		},
		show:function(){
			FnDropDownHover.showItem.addClass('hover');
			clearTimeout(FnDropDownHover.hideTimer);					
			if (FnDropDownHover.activeList != null && FnDropDownHover.activeList != FnDropDownHover.showItemInt.list) {
				FnDropDownHover.activeList.dequeue("fx");
				FnDropDownHover.hide();
			}
			FnDropDownHover.activeList = FnDropDownHover.showItemInt.list;
			FnDropDownHover.showItem.parent().addClass('open');	
			/*FnDropDownHover.showItemInt.list.slideDown({
				duration:500, 
				easing:'easeOutExpo', 
				complete:function(){
					FnDropDownHover.showItem.parent().addClass('open');	
				}
			});*/		
		}
	
	
	/* -------------------------------------- */
}


/* ::IE6 Iframe fix */
FnIframeFix = {
	init:function() {
		$('#userAgent').html(navigator.userAgent);
		$('#main-navigation .submenu-container').bgiframe();
	}	
	
	
	/* -------------------------------------- */
}

/* ::!slideswitch */
FnSlideSwitch = {
	init:function() {

    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });



	}	
	
	
	/* -------------------------------------- */
}

/* ::!triggers slideswitch */
FnSlideSwitchTrigger = {
	init:function() {
	    setInterval( "FnSlideSwitch.init()", 5000 );
	}

}


/* ::TextResize Function */
FnTextResize = {
	init:function() {
		  // Reset Font Size
		  var contentArea = $('.sub_content_area');
		  var originalFontSize = contentArea.css('font-size');
		  $("#resetFont").click(function(){
			contentArea.css('font-size', originalFontSize);
		  });
		  // Increase Font Size
		  $("#increaseFont").click(function(){
			var currentFontSize = contentArea.css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			if ( newFontSize < 20 ) {
			contentArea.css('font-size', newFontSize);
			}
			return false;
		  });
		  // Decrease Font Size
		  $("#decreaseFont").click(function(){
			var currentFontSize = contentArea.css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*0.8;
			if ( newFontSize > 8 ) {
			contentArea.css('font-size', newFontSize);
			}
			return false;
		  });

	}
}
FnTextResizeX = {
	init:function() {
	
	// only do this if text-resize element is there
	if ( $("#text-resize-tool").length > 0 ) {
	
	alert("text resize init()");
	
		var $content = $('div#page-container');			// save main content div in var

	// if a cookie already exists: load saved value, otherwise do nothing
		if($.cookie('cookie_textsize_class')) {
			var $getClassname = $.cookie('cookie_textsize_class'); // get value
			// add class to content div
			$content.addClass($getClassname);
			//Cufon.refresh();
			// depending on which classname is set to the content div, the li class changes
			if ($getClassname=='textsize-normal'){
				//$('span#textsize-normal-link').addClass("active");
			} else if ($getClassname=='textsize-large' || $getClassname=='textsize-larger'){
				//$('span#textsize-large-link').addClass("active");
			} else if ($getClassname=='textsize-small' || $getClassname=='textsize-smaller'){
				//$('span#textsize-small-link').addClass("active");
			}
		} else {
			// set normal textsize link to be active
			//$('span#textsize-normal-link').addClass("active");
		}
	$('#text-resize span').click(function(){
		if (this.id == 'textsize-large-link') { 				// enlarge text
		//alert("text resize : large");
		
		// check class on page-container
		var setClass = $('#page-container').attr('class');
			if (setClass == "textsize-larger") {
				// don't do anything
				//alert('You have reached the max enlargement')
				return false;
			
			} else if (setClass == "textsize-large") {
				// set class to larger
				// disable enlarge link
				$content.addClass("textsize-larger");
				$content.removeClass("textsize-large");			
				$content.removeClass("textsize-small");
				$('span#textsize-large-link').addClass("disabled");
				$('span#textsize-large-link a').attr('title','');							
				$.cookie('cookie_textsize_class', 'textsize-larger', { expires: 365, path: "/"} ); // save cookie
				//Cufon.refresh();
				//FnSitemap();
				return false;		
	
			} else if (setClass == "textsize-smaller") {
				// enlarge 1 time
				$content.attr('class', '');
				$content.addClass("textsize-small");
				$('span#textsize-small-link').removeClass("disabled");				
				$.cookie('cookie_textsize_class', 'textsize-small', { expires: 365, path: "/"} ); // save cookie
				//Cufon.refresh();
				return false;
			} else if (setClass == "textsize-small") {
				// enlarge 1 time
				$content.attr('class', '');
				$.cookie('cookie_textsize_class', null, { path: "/"} ); // delete cookie
				//Cufon.refresh();
				return false;								
			} else {
				$content.addClass("textsize-large");
				$content.removeClass("textsize-small");
				$('span#textsize-large-link').addClass("active");
				$('span#textsize-large-link a').attr('title','Set fontsize to Largest');
				$('span#textsize-normal-link').removeClass("active");
				$('span#textsize-small-link').removeClass("active");
				$.cookie('cookie_textsize_class', 'textsize-large', { expires: 365, path: "/"} ); // save cookie
				//Cufon.refresh();
				return false;		
			
			}
		
		}
		else if (this.id == 'textsize-normal-link') { 			// else reduce it by same amount			
			//alert("text resize : large");
			$content.removeClass("textsize-larger");
			$content.removeClass("textsize-large");
			$content.removeClass("textsize-small");
			$content.removeClass("textsize-smaller");			
			$('span#textsize-normal-link').addClass("active");
			$('span#textsize-large-link').removeClass("active");
			$('span#textsize-small-link').removeClass("active");
			$.cookie('cookie_textsize_class', null, { path: "/"} ); // delete cookie
			//Cufon.refresh();			
			return false;
		}
		else if (this.id == 'textsize-small-link') { 			// else reduce it by same amount
			//alert("text resize : small");
			var setClass = $('#page-container').attr('class');
			if (setClass == "textsize-larger") {
			// set 1 size smaller
				$content.attr('class', '');
				$content.addClass("textsize-large");
				$('span#textsize-large-link').removeClass("disabled");				
				$.cookie('cookie_textsize_class', 'textsize-large', { expires: 365, path: "/"} ); // save cookie				
				//Cufon.refresh();
				return false;
			} else if (setClass == "textsize-large") {
				// set 1 size smaller
				$content.attr('class', '');
				$.cookie('cookie_textsize_class', null, { path: "/"} ); // delete cookie
				//Cufon.refresh();			
				return false;				
			} else if (setClass == "textsize-smaller") {
				// dont do anything
				return false;
			} else if (setClass == "textsize-small") {
				// 1 smaller 
				$content.attr('class', '');
				$content.addClass("textsize-smaller");
				$('span#textsize-small-link').addClass("disabled");				
				$.cookie('cookie_textsize_class', 'textsize-smaller', { expires: 365, path: "/"} ); // save cookie				
				//Cufon.refresh();
				return false;
			} else {
				$content.addClass("textsize-small");
				$content.removeClass("textsize-large");
				$('span#textsize-small-link').addClass("active");
				$('span#textsize-normal-link').removeClass("active");
				$('span#textsize-large-link').removeClass("active");
				$.cookie('cookie_textsize_class', 'textsize-small', { expires: 365, path: "/"} ); // save cookie
				//Cufon.refresh();			
				return false;			
			}
		}
	})
	}  else { return false; }
	}	
	/* -------------------------------------- */
}

/* ::Submit hover */
FnSubmitHover = {
	init:function() {
		$('input.submit').hover(function() {
			$(this).addClass('hover')
			$(this).parent().addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
			$(this).parent().removeClass('hover');
		});
	}	
	
	/* -------------------------------------- */
}

/* FnInputFocus */
/* :: remove and replace text field example content */
FnInputFocus = {
	init:function() {
		
	// when text field gets clicked
	$(':input.example').focus(function() {
		// if not empty and if class example get value
		if ($ (this) .is('.example') && !(this.value == ''))  {
			// store value
			exampleValue = this.value;
			// empty value
			$(this).attr({'value' : ''})
			// remove class example
			$(this).removeClass('example');
		};
	}).blur(function() {
		// if empty: replace example value
		if (this.value == '') {
			$(this).addClass('example')
			$(this).attr({'value' : exampleValue})
		};
	});
	}
	
	/* -------------------------------------- */
}

/* ::Click handler for print button */
FnPrintBtn = {
	init:function() {
		$('#print-button').click(function() {
			window.print();
		});			
	}	
	
	
	/* -------------------------------------- */
}


/* ::This function replaces the legend element with a h2 to be able style it */
FnLegends = {
	init:function() {
		// get all legend elems
		$('legend').each(function() {
			// loop and get text from legend
			var thisLegend = $(this).text();
			// create h2
			//$('<h2></h2>')
			// insert h2 and remove legend
			$(this).after('<h2>'+thisLegend+'</h2>').remove();
			//$(this).after('<h2>' . thisLegend . '</h2>').remove()
			//alert(thisLegend);
		});
	}		
	/* -------------------------------------- */
}

/* ::add functionality to target selector */
FnTargetSelector = {
	init:function() {
		$('#target-select-1').change(function() {
			thisUrl = $('#target-select-1 :selected').val()
			$('#form-target').attr({'action' : thisUrl}); // set url (from html code)
			$('#form-target').submit(); // submit this form
		});
	}	
						
	
	/* -------------------------------------- */
}

/* ::Function for toggling the events list
FnEvents = {
	init:function() {
		$('.events li span a').toggle(function() {
			// open
			$(this).parent().parent().addClass('open');
		}, function() {
			// close
			$(this).parent().parent().removeClass('open');			
		});
	}	 */
	
	/* -------------------------------------- 
}*/

/* ::Function for toggling the polls */
FnPolls = {
	init:function() {
		$('.polls li a.toggle').toggle(function() {
			// open
			$(this).parent().parent().addClass('open');
		}, function() {
			// close
			$(this).parent().parent().removeClass('open');			
		});
	}	
	
	/* -------------------------------------- */
}

/* ::Function for social media tooltips */
FnSMtoolTips = {
	init:function() {
		$('.social-media a').hover(function(event) {
			// get title attr of link
			thisTitle = $(this).attr('title');
			// set title attr to empty
			$(this).attr('title', '')
			// get class name of link
			var thisClass = $(this).attr('class');
			// create span for text and also bg img.
			var thisSpan = "<span>" + thisTitle + "</span>";
			var thisPointer = "<span class='pointer'>pointer</span>";
			// create tooltip html
			$('<div></div>')
				.addClass('tooltip')
				.addClass(thisClass)
				.html(thisPointer)
				.append(thisSpan)
				.appendTo('.social-media')
		},
		function(event) {
			// put title back
			$(this).attr('title', thisTitle)			
			$('.social-media .tooltip').remove();
		});

	}	
	
	
	/* -------------------------------------- */
}

/* ::DatePicker Functions */
FnDatePickers = {
	init:function() {
		// check for input.date 
		if ( $('input.date').length > 0 ) {
			$("#posted-between").datepicker({showOn: 'button', buttonImage: 'images/calendar.gif', buttonImageOnly: true, dateFormat: 'dd-mm-yy' });
			$("#posted-to").datepicker({showOn: 'button', buttonImage: 'images/calendar.gif', buttonImageOnly: true, dateFormat: 'dd-mm-yy' });			
		} else {
			//alert('no date field in this doc');
			return false;
		}	
	}	
	
	
	/* -------------------------------------- */
}

// !!! this must be the last function in this file !!!
FnAccordion = {
	init:function() {
		// check for class accordion
		if ( $("div.accordion").length > 0 ) {
			//alert("The DIV exists");
		jQuery('.accordion').accordion({
			header: 'h2.accordion-heading',
			active: '.open',
			selectedClass: 'open',
			alwaysOpen: false,
			animated: 'slide',
			autoheight: false
		});
		};

	}	
	
	
	/* -------------------------------------- */
}


/* SITE CATALYST codes */

function scodeChangeProfile(obj){
    if(obj) {
        var profile_type = obj.options[obj.selectedIndex].text;
        var s=s_gi(s_account);
         s.linkTrackVars='prop8,prop9';
        s.linkTrackEvents='none';
        s.prop8=profile_type;
        s.prop9=s.pageName;
        s.tl(true,'o','Profile Change');
    }
}

function scodeArchiveWebinar(webinar) {
    var s=s_gi(s_account);
    s.linkTrackVars='events,eVar17';
    s.linkTrackEvents='event16';
    s.events='event16';
    s.eVar17 = '' + scodeSanitize(webinar) + '';
    s.tl(true,'o','Webinar Archive Views');
}     

function scodeEventSignup(eventTitle) {
    var s=s_gi(s_account);
    s.linkTrackVars='events,eVar10';
    s.linkTrackEvents='event12';
    s.events='event12';
    s.eVar10 = '' + scodeSanitize(eventTitle) + '';
    s.tl(true,'o','Event Sign Ups');
}     

function scodeEventEnquiry(eventTitle) {
    var s=s_gi(s_account);
    s.linkTrackVars='events,eVar16';
    s.linkTrackEvents='event13';
    s.events='event13';
    s.eVar16 = '' + scodeSanitize(eventTitle) + '';
    s.tl(true,'o','Event Enquiries');
}   

function scodeEventDetails(eventTitle) {
    var s=s_gi(s_account);
    s.linkTrackVars='events,eVar18';
    s.linkTrackEvents='event17';
    s.events='event17';
    s.eVar18 = '' + scodeSanitize(eventTitle) + '';
    s.tl(true,'o','Event More Details');
}    

function scodePhotoGalleryLikes(galleryCaption) {
    var s=s_gi(s_account);
    s.linkTrackVars='events';
    s.linkTrackEvents='event18';
    s.events='event18';
    s.tl(true,'o','Photo Gallery Likes');
}    

function scodeVideoGalleryLikes(galleryCaption) {
    var s=s_gi(s_account);
    s.linkTrackVars='events';
    s.linkTrackEvents='event19';
    s.events='event19';
    s.tl(true,'o','Video Gallery Likes');
}    

function scodeSendToFriend() {
    var s=s_gi(s_account);
    s.linkTrackVars='events';
    s.linkTrackEvents='event14';
    s.events='event14';
    s.tl(true,'o','Send To Friend');
}       

function scodeAddThis() {
    var s=s_gi(s_account);
    s.linkTrackVars='events';
    s.linkTrackEvents='event15';
    s.events='event15';
    s.tl(this,'o','AddThis Button Clicks');
}        

function scodeSanitize(input) {
	output = input.replace( new RegExp( '"', "g" ), "&quot;" );
	output = output.replace( new RegExp( "'", "g" ), "&quot;" );
	return output;
}
