function encode(url) {
    return escape(url);
}

function decode(encoded) {
	return unescape(encoded.replace(/\+/g,  " "));
}

//current slide index counter
function onBefore(curr, next, opts) {
    var index = $(this).parent().children().index(this);
	$('span.current_num').text(index + 1);
}

//equal heights function - matches heights of a group of elements to the largest 
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function mycarousel_initCallback(carousel) {
	$('.slider-next').bind('click', function() {
		carousel.next();
		return false;
	});

	$('.slider-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
}

// get filename from full path
function getFileName(data){
	var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
	return {path: m[1], file: m[2]}
}

// load SWFObject
function loadAudio(path) {
	// swf accepts filename: ex. test.mp3
	var so = new SWFObject(path, "flashplayer", "239", "26", "9", "#efefef");
	so.addParam("wmode", "transparent");
	so.write("audio-player");
}

$(document).ready(function() {

	/**********
	open external links without _blank
	**********/
	$('a[@href^="http"]').click(function() {
		window.open( $(this).attr('href') );
		return false;
	});

	/****************************
	=SWF links (open in fancybox)
	*****************************/
    $('a[href$=.swf]').each(function(){
        //var swf = getFileName($(this).attr('href')).file;
        var swf = $(this).attr('href');
        //$(this).attr('href', '/_common/flash/swf_player.php?file=' + swf);
        $(this).attr('href', '/_common/flash/swf_player.php?file=' + encode(swf));
        $(this).fancybox({ 
            'imageScale': false,
            'zoomSpeedIn': 0,
            'zoomSpeedOut': 0,
            'frameHeight': 345,
            'frameWidth': 480,
            'overlayShow': true,
            'navigation': false,
            'overlayOpacity' : 0.75
        });
    });

	/*************************
	fancybox for photo gallery
	**************************/
	// general photo lightbox
    $('a.fancybox').fancybox({ 
		'zoomSpeedIn': 0,
		'zoomSpeedOut': 0,
		'frameHeight': 390,
		'frameWidth': 455,
		'overlayShow': true,
        'navigation': true,
		'overlayOpacity' : 0.75
	});

    // ie6 detection - adjust height/width for ie6
    if ($.browser.msie && $.browser.version.substr(0,1) < 7) {
        $('a.videobox').fancybox({ 
            'imageScale': false,
            'zoomSpeedIn': 0,
            'zoomSpeedOut': 0,
            'frameHeight': 395,
            'frameWidth': 425,
            'overlayShow': true,
            'navigation': false,
            'overlayOpacity' : 0.75
        });	
    } else {
        $('a.videobox').fancybox({ 
            'imageScale': false,
            'zoomSpeedIn': 0,
            'zoomSpeedOut': 0,
            'frameHeight': 355,
            'frameWidth': 425,
            'overlayShow': true,
            'navigation': false,
            'overlayOpacity' : 0.75
        });
    }

	/********
    rss links
	*********/
	$('a.rss-link').each(function () {
		var showDistance = 25;
		var hideDistance = 25;
	    var time = 250;
	    var hideDelay = 100;
	    var hideDelayTimer = null;
	    var beingShown = false;
	    var shown = false;
	    var quote = $(this).parent('p').find('span.rss-menu', this).css('opacity', 0);

	    // set the mouseover and mouseout on both element
	    $(this).parent('p').hover(function () {
			// stops the hide event
			if (hideDelayTimer) 
				clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				quote.css({ 
					position: 'absolute', 
					display: 'block'
				}).animate({
					top: showDistance,
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}
	    },function () {
			// reset the timer
			if (hideDelayTimer) 
				clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				quote.animate({
					top: hideDistance,
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					quote.css({'display' : 'none', 'top' : hideDistance });
				});
			}, hideDelay);
	    });

        $(this).click(function(){return false;});
         
	});

	/**********************
    sidenav content rotater
	***********************/
	$('#content-rotator-inner').cycle({ 
	    fx: 'scrollHorz', 
	    speed: 1500,
		easing: 'easeOutQuad',
	    timeout: 7000,
		delay: -2000, 
	    next: '#next-slide', 
	    prev: '#prev-slide',
		pause: 1,
		before: onBefore
	});
	
	/***********
	photo slider
	************/
	$('.photo-slider ul').jcarousel({
		'animation': 1000,
		initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
	});
	
	/************
	audio sidebar
	*************/
	if ( $('#audio-bar').size() > 0 ) {
		
		// get first audio file
		flashSrc = $('#audio-bar .audio-file:first').find('a.audio-src').attr('href');
		flashSrc = '/_common/flash/player.swf?fid=' + getFileName(flashSrc).file + "&autoplay=false";
		loadAudio(flashSrc);
		
		$('#audio-bar .audio-file').each(function(){
			$(this).find('a.audio-src').click(function(){
				flashSrc = $(this).attr('href');
				flashSrc = '/_common/flash/player.swf?fid=' + getFileName(flashSrc).file + "&autoplay=true";
				loadAudio(flashSrc);
				return false;
			});
		});
	
	}
	
	/************************************************
	set equalHeights on content rotater, and it children
	*************************************************/
    $('#content-rotator-inner .rotator_item:eq(0)').show();


	// fix: add image preload instead of setTimeOut
	setTimeout( function(){ equalHeight($('#content-rotator-inner, #content-rotator-inner .rotator_item')) }, 250);

	
	/*****************
	calendar show/hide
	******************/
	// hide all TextContent areas
	$('#event_items .event-details, .event_item .event-details').hide();
	
	// monthly view
	$('.teaser_looper_item').each(function(){
		
		// adding more links
		$(this).append('<a href="#" class="more-link">More Details</a>');
		
		$(this).find('h3 a, a.more-link').toggle(function(){
			$(this).parents('.teaser_looper_item').find('.event-details').slideDown();
			$(this).parents('.teaser_looper_item').find('a.more-link').text('Hide Detail');
			return false;
		}, function(){
			$(this).parents('.teaser_looper_item').find('.event-details').slideUp();
			$(this).parents('.teaser_looper_item').find('a.more-link').text('More Detail');
			return false;		
		});

	});
	
	// weekly view
	$('.event_item').each(function(){
		
		// adding more links
		$(this).append('<a href="#" class="more-link">More Details</a>');
		
		$(this).find('h3 a, a.more-link').toggle(function(){
			$(this).parents('.event_item').find('.event-details').slideDown();
			$(this).parents('.event_item').find('a.more-link').text('Hide Detail');
			return false;
		}, function(){
			$(this).parents('.event_item').find('.event-details').slideUp();
			$(this).parents('.event_item').find('a.more-link').text('More Detail');
			return false;		
		});

	});	
	
	
	/******************************
	accordian fold on services page
	*******************************/
	//hide nested ul's
	$('ul.accordian li ul').hide();
	
	//hide labels
	$('span.label').hide();
	
	//forward to href of anchor
	$('span.label').click(function(){
		//alert($(this).parent().attr('href'));
		window.location = $(this).parent().attr('href');
		return false;
	});
	
	var animation = "600, 'easeOutQuad'";
	//expand all nested lists
	$('a.expand-all').click(function(){
		$('ul.accordian > li > ul').slideDown(animation);
		$('span.label').slideDown(animation);
		$('ul.accordian li a').addClass('on');
		return false;
	});
	
	//collapse all nested lists
	$('a.collapse-all').click(function(){
		$('ul.accordian > li > ul').slideUp(animation);
		$('span.label').slideUp(animation);
		$('ul.accordian li a').removeClass('on');
		return false;
	});
	
	//click and expand individual nested lists
	$('ul.accordian').each(function(){
		var accordian = $(this);
		
		accordian.children('li.accordian-header').children('a').click(function(){
		

			accordian.children('li.accordian-header').children('a.on').removeClass('on');
			accordian.children('li.accordian-header').children('ul:visible').slideUp(animation);
			accordian.children('li.accordian-header').children('a').removeClass('on').children('span.label:visible').slideUp(animation);
			
			$(this).parent('li').children('ul:hidden').slideDown(animation);
			$(this).children('span.label:hidden').slideDown(animation).parent('a').addClass('on');
			
			return false;
		});	

	});		

});

