/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            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];

           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]];
           }
        }
	});
})(jQuery);

function slideSwitch() {
    var $active = $('#main-image img.active');

    if ( $active.length == 0 ) $active = $('#main-image img:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#main-image img:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
	$('#main-image img:eq(0)').addClass('active');
	if($('.slideshow #main-image img').length > 1){
		setInterval( "slideSwitch()", 5000 );
	}
	
	if ( $("#news").length > 0 ) {
		var options = {
  			newsList: "#news",
 			startDelay: 90,
			tickerRate: 70,
 			placeHolder1: "_"
		}
		$().newsTicker(options);
	}
	
});

$(document).ready(function() {

    function addMega() {
        $(this).children("ul").fadeIn("fast");

    }

    function removeMega() {
        $(this).children("ul").fadeOut();
    }

    var megaConfig = {
        interval: 100,
        sensitivity: 3,
        over: addMega,
        timeout: 100,
        out: removeMega
    };

    $("li.mega").hoverIntent(megaConfig);

    $("#top-navigation ul li").addClass("no-hover");

    $('#top-navigation a')
    .css({
        backgroundPosition: "0 -172px"
    })
    .mouseover(function() {
        $(this).stop().animate({
            backgroundPosition: "(0 -1px)"
        },
        {
            duration: 400
        })
    })
    .mouseout(function() {
        $(this).stop().animate({
            backgroundPosition: "(0 -172px)"
        },
        {
            duration: 200,
            complete: function() {
                $(this).css({
                    backgroundPosition: "0 -172px"
                })
            }
        })
    })

	/* make the box on jumpoffs links */
	if ( $(".jumpoff .handle").length > 0 ) {
		
	}else{
		$(".jumpoff").click(function() {
	  		window.location = $(this).find("h3 a:first").attr("href");
		});
		
		$(".jumpoff").hover(
 			function() {
        		$(this).addClass("hover");
      		},
      		function() {
        		$(this).removeClass("hover");
      		}
    	);
	}
	

});

