// smple pop-up
function nuWin(url,w,h) {
	myname = 'cully' + Math.floor(Math.random()*111)
  	theWindow = window.open(url, myname, 'status=no, resizable=yes, menubar=no, scrollbars=no ,location=no, width='+w+', height='+h);
	if (theWindow) {
		return false;
		theWindow.focus();
  		} else { 
		return true;
	}
}

//addJs('./js/mooforms.js');

/**
* @desc mooForms.js - custom radio and checkboxes for forms.
* @author Shaun Freeman <shaun@shaunfreeman.co.uk>
* @Copyright Shaun Freeman 2007
* @date Wed Oct 24 13:31:24 BST 2007 
* @license      GNU/GPL, see LICENSE.txt
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
* @version 1.0 - Wed Oct 24 13:31:24 BST 2007
*     - 1st release
*
* @version 1.1 - Sat Jan 19 15:31:36 GMT 2008
*     - added labelPosition
*		- put label on left or right
*
* @version 1.2 - Tue 25 Mar 2008 09:56:15 GMT 
*     - updated to work with mootools 1.2b-dev
*     - fixed ie drag 'n' drop so image clears
*
*/

var mooForms = new Class({

    Implements: [Options],

    options: {
			imageDir: 'media/elements/',
			checkboxImage: {image: 'check.png', width: 12, height: 12},
			radioImage: {image: 'check.png', width: 12, height: 12},
			spacer: 'spacer.gif',
			inputs: ['checkbox', 'radio'],
			labelPosition: 'right'
    },
    
    initialize: function(element, options){
		
		if(options) this.setOptions(options);
		
        this.el = $(element);
        this.elid = element;
		
		this.options.inputs.each(function (input) {
        	this.build(input);
        	
		}.bind(this));
		
		this.divs = this.el.getElements('div');
		        

        this.divs.each(function(item){ 
		
            if (item.hasClass('checkbox') || item.hasClass('radio')) {
				
                item.addEvents({
					
					'mousedown': function(event){
                    	new Event(event).stop();
                    	this.effect(item);
                    	return false;
					}.bind(this),
					
	 				'click': function(event){	 
		 				this.handle(item);
					}.bind(this)
					
				});
				
				// ie fix for default drag 'n' drop on image.
				if (Browser.Engine.trident) {
					
					item.getElement('img').ondragend = function(event){
						//window.event.returnValue=false;
						this.clear(item);
						return false;
					}.bind(this);
					
				}
				
				window.addEvent('mouseup', function(event){
					this.clear(item);
				}.bind(this));
			}
			
		}.bind(this));
	},
 
    build: function(input){
		formElement = this.el.getElements('input[type=' + input + ']');
		
        formElement.each(function (inputElement) {
			
			if (input == 'checkbox') {
				//alert('checkbox: '+this.options.checkboxImage.image);
				this.image = this.options.imageDir + this.options.checkboxImage.image;
				this.imageWidth = this.options.checkboxImage.width;
				this.imageHeight = this.options.checkboxImage.height;
				
			}
			if (input == 'radio') {
				this.image = this.options.imageDir + this.options.radioImage.image;
				this.imageWidth = this.options.radioImage.width;
				this.imageHeight = this.options.radioImage.height;
			}

			this.spacer = new Asset.image(this.options.imageDir + this.options.spacer);
			
			this.spacer.setStyles({
				'width': this.imageWidth,
				'height': this.imageHeight,
				'vertical-align': 'middle',
				'background-image': 'url(' + this.image + ')',
				'background-repeat': 'no-repeat',
				'background-position': '0px 0px'
			});
			
            wrapper = new Element('div', {
                'class': input,
				'styles': {
				 	'cursor': 'default', 
					'display': 'inline'
				}
			});
			
			if (this.options.labelPosition == 'left') {
				label = inputElement.getPrevious();
				wrapper.injectBefore(inputElement).
						adopt(label)
						.adopt(this.spacer)
						.adopt(inputElement); 
			} else {
				label = inputElement.getNext();
				wrapper.injectBefore(inputElement)
						.adopt(this.spacer)
						.adopt(inputElement)
						.adopt(label);
			}
			
/*			label.setStyles({
				'vertical-align': 'middle',
				'display': 'inline'
			});
*/			
			inputElement.setStyle('display', 'none');
			
            if (inputElement.getAttribute('checked')) {
                wrapper.addClass('selected');
				this.spacer.setStyle('background-position', '0px -' + (this.imageHeight * 2) + 'px');
			}
		}.bind(this)); 
	},
	
	getImageHeight: function(item){
		if (item.hasClass('checkbox')) {
			this.imageHeight = this.options.checkboxImage.height;
		} else {
			this.imageHeight = this.options.radioImage.height;
		}
	},
 	
    effect: function(item){
	  	
		this.getImageHeight(item);
		
        if(item.className == 'checkbox' || item.className == 'radio') {
            item.getElement('img').setStyle('background-position', '0px -' + this.imageHeight + 'px');
		} else {
			item.getElement('img').setStyle('background-position', '0px -' + (this.imageHeight * 3) + 'px');
		}
	},
 
    handle: function(item){
    
        selector = item.getElement('input');
		
        if(item.className == 'checkbox') {
            selector.setProperty('checked', 'checked');
            item.addClass('selected');
			item.getElement('img').setStyle('background-position', '0px -' + (this.options.checkboxImage.height * 2) + 'px');
		} else if (item.className == 'checkbox selected') {
            selector.removeProperty('checked');
            item.removeClass('selected');
			item.getElement('img').setStyle('background-position', '0px 0px');
		} else {
            selector.setProperty('checked', 'checked');
            item.addClass('selected');
			item.getElement('img').setStyle('background-position', '0px -' + (this.options.radioImage.height * 2) + 'px');
            itemName = selector.getProperty('name');
            
			this.inputs = this.el.getElements('input[name=' + itemName + ']');
            
            this.inputs.each(function(radio){
                if (radio != selector) {
                    radio.getParent().removeProperty('checked');
                    radio.getParent().removeClass('selected');			
					radio.getParent().getElement('img').setStyle('background-position', '0px 0px');
				}
			});
		}
	},
    
    clear: function(item){
	
	   	this.getImageHeight(item);
		
        if (item.className == 'checkbox' || item.className == 'radio') {
			item.getElement('img').setStyle('background-position', '0px 0px');
		} else if (item.className == 'checkbox selected' || item.className == 'radio selected') {
			item.getElement('img').setStyle('background-position', '0px -' + (this.imageHeight * 2) + 'px');
		}
	}
});



