/* Cookie handling from
 * http://techpatterns.com/downloads/javascript_cookies.php
 */
function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );

		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name ) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) {
		return null;
	}
}
function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if (expires){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* Hide the splash */
function hideSplash() {
	$("#splash-container").remove();
	$("#splash-overlay").fadeOut("fast", function() {
		$(this).remove();
		$("html").removeClass('splash');
	});
}

$.fn.highlightsPager = function(delay) {
	delay = delay || 4000;
	initPager = function(el) {
		stopPager(el);
		el.items = $("li", el);
		// current item
		el.currentitem = 0;
		startPager(el);
	};
	startPager = function(el) {
		el.Pagefn = setInterval(function() { doPage(el); }, delay);
	};
	stopPager = function(el) {
		clearInterval(el.Pagefn);
	};
	pausePager = function(el) {
		el.pause = true;
	};
	resumePager = function(el) {
		el.pause = false;
	};
	doPage = function(el) {
		// don't run if paused
		if(el.pause) return;
		// pause until animation has finished
		el.pause = true;
		// move to next item and center
		el.currentitem = ++el.currentitem % (el.items.size());
		$(el.items[el.currentitem]).find('a').trigger('manual');
		$('.back', el).animate({
            width: el.items[el.currentitem].offsetWidth,
            left: el.items[el.currentitem].offsetLeft
        }, 300);
		el.pause = false;
	};
	this.each(
		function() {
			initPager(this);
		}
	)
	// Add ARIA attributes
	.attr("aria-live", "polite") // Make the pager a live region
	.hover(
		function() {
			// pause if hovered over
			pausePager(this);
		},
		function() {
			// resume when not hovered over
			resumePager(this);
		}
	);
	return this;
};


/* Inits */
$(document).ready(function() {
	var selfUrl;
	if (document.getElementById("splash-overlay")) {
		/* Hide splashes for everyone lacking cookie support */
		Set_Cookie('frolundatest','none','','/','','');
		if(Get_Cookie('frolundatest')) {
			Delete_Cookie('frolundatest', '/', '');
			$("html").addClass('splash');
			$('#splash-overlay, #splash-container').click(function(e) {
				if ($(e.target).is('div')) {
					hideSplash();
				}
			});
			$("#splash a").click(function() {
				selfUrl = $(this).attr("href");
				hideSplash();
				if(/^(?!http)/.test(selfUrl)) {
					return false;
				}
			});
		} else {
			$("#splash-container, #splash-overlay").remove();
		}
	}
	/* Initialize the news ticker */
	$("#news ul").newsTicker(4000);
	
	if (typeof $.fn.prettyPhoto !== "undefined") {
		$("a[rel^='prettyPhoto']").prettyPhoto();
	}

	/* Shuffle partner logos */
	$("#m-platina-link ul").newsTicker(4000);
	$("#m-gold-link ul").newsTicker(4000);	

});
