function switchText(size){
	//text size style sheet switching	
	$.stylesheetSwitch(size);
	if(size == 'Extra')
	{
		$('a.changeTextE').css("color","#FF3300");
		$('.changeTextM').css("color","#666666");
		$('.changeTextL').css("color","#666666");
	}else if(size == 'Medium')
	{
		$('a.changeTextE').css("color","#666666");
		$('.changeTextM').css("color","#FF3300");
		$('.changeTextL').css("color","#666666");
	}else if(size == 'Large')
	{
		$('a.changeTextE').css("color","#666666");
		$('.changeTextM').css("color","#666666");
		$('.changeTextL').css("color","#FF3300");
	}
	
	
	//return false;
}

(function($){
		// Local vars for toggle
		var cookieName = 'govhkstyle';
		var availableStylesheets = [];
		var activeStylesheetIndex = 0;
		
		// To loop through available stylesheets
		$.stylesheetToggle = function(){
			activeStylesheetIndex ++;
			activeStylesheetIndex %= availableStylesheets.length;
			$.stylesheetSwitch(availableStylesheets[activeStylesheetIndex]);
		};
		
		// To switch to a specific named stylesheet
		$.stylesheetSwitch = function(styleName){
			styleSheetName = "modeFormat"+styleName;
			$('link[id*=modeFormat][title]').each(
				function(i){
					this.disabled = true;
					if (this.getAttribute('id') == styleSheetName) {
						this.disabled = false;
						activeStylesheetIndex = i;
					}
				}
			);
			eraseCookie(cookieName);
			createCookie(cookieName, styleName, 365);
		};
		
		// To initialise the stylesheet with it's 
		$.stylesheetInit = function()
		{
			/*$('link[@rel*=style][title]').each(
				function(i) {
					availableStylesheets.push(this.getAttribute('title'));
				}
			);*/
			var c = readCookie(cookieName);
			if (c) {				
				$.stylesheetSwitch(c);
				if(c == 'Extra')
				{
					$('a.changeTextE').css("color","#FF3300");
					$('.changeTextM').css("color","#666666");
					$('.changeTextL').css("color","#666666");
				}else if(c == 'Medium')
				{
					$('a.changeTextE').css("color","#666666");
					$('.changeTextM').css("color","#FF3300");
					$('.changeTextL').css("color","#666666");
				}else if(c == 'Large')
				{
					$('a.changeTextE').css("color","#666666");
					$('.changeTextM').css("color","#666666");
					$('.changeTextL').css("color","#FF3300");
				}
			}
		};
	}
)(jQuery);

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name){
	createCookie(name,"",-1);
}


/* Single Collapsable Menu */
function initSingleMenu() {
	$('.singleMenu li div.singleMenu_open, .singleMenu li div.singleMenu_close').click(function(event) {
		//[optional] prevent default behaviour here			
		$(this).next().slideToggle('normal');
		if($(this).attr('class') == 'singleMenu_close'){			
			$(this).removeClass('singleMenu_close');
            $(this).addClass('singleMenu_open');
		}else{				
			$(this).removeClass('singleMenu_open');
            $(this).addClass('singleMenu_close');		
		}		
	});
}