// form validation
function validateFormOnSubmit(theForm,theFields) {

	
	/*if(!theForm.mail.value) {
	   alert("Bitte geben Sie ihre Email Adresse ein.\n\nPlease enter your Email address.");
	   theForm.mail.focus();
	   return false;
	}
	*/

	var reason = "";

	if(theFields==1) {
		reason += validateEmpty(theForm.name);
		reason += validateEmpty(theForm.email);
	}

	if(theFields==2) {
		reason += validateEmpty(theForm.name);
		reason += validateEmpty(theForm.adresse);
		reason += validateEmpty(theForm.email);
		reason += validateEmpty(theForm.fon);
	}
	
	if(theFields==3) {
		reason += validateEmpty(theForm.media);
		reason += validateEmpty(theForm.name);
		reason += validateEmpty(theForm.email);
	}

      
  if (reason != "") {
    alert("Rouge est obligatoire.\nRot ist obligatorisch.\nRed is mandatory.\n\n" + reason);
    return false;
  	} 

  	return true;
	
}	

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#8e2320'; 
        error = " "
    } else {
        fld.style.background = 'none';
    }
    return error;   
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = '#8e2320';
        error = " ";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#8e2320';
        error = "Email is not valid.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Email is not valid.\n";
    } else {
        fld.style.background = 'none';
    }
    return error;
}



