var GLOBAL_URL_PREFIX = '/';

(function($){
	$(function(){
		
		/* Custom fonts */
		if(!($.browser.msie && $.browser.version == 6)){
			Cufon.replace('#menu>li>a',{
					textShadow: '#232323 1px 1px'
				});
			Cufon.replace('div.home-body h1');
			Cufon.replace('div.ni-header');
			Cufon.replace('div.home-top-text h3, div.home-top-slogan h3, div.section-name .cont');
			Cufon.replace('.home-top-programmes a',{
					textShadow: '#123568 1px 1px'
				});
			Cufon.replace('div.hb-item h3');
			Cufon.replace('div.quick-links h3');
			Cufon.replace('div.course-finder h3');
			Cufon.replace('ul.ft-nav');
			Cufon.now();
		}
		
		/* Search field */
		$('div.seach-field input').focus(function(){
			if(this.defaultValue == this.value){
				this.value = '';
			}
		})
		.blur(function(){
			if(!this.value){
				this.value = this.defaultValue;
			}
		});
		
		/* Change PNG Image for IE6 */
		function changePNGImage(imgElem){
			imgElem = $(imgElem);
			imgElemSrc = imgElem[0].src;
			if(imgElemSrc.indexOf('.png') != -1){
				imgElem[0].src = GLOBAL_URL_PREFIX + 'images/blank.gif';
				imgElem.css({
					background:'none',
					filter:'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + imgElemSrc + '",sizingMethod="crop")'
				});
			};
		}
		if($.browser.msie && $.browser.version == 6){
			$('div.head-links img').each(function(){
				changePNGImage(this);
			});
			$('div.news-item img').each(function(){
				changePNGImage(this);
			});
			$('a.btb-panel-arrow-left img').each(function(){
				changePNGImage(this);
			});
			$('a.btb-panel-arrow-right img').each(function(){
				changePNGImage(this);
			});
		};
		
		(function ($) {

			var operaSubmenuTimeout,
				submenuHideTimeout = new Object(),
				firstInit = true;
			
			
			var menuItems = $('#menu li div.submenu').each(function () {
				var submenu = $(this),
					menuItem = submenu.prev(),
					firstChildItem = submenu.find(':first');
				if (typeof firstChildItem[0] != 'undefined') {
					var submenuElem = submenu.find('a').filter(function () { return $(this).next().hasClass('submenu-item'); });
					submenuElem.addClass('isSubmenu').each(function(i) {
						var obj = $(this),
							nextObj = $(this).next().css({zIndex:520 + i});
						obj.mouseenter(function () {
							nextObj.css({ display: 'block' });
							obj.addClass('submenuOpen');
							if (typeof obj[0].timeOut != 'undefined'){
								clearTimeout(obj[0].timeOut);
							}
							var aTags = nextObj.children('a'),
								innerWidth = obj.innerWidth(),
								maxWidth = 165;
							for (var i = 0; i < aTags.length; i++) {
								var spanElem = aTags.eq(i).children().eq(0).css({ display: 'inline' }),
									aElem = aTags.eq(i),
									aElemInnerWidth = spanElem.innerWidth() + 15 + parseInt(aElem.css('padding-left')) + parseInt(aElem.css('padding-right'));
								spanElem.css({ display: 'block' });
								if (aElemInnerWidth > maxWidth) {
									maxWidth = aElemInnerWidth;
								};
							};
							nextObj.css({
								width: maxWidth,
								left: innerWidth + 32,
								top: obj.offset().top - obj.parent().offset().top
							});
						}).mouseleave(function () {
							obj[0].timeOut = setTimeout(function () {
								nextObj.css({ display: 'none' });
								obj.removeClass('submenuOpen');
							}, 400);
						});
						nextObj.mouseenter(function () {
							if (typeof obj[0].timeOut != 'undefined')
								clearTimeout(obj[0].timeOut);
						}).mouseleave(function () {
							obj[0].timeOut = setTimeout(function () {
								nextObj.css({ display: 'none' });
								obj.removeClass('submenuOpen');
							}, 400);
						});
						obj.parent().children('a').not(obj).mouseenter(function () {
							if (typeof obj[0].timeOut != 'undefined')
								clearTimeout(obj[0].timeOut);
							nextObj.css({ display: 'none' });
							obj.removeClass('submenuOpen');
						});
					});
					var firstChildItemATag = firstChildItem.children('a');
					menuItem.mouseenter(function () {
						menuItem.addClass('indexSubmenuOpen');
						
						if (typeof operaSubmenuTimeout != 'undefined' && $.browser.opera) {
							clearTimeout(operaSubmenuTimeout);
						};
						
						if (typeof submenuHideTimeout.timeOut != 'undefined') {
							if (typeof submenuHideTimeout.submenu != 'undefined' && submenuHideTimeout.submenu[0] != submenu[0]) {
								clearTimeout(submenuHideTimeout.timeOut);
								submenuHideTimeout.submenu.prev().removeClass('indexSubmenuOpen');
								submenuHideTimeout.submenu.css({ display: 'none' });
								submenuHideTimeout.submenuElem.each(function () {
									if (typeof this.timeOut != 'undefined')
										clearTimeout(this.timeOut);
									$(this).removeClass('submenuOpen');
								})
								.next().css({ display: 'none' });
								submenuHideTimeout.firstChildItem.css({ display: 'none' });
								if ($.browser.opera) {
									$('body').css({ display: 'inline' });
									operaSubmenuTimeout = setTimeout(function () { $('body').css({ display: 'block' }); }, 100);
								};
							};
						};
						
						
						submenu.css({ display: 'block' });
						firstChildItem.css({ display: 'block' });
						
						if(menuItem.text().indexOf('Courses') != -1){
							if(firstInit){
								maxWidth = 785;
								var cols = submenu.find('div.sct-item-col');
								for(var i=0;i< cols.length; i++){
									var col = cols.eq(i),
										colChildren = col.children('dl'),
										colWidth = colChildren.eq(0).width()*colChildren.length + parseInt(colChildren.eq(0).css('padding-right'))*colChildren.length;
									col.css({width: colWidth + 'px'});
								}
								firstInit = false;
							}
						}
						else {
							var maxWidth = 165;
							for (var i = 0; i < firstChildItemATag.length; i++) {
								var spanElem = firstChildItemATag.eq(i).children().eq(0).css({ display: 'inline' });
								var aElem = firstChildItemATag.eq(i);
								var innerWidth = spanElem.innerWidth() + 15 + parseInt(aElem.css('padding-left')) + parseInt(aElem.css('padding-right'));
								spanElem.css({ display: 'block' })
								if (innerWidth > maxWidth) {
									maxWidth = innerWidth;
								};
							};
						}
						
						firstChildItem.css({ width: maxWidth + 'px' });
						submenuHideTimeout.submenu = submenu;
						submenuHideTimeout.submenuElem = submenuElem;
						submenuHideTimeout.firstChildItem = firstChildItem;
						
					})
					.mouseleave(function(){
						submenuHideTimeout.timeOut = setTimeout(function () {hideMenu()},400);
					});
					submenu.mouseenter(function () {
						clearTimeout(submenuHideTimeout.timeOut);
					})
					.mouseleave(function(){
						submenuHideTimeout.timeOut = setTimeout(function () {hideMenu()},400);
					});
					
					
				};
			});
		
			$('#menu>li>a').not(menuItems.prev()).mouseenter(function () {
				if (typeof submenuHideTimeout.timeOut != 'undefined') {
					clearTimeout(submenuHideTimeout.timeOut);
					hideMenu();
				};
			});
			
			$('#menu>li').each(function(){ this.submenuFlag = true; $('*',this).each(function(){this.submenuFlag = true; }) });
			
			$(document).mouseover(function(e){
				if(typeof submenuHideTimeout.submenu != 'undefined' && typeof e.target.submenuFlag != 'undefined'){
					clearTimeout(submenuHideTimeout.timeOut);
				};
			});
			
			
			function hideMenu(){
				submenuHideTimeout.submenu.prev().removeClass('indexSubmenuOpen');
				submenuHideTimeout.submenu.css({ display: 'none' });
				submenuHideTimeout.submenuElem.each(function () {
					if (typeof this.timeOut != 'undefined')
						clearTimeout(this.timeOut);
					$(this).removeClass('submenuOpen');
				})
				.next().css({ display: 'none' });
				submenuHideTimeout.firstChildItem.css({ display: 'none' });
				if ($.browser.opera) {
					$('body').css({ display: 'inline' });
					operaSubmenuTimeout = setTimeout(function () { $('body').css({ display: 'block' }); }, 100);
				};
			};
			
			
		})(jQuery)
		
		/* Menu tabs */
		
		var submenuCourses = $('div.submenu-courses-tabs'),
			submenuCoursesTabs = submenuCourses.find('li.no-items');
		submenuCourses.tabs({
			event: 'mouseover'
		});
		submenuCoursesTabs.each(function(){
			if($.browser.msie && $.browser.version < 7){
				$(this).removeClass('no-items');
			};
		}).mouseenter(function(){
			if($.browser.msie && $.browser.version < 7){
				$(this).addClass('no-items-selected');
			};
		}).mouseleave(function(){
			if($.browser.msie && $.browser.version < 7){
				$(this).removeClass('no-items-selected');
			};
		});
		submenuCourses.find('.sct-nav a').click(function(){
			var href = $(this).attr('rel');
			window.location.href = href.replace(/\#.*/i,'');
			return false;
		});
		
		/* Home top header */
		$('div.home-top').each(function(){
			
			var parent = $(this),
				buttons = parent.children('.home-top-programmes').find('a'),
				items = parent.children('.home-top-item'),
				items = items.not(items.eq(0)).hide(),
				activeButton = false,
				activeItem = false;
			
			buttons.parent().parent().find('*').each(function(){ this.homeTopBanner = true });
			
			buttons.each(function(i){
				
				var button = $(this),
					index = buttons.index(this),
					hoverElem = items.eq(index);
				button.click(function(){
					if(!activeItem){
						button.removeClass('el-0' + (index + 1)).addClass('el-0' + (index + 1) + '-act');
						Cufon.replace('.home-top-programmes a.el-0' + (index + 1) + '-act',{
							textShadow: '#eceff3 1px 1px'
						});
						items.eq(0).prev().stop().animate({opacity:0},300,function(){
							$(this).hide();
							hoverElem.stop().show().css({opacity:0}).animate({opacity:1},300,function(){
								hoverElem.css({opacity:'auto'});
							});
						});
						activeItem = hoverElem;
						activeButton = button;
					}
					else {
						if(activeItem[0] != hoverElem[0]){
							var activeButtonIndex = buttons.index(activeButton)+1;
							activeButton.addClass('el-0' + activeButtonIndex).removeClass('el-0' + activeButtonIndex + '-act');
							Cufon.replace('.home-top-programmes a.el-0' + activeButtonIndex,{
								textShadow: '#123568 1px 1px'
							});
							button.removeClass('el-0' + (index + 1)).addClass('el-0' + (index + 1) + '-act');
							Cufon.replace('.home-top-programmes a.el-0' + (index + 1) + '-act',{
								textShadow: '#eceff3 1px 1px'
							});
							activeItem.stop().animate({opacity:0},300,function(){
								$(this).hide();
								hoverElem.stop().show().css({opacity:0}).animate({opacity:1},300,function(){
									hoverElem.css({opacity:'auto'});
								});
							});
							activeItem = hoverElem;
							activeButton = button;
						}
					}
					return false;
				});
			});
			
		});
		
		/* Right frame tabs */
		$('div.light-tabs, div.dark-tabs').tabs();
		
		/* Footer tabs */
		$('#news-events-panel').tabs();
		
		/* Expand-collapse */
		$('a.ec-name').next().addClass('ec-cont-hide').end().each(function(){
			
			var button = $(this),
				content = button.next(),
				parent = button.parent();
			
			if(button.hasClass('ec-name-open')){
				parent.addClass('expand-collapse-open');
				content.removeClass('ec-cont-hide');
			}
			
			button.click(function(){
				if(button.hasClass('ec-name-open')){
					parent.removeClass('expand-collapse-open');
					button.removeClass('ec-name-open');
					content.slideUp(300);
				}
				else {
					button.addClass('ec-name-open');
					parent.addClass('expand-collapse-open');
					if(content.hasClass('ec-cont-hide')){
						content.removeClass('ec-cont-hide').hide();
					}
					content.slideDown(300);
				}
				return false;
			})
				
		});
		
		
	});
})(jQuery)