/* Accordion Menu */
function initAccordionMenu(elmID) {
  $(elmID +' ul').hide();
  $(elmID +' li div').click(	
    function(event) {
      event.preventDefault();
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
      }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		$(elmID +' ul li').hide();
        $(elmID +' ul:visible').slideUp('normal');		
        checkElement.slideDown('normal', function(){
			checkElement.children('li').show();
		});	
		
		if ($(this).attr('id') == 'featureSectionFirstTitle'){
			$(this).css("background"," transparent url(/img/mainPage/featureSectionOne_on.gif) no-repeat");
			$(this).children('.copy').css("color","#ffffff");
			$('#featureSectionSecondTitle').css("background","transparent url(/img/mainPage/featureSectionTwo_off.gif) no-repeat");
			$('#featureSectionSecondTitle .copy').css("color","#000000");
		}else{
			$(this).css("background"," transparent url(/img/mainPage/featureSectionTwo_on.gif) no-repeat");
			$(this).children('.copy').css("color","#ffffff");
			$('#featureSectionFirstTitle').css("background"," transparent url(/img/mainPage/featureSectionOne_off.gif) no-repeat");
			$('#featureSectionFirstTitle .copy').css("color","#000000");
		}		
        return false;
        }
      }
    );
  
}
function initIE6(){
	$('.usrGrpItemFirst').mouseover(function(e){
		$('.usrGrpItemFirst').css("background","transparent url(/img/govhk/usergp_leftbg_over.gif) no-repeat");
	}).mouseout(function(e){
		$('.usrGrpItemFirst').css("background","transparent url(/img/govhk/usergp_leftbg_normal.gif) no-repeat");
	});
	$('.usrGrpItem').mouseover(function(e){
		$(this).css("background","transparent url(/img/govhk/itemhover.gif) repeat-x");
	}).mouseout(function(e){
		$(this).css("background","transparent url(/img/govhk/itemnormal.gif) repeat-x");
	});
	$('#relatedLinksFirstList li, #relatedLinksSecondList li,#relatedLinksThirdList li').mouseover(function(e){
		if($(this).hasClass('contacttable') || $(this).hasClass('last'))
		{
			return false;
		}
		else{
			$(this).css("background","#EBD7A5 url(/img/mainPage/iwtarrow.gif) no-repeat scroll 95% center");
		}
	}).mouseout(function(e){
		if($(this).hasClass('contacttable') || $(this).hasClass('last'))
		{
			return false;
		}else if($(this).hasClass('alt')){
			$(this).css("background","#E1E0D4 url(/img/mainPage/iwtarrow.gif) no-repeat scroll 95% center");
		}
		else{
			$(this).css("background","#FAFAFA url(/img/mainPage/iwtarrow.gif) no-repeat scroll 95% center");
		}
	});
	
}
function initSmartSubMenu() {
	$('.smartSubSection').hide();
	$('.smartSubSubSection').hide();
	$('.subMenu a').click(function(e) {
		e.preventDefault();
		$(this).closest('div').next().slideToggle('normal');
	});
}

/* related link accordion */
function initRelatedLinks() {
  $('#innerPageRelatedLinks ul').hide();
  $('#innerPageRelatedLinks li div').click(
    function(event) {
     /* event.preventDefault();*/
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
      }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		$('#innerPageRelatedLinks ul li').hide(); 		
        $('#innerPageRelatedLinks ul:visible').slideUp('normal');
        checkElement.slideDown('normal', function(){
			checkElement.children('li').show();
		});  
		
		if ($(this).attr('id') == 'relatedLinksFirstTitle'){
			$(this).css("background"," transparent url(/img/smartMenu/relatedLinksFirst_on.gif) no-repeat");
			$(this).css("color","#ffffff");
			$('#relatedLinksSecondTitle').css("background","transparent url(/img/smartMenu/relatedLinksNormal_off.gif) no-repeat");
			$('#relatedLinksSecondTitle').css("color","#000000");
			$('#relatedLinksThirdTitle').css("background","transparent url(/img/smartMenu/relatedLinksNormal_off.gif) no-repeat");
			$('#relatedLinksThirdTitle').css("color","#000000");
		}else if ($(this).attr('id') == 'relatedLinksSecondTitle'){
			$(this).css("background"," transparent url(/img/smartMenu/relatedLinksNormal_on.gif) no-repeat");
			$(this).css("color","#ffffff");
			$('#relatedLinksFirstTitle').css("background","transparent url(/img/smartMenu/relatedLinksFirst_off.gif) no-repeat");
			$('#relatedLinksFirstTitle').css("color","#000000");
			$('#relatedLinksThirdTitle').css("background","transparent url(/img/smartMenu/relatedLinksNormal_off.gif) no-repeat");
			$('#relatedLinksThirdTitle').css("color","#000000");
		}else{
			$(this).css("background"," transparent url(/img/smartMenu/relatedLinksNormal_on.gif) no-repeat");
			$(this).css("color","#ffffff");
			$('#relatedLinksFirstTitle').css("background","transparent url(/img/smartMenu/relatedLinksFirst_off.gif) no-repeat");
			$('#relatedLinksFirstTitle').css("color","#000000");
			$('#relatedLinksSecondTitle').css("background","transparent url(/img/smartMenu/relatedLinksNormal_off.gif) no-repeat");
			$('#relatedLinksSecondTitle').css("color","#000000");
		}
        return false;
        }
      }
    );
   $('#relatedLinksThirdList').slideDown('normal');
   
}

