$(function() {
	
	// determine the langauge type	   
	var langaugeUri = switchLangauge_uri();
	$(".lang-switch").attr("href",langaugeUri);


	// detect the drop down box for calendar
	$('#calendar select').change(function() {
		//$('#output').text("done");
		this.form.submit();
	});
	
	
		// detect the month drop down box for calendar
	$('.cal_DDMonthbox').change(function() {
		//$('#output').text("change month");
		//console.log("Month change");
		selectedMonth = $(this).val()-1 ;
		$('#calendar_calView').fullCalendar('gotoDate', selectedYear, selectedMonth,1);

	});
	// initialize the lightbox for frist page slideshow pic lightbox display
	$('#pic_slideshow a').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: 'js/jquery.lightbox-0.5/images/lightbox-ico-loading.gif',
		imageBtnClose: 'js/jquery.lightbox-0.5/images/lightbox-btn-close.gif',
		imageBtnPrev: 'js/jquery.lightbox-0.5/images/lightbox-btn-prev.gif',
		imageBtnNext: 'js/jquery.lightbox-0.5/images/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Image',
		txtOf: 'of'
						 
	});
	
	// initialize the lightbox for first page gallery picture slideshow lightbox display
	$('#gallery a').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: 'js/jquery.lightbox-0.5/images/lightbox-ico-loading.gif',
		imageBtnClose: 'js/jquery.lightbox-0.5/images/lightbox-btn-close.gif',
		imageBtnPrev: 'js/jquery.lightbox-0.5/images/lightbox-btn-prev.gif',
		imageBtnNext: 'js/jquery.lightbox-0.5/images/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Image',
		txtOf: 'of'
						 
	});	
		   
	// initialize the picture cycling slideshow plug-in for first page slideshow lightbox display
    $('.slideshow').cycle({
        fx: 'scrollHorz, scrollVert, fade',
        timeout:  9000,
        prev:    '#prev',
        next:    '#next',
        pager:   '#nav',
        pagerAnchorBuilder: pagerFactory
    });
 
 	// initialize the picture cycling slideshow plug-in for first page gallery slideshow lightbox display
     $('.pic_slideshow').cycle({
        fx: 'scrollHorz, fade',
        timeout:  9000
    });

	// for cycle plug-in pagination purpose.
    function pagerFactory(idx, slide) {
        var s = idx > 5 ? ' style="display:none;"' : '';
       return '<li' + s + '><a style="" href="#">'+(idx+1)+'</a></li>';
    };
    
	//slideShow();
	
});

// setting up css for gallery (if have time, remove these and put some of them in css)
function slideShow() {
	//Set the opacity of all images to 0
	$('#gallery a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#gallery a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#gallery .caption').css({opacity: 0.7});
 
	//Resize the width of the caption according to the image width
	$('#gallery .caption').css({width: $('#gallery a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	$('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
	.animate({opacity: 0.7}, 400);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
}
 
// gallery start up setting 
function gallery() {
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));
 
	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
 
	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '100px'},500 );
	
	//Display the content
	$('#gallery .content').html(caption);
	
}

// return current page name and switch the langauge.
var switchLangauge_uri = (function() {
		var uri = window.location.pathname;
		var query_str = window.location.search;
		uri = uri.split("/");
		
		//console.log(uri[uri.length-1]);

		if(uri[uri.length-1] !== null || uri[uri.length-1] !== undefined) {
			switch(uri[uri.length-1]) {
				case "content.php": case "event_gallery.php": case "events.php": 
				case "news.php": case "events_content.php": case "news_content.php":
				case "event_gallery.php": case "pictureList.php": case "index.php":
					//console.log("e to c");
					uri = ("c_" + uri[uri.length-1] + query_str);
					break;
				case "c_index.php": case "c_content.php": case "c_event_gallery.php": case "c_news.php": 
				case "c_events.php": case "c_events_content.php": case "c_news_content.php":
				case "c_event_gallery.php": case "c_pictureList.php":				
					//console.log("c to e");
					uri = uri[uri.length-1].substr(2);
					uri = (uri + query_str);
					break;
				case "": 
					//console.log("index.php");				
					uri = "c_index.php";
					break;
				default:
					//console.log("default");
					uri = "index.php";	
			}
		
		} else {
			//console.log("index");
			uri = "index.php";	
		}
		
		return uri
});


