$(window).load(function () {
	$('#loading').fadeOut('slow');
});

jQuery(function( $ ){
	// This one is important, many browsers don't reset scroll on refreshes
	// Reset all scrollable panes to (0,0)
	$('div.pane').scrollTo( 0 );
	// Reset the screen to (0,0)
	$.scrollTo( 0 );
	
	var regions = $('.region');
	var active_region = 0, prev, next;
		
	function setup_region ()
	{
		if ( active_region == 0 )
		{
			$('#toggle .previous a').fadeOut('slow');
			prev = regions.size() - 1;	
		}
		else
		{
			$('#toggle .previous a').fadeIn('slow');
			prev = active_region - 1;	
		}
		
		if ( active_region == ( regions.size() - 1 ) )
		{
			$('#toggle .next a').fadeOut('slow');
			next = 0;
		}
		else
		{
			$('#toggle .next a').fadeIn('slow');
			next = active_region+1;	
		}
				
		$('#toggle .previous a').attr('href', '#'+ $(regions).eq(prev).attr('id')).click(function(){ active_region = prev; });
		$('#toggle .next a').attr('href', '#'+ $(regions).eq(next).attr('id')).click(function(){ active_region = next; });
	}
	setup_region();
	// TOGGLE, shows how to scroll the whole window  
	$('#toggle a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen  

	
	$.scrollTo( this.hash, 1500, {center:true});
	setup_region();
	//$(this.hash).find('span.message').text( this.title );  

	return false; 

	});
});

function makeScrollable(wrapper, scrollable){
	// Get jQuery elements
	var wrapper = $(wrapper), scrollable = $(scrollable);

	// Hide images until they are not loaded
	scrollable.hide();
	var loading = $('<div class="loading">Loading...</div>').appendTo(wrapper);

	// Set function that will check if all images are loaded
	var interval = setInterval(function(){
		var images = scrollable.find('img');
		var completed = 0;

		// Counts number of images that are succesfully loaded
		images.each(function(){
			if (this.complete) completed++;
		});

		if (completed == images.length){
			clearInterval(interval);
			// Timeout added to fix problem with Chrome
			setTimeout(function(){

				loading.hide();
				// Remove scrollbars
				wrapper.css({overflow: 'hidden'});

				scrollable.slideDown('slow', function(){
					enable();
				});
			}, 1000);
		}
	}, 100);

	function enable(){
		var inactiveMargin = 99;
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;

		var lastTarget;
		wrapper.mousemove(function(e){
		lastTarget = e.target;

		var wrapperOffset = wrapper.offset();

		var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
		if (top < 0){
			top = 0;
		}
		wrapper.scrollTop(top);
	});

	}

}

$(function(){
	makeScrollable("div.sc_menu_wrapper", "div.sc_menu");
});
/*]]>*/


// Focus hack for IE forms
$(document).ready(function(){
    if (jQuery.browser.msie === true) {
        jQuery('input')
                .bind('focus', function() {
                        $(this).addClass('ieFocusHack');
                }).bind('blur', function() {
                        $(this).removeClass('ieFocusHack');
                });
        jQuery('textarea')
                .bind('focus', function() {
                        $(this).addClass('ieFocusHack');
                }).bind('blur', function() {
                        $(this).removeClass('ieFocusHack');
                });
    }
});

// Form Hacks
$(document).ready(function(){
// check for what is/isn't already checked and match it on the fake ones
$("input:checkbox").each( function() {
    (this.checked) ? $("#fake"+this.id).addClass('fakechecked') : $("#fake"+this.id).removeClass('fakechecked');
});
// function to 'check' the fake ones and their matching checkboxes
$(".fakecheck").click(function(){
    ($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
    $(this.hash).trigger("click");
    return false;
});

// check for what is/isn't already checked and match it on the fake ones
$("input:radio").each( function() {
    (this.checked) ? $("#fake"+this.id).addClass('fakeradioed') : $("#fake"+this.id).removeClass('fakeradioed');
});
// function to 'check' the fake ones and their matching checkboxes
$(".fakeradio").click(function(){
    $('.fakeradioed').removeClass('fakeradioed');
    ($(this).hasClass('fakeradioed')) ? $(this).removeClass('fakeradioed') : $(this).addClass('fakeradioed');
    $(this.hash).trigger("click");
    return false;
});

});

// function to show form message
$(document).ready(function() {
	$('#submitform').ajaxForm ({
		target: '#error',
		success: function() {
		$('#error').fadein(3000);		
		}
	});
});
