// infrastruct javascript

$(document).ready(function(){

	if($('#footerlogosinner')){
	
		var w = 0;
		$('.footerlogolist').each(function(){
			if($(this).width() > w){ w = $(this).width(); };
		});
		$('#footerlogosinner').width(w+'px');
	
		$('#footerlogosinner').cycle({
			timeout:		5000,  // milliseconds between slide transitions (0 to disable auto advance) 
			speed:			2000,  // speed of the transition (any valid fx speed value) 
			next:			null,  // id of element to use as click trigger for next slide 
			prev:			null,  // id of element to use as click trigger for previous slide 
			before:			null,  // transition callback (scope set to element to be shown) 
			after:			null,  // transition callback (scope set to element that was shown) 
			height:			'79px', // container height 
			sync:			1,     // true if in/out transitions should occur simultaneously 
			fit:			0,     // force slides to fit container 
			pause:			1,     // true to enable "pause on hover" 
			delay:			0,     // additional delay (in ms) for first transition (hint: can be negative) 
			slideExpr:		'ul'   // expression for selecting slides (if something other than all children is required)
		});
	}

	$('.dl_gallery').each(function(){
		$(this).find('a').lightBox();
	});
//	$('.dl_gallery a').lightBox();
	
	/*if($('#duocmsTopbarOuter')){
		$('.tablefactfile').each(function(){
			$(this).after('<p>&nbsp;</p>');
		});
	}*/
	
	var defaultSearchText = $('#searchstring').val();
	$('#searchstring').focus(function(){
		if($(this).val() == defaultSearchText){
			$(this).val('');
			$(this).blur(function(){
				if($(this).val() == ''){
					$(this).val(defaultSearchText);
				}
			});
		}
	});
	
	var defaultLoginText = $('#editor_name').val();
	$('#editor_name').focus(function(){
		if($(this).val() == defaultLoginText){
			$(this).val('');
			$(this).blur(function(){
				if($(this).val() == ''){
					$(this).val(defaultLoginText);
				}
			});
		}
	});
	
	$('#duoPasswordent').focus(function(){
		if($(this).val() == ''){
			$(this).css('background','#fff');
			$(this).blur(function(){
				if($(this).val() == ''){
					$(this).css('background','#fff url(\'/images/site_bits/passwordbg.gif\') left center no-repeat');
				}
			});
		}
	});
	
	if(c=cookieVal('infrastructtextsize')){
		textSize(c);
	}
	
	$('.aaa').live('click',function(){
		var size = $(this).attr('id');
		size = size.replace('aaa','');
		textSize(size);
	});
	
});





function textSize(s){
	var b = $('body');
	var basesize = 0.8;
	var multiplier = 0.2;
	var addition = s*multiplier;
	var size = basesize+addition + 'em';
	b.css('fontSize',size);
	cookieMaker('infrastructtextsize',s);
	$('.aaa').each(function(){ $(this).removeClass('active'); });
	$('#aaa'+s).addClass('active');
}


function cookieMaker(myName,textsize){
	document.cookie=myName+'='+textsize+'; path=/';
}

function cookieVal(cookieName){
	var thisCookie=document.cookie.split("; ");
	if(thisCookie.length>0){
		for (cook=0; cook<thisCookie.length; cook++){
			if (cookieName==thisCookie[cook].split("=")[0]){
				return thisCookie[cook].split("=")[1];
			}
		}
	}
}



function dl_signup(){
	var errors=0;
	//reset highlight
	var inputs=document.getElementById('dl_signupForm').getElementsByTagName('INPUT');
	for(var i=0;i<inputs.length;i++){
		if(inputs[i] && (inputs[i].type=="text" || inputs[i].type=="password")){
			inputs[i].style.border="1px solid #ccc";
		}
	}
	if(document.getElementById('dl_first_name').value.length<2){
		document.getElementById('dl_first_name').style.border="1px solid #ff7800";
		errors++;
	}					
	if(document.getElementById('dl_surname').value.length<2){
		document.getElementById('dl_surname').style.border="1px solid #ff7800";
		errors++;
	}
	if(document.getElementById('dl_phone').value.length<7 || document.getElementById('dl_phone').value.match(/[a-z]+/)){
		document.getElementById('dl_phone').style.border="1px solid #ff7800";
		errors++;
	}	
	if(document.getElementById('dl_email').value.length<7 || !document.getElementById('dl_email').value.match(/[^@]+@.+/)){
		document.getElementById('dl_email').style.border="1px solid #ff7800";
		errors++;
	}
	
	if(document.getElementById('dl_pwd1').value.length<4 || document.getElementById('dl_pwd1').value!=document.getElementById('dl_pwd2').value){
		document.getElementById('dl_pwd1').style.border="1px solid #ff7800";
		document.getElementById('dl_pwd2').style.border="1px solid #ff7800";
		errors++;
	}
	if(errors>0){
		document.getElementById('dl_messageBox').style.display="block";
		document.getElementById('dl_messageBox').innerHTML="Please correct the highlighted form fields above and try again";
	}else{	
		document.getElementById('dl_signupForm').submit();			
	}
	return false;
}

function dl_passwordreset(){
	var errors=0;
	//reset highlight
	var inputs=document.getElementById('dl_passwordResetForm').getElementsByTagName('INPUT');
	for(var i=0;i<inputs.length;i++){
		if(inputs[i] && (inputs[i].type=="text" || inputs[i].type=="password")){
			inputs[i].style.border="1px solid #ccc";
		}
	}	
	if(document.getElementById('dl_email').value.length<7 || !document.getElementById('dl_email').value.match(/[^@]+@.+/)){
		document.getElementById('dl_email').style.border="1px solid #ff7800";
		errors++;
	}
	if(document.getElementById('dl_pwd1').value.length<4 || document.getElementById('dl_pwd1').value!=document.getElementById('dl_pwd2').value){
		document.getElementById('dl_pwd1').style.border="1px solid #ff7800";
		document.getElementById('dl_pwd2').style.border="1px solid #ff7800";
		errors++;
	}
	if(errors>0){
		document.getElementById('dl_messageBox').style.display="block";
		document.getElementById('dl_messageBox').innerHTML="Please correct the highlighted form fields above and try again";
	}else{	
		document.getElementById('dl_passwordResetForm').submit();			
	}
	return false;
}

