//$.ui.dialog.defaults.bgiframe = true;

function OnLoad() {
	$('#maintable').css('height',$(document).height());
	$('.sidecolumn').css('width',(($(document).width()-259)/2)+'px');

	var name = $("#name"),
			email = $("#email"),
			message = $("#message"),
			allFields = $([]).add(name).add(email).add(message),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("pulsate",{queue:false},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#contact-form-div").dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			resizable: false,
			show: 'fold',
			hide: 'fold',
			buttons: {
				'Send Message': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(name,"Name",1,40);
					bValid = bValid && checkLength(email,"Email",6,80);
					bValid = bValid && checkLength(message,"Message",1,4000);

					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. Contact"+"@"+"xpadx.com");
					
					if (bValid) {
						if($('.ui-dialog-buttonpane button:first').html()=='Message was sent'){
							$('.ui-dialog-buttonpane button:first').html('Send Message');
							$(this).dialog('enable');
							$(this).dialog('close');
							return;
						}
						
						$.ajax({ 
							type: "POST", 
							url: "/", 
							data: "ajax=1&action=contact&name="+name.val()+"&email="+email.val()+"&message="+message.val().replace(/=/g,'%3D').replace(/&/g,'%26'), 
							success: function(msg){ 
								$('.ui-dialog-buttonpane button:first').html('Message was sent')
							} 
						});
						$(this).dialog('disable');
						$('.ui-dialog-buttonpane button:first').html('Please Wait');
					}
				},
				Cancel: function() {
					$(this).dialog('close');
					$(this).dialog('enable');
					$('.ui-dialog-buttonpane button:first').html('Send Message');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		
		$('#contact-form').click(function() {
			$('#contact-form-div').dialog('open');
			$('#validateTips').html('All form fields are required.');
		})
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});
	
	$('#About-Text').css('display','');
	$('#Clients-List').css('display','');
	
	About_Div_Height=$('#About-Text').height()-134;
	Clients_Div_Height=$('#Clients-List').height()-($('#maintable').css('height').replace('px','')-216);
	Clients_Scroll=true;
	if(Clients_Div_Height<0)Clients_Scroll=false;

	$("#About-Scroll").slider({
		orientation: "vertical",
		range: "min",
		min: 0,
		max: About_Div_Height,
		value: About_Div_Height,
		animate: true,
		slide: function(event, ui) {
			$('#About-Text').animate({scrollTop: About_Div_Height-ui.value }, { queue:false, duration:300 });
		}
	});
	
	if(Clients_Scroll){
		$("#Clients-Scroll").css('height',($('#maintable').css('height').replace('px','')-226)+'px');
		$("#Clients-Scroll").slider({
			orientation: "vertical",
			range: "min",
			min: 0,
			max: Clients_Div_Height,
			value: Clients_Div_Height,
			animate: true,
			slide: function(event, ui) {
				$('#Clients-List').animate({scrollTop: Clients_Div_Height-ui.value }, { queue:false, duration:300 });
			}
		});
	} else {
		Clients_Div_Height=($('#maintable').css('height').replace('px','')-216);
		$("#Clients-Scroll").slider( 'destroy' );
	}

	$('#About-Text').css('height','134px');
	$('#Clients-List').css('height',($('#maintable').css('height').replace('px','')-216)+'px');

	$('#About-Text').mousewheel(function(event, delta) {
		delta=$("#About-Scroll").slider( 'value' )+delta*About_Div_Height/8;
		if(delta<0)delta=0;
		if(delta>About_Div_Height)delta=About_Div_Height;
		$("#About-Scroll").slider( 'value' , delta);
		$('#About-Text').animate({scrollTop: About_Div_Height-delta }, { queue:false, duration:300 });
		return false;
	});
	if(Clients_Scroll) $('#Clients-List').mousewheel(function(event, delta) {
		delta=$("#Clients-Scroll").slider( 'value' )+delta*Clients_Div_Height/8;
		if(delta<0)delta=0;
		if(delta>Clients_Div_Height)delta=Clients_Div_Height;
		$("#Clients-Scroll").slider( 'value' , delta);
		$('#Clients-List').animate({scrollTop: Clients_Div_Height-delta }, { queue:false, duration:300 });
		return false;
	});
	
	$('#Clients-List div.Clients-Icon').click(function() {
			$('#'+$(this).attr('id')+'details').dialog({
				bgiframe: true,
				modal: true,
				resizable: false,
				width: 550,
				show: 'fold',
				buttons: {Close: function() {$(this).dialog('destroy');}},
				close: function() {
					$(this).dialog('destroy');
				}
			});
		})
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});
	
	var tempid='';
	$('#news_older').click(function(){
		if ($('#news_older span.my-ui-class1').attr('id')) $('#news_td div.selected-news').fadeOut('slow',function(){
			tempid=$(this).attr('id');
			$(this).attr('class','hidden-news');
			$(this).css('display','none');
			$('#'+tempid+' + div.hidden-news:first').fadeIn('slow');
			if(!$('#'+tempid+' + div.hidden-news:first + div.hidden-news:first').attr('id'))$('#news_older span').removeClass('my-ui-class1');
			$('#'+tempid+' + div.hidden-news:first').attr('class','selected-news');
			
			$('#news_newer span').addClass('my-ui-class1');
		});
		return false;
	});
	$('#news_newer').click(function(){
		if ($('#news_newer span.my-ui-class1').attr('id')) $('#news_td div.selected-news').fadeOut('slow',function(){
			tempid=$(this).attr('id');
			$(this).attr('class','hidden-news');
			$(this).css('display','none');
			$('#'+(tempid-1)).fadeIn('slow');
			if(!$('#'+(tempid-2)).attr('id'))$('#news_newer span').removeClass('my-ui-class1');
			$('#'+(tempid-1)).attr('class','selected-news');
			
			$('#news_older span').addClass('my-ui-class1');
		});
		return false;
	});
}