//preload image functions
jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++)  {
		jQuery("<img>").attr("src", arguments[i]);
	}
}


/* === language selector dropdown === */
$(document).ready(function()
{
	var lang = ($('body').attr('lang')=='zh-CN')?'sc':($('body').attr('lang') == 'zh-HK')?'tc':'en';
	$.stylesheetInit();	
	if(lang == "tc"){
		$("#languageSelector").qtip({
		 content: {
			url: 'languageSelector_tc.html'
		 },
		 delay: '30',	 
		 position: {
		  corner: {
			 target: 'bottomRight',
			 tooltip: 'topRight'
		  }
		 },	 
		 hide: {
			fixed: true // Make it fixed so it can be hovered over
			//when: { target: $('#languageSelectorClose') }
		 },
		 style: {
			padding: '5px 5px', // Give it some extra padding
			width: 70,
			name: 'light' // And style it with the preset dark theme
		 }
		}).click(function(e){
			e.preventDefault();
		});
	 }
	else if(lang == "en")
	{
		$("#languageSelector").qtip({
		 content: {
			url: 'languageSelector.html'
		 },
		 delay: '30',	 
		 position: {
		  corner: {
			 target: 'bottomRight',
			 tooltip: 'topRight'
		  }
		 },	 
		 hide: {
			fixed: true // Make it fixed so it can be hovered over
			//when: { target: $('#languageSelectorClose') }
		 },
		 style: {
			padding: '5px 5px', // Give it some extra padding
			width: 70,
			name: 'light' // And style it with the preset dark theme
		 }
		}).click(function(e){
			e.preventDefault();
		});
	}
	else if(lang == "sc")
	{
		$("#languageSelector").qtip({
		 content: {
			url: 'languageSelector_sc.html'
		 },
		 delay: '30',	 
		 position: {
		  corner: {
			 target: 'bottomRight',
			 tooltip: 'topRight'
		  }
		 },	 
		 hide: {
			fixed: true // Make it fixed so it can be hovered over
			//when: { target: $('#languageSelectorClose') }
		 },
		 style: {
			padding: '5px 5px', // Give it some extra padding
			width: 70,
			name: 'light' // And style it with the preset dark theme
		 }
		}).click(function(e){
			e.preventDefault();
		});
	}
	
});

// load inline before <body>
$.stylesheetInit();

function initRelatedLinksSingleMenu() {
	$('#innerPageRelatedLinks ul').hide();
	$('#innerPageRelatedLinks li div.relatedLinksSingleMenu_open, #innerPageRelatedLinks li div.relatedLinksSingleMenu_close').click(function(event) {
		event.preventDefault();
		$('#innerPageRelatedLinks ul').slideToggle(300);
		if($(this).attr('class') == 'relatedLinksSingleMenu_close'){			
			$(this).removeClass('relatedLinksSingleMenu_close');
            $(this).addClass('relatedLinksSingleMenu_open');
		}else{				
			$(this).removeClass('relatedLinksSingleMenu_open');
            $(this).addClass('relatedLinksSingleMenu_close');		
		}		
	});
}

function initFaqMenu(){
	$(".faqAnswer").hide();
	$(".faqQuestion").each(function(){
		$(this).css("cursor","pointer");
	}).click(function(event){
		$(this).next().slideToggle('normal');
		$(this).toggleClass("faqQuestionActive");
	});

	
	$("#expandAll").click(function(event){
		event.preventDefault();								   	
		$(".faqQuestion").each(function(){
			$(this).next().slideDown('normal');
			$(this).addClass("faqQuestionActive");
		});								  	
	});

}