// mootools starters
window.addEvent('domready', function() {

  //navigation Accordion instance
	var myAccordion = new multipleOpenAccordion($('nav'), 'div.accToggler', 'div.accContent', {
		alwaysHide: 1,
		openActive: 1,
		show: active,
		onActive: function(toggler, element){
		  //toggler.setStyle('background-position', '-140px 0');
		},
		onBackground: function(toggler, element){
		  //toggler.setStyle('background-position', '0 0');
		}
    
	});
  




	//programm Accordion instance
	var artAccordion = new Accordion($('accordion'), 'div.toggler', 'div.element', {
		opacity: true,
		start:'all-close',
 		alwaysHide: 1,
 		
 		
		onActive: function(toggler, element){
			var children = toggler.getChildren();
			if(children[1]) {
				child=children[1];
				child.setStyle('display', 'none');
			};
 				children[0].setStyle('font-weight', 'bold');
 				toggler.setStyle('color', '#ee5039'); //font-size: 14px;
 				element.setStyle('padding-bottom', '15px');
		},

		onBackground: function(toggler, element){
			var children = toggler.getChildren();
			if(children[1]) {
				child=children[1];
				child.setStyle('display', 'block');
			};
 				toggler.setStyle('color', '#000'); //font-size: 14px;
 				element.setStyle('padding-bottom', '0');
 		},

		onComplete: function(){
			//$('myThumb').tween('height', 10);
			th = $('myThumb').offsetHeight;
			t = $('myTrack').offsetHeight;
			c = $('content').scrollHeight;
			ct = $('content').scrollTop;
			m = $('main').offsetHeight;
			//alert(' scrollHeight '+c);
			nth = t * (m / c);
//			alert(' myThumb '+$('myThumb'));
			//alert(' scrollTop '+ct);
			$('myThumb').morph({height: t * (m / c)});
			scr.vUpdateThumbFromAccordion(nth,ct);
			// ausserdem wäre ein autoscroll noch schön um den offenen content zu plazieren bzw. bei schliessen den leeraum weg zu fahren ... aber wer zahlt das ?????????
		}

	

	});



	//artist Accordion instance
	var artAccordion = new Accordion($('accordion'), 'div.atoggler', 'div.aelement', {
		opacity: true,
		start:'all-close',
 		alwaysHide: 1,
 		
 		
		onActive: function(toggler, element){
			var children = toggler.getChildren();
			if(children[3]) {
				child=children[3];
				child.setStyle('display', 'none');
			};
			var next = toggler.getNext();
			var children = next.getChildren();
			child=children[1];
			if(child.getChildren()!=false) { 
				toggler.setStyle('color', '#c76330'); //font-size: 14px;
				toggler.setStyle('margin-top', '0px'); //font-size: 14px;
				toggler.setStyle('margin-bottom', '6px'); //font-size: 14px;
 			}
		},

		onBackground: function(toggler, element){
			var children = toggler.getChildren();
			if(children[3]) {
				child=children[3];
				child.setStyle('display', 'block');
			};
 			toggler.setStyle('font-weight', 'normal');
 			toggler.setStyle('margin-top', '0px'); //font-size: 14px;
 		},

		onComplete: function(){
			//$('myThumb').tween('height', 10);
			th = $('myThumb').offsetHeight;
			t = $('myTrack').offsetHeight;
			c = $('content').scrollHeight;
			ct = $('content').scrollTop;
			m = $('main').offsetHeight;
			nth = t * (m / c);
			//alert(' scrollHeight '+c);
			$('myThumb').morph({height: t * (m / c)});
//			$('myThumb').setStyle('height', nth);
			scr.vUpdateThumbFromAccordion(nth,ct);
			// ausserdem wäre ein autoscroll noch schön um den offenen content zu plazieren bzw. bei schliessen den leeraum weg zu fahren ... aber wer zahlt das ?????????
		}

	});



	// artist accordion	rollover
	artAccordion.togglers.each(function(toggler){
			var next = toggler.getNext();
			//var props = next.getProperty('class');
			var children = next.getChildren();
			child=children[1];
			if(child.getChildren()==false) { 			
				imgdiv=children[0];
				imgdiv.setStyle('display', 'none');
				//var props = imgdiv.getProperty('class');
	//		alert('props:'+props);
			}			
			//var props=child.getProperty('class');
			//alert('props: '+props);
			if(child.getChildren()!=false) { 			
				toggler.addEvent('mouseover',function(){
					//this.fireEvent('click');
					toggler.setStyle('color', '#c76330');
				});
			}	
				toggler.addEvent('mouseleave',function(){
					//this.fireEvent('click');
					toggler.setStyle('color', '#8e2320');
	
				});
			
		});
	
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
	var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	if (ieversion>=9) {

	} else {
		var scr = new ScrollBar('main', 'content', {
			'arrows': true,
			'hScroll': false
		});
	 }
} else {
	var scr = new ScrollBar('main', 'content', {
		'arrows': true,
		'hScroll': false
	});
}
	
	//custom checkbox and radio images

	if($('form')) {

	mooForm = new mooForms('form', {
		
		imageDir: 'media/elements/',
		checkboxImage: {image: 'check.gif', width: 12, height: 17},
		radioImage: {image: 'check.gif', width: 12, height: 17}
		

	});
	};

window.addEvent('load', function() {
	th = $('myThumb').offsetHeight;
	t = $('myTrack').offsetHeight;
	c = $('content').scrollHeight;
	ct = $('content').scrollTop;
	m = $('main').offsetHeight;
	nth = t * (m / c);
//	alert(' myThumb '+$('myThumb'));
	$('myThumb').morph({height: t * (m / c)});
	scr.vUpdateThumbFromAccordion(nth);			
})

	
	

});


