/*
 * Author: Teon Ooi (teon.ooi@pixl8.co.uk/gmail.com)
 * Date: 04/06/2009
 */

$(document).ready( function() {
	//Hover Menu 
	$('#layer-menu ol li').hover(function() {
		if(!$(this).is('.state-selected')){
			$(this).find("ol:first").fadeIn('fast').show(); 
			$(this).addClass('hover');
			
			$('#layer-menu ol li.hover>ol>li').hover(function() {
				$(this).find('ol').css('top',($(this).offset().top-90));
		 	}, function(){ 
			});
		}
		
			
	}, function(){ 	
		if(!$(this).is('.state-selected')){
			$(this).find("ol:first").hide();
			$(this).removeClass("hover");
		}
    });	

	if($("#layer-menu ol li.state-selected>ol").css("display") == "block"){
		$('#submenu-bg').addClass("layer-submenu-bg");
	}
	
	if($("#layer-menu ol li.state-selected ol li.state-selected>ol").css("display") == "block"){
		$('#second-submenu').addClass("second-submenu-bg");
	}
	
	//Remove the divider for top menu 
	$('#layer-menu ol:first>li>a').hover(function() {
			$(this).parent().prev().css('background','none');
	}, function(){ 	
			if(!$(this).parent().hasClass('state-selected')){
				$(this).parent().prev().css('background','url("/graphics/new/img-menu-divider.gif") no-repeat right center')
			}
    });	
	
	$('#layer-menu ol:first>li.state-selected').prev().css('background','none');
		
    // External links
    $('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
    
    $('#search-keyword').focus(function() {
    	if ($(this).attr('value') == 'Search...') $(this).attr('value', '');
    }).blur(function() {
    	if ($(this).attr('value') == '') $(this).attr('value', 'Search...');
    });
    
    $('#search-website-switcher').toggle(
    	function() {
    		$(this).css('background', 'url(/graphics/ico_arrow-slideup.gif) right 5px no-repeat');
			$('#layer-page').animate({marginTop: '35px'});
            $('#layer-form-search').slideDown();
            return false;
        },
	    function() {	    
	    	$(this).css('background', 'url(/graphics/ico_arrow-slidedown.gif) right 5px no-repeat');
	        $('#layer-page').animate({marginTop: '0px'});
			$('#layer-form-search').slideUp();
	        return false;
	});
	
	// Glossary tooltip
	$("a.glossary").tooltip({
		track: true, 
		showURL: false
	});	

	$('body').append('<div class="jqmWindow" id="jqmWindow"><img src="/graphics/animated_preloader.gif" alt="Loading"/></div>');
	$('#jqmWindow').jqm({
		ajax: '@href'
	});
		
	//$('#banner').jqGalScroll({height:515,width:590,ease:'easeInOutCubic',speed:1000});


	// Initialise collapsible toggle 				
	$('a.toggle-item').click(function(){
		if($(this).hasClass('collapse')) {
			$(this).removeClass('collapse');
			$('#'+$(this).attr('rel')).show();
			// Toggle Hide/Show wordings
			$('span.expand', this).hide();	
			$('span.collapse', this).show();
			
		} else {
			$(this).addClass('collapse');
			$('#'+$(this).attr('rel')).hide();
			// Toggle Hide/Show wordings
			$('span.expand', this).show();	
			$('span.collapse', this).hide();				
		}		
		return false;
	});		


	// pullquote functions
	$('span.pullquote-left').each(function() {
		var parentTag = $(this).parent();
		var newNode = $('<blockquote class="pulledquote"></blockquote>');
		
		$(this).removeClass('pullquote-left');
		$('<span class="pulledquote-text">'+$(this).html()+'</span>').appendTo(newNode);
		$('<span class="pullquote-bottom"></span>').appendTo(newNode);
		newNode.removeClass('pullquote-left');
		newNode.addClass('pulledquote-left').prependTo( $(this).parent());
		
	});
	
	$('span.pullquote-right').each(function() {
		var parentTag = $(this).parent();
		var newNode = $('<blockquote class="pulledquote"></blockquote>');
		
		$(this).removeClass('pullquote-right');
		newNode.html('<span class="pulledquote-text">'+$(this).html()+'</span>');
		$('<span class="pullquote-bottom"></span>').appendTo(newNode);
		newNode.removeClass('pullquote-right');
		newNode.addClass('pulledquote-right').prependTo( $(this).parent());
	});
	
	$('span.pullquote-center').each(function() {
		var newNode = $('<blockquote class="pulledquote"></blockquote>');
		
		$(this).removeClass('pullquote-center');
		newNode.html('<span class="pulledquote-text">'+$(this).html()+'</span>');
		$('<span class="pullquote-bottom"></span><span style="display:block;clear:both></span>"').appendTo(newNode);
		$('<div class="pullquote-line"></div>').appendTo(newNode);
		$('<div class="pullquote-line"></div>').prependTo(newNode);
		newNode.removeClass('pullquote-center');
		newNode.addClass('pulledquote-center').prependTo($(this).parent());
		$(this).remove();
	});


});
