var showMore;
(function($){
	var currentStart = 0
	var pagesize = 6;
	var optSize;
	var choices;
	var display = function(el,start,end){
		currentStart = start;
		el.hide().slice(start,end).show();
	}

	//Safari iframe issue:
	//  Safari does not allow an iframe with a domain different from the domain of 
	//	the main page to set cookies unless the user 'actively' navigates it
	//Fix: Post then redirect to trick safari into thinking it has been here before.
	$(function(){
		if (document.cookie.indexOf("c_sf") == -1 && 
		    navigator.userAgent.indexOf("Safari") > -1) {
			
			var redirect_url = '/registration/redirect.html';
			
			$('body').append("<form id='safariFixSession'></form>");
			var f = $('#safariFixSession').attr('method', 'POST')
										  .attr('action', redirect_url)
										  .append("<input id='safariFixSessionInput' type='hidden'>");

			var i = $('#safariFixSessionInput').attr('name', 'redirectUrl')
											   .attr('value', location.href);
			f.submit();
		}
	});

	seymour = function(){
		var skipNext = false;
		
		currentPage = choices.slice(currentStart, currentStart+pagesize);
		
		$.each(currentPage, function(index, value) { 
			if(value.style.display == "none")
				skipNext = true;
		});
		var nextUp = (currentStart+pagesize) >= optSize || skipNext ? 0 : (currentStart+pagesize);
		display(choices,nextUp,nextUp+pagesize);
		
		if(optSize <= pagesize)
			document.getElementById('seemore').style.visibility = "hidden";
	}
	
	$(function(){	
		//non-mobile "remember me" checkbox handler - adapts rpx link url query string
		//init
		$('#ssoLogin .providerlink').each(function() {
			var linkhref = $(this).attr("rel") + "&remember_check_sso=true";
			$(this).attr("href",linkhref);
		});
		//checkbox change handler
		$('input#remember_check_desk').change(function(e) {
			if ($(this).is(":checked")) {
				$('#ssoLogin .providerlink').each(function() {
					var linkhref = $(this).attr("rel") + "&remember_check_sso=true";
					$(this).attr("href",linkhref);
					$('input#remember_check_iso').val("true");
				});
			} else {
				$('#ssoLogin .providerlink').each(function() {
					var linkhref = $(this).attr("rel") + "&remember_check_sso=false";
					$(this).attr("href",linkhref);
					$('input#remember_check_iso').val("false");
				});
			}
		});
		
		//&amp;_spring_security_remember_me=true
	});
	
	$(function(){
		choices = $('#providerChoice .providerOption');
		optSize = choices.length;
		if(optSize > pagesize || (typeof(lastLoginProvider) != 'undefined' && !!lastLoginProvider)){
			if(typeof(lastLoginProvider) == 'undefined' || !lastLoginProvider){
				display(choices,0,pagesize);
			}
			$('#seemore').html('<a href="#" onclick="seymour();return false;">See Additional providers</a>').show();
		}
	});
    

})(jQuery);

