$(function(){
	
	/* EXTERNAL LINKS start ***********************************************************/
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});
	/* EXTERNAL LINKS end *************************************************************/
	
	$('html').addClass('js');
	
	/*SEARCH start ********************************************************************/
	$('#search-site input[type="text"]')
		.val('Search')
		.focus(function(){ if(this.value=='Search') { $(this).val('');} })
		.blur(function(){ if(this.value =='') { $(this).val('Search');} });
	/*SEARCH end **********************************************************************/

	/* HOMEPAGE SLIDER ****************************************************/
	var selector = '#slideshow .photo > div';
	var fade_speed = 1000;
	var slideshow = setInterval( "slideSwitch('"+selector+"',"+fade_speed+",'Next')", 5000 );
	var selector_num = '#slideshow .nav .slide';
	var fade_speed_num = 0;
	var slideshow_num = setInterval( "slideSwitch('"+selector_num+"',"+fade_speed_num+",'Next')", 5000 );
	
	$('#slideshow .nav .slide').click(
		
		function(){
			clearInterval(slideshow);
			clearInterval(slideshow_num);
			var id = $(this).data('photo');
			$(selector).removeClass('active');
			$(selector_num).removeClass('active');
			$('#'+id).addClass('active');
			$(this).addClass('active');
			return false;
		}
	);	
	/* HOMEPAGE SLIDER ****************************************************/
	
	if( $('#page-calendar-events .event .description').text() !='' ){
		$('#page-calendar-events .event .description').before('<span class="view">View</span>');
	}
	$('#page-calendar-events .event .view').click(function(){	
		popup_build('popup-event-description',$(this).next('.description').html(),true);
	});
	
}); //close document.ready

/* SLIDESHOW function start *********************************************************/
function slideSwitch(s,o,d){var $a=$(s+'.active');if($a.length==0)$a=$(s+':first');if(d=='Next'||d==null){var $n=$a.next().length?$a.next():$(s+':first');}else if(d=='Previous'){var $n=$a.prev().length?$a.prev():$(s+':last');}$a.addClass('last-active');$n.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0},o,function(){$a.removeClass('active last-active');});}
/* SLIDESHOW function end ***********************************************************/

var popup_status = 0;
function popup_build(id,html_build,bg_close){
	$('body').append('<div id="'+id+'" class="popup"><span class="close"></span><div class="d"></div></div><div id="popup-bg"></div>');
	$display = $('#'+id);
	$popup_bg = $("#popup-bg");

	popup_load($display,$popup_bg);
	$('.d',$display).html(html_build);

	if(bg_close){
		$popup_bg.add($('.close',$display)).click(function(){popup_disable($display);});
		$(document).keypress(function(e){ if(e.keyCode==27 && popupStatus==1){ popup_disable($display); } });
	}
	return $display;
}
function popup_load($display,$popup_bg){
    //loads popup only if it is disabled
    if(popup_status==0){
        $popup_bg.css({"opacity": "0.7"});
        $popup_bg.fadeIn("slow");
        $display.show();
        popup_status = 1;

        //center popup
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $display.height();
        var popupWidth = $display.width();
        //centering
        $display.css({"position": "absolute","left": windowWidth/2-popupWidth/2,"top": getYX('Y')+100}); //,"top":100
        //only need force for IE6
        $popup_bg.css({"height": windowHeight});
    }
}
function popup_disable($display){
    //disables popup only if it is enabled
    if(popup_status==1){
        $display.fadeOut("slow");
        $('#popup-bg').fadeOut("slow");
		setTimeout(function () { $('#popup-bg').remove();$display.remove(); }, 1000);
        popup_status = 0;
    }
}
//getYX
eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('k g(a){f b=0,3=0;4(i(9.c)==\'e\'){3=9.c;b=9.h}8 4(1.5&&(1.5.6||1.5.7)){3=1.5.7;b=1.5.6}8 4(1.2&&(1.2.6||1.2.7)){3=1.2.7;b=1.2.6}4(a=="j"){d b}8 4(a=="l"){d 3}}',22,22,'|document|documentElement|scrOfY|if|body|scrollLeft|scrollTop|else|window|||pageYOffset|return|number|var|getYX|pageXOffset|typeof|X|function|Y'.split('|'),0,{}))
