
function strstr( haystack, needle, bool ) {
    var pos = 0;
    haystack += '';
    pos = haystack.indexOf( needle );
    if( pos == -1 ){
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

var OpenLinks = new Class({
	initialize: function(file_extensions,override_targets,target,no_class) {
		$$('a').each(function(el) {
			//check each href for case-insensitive file extensions
			var str = el.get('href');
			if (str != null) {
				var ext = str.substring(str.lastIndexOf('.') + 1,str.length);
				if(strstr(str, 'http') || file_extensions.contains(ext.toLowerCase()) && ((override_targets || !el.get('target')) && !el.hasClass(no_class + '')))
				{
					el.setProperty('target',target ? target : '_blank');
				}
			}
		});
	}
});


window.addEvent('domready', function() {
	
	var olinks = new OpenLinks(['doc','pdf','xls','jpg','gif','png', 'pps', 'ppt', 'zip', 'swf', 'gif', 'doc'],1,'_blank','no-target');
	
	
	// menu
	var myacc = new Accordion($('menu_left'), 'div.toggler', 'div.submenu', {
		opacity: false,
	});
	
		
	if ($chk($('captcha_img'))) {
		$('reload_captcha').addEvent('click', function(e){
			reload_captcha();
			return false;
		});
	}
	
	//store titles and text  
	$$('a.tips').each(function(element,index) {  
		var coord = element.getCoordinates();
		var content = element.get('title').split(' :: ');  
		element.store('tip:title', content[0]);  
		element.store('tip:text', content[1]);  
	});  
	
	//create the tooltips
	var tipz = new Tips('.tips',{
		fixed: true,
		hideDelay: 20,
		showDelay: 20,
		offsets: {
			'x': 100,
			'y': 1
		}
	});
	
	tipz.addEvents({
		'show': function(tip) {
			tip.fade('in');
		},
		'hide': function(tip) {
			tip.fade('out');
		}
	});

	
	if ($chk($$('.sweep'))) {
		$$('.sweep').each(function(input){
			input.addEvent('click', function(e){
				e.stop();
				input.set('value', '');
			});
		});
	}
	
	enable_upop();
	
	if ($chk($('slide_pop'))) {
		var SPop = new Fx.Slide('slide_pop');
		SPop.hide();
		if ($chk($$('.s_pop'))) {
			$$('.s_pop').addEvent('click', function(e){
				e.stop();
				SPop.toggle();
			});
		}
	}
	
});

var enable_upop = function() {
	if ($chk($('unique_pop'))) {
		var UPop = new Fx.Slide('unique_pop');
		UPop.hide();
		if ($chk($$('.u_pop'))) {
			$$('.u_pop').addEvent('click', function(e){
				e.stop();
				var req = new Request.HTML({
					method: 'get', 
					url: this.get('href'),
					update: $('unique_pop'),
					onRequest: function() {
						UPop.show().hide();
					},
					onComplete: function() {
						UPop.toggle();
						window.scrollTo(0, 0);
						enable_ipop();
					}
				}).send();
			});
		}
	}
}

var submitform = function(id_form, id_container) {
	var APop = new Fx.Slide(id_container);
	var req = new Request.HTML({
		method: 'post',
		url: $(id_form).get('action'),
		data: $(id_form),
		update: $(id_container),
		//onRequest: function() {tinyMCE.triggerSave(true,true);},
		onComplete: function() {
			APop.hide().show();
			if (id_container == 'main') enable_upop()
		}
	}).send();
}

var close_pop = function(id_pop) {
	var Pop = new Fx.Slide(id_pop);
	Pop.toggle();
}

/* contacts */
var c = 0
var reload_captcha = function() {
	c = c + 1;
	var src = $('reload_captcha').get('href');
	$('captcha_img').dispose();
	
	var newcha = new Element('img', {
		'id': 'captcha_img',
		'src': src + '/' + c,
		'alt': 'captcha'
    });
	newcha.inject('cha', 'top');
}

/* shop */
var checkq = function(msg){
	var tot = $('boxes').get('value') + $('packages').get('value') + $('europallet').get('value');
	if (tot == 0) alert(msg);
	else {
		f = document.getElementById('shopform');
		f.submit();
	}
}

