jQuery(function(){
jQuery('ul.sf-menu').superfish();
});

$(window).load(function() {
	$('#slider').nivoSlider({
		effect:'fade', 
		animSpeed:500, 
		pauseTime:4000,
		startSlide:0, 
		directionNav:true, 
		directionNavHide:true, 
		controlNav:false, 
		controlNavThumbs:false, 
      	controlNavThumbsFromRel:false, 
		controlNavThumbsSearch: '.jpg', 
		controlNavThumbsReplace: '_thumb.jpg', 
		keyboardNav:true, 
		pauseOnHover:false, 
		manualAdvance:false, 
		captionOpacity:0.8, 
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} 
	});
});

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}

function PopupCenter(pageURL, title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

jQuery(document).ready(function($) {
	$('input[type=submit]').click(function(){
		$.blockUI({
			message: '<div class="blockUI"><img src="../images/loading.gif" alt="">Please wait..</div>',
			applyPlatformOpacityRules: false,
			focusInput: false
		});

		$.ajax({
			type: 'POST',
			url: '../dbs/api/process/?e=' + _e,
			data: $('form input, form select, form textarea').serialize() + '&dbauth=true',
			success: function(data){
				$.unblockUI();
				resp = data.split(':==:');

				if(resp[0] == 'error')
				{
					alert(resp[2]);
					$('*[name=' + resp[1] + ']:first').focus();
				}
				else if(resp[0] == 'done')
				{
					if(resp[2] != '')
						alert(resp[2]);

					location.href = resp[1];
				}
				else
					alert('Unknown error occured.' + data);
			}
		});

		return false;
	});

	// Pre-load images
	$('<img>').appendTo('body').hide().attr('src','../images/loading.gif');

	// masks
	$('input[rel=date]').setMask({
		mask: '99/99/9999',
		autoTab: false
	});

	// populate the fields from session/id
	$.ajax({
		type: 'POST',
		url: '../dbs/api/getrunner/?e=' + _e,
		data: '../dbauth=true',
		dataType: 'json',
		success: function(data){
			$.each(data, function(key, value) {
				if($('input[name='+ key +']').size() >= 1 && $('input[name='+ key +']:checkbox, input[name='+ key +']:radio').size() <= 0)
					$('input[name='+ key +']').val(value);
				else if($('input[name='+ key +']').size() >= 1)
					$('input[name='+ key +'][value='+ value +']').attr('checked', true);

				if($('select[name='+ key +']').size() >= 1)
					$('select[name='+ key +']').find('option[value='+ value +']').attr('selected', true);
			});
		}
	});
});
