/**
 * @author Alexander Farkas
 * v. 1.1
 */

(function($){
	if(!document.defaultView || !document.defaultView.getComputedStyle){
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
})(jQuery);

(function($) {
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

/*
* Add Background animation effects to navigation and drop down menu animation
*/
var isOverActive = false;

$(function(){
	$("#menu").children()
		.hover(function(){
			if(!$(this).hasClass("active")){
				$(this)
					.css({backgroundPosition: "0px 28px"})
					.stop()
					.animate({backgroundPosition: "(0px -224px)"}, {duration:500});
				jQuery.isOverActive = true;
				setTimeout("hideActive()", 200);
			};
		}, function(){
			if(!$(this).hasClass("active")){
				$(this)
					.stop()
					.animate({backgroundPosition: "(0px 28px)"}, {duration:300});
				jQuery.isOverActive = false;
				setTimeout("showActive()", 500);
			};
		});
});

$(function(){
	//jQuery("#nav a, #subnav a").removeAttr('title');
	$("#menu ol").css({display: "none"});
	$("#menu li").hover(function(){
		$(this).find('ol:first')
			.css({visibility: "visible",display: "none"})
			.slideDown(400);
	},function(){
		$(this).find('ol:first')
			.css({visibility: "hidden"});
	});
});

function hideActive(){
	if(isOverActive){
		$("#menu").find(".active")
			.css({backgroundPosition: "0px 28px"})
			.stop()
			.animate({backgroundPosition: "(0px 28px)"}, {duration:300})
			.addClass("temp")
			.removeClass("active");
	};
};

function showActive(){
	if(!isOverActive){
		$("#menu").find(".temp")
			.stop()
			.animate({backgroundPosition: "(0px -224px)"}, {duration:500})
			.addClass("active")
			.removeClass("temp");
	};
};

/*
* Add animation and functionality for Featured Items in the Ribbon
*/

function initActiveAnchor(){
	$('#featured-thumbs a')
		.each(function(){
			$(this)
				.bind("mouseenter", function(){
					if(!$(this).hasClass("activeSlide")){
						$(this)
						.stop()
						.animate({left: "10px"})
					}
				})
				.bind("mouseleave", function(){
					if(!$(this).hasClass("activeSlide")){
						$(this)
						.stop()
						.animate({left: "22px"})
					}
				})
				.stop()
				.animate({left: "22px", opacity: .5});		   
		});
	$('#featured-thumbs a:eq(0)')
		.stop()
		.animate({left: "10px", opacity: 1});
};

function resetActiveAnchor(){
	$('#featured-thumbs a')
		.each(function(){
			$(this)
				.stop()
				.animate({left: "22px", opacity: .5});		   
		});
};

function setActiveAnchor(){
	showFeaturedText(parseFloat(this.id));
	resetActiveAnchor();
	$('#featured-thumbs')
		.find(".activeSlide")
		.stop()
		.animate({left: "10px", opacity: 1});
};

var featNum = 0;

function showFeaturedText(id){
	var pos = id;
	var pos2;
	if(id > 3){
		pos2 = 0;
	} else {
		pos2 = id;
	};

	$(".featured-text-item")
		.stop()
		.fadeOut("medium")
		.eq(pos)
		.stop()
		.fadeIn("medium");
};

$(document).ready(function() {
	$('#featured-image-cycle').cycle({
		fx: 'fade',
		timeout: 7000,
		speed: 500,
		sync: 1,
		pause: 1,
		next: $("#featured-image-wrapper"),
		pager: "#featured-thumbs",
		after: setActiveAnchor,
		pagerAnchorBuilder: function(idx, slide) {
			return '#featured-thumbs li:eq(' + idx + ') a'; 
		}
	});
	initActiveAnchor();
});



    






$(document).ready(function() {
    $('#top-menu li').hover(
		function() {
		    $('#' + $(this).attr('id') + '-panel').fadeIn('fast');
		},
		function() {
		    $('#' + $(this).attr('id') + '-panel').fadeOut('fast');
		}
	);

    function showEvents(dayclass) {
        $('a.allevents').css({
            backgroundImage: 'none',
            color: '#816D58'
        });
        var image = 'url(' + base_url + 'images/background-calendar-day-' + dayclass + '.png)';
        $('a.' + dayclass).css({
            backgroundImage: image,
            color: '#ffffff'
        });
    }
    $('#calendar-legend li').click(function() {
        showEvents($(this).attr('class'));
    });


    function showMainEvents(eventClass) {
        $('.calendar-cell-events .all').hide();
        $('.calendar-cell-events .' + eventClass).show();
    }
    $('#full-calendar-legend li').click(function() {
        showMainEvents($(this).attr('class'));
    });

    $('#homerotation').cycle({ timeout: 7000 });

});
