window.addEvent('domready', function(){
	if($chk($('LOADED_FIRST')) && $('LOADED_FIRST').value == 0){
		milkbox = new Milkbox({maxHeight: 0, initialWidth:350, initialHeight: 250});
		milkbox.showGallery({ gallery:'gall1' })
	}
	

	var heighest = 0;	
	$$('.trippleCol').each(function(el,i){
		heighest = (el.getSize().y > heighest ? el.getSize().y : heighest);
	});
	if(heighest>0){
		$$('.trippleCol').each(function(el,i){	
			el.setStyle('height', heighest+'px');
		});
	}
	
	if($chk($('nav'))){
		var lis = $('nav').getChildren();
		lis.each(function(el,i){
			if(el.hasClass('active')){
				removeBackgroundOnPreviousChild(el);
				initLowerNav(el);
			}
		});
	}
	
	$$('a.make_bluebutton').each(function(el,i){
		make_bluebutton(el);
	});
	
	
	/* generic code to open all links with rel=_blank in new window, to pass validation in xhtml strict. */
	$$('a').each(function(link,i){
		if(link.get('rel')=='_blank') link.setProperty('target','_blank');
	});	
	/* end link code */	
	
	/* Standard function to give mCCWrapper the remaining window height if avoidElements is defined */
	autosize_mCCWrapper();

	$$('.flashmovie .picture').each(function(element, index){
		var img = element.getPrevious('img');
		if(img != null){
			var flashvars = {}
			flashvars.debugMode = false;
			flashvars.posterFrame = '/ai_files/' + img.get('src');
			flashvars.file = '/ai_files/' + element.get('rel');
			flashvars.width = 512;
			flashvars.height = 288;
			flashvars.enableFullscreen = true;
			var params = {}
			params.allowfullscreen = true;
			params.wmode = 'window';

			swfobject.embedSWF('/_swf/AIVideoPlayer.swf', element.id, 512, 288, '9.0.0', '/swf/expressInstall.swf', flashvars, params);
		}
	});
	
/*
	if($chk($('flashC'))){
			var flashvars = {}
			flashvars.debugMode = false;
			flashvars.width = 851;
			flashvars.height = 328;
			flashvars.t = "123";
			flashvars.dataUrl = "/_xml/data.php";
			flashvars.assetsUrl = "/_swf/JotronLibrary.swf";
						
			var params = {}
			params.allowfullscreen = true;
			params.wmode = 'window';

			swfobject.embedSWF('/_swf/JotronFront.swf', 'flashC', 851, 328, '9.0.0', '/swf/expressInstall.swf', flashvars, params);		
	}
*/
	
	if($chk($('contactform'))){
		$('sendrequest').addEvent('click', function(e){
			if(validatemail($('EMAIL').value) === false || $('NAME').value == ''){
				alert('One or more required fields are missing');
				e.stop();
			}
			
		});		
	}
	
	if($chk($('ditributorform'))){
		$('region')[0].setProperty('selected', 'selected');
		$('country')[0].setProperty('selected', 'selected');
	}
	
	if($chk($('DEPARTMENT'))){

		$('DEPARTMENT').getSelected().each(function(x, y){
//			console.log(x.value + " - " + y);			
			if(x.value > 0){
				doDepartment();
			}
		});


		$('DEPARTMENT').addEvent('change', function(e){
			doDepartment();

		});
	}
});

window.addEvent('resize', function(){
	
	/* Standard function to give mCCWrapper the remaining window height if avoidElements is defined */
	autosize_mCCWrapper();
	
});

function doDepartment(){
			var req = new Request({
				url : '/_ajax/getFields.php',
				data : {
					'id' : $('DEPARTMENT').value
				},
				onComplete : function(response){
//					alert(response);
					$('deparmentcontentdiv').innerHTML = response;
				}
			}).send();

}

/* function to rewrite DOM bluebuttons */
function make_bluebutton(link){

	var c = new Element('a',{
		'class': 'bluebutton',
		'href': link.get('href'),
		'alt': link.get('alt'),
		'title': link.get('title')
	});

	if(link.hasClass('_blank')) c.set('rel','_blank');
	if(link.hasClass('rMarg15')) c.addClass('rMarg15');

	var s = new Element('span');

	if(link.get('rel')!=null){
		var img = new Element('img',{
			'src': link.get('rel'),
			'alt': ''
		});
		img.injectInside(s);		
	}
	
	s.appendText(link.get('text'));
	s.injectInside(c);
	c.replaces(link);
}

/* function to validate email */
function validatemail(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false;
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false;
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false;
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false;
	 }
	
	if (str.indexOf(" ")!=-1){
		return false;
	}

	return true;			
}

