var systemObject = {
	heightAlertMax : 500,
	windowHeight : 0,
	windowWidth : 0,
	ieVersion : '',
	getIEVersionNumber : function() {
	    var ua = navigator.userAgent;
	    var MSIEOffset = ua.indexOf("MSIE ");	    
	    if (MSIEOffset !== -1) {
	        systemObject.ieVersion = parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
	    }
	},
	getDimensions : function() {	
		if (typeof window.innerWidth !== 'undefined') {		
			systemObject.windowWidth = window.innerWidth;
			systemObject.windowHeight = window.innerHeight;
		} else if (
			typeof document.documentElement !== 'undefined' && 
			typeof document.documentElement.clientWidth !== 'undefined' && 
			document.documentElement.clientWidth !== 0
		) {
			systemObject.windowWidth = document.documentElement.clientWidth;
			systemObject.windowHeight = document.documentElement.clientHeight;
		} else {
			systemObject.windowWidth = document.getElementsByTagName('body')[0].clientWidth;
			systemObject.windowHeight = document.getElementsByTagName('body')[0].clientHeight;
		}		
	},
	loadAlert : function() {
		if ($('#alert').length > 0) {
			systemObject.getIEVersionNumber();
			systemObject.getDimensions();
			if (systemObject.ieVersion !== '' && systemObject.ieVersion < 7) {
				$('#overlay').css('height', systemObject.windowHeight + 'px');
			}			
			var heightAlert = $('#alert').height();
			var heightSpan = $('#alert-close').height();			
			if (heightAlert > systemObject.heightAlertMax) {
				heightAlert = systemObject.heightAlertMax;
				$('#alert').css('height', heightAlert + 'px');		
			}
			$('#alert-content').css('height', (heightAlert-(heightSpan+20)) + 'px');			
			var posTop = (systemObject.windowHeight/2)-(heightAlert/2);	
			$('#alert').hide().delay(500).css('top', posTop + 'px').fadeIn(500);			
		}
	},
	closeAlert : function() {
		$('#alert').fadeOut(300, function() {
			$('#overlay').fadeOut(300, function() {
				$('#alert').remove();
				$('#overlay').remove();
			});
		});
	},
	positionAlert : function() {
		if ($('#alert').length > 0) {	
			systemObject.getDimensions();
			var heightAlert = $('#alert').height();
			if (heightAlert > systemObject.heightAlertMax) {
				heightAlert = systemObject.heightAlertMax;
				$('#alert').css('height', heightAlert + 'px');		
			}
			var posTop = (systemObject.windowHeight/2)-(heightAlert/2);	
			$('#alert').css('top', posTop + 'px');			
		}
	}
};
$(function() {

	
	systemObject.loadAlert();	
	$('#alert-close span').live('click', function() {
		systemObject.closeAlert();
	});
	$('#overlay').live('click', function() {
		systemObject.closeAlert();
	});
	$(window).resize(function() {
	    systemObject.positionAlert();
	});
	
	if ($('.gtranslate').length > 0) {
		$('.gtranslate').click(function() {
			$('#google_translate_element').stop(true, true).slideToggle(200);
			$('.goog-te-menu2').click(function() {
				$('#google_translate_element').hide();
			});
		});
	}
	
	
	if ($('#print_page').length > 0) {
		$('#print_page').click(function() {
			window.print();
  			return false;
		});
	}
	
	
	
	// to be amended with ajax
	if ($('#font_size').length > 0) {
		$('#font_size li').click(function() {
			$(this).siblings().each(function() {
				$(this).removeClass('active');			
			});
			$(this).addClass('active');
			var id = $(this).attr('id').split('_');
			$.cookie("accessibility_font", id[1], { expires: 31, path: '/' });
			location.reload();			
		});
	}
	


	if ($('.viewing_trigger').length > 0) {		
		$('.viewing_trigger').click(function() {
			var w = $('#viewing_options').width();
			$('#viewing_items').width(w);
			$('#viewing_items').stop(true, true).slideToggle(200, function() {
				if ($(this).is(":visible")) {
					$('#viewing_items dl').children('dt').click(function() {
						var id = $(this).attr('id');
						$.cookie("accessibility_view", id, { expires: 31, path: '/' });
						location.reload();
					});
				}
			});
		});
	}
	
	
	$('#header').cycle({
		fx:'fade',
		speed:1200,
		timeout:10000,
		next:'#header',
		manualTrump:false,
		pause:1
	});
	
	
	if ($('.news_feed').length > 0) {
		$('.news_feed').cycle({
			fx:'scrollUp',
			speed:800,
			timeout:5000,
			pause:1
		});
	}
	
		
	if ($('#navigation').length > 0) {
		$('#navigation li').hover(function() {
			if ($(this).attr('id') != 'home' && $(this).parent().attr('id') == 'navigation' && $(this).has('dl')) {
				
				var wr = $(this).children('dl');
				var wo = wr.width();
				var wi = wo - parseInt(30, 10); // padding of the left and right side of the a tag
				
				wr.children('dt').width(wo);
				wr.children('dt').children('a').width(wi);
								
				$(this).siblings().removeClass('current');			
				$(this).addClass('current');
				$(this).children('dl').stop(true, true).fadeIn(100);
			}
		}, function() {
			if ($(this).attr('id') != 'home' && $(this).parent().attr('id') == 'navigation' && $(this).has('dl')) {
				$(this).removeClass('current');
				$(this).children('dl').stop(true, true).fadeOut(100);
			}
		});
	}
	
	
	
	if ($('.pm').length > 0) {
		$('.pm').click(function() {
			var wrapper = $(this).parent('.heading');
			var id = $(this).attr('id');
			if ($(this).hasClass('minus')) {
				$(this).removeClass('minus');
				$(this).addClass('plus');
				$(this).parent().next('.block_right_inside').slideUp(200, function() {
					wrapper.addClass('heading_both_rounded');					
					$.cookie("panel[" + id + "]", 'closed', { expires: 31, path: '/' });
				});
			} else {
				$(this).removeClass('plus');
				$(this).addClass('minus');
				wrapper.removeClass('heading_both_rounded');
				$(this).parent().next('.block_right_inside').slideDown(200);
				$.cookie("panel[" + id + "]", 'open', { expires: 31, path: '/' });				
			}
		});
	}
	
	
	
	
	
	if ($('.hdpm').length > 0) {
		$('.hdpm').click(function() {
			var id = $(this).attr('id');
			if ($(this).hasClass('minus')) {
				$(this).removeClass('minus');
				$(this).addClass('plus');
				$('.' + id).slideUp(200, function() {					
					$.cookie("panel[" + id + "]", 'closed', { expires: 31, path: '/' });
				});
			} else {
				$(this).removeClass('plus');
				$(this).addClass('minus');
				$('.' + id).slideDown(200);
				$.cookie("panel[" + id + "]", 'open', { expires: 31, path: '/' });				
			}
		});
	}
	
	
	
	if ($('#poll').length > 0) {
		$('#form_poll label').click(function() {
			var v = $(this).children('input').val();
			$.ajax({
				type: "POST",
				url: '/poll/action/submit',
				data: ({ id : v }),
				dataType: 'json',
				success: function(data) {
					if (data.error) {
						$('#poll').slideUp(200, function() {
							$(this).html(data.message).slideDown(200);						
						});
					} else {
						$.cookie("poll", data.poll, { expires: 365, path: '/' });
						window.location.replace("/poll.html");
					}					
				}
			});	
			return false;				
		});
	}
	
	
	
	if ($('.gallery_single_inside').length > 0) {
		$('.gallery_single_inside').after('<div class="gallery_single_navigation">').cycle({
			fx:'fade',
			speed:1200,
			timeout:10000,
			manualTrump:false,
			pause:1,
			pager:  '.gallery_single_navigation'
		});
	}
	
	
	
	
	
	
	if ($('.gallery_large').length > 0) {
		$('.gallery_large').fancybox({
			'overlayOpacity'	: 0.9,
			'overlayColor'		: '#000',
			'autoScale' 		: true,
			'scrolling' 		: 'no',
			'centerOnScroll'	: true,
			'speedIn'			: 200, 
			'speedOut'			: 200,
			'hideOnOverlayClick': true
		});
	}	
	
	
	
	
   	if ($('#totop').length > 0) {
	    $('#totop').click(function(){
	        $('html, body').animate({scrollTop:0}, 'slow');
	        return false;
	    });
    }
    
    
    
    if ($('.nav_tooltip').length > 0) {
    	$('.nav_tooltip').hover(function() {
    		$(this).children('.nav_tooltip_target').stop(true, true).fadeIn(100);
    	}, function() {
    		$(this).children('.nav_tooltip_target').stop(true, true).fadeOut(100);
    	});
    }
    
    
    
    
    if ($('.sitemap').length > 0) {
    	$('.sitemap_heading a.trigger').click(function() {
    		if ($(this).parent().hasClass('close')) {
    			$(this).parent().removeClass('close');
    			$(this).next('.sitemap_list').slideUp(200);
    		} else {
	    		$('.sitemap_list').each(function() {
	    			if ($(this).parent().hasClass('close')) {
	    				$(this).slideUp(200);
	    				$(this).parent().removeClass('close');
	    			}
	    		});
	    		$(this).parent().addClass('close');
	    		$(this).next('.sitemap_list').slideDown(200);
	    	}
	    	return false;
    	});    	
    }
	
	

});
