
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({
	//initialization
	initialize: function(file_extensions,override_targets,target,no_class) {
		//analyze all anchors
		$$('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');
	
	if ($chk($('captcha_img'))) {
		$('reload_captcha').addEvent('click', function(e){
			reload_captcha();
			return false;
		});
	}
	
	if ($chk($$('.sweep'))) {
		$$('.sweep').each(function(input){
			input.addEvent('click', function(e){
				e.stop();
				input.set('value', '');
			});
		});
	}
	
	if ($chk($('upload'))) {
		new MultiUpload($('upload').name, 0, '[{id}]', true, true );
		var ida = $('id_area');
		var link = $('link').get('value');
	}
	
	enable_upop();
	
});

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);
						}
				}).send();
			});
		}
	}
}

var loadersubmit = function(id_form) {
	new StickyWin.Modal({
		content: '<img src="'+root+'/files/files/ajax-loader.gif" alt="Loading..." />',
		modalOptions: {
			modalStyle:{
			  'background-color':'#fff',
			  'opacity':.6
			}
		}
	});
	f = document.getElementById(id_form);
	f.submit();
}

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();
		}
	}).send();
}

var submitlink = function(id_container, url) {
	var BPop = new Fx.Slide(id_container);
	var req = new Request.HTML({
		method: 'get',
		url: url,
		//data: $(id_form),
		update: $(id_container),
		onComplete: function() {
			BPop.hide().show();
		}
	}).send();
	return false;
}

var close_pop = function(id_pop) {
	var Pop = new Fx.Slide(id_pop);
	Pop.toggle();
}

var submission = function($form) {
	f = document.getElementById($form);
	f.submit();
}

/* 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');
}
