//function to replace target="_blank" which is not xhtml valid
//example <a href="" rel="external"></a>
externalLinks = function() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target="_blank"; 
 } 
} 


//automatically clear default value form fields
showHideFields = function(){
	$('#password-clear').show();
	$('#password-password').hide();

	$('#password-clear').focus(function() {
		$('#password-clear').hide();
		$('#password-password').show();
		$('#password-password').focus();
	});
	$('#password-password').blur(function() {
		if($('#password-password').val() == '') {
			$('#password-clear').show();
			$('#password-password').hide();
		}
	});

	$('.email_input').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
}


loadPhoto = function(el, id){
	//
	var nextId = id+1;
	var prevId = id-1;
	//$("#image"+id).html("Previous Photo");
	//alert($("#image"+prevId+" img").attr("src"));
	//$("#image"+prevId+" img").attr("src",$("#image"+id+" img").attr("src"));
	//$("#image"+prevId+" a.link").attr("onClick","loadPhoto(this,"+id+");return false;");
	if($(el).html()=="Next Photo"){
		$(el).parent().prev().find("img").attr("src",$("#image"+id+" img").attr("src"));
		$(el).prev().find("img").attr("src",$("#image"+nextId+" img").attr("src"));
		$(el).attr("onClick","loadPhoto(this,"+nextId+");return false;");
		$(el).parent().prev().find("a.link").attr("onClick","loadPhoto(this,"+id+");return false;");
	}else{
		$(el).parent().next().find("img").attr("src",$(el).prev().find("img").attr("src"));
		//alert(prevId);
		$(el).prev().find("img").attr("src",$(el).parent().prev().find("img").attr("src"));
		//$(el).attr("onClick","loadPhoto(this,"+prevId+");return false;");
		//$(el).parent().prev().find("a.link").attr("onClick","loadPhoto(this,"+nextId+");return false;");
	}
	//$("#image"+nextId).toggleClass("hidden_gallery");
}

//load the slideshow etc.
$(document).ready(function(){
	showHideFields();
	externalLinks();
	/*$("#events_calendar").datepicker({
		 beforeShowDay: function(thedate) {
			var theday    = thedate.getDate();
			var specialDays = [];
			specialDays[0] = 13;
			specialDays[1] = 25;
			specialDays[2] = 29;
			if( $.inArray(theday,specialDays) == -1 ){ 
				return [true,""];
			}else{
				return [true, "specialDate"];
				
			}
		  }
	 });*/
	$('#slideshow').nivoSlider({
		effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
		animSpeed:500, //Slide transition speed
		pauseTime:3000,
		directionNav:false, //Next & Prev
		controlNav:false, //1,2,3...
                pauseOnHover:false

	});

	$("a[rel='news']").colorbox();
	
	
	//jobs slideshow
	$("#gallery").easySlider({
		auto: true,
		continuous: true,
		pause:5000
	});
	$("a[rel='gallery']").colorbox();
	
});


// member area calendar
jQuery(function(){
    jQuery.ajax({
        url: "events-dates.html",
        dataType: "text",
        success: function(json){
            jQuery("#memberAreaCalendar" ).datepicker({
                firstDay:1,
                numberOfMonths: 2,
                onSelect: function(dateText){
                    
                    window.location="events.html?date="+dateText;
                },
                beforeShowDay: function(date){
                      var result = [false,""];
                      jQuery.each(eval(json), function(idx, val){
                        var d = new Date(val);
                        if (d.getDate()==date.getDate() &&
                            d.getMonth()==date.getMonth() &&
                            d.getFullYear()==date.getFullYear()){
                            result = [true,"preselected"];
                            return;
                        }
                      });
                    return result;
                }
            });
        }
    });
});

//loadCalendar = function(){
//    $.ajax({
//        url: "ajax/events.jsp",
//        data: "",
//        dataType: "json",
//        success: function(calendarEvents){
//            //alert(calendarEvents);
//            $("#events_calendar").datepicker({
//                onSelect: function( selectedDate ) {
//                    //alert(selectedDate);
//                    //window.location="training-and-events.html?date="+selectedDate;
//                    window.location="training-and-events.html";
//                },
//                beforeShowDay: function(date) {
//
//                    for (i = 0; i < calendarEvents.length; i++) {
//                        //alert("ici");
//                        if (date.getMonth() == calendarEvents[i][2] - 1
//                            && date.getDate() == calendarEvents[i][1]
//                            && date.getFullYear() == calendarEvents[i][0]) {
//                            //[disable/enable, class for styling appearance, tool tip]
//
//                            return [true,"specialDate",'Event On'];
//                        }else{
//                    //return [false,""];
//                    }
//                    }
//                    return [true,""];
//
//                }
//
//            });
//
//        }
//
//    });
//
//}

