var rootFolder = '';

jQuery.extend( jQuery.easing, {
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	}
});

function getCookie(key) {
    var cookies = document.cookie.split('; ');
    for (index in cookies) {
        if (cookies[index].indexOf(key) == 0) { // found 
            return cookies[index].substr(key.length + 1); //"<key>="
        }
    }
    return null;
}

$(document).ready(function () {
	// Clears input fields on Focus
	$("input").bind("blur", function() {
		if ($(this).val()=="") {
			$(this).val($(this).attr("title"));
		};
	});
	$("input").bind("focus", function() {
		if ($(this).val()==$(this).attr("title")) {
			$(this).val("");
		};
	});
	
	$('#header a.open').click(function(e)
	{
		if ($('.shoppingcartdisplay-box:visible').length <= 0) {
			e.preventDefault();
			e.stopPropagation();
			$('.shoppingcartdisplay-box').fadeIn(100);

			$(document).click(function (e) {
				if ($(e.target).parents('.shoppingcartdisplay-box').length <= 0)
					$('.shoppingcartdisplay-box').fadeOut(100);
			});

			$('a#membersMenu').removeClass('active');
			$('#membersDropdown').fadeOut(100);
		}
	});
	
	if ($('#membersDropdown').length) {
		$('a#membersMenu, div.mainNavigation li.login a').click(function(e)
		{
			if ($('#membersDropdown:visible').length <= 0) {
				e.preventDefault();
				e.stopPropagation();
				$(window).scrollTop(0); /*$('body').animate({ scrollTop: 0 }, 500);*/
				$('a#membersMenu').addClass('active');
				$('#membersDropdown').fadeIn(100).first('input').focus();
				
				$(document).click(function (e) {
					if ($(e.target).closest('#membersDropdown').length <= 0) {
						$('a#membersMenu').removeClass('active');
						$('#membersDropdown').fadeOut(100);
					}
				});
			}
		});
	} else {
		$('div.mainNavigation li.login').remove();
	}
	
	$('input').keydown(function (ev) {
		if (ev.which == 13) {
			$(this).closest('form').submit();
		}
	});
	
	$('a#print').click(function (e) {
		window.print();
	});
	
	$('.news.tabs .tab').click(function (e) {
		e.preventDefault();
		
		$('.news.tabs .tab').removeClass('active');
		$('.news.tabs .tabcontent').hide();
		
		var tab = $(this).closest('div');
		tab.addClass('active');
		$('#'+ tab.data('target')).fadeIn();
	});
	$('.news.tabs .tabcontent').hide().first().show();
	
	// Free font resizing
	$('.fontSizeReset').click(function () {
		sIFR.rollback();
		$('html').css('fontSize', 'inherit');
		runsIFR();
		document.cookie = "userFontSize=none;max-age=1";
	});
	$('.fontSizeUp').click(function () {
		sIFR.rollback();
		$('html').css('fontSize',
			Math.min(parseInt($('html').css('fontSize')) + 3, 45));
		runsIFR();
		document.cookie = "userFontSize="+ $('html').css('fontSize');
	});
	$('.fontSizeDown').click(function () {
		sIFR.rollback();
		$('html').css('fontSize',
			Math.max(parseInt($('html').css('fontSize')) - 3, 10));
		runsIFR();
		document.cookie = "userFontSize="+ $('html').css('fontSize');
	});
	var userFontSize = getCookie("userFontSize");
	if (userFontSize) {
		$('html').css('fontSize', userFontSize);
	}
	
	new Ticker('.latestNews');
});

// constructor
var Ticker = function(sel) {
	this.timeout = 4500;
	this.offset = 0;
	this.element = this.parent = $(sel);
	if (this.parent.length <= 0)
		return;
	
	if (this.element.get(0).nodeName != 'UL')
		this.element = this.parent.children('ul');
	
	if (this.element.children().length <= 1) {
		$('.tickNext, .tickPrev').fadeOut();
		return;
	}
	
	var thisObj = this;
	$('.tickNext', this.parent).click(function() { thisObj.next(); return false; });
	$('.tickPrev', this.parent).click(function(){ thisObj.prev(); return false; });
	
	this.element.children('li').hide().first().css({ position: 'absolute', top: '0px' }).fadeIn();
	this.tick();
}

// resets timeout
Ticker.prototype.tick = function () {
	if (this.timeoutId)
		clearTimeout(this.timeoutId);
	var thisObj = this; // fix scope
	this.timeoutId = setTimeout(function () { 	
		thisObj.next.call(thisObj); 
	}, this.timeout);
}

// scroll to next item
Ticker.prototype.next = function () {
	if ($(':animated', this.element).length)
		return; // bail if we're already mid-animation
	
	var children = this.element.children();
	var current = children.eq(Math.abs(this.offset));
	this.offset = (this.offset + 1) % children.length;
	var next = children.eq(Math.abs(this.offset));
	current.css({ position: 'absolute', top: '0px' });
	current.animate({
		opacity: 'hide',
		top: '+=40px'
	}, 'slow');
	next.css({ position: 'absolute', top: '-40px' });
	next.animate({
		opacity: 'show',
		top: '+=40px'
	}, 'slow');
	
	this.tick();
}

// scroll to previous item
Ticker.prototype.prev = function () {
	if ($(':animated', this.element).length)
		return; // bail if we're already mid-animation

	var children = this.element.children();
	var current = children.eq(Math.abs(this.offset));
	this.offset = (this.offset - 1) % children.length;
	if (this.offset < 0) this.offset += children.length;
	var prev = children.eq(Math.abs(this.offset));
	current.css({ position: 'absolute', top: '0px' });
	current.animate({
		opacity: 'hide',
		top: '-=40px'
	}, 'slow');
	prev.css({ position: 'absolute', top: '+40px' });
	prev.animate({
		opacity: 'show',
		top: '-=40px'
	}, 'slow');
	
	this.tick();
}

