$(document).ready(function(){
	
	// boarding school search
	$(".bss-form").submit(function(){
			var styp = this.stype.value;
			var sloc = this.slocation.value;
			var smaxfee = this.sfees.value;
			var rule1,rule2,rule3;
			if (styp != "所有") {
				rule1 = "typ == styp";
			} else {
				rule1 = "1 == 1";
			}
			if (sloc != "All") {
				rule2 = "loc == sloc";
			} else {
				rule2 = "1 == 1";
			}
			rule3 = "maxfee < smaxfee";
			//rule3 = "1 == 1";
			var allrules = "if(" + rule1 + " && " + rule2 + " && " + rule3 + ") { $(this).show();}";
			var jx = 0;
 			var bsstable = $("table.bss-results");
			$(".bss-hasresults").hide();
			$(".bss-noresults").hide();
			$("tr",bsstable).hide();
			$("tr",bsstable).eq(0).show();
			$("tr",bsstable).eq(1).show();
			$("tr",bsstable).each(function (i) {
				if (i < 2) return; // skip first 2 rows
				var typ = this.cells[3].innerHTML;
				var loc = this.cells[2].innerHTML;
				var fee1 = this.cells[4].innerHTML;
				if (fee1 == '-') fee1 = 0;
				var fee2 = this.cells[5].innerHTML;
				if (fee2 == '-') fee2 = 0;
				var fee3 = this.cells[6].innerHTML;
				if (fee3 == '-') fee3 = 0;
				var fee4 = this.cells[7].innerHTML;
				if (fee4 == '-') fee4 = 0;
				var fee5 = this.cells[8].innerHTML;
				if (fee5 == '-') fee5 = 0;
				var fee6 = this.cells[9].innerHTML;
				if (fee6 == '-') fee6 = 0;
				var maxfee = Math.max(fee1,fee2,fee3,fee4,fee5,fee6);
				eval(allrules);
				if ($(this).is(":visible")) {
					jx++;
				}
			});
			if (jx > 0) {
				$(".bss-hasresults").show();
			} else {
				$("tr",bsstable).eq(0).hide();
				$("tr",bsstable).eq(1).hide();
				$(".bss-noresults").show();
			}
			
			
			return false;
		});
		
		$(".bss-form").trigger('submit');
	  
	
	//tab
	var tabContents = $(".tab_content").hide(),
    tabs = $("ul.tabs li");
	tabs.first().addClass("active").show();
	tabContents.first().show();

	tabs.hover(function() {
		var $this = $(this),
			activeTab = $this.find('a').attr('rel');
		
		if(!$this.hasClass('active')){
			$this.addClass('active').siblings().removeClass('active');
			tabContents.hide().filter(activeTab).show();
		}
		
		return false;
	});
	
	
	
	//search tab
	var stabContents = $(".stab_content").hide(),
    stabs = $("ul.search-tabs li");
	stabs.first().addClass("active").show();
	stabContents.first().show();

	stabs.hover(function() {
		var $this = $(this),
			activeTab = $this.find('a').attr('href');
		
		if(!$this.hasClass('active')){
			$this.addClass('active').siblings().removeClass('active');
			stabContents.hide().filter(activeTab).show();
		}
		
		return false;
	});
	
	



	//Show the paging and activate its first link

	var paging = $(".paging");
	var imageReel = $(".image_reel");

	$(paging).show();
	$("a:first", paging).addClass("active");

	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $(".banners").width();
	var imageSum = $("img",imageReel).size();
	var imageReelWidth = imageWidth * imageSum;

	//Adjust the image reel to its new size
	$(imageReel).css({'width' : imageReelWidth});

	//Paging  and Slider Function
	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$("a", paging).removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

		//Slider Animation
		$(imageReel).animate({
			left: -image_reelPosition
		}, 500 );

	}; 

	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = $('a.active',paging).next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('a:first',paging); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (7 seconds)
	};

	rotateSwitch(); //Run function on launch

	//On Hover
	$("a",imageReel).hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});	

	//On Click
	$("a",paging).click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});



	var wrap = $('.newspagin');
	// set up click events to trigger the pagination plugins' behaviour 

	$('.prev').click(function(){
	wrap.trigger('prev.evtpaginate');
		return false;
	});

	$('.next').click(function(){
		wrap.trigger('next.evtpaginate');
		return false;
	});

	// listen out for events triggered by the plugin to update the counter

	wrap.bind( 'initialized.evtpaginate', function(e, startnum, totalnum ){
		$('#count').text(startnum);
		$('#total').text(totalnum);
	}); 

	wrap.bind( 'finished.evtpaginate', function(e, num, isFirst, isLast ){ $('#count').text(num); } ); 	

	wrap.evtpaginate({perPage:5}); // call the plugin!	
	
	
	
	var wrap_an = $('.newspagin_an');
	// set up click events to trigger the pagination plugins' behaviour 
	
	

	$('.prev_an').click(function(){
	wrap_an.trigger('prev.evtpaginate');
		return false;
	});

	$('.next_an').click(function(){
		wrap_an.trigger('next.evtpaginate');
		return false;
	});

	// listen out for events triggered by the plugin to update the counter

	wrap_an.bind( 'initialized.evtpaginate', function(e, startnum, totalnum ){
		$('#count_an').text(startnum);
		$('#total_an').text(totalnum);
	}); 

	wrap_an.bind( 'finished.evtpaginate', function(e, num, isFirst, isLast ){ $('#count_an').text(num);$('html, body').animate({scrollTop:0}, 500);
 } ); 	

	wrap_an.evtpaginate({perPage:15}); // call the plugin!	
	
	
	





	//fancybox
			
	$("#csl").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
			
			
	$(".bf").fancybox({
		'width' : '7',
		'height' : '6',
		'autoScale' : false,
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'type' : 'iframe',
		'titleShow'			: false
	});
			
			
	$(".SF_galmain a").fancybox();
			
	$(".image-pop").fancybox({
		'overlayOpacity' : 0.5,
		'hideOnContentClick' : true
	});
			
			
	/*
	$('#ss-form').submit(function() {
		$.fancybox(
			'<h2>Searching!</h2><p style="line-height:35px;"><img src="<?php bloginfo('template_url'); ?>/images/ajax_loader.gif" alt="" /> Please wait...</p>',
					{
							'autoDimensions'	: false,
						'width'         		: 250,
						'height'        		: 'auto',
						'transitionIn'		: 'none',
						'transitionOut'		: 'none',
						'modal'				: true
					}
				);
				return true;
			});
	*/		
			
	
	//booking form validation
	$('form#sbookform').submit(function() {
		if ($('#first_name').val() == '姓' ) {
			$('#first_name').val('');
		}
		if ($('#last_name').val() == '名' ) {
			$('#last_name').val('');
		}
		var hasError = false;
		$('.requiredField').each(function() {
			var currentId = $(this).attr('id');
			$('.' + currentId + '_required').html('*').removeClass('error');
			if(jQuery.trim($(this).val()) == '') {
				$('.' + currentId + '_required').html('* 必須填寫!').addClass('error');
				hasError = true;
			} else if($(this).hasClass('email')) {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if(!emailReg.test(jQuery.trim($(this).val()))) {
					$('.' + currentId + '_required').html('* 電郵地址不正確!').addClass('error');
					hasError = true;
				}
			}
		});
				
		if (hasError) {
			window.alert('表格資料中有錯誤!');
			return false;
		} else {
			return true;
		}
	});
	
		
	$("select#source").change(function(){
		$("select#source-search").css("display","none");
		$("#source-remarks").css("display","none");
		if($(this).val() == '搜尋網站') {
			$("select#source-search").css("display","inline");
		} else if($(this).val() == '其他') {
			$("#source-remarks").css("display","inline");
		}
	});
			
	$("select#source-search").change(function(){
		$("#source-remarks").css("display","none");
		if($(this).val() == 'Others') {
			$("#source-remarks").css("display","inline");
		}
	});
			
			
	$('#00N900000022DDf').datepick({
		yearRange: '2011:2015',
		dateFormat: 'yyyy-mm-dd'
	});
			
					
					
});