function jumpMenu(selObj,restore){ //v3.0
 var targ = "self";
  
 eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function showhide(row,count) {
	//for(i=0; i<count; i++){
		which = document.getElementById(row);
		if(which.style.display == 'none') {
			which.style.display = '';
		} else{ 
			which.style.display = 'none';
		}
	//}
	return false;
}

function swapdivs(div1,div2) {
	//alert("Swap divs");
	swap1 = document.getElementById(div1);
	swap2 = document.getElementById(div2);
	
	swap1.style.display = 'none';
	swap2.style.display = '';
	return false;
	
}

function swaptrs(tr1,tr2) {

	swap1 = document.getElementById(tr1);
	swap2 = document.getElementById(tr2);
	
	swap1.style.display = 'none';
	swap2.style.display = 'table-row';
	return false;

}

function popUp2(URL, winName, oOverride) {
	//
	//	SYNTAX: oProp={ width:496, height:642 }; popUp2('print_me.php', 'PrintMe', oProp); return false;
	//
	var oWin = { toolbar:0, location:'no', status:'no', menubar:'no', scrollbars:'yes', resizable:'yes', width:460, height:400, top:0, left:0 };
	var str = '';
	for (prop in oOverride) { oWin[prop] = oOverride[prop]; }
	
	for (prop in oWin) {
		if (prop=='top') oWin['top'] = (screen.height/2) - (oWin['height']/2);
		if (prop=='left') oWin['left'] = (screen.width/2) - (oWin['width']/2);
		
		str += prop +'='+ oWin[prop] +',';
	}
	
	//alert(str);
	newWindow=window.open(URL, winName, str);
	//newWindow.focus();
}
