/*
 * jQuery Global Functions
 * By Layered Pixels
 * Version 1.1	
 */

var j = jQuery.noConflict();

j(document).ready(function(){

	/* Dropdown Navigation */
	j('#nav li ul').hide();
	j('#nav li').hover(function(){
		j(this).find('ul').show();
	},
	function(){
		j(this).find('ul').hide("fast","easeOutSine");
	})

	/* Contact Form */
	j('#contactform').submit(function() {

	// Disable the submit button
	j('#contactform input[type=submit]')
		.attr('value', 'Sending message…')
		.attr('disabled', 'disabled');

	// AJAX POST request
	j.post(
		j(this).attr('action'),
		{
			name:j('#name').val(),
			email:j('#email').val(),
			message:j('#message').val()
		},
		function(errors) {
			// No errors
			if (errors == null) {
				j('#contactform')
					.hide()
					.html('<h3>Thank you</h3><p>Your message has been sent.</p>')
					.show();
			}

			// Errors
			else {
				// Re-enable the submit button
				j('#contactform input[type=submit]')
					.removeAttr('disabled')
					.attr('value', 'Send your Question');

				// Technical server problem, the email could not be sent
				if (errors.server != null) {
					alert(errors.server);
					return false;
				}

				// Empty the errorbox and reset the error alerts
				j('#contactform .errorbox').html('<ul></ul>').show();
				j('#contactform li').removeClass('alert');

				// Loop over the errors, mark the corresponding input fields,
				// and add the error messages to the errorbox.
				for (field in errors) {
					if (errors[field] != null) {
						j('#' + field).parent('li').addClass('alert');
						j('#contactform .errorbox ul').append('<li>' + errors[field] + '</li>');
					}
				}
			}
		},
		'json'
	);

	// Prevent non-AJAX form submission
	return false;
	});

	/* Fix IE7 z-index issue */
	j(function() {
		var zIndexNumber = 1000;
		j('div').each(function() {
			j(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});

	ScrollBoxe();
	ScrollNews();
	ScrollSpotligh();

});

/*
 * 		Cycle functions (jquery.cycle.js)
 */

// Add 'scrollVert' functionality for scroll boxe
function ScrollBoxe() {

	j.fn.cycle.transitions.scrollVert = function($cont, $slides, opts) {
	    $cont.css('overflow','hidden');
	    opts.before.push(function(curr, next, opts, fwd) {
	        j(this).show();
	        var currH = curr.offsetHeight, nextH = next.offsetHeight;
	        opts.cssBefore = fwd ? { top: -nextH } : { top: nextH };
	        opts.animIn.top = 0;
	        opts.animOut.top = fwd ? currH : -currH;
	        $slides.not(curr).css(opts.cssBefore);
	    });
	    opts.cssFirst = { top: 0 };
	    opts.cssAfter = { display: 'none' }
	};

}


/*
* 		Latest News Scroller
*/

function ScrollNews() {
	j('#scrollNews ul').cycle({ 
	    fx: 'scrollVert',
		speed: 650,
		rev: true,
		timeout: 10000,
		next:   '#scrollNews ol li.next', 
	    prev:   '#scrollNews ol li.previous'
	});
}


/*
* 		Spotlight Scroller
*/

function ScrollSpotligh() {
	j('#scrollSpotlight ul').cycle({ 
	    fx: 'scrollVert',
		speed: 850,
		rev: true,
		timeout: 10000,
		next:   '#scrollSpotlight ol li.next', 
	    prev:   '#scrollSpotlight ol li.previous'
	});
}