(function($) {
	$.fn.slideshowGov1 = function(options) {
		return this.each(function() {
			$.slideshowGov1(this, options);
			//setInterval( "slideSwitchShuffle('#slideshowbanner',200)", 5000 );
		});
	};
		$.slideshowGov1 = function(container, options) {
		var settings = {
		'fadetimebutton':          2000,
		'fadetimeleft':          500,
		'fadetimeright':          500,
		'fadetimeshuffle':          2000
		};
		if (options){
			$.extend(settings, options);
		}
		var containerwidth = $(container).css('width');
		var containerheight = $(container).css('height');
		$(container).children("#slideshow3").css({width:containerwidth,height:parseInt(containerheight)-28});
		$(container).children("#slideshow3").children("a").children("img").css({width:containerwidth, height:parseInt(containerheight)-28});
		$(container).children("#slidebar3").css({width:containerwidth});
		$(container).children("#slidetitle").css({width:parseInt(containerwidth)-20});
		$(container).children("#slidebar3").children(".slidebar3c").css({width:(parseInt(containerwidth)-40)});
		var slideVehicleItems =  $(container).children("#slideshow3").children("a");
		var numItems = slideVehicleItems.length;
		$(container).children("#slidebar3").children(".slidebar3c").css('padding-left', (parseInt(containerwidth)-40)/2-5*numItems);
		$(container).children("#slidebar3").children(".slidebar3c").css('width', (parseInt(containerwidth)-40)/2+5*numItems);
		//randomize beginning index
		

	//generate page indicator dots
		var indicators = '';
		var slideIntervalID;
		var currentIndex = Math.floor(numItems * (Math.random() % 1));
		for (var i = 0; i<numItems; i++){
			$(slideVehicleItems[i]).css({display:"none"});
			if(i==currentIndex){
			indicators += '<div class="slideIndicator active"></div>'; //activated
			}else{
			indicators += '<div class="slideIndicator"></div>';
			}
		}
		
		$(slideVehicleItems[currentIndex]).addClass('active');
		$(slideVehicleItems[currentIndex]).css({display:"block"});
		$(slideVehicleItems[currentIndex]).children("img").css({opacity: 0.0})
		.animate({opacity: 1.0}, 500);
		$(container).children("#slidetitle").html($(slideVehicleItems[currentIndex]).attr('title'));
		indicators += '';
		$(container).children("#slidebar3").children(".slidebar3c").html(indicators);
		
		var prevBtn = $(container).children("#slidebar3").children(".slidebar3l");
		prevBtn.click(function(event){
			slideSwitchLeft(container, settings.fadetimeleft);
			clearInterval(slideIntervalID);
		});
		
		$("#slideshow3,#slidebar3").mouseenter(function(event){
			clearInterval(slideIntervalID);
		}).mouseleave(function(event){
			slideIntervalID = setInterval( (function() {
            	slideSwitchRight(container,200);
        	}), 4000 );
		})
		
		var nextBtn =$(container).children("#slidebar3").children(".slidebar3r");
		nextBtn.click(function(event){
			slideSwitchRight(container, settings.fadetimeright);

		});
		var indBtn = $(container).children("#slidebar3").children(".slidebar3c").children(".slideIndicator");
		indBtn.click(function(event){
			slideSwitchClick(container, this, settings.fadetimebutton);

		});
		slideIntervalID = setInterval( (function() {
            slideSwitchRight(container,200);
        }), 4000 );
	};
})(jQuery);

