﻿$(document).ready(function() {
	$('.js .noDisplay').show();
	$('a.expand').parent().nextAll().hide();
	$('a.expand').toggleClass("expanded").parent().nextAll().animatIE({ "height": "toggle" }, 0).toggleClass("expanded-item");

	$('.collapsed-content').eq(0).each(function() {
		$('.expanded-item', this).animatIE({ "height": "toggle", "opacity": "toggle" }, 0).toggleClass("expanded-item");
		$('.expanded', this).toggleClass("expanded");
	});

	var b = navigator.userAgent.toLowerCase();
	jQuery.browser = {
		safari: /webkit/.test(b),
		opera: /opera/.test(b),
		msie: /msie/.test(b) && !/opera/.test(b),
		msie6: /msie 6.0/.test(b) && !/opera/.test(b),
		mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
	};

	// Tabs

	// Set active tab from cookie, or if not present, select the first tab
	if ($("#" + $.cookie("tabSettings")).length != 0) {
		$("#" + $.cookie("tabSettings")).parents('li').toggleClass('active');
	} else {
		$('.tabs li:first').toggleClass('active');
	};

	// Hide inactive elements
	$('.tabs li:not(.active)').each(function() {
		var cls = $('a', this).attr('href').split('#')[1];
		$('.' + cls).hide().parents('li').removeClass('active');
	});

	// Set active element visible
	$('.tabs li.active').each(function() {
		var cls = $('a', this).attr('href').split('#')[1];
		$('.' + cls).animatIE({ "opacity": "100", "height": "100%" }, 600);
	});

	$('.tabs li a').click(function() {
		var ncls = $(this).attr('href').split('#')[1];
		$('.tabs li a').each(function() {
			var tcls = $(this).attr('href').split('#')[1];
			if (ncls != tcls) {
				$(this).parents('li').removeClass('active');
				$('.' + tcls).stop(true, true).hide();
			} else {
				$(this).parents('li').toggleClass('active');
				$('.' + ncls).animatIE({ "opacity": "toggle", "height": "toggle" }, 600);

			}
		});

		// Store active element
		$.cookie("tabSettings", $(this).attr('Id'), { path: '/', expires: 1 });

		return false;
	});

	// text-size
	$('a.cssswap-fonts').cssSwap({ classGroup: '.cssswap-fonts' });

	// text / graphic version
	$('a.cssswap-text').cssSwap({ classGroup: '.cssswap-text' });

	// expand / collapse actions
	$('a.expand').click(function() {
		$(this).toggleClass("expanded").parent().nextAll().animatIE({ "height": "toggle" }, 250).toggleClass("expanded-item");
		return false;
	});

	$('a.collapse').click(function() {
		$(this).parents('.content').eq(0).each(function() {
			$('.expanded-item', this).animatIE({ "height": "toggle", "opacity": "toggle" }, 200).toggleClass("expanded-item");
			$('.expanded', this).toggleClass("expanded");
		});
		$(this).parents('.collapsed-content').eq(0).each(function() {
			$('.expanded-item', this).animatIE({ "height": "toggle", "opacity": "toggle" }, 200).toggleClass("expanded-item");
			$('.expanded', this).toggleClass("expanded");
		});
		return false;
	});

	// Fixes png image transparency in images and image buttons in IE6
	if ($.browser.msie6) {
		$('[src$=.png]').ifixpng().each(function() {
			$(this).css("padding-bottom", parseInt($(this).css("padding-bottom").replace(/[^!0-9]/g, '')) + 4 + "px");
			$(this).css("padding-right", parseInt($(this).css("padding-right").replace(/[^!0-9]/g, '')) + 4 + "px");
		});
	}
});

// Custom animation function to fix MSIE opacity issues
(function($) {
    $.fn.animatIE = function(params, speed, callback) {
        if ($.browser.msie) {
            var tmp = $(this).css("background-color");
            $(this).css("background-color", "#fff").animate(params, speed, function() {
                $(this).css("background-color", tmp).get(0).style.removeAttribute('filter');
                if (callback != undefined) callback();
            });
        } else {
            $(this).animate(params, speed, function() {
                if (callback != undefined) callback();
            });
        }
        return this;
    };
})(jQuery);