function slideSwitchShuffle(container, fadeoutshuffle){
	var $active = $(container).children("#slideshow3").children("a.active");
	var $activeind =$(container).children("#slidebar3").children(".slidebar3c").children(".active");
	if ( $active.length == 0 ) $active = $(container).children("#slideshow3").children("a:last");
	if ( $activeind.length == 0 ) $activeind = $(container).children("#slidebar3").children(".slidebar3c").children(".slideIndicator:last");
	// use this to pull the images in the order they appear in the markup
	var $sibs  = $active.siblings();
	var $sibsind  = $activeind.siblings();
	var rndNum = Math.floor(Math.random() * $sibs.length );
	var $next  = $( $sibs[ rndNum ] );
	var $nextind = $( $sibsind[ rndNum ]);
	$active.addClass('last-active');
	$next.addClass('active');
	$active.css({display: "none"});
	$next.css({display:"block"})
	$next.css({opacity: 0.0}).animate({opacity: 1.0}, fadeoutshuffle, function() {
	$active.removeClass('active last-active');
	
	});
	$activeind.removeClass('active');
	$nextind.addClass('active');
	$(container).children("#slidetitle").html($next.attr('title'));
}
function slideSwitchRight(container, fadeoutright) {
	var $active = $(container).children("#slideshow3").children("a.active");
	var $activeind = $(container).children("#slidebar3").children(".slidebar3c").children(".active");
	if ( $active.length == 0 ) $active = $(container).children("#slideshow3").children("a:last");
	if ( $activeind.length == 0 ) $activeind = $(container).children("#slidebar3").children(".slidebar3c").children(".slideIndicator:last");
	// use this to pull the images in the order they appear in the markup
	var $next =  $active.next().length ? $active.next() : $(container).children("#slideshow3").children("a:first");
	var $nextind =  $activeind.next().length ? $activeind.next() : $(container).children("#slidebar3").children(".slidebar3c").children(".slideIndicator:first");
	$active.addClass('last-active');
	$active.css({display:"none"});
	$next.addClass('active');
	$next.css({display:"block"})
	$next.css({opacity: 0.0}).animate({opacity: 1.0}, fadeoutright, function() {
	$active.removeClass('active last-active');
	});
	$activeind.removeClass('active');
	$nextind.addClass('active');
	$(container).children("#slidetitle").html($next.attr('title'));
}
function slideSwitchLeft(container, fadeoutleft) {
	var $active = $(container).children("#slideshow3").children("a.active");
	var $activeind = $(container).children("#slidebar3").children(".slidebar3c").children(".active");
	if ( $active.length == 0 ) $active = $(container).children("#slideshow3").children("a:last");
	if ( $activeind.length == 0 ) $activeind = $(container).children("#slidebar3").children(".slidebar3c").children(".slideIndicator:last");
	// use this to pull the images in the order they appear in the markup
	var $next =  $active.prev().length ? $active.prev() : $(container).children("#slideshow3").children("a:last");
	var $nextind =  $activeind.prev().length ? $activeind.prev() : $(container).children("#slidebar3").children(".slidebar3c").children(".slideIndicator:last");
	$active.addClass('last-active');
	$active.css({display:"none"});
	$next.addClass('active');
	$next.css({display:"block"})
	$next.css({opacity: 0.0}).animate({opacity: 1.0}, fadeoutleft, function() {
	$active.removeClass('active last-active');
	});
	$activeind.removeClass('active');
	$nextind.addClass('active');
	$(container).children("#slidetitle").html($next.attr('alt'));
}
function slideSwitchClick(container, container2, timeout) {
	var slideVehicleItems2 = $(container).children("#slideshow3").children("a");
	var $active = $(container).children("#slideshow3").children("a.active");
	var $activeind = $(container).children("#slidebar3").children(".slidebar3c").children(".active");
	if ( $active.length == 0 ) $active = $(container).children("#slideshow3").children("a:last");
	if ( $activeind.length == 0 ) $activeind = $(container).children("#slidebar3").children(".slidebar3c").children(".slideIndicator:last");
	$activeind.removeClass('active');
	$(container2).addClass("active");
	var index = $(container2).parent().find('> '+ container2.tagName).index(container2);
	$active.addClass('last-active');
	$active.css({display:"none"});
	$(slideVehicleItems2[index]).css({display:"block"})
	$(slideVehicleItems2[index]).addClass('active');
	$(slideVehicleItems2[index]).css({opacity: 0.0})
		.animate({opacity: 1.0}, timeout, function() {
			$active.removeClass('active last-active');
	});
	$(container).children("#slidetitle").html($(slideVehicleItems2[index]).attr('title'));
}
