
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');
	
	if ($chk($('captcha_img'))) {
		$('reload_captcha').addEvent('click', function(e){
			reload_captcha();
			return false;
		});
	}
	
	ajaxed('carter', 'cart');
	
	enable_pop('unique_pop', 'u_pop');
	
	milkbox.setAutoPlay([
			{gallery:'zoom', autoSize:true, autoplay:true, delay:5}
	]);
	
	if ($chk($('fiscal'))) {
		var df = new Fx.Slide('fiscal');
		df.toggle();
		$('fiscal_doc').addEvent('click', function(){
			df.toggle();
		});
	}

});

var ajaxed = function(which, to_update) {
	if ($chk($$('.'+which))) {
		$$('.'+which).addEvent('click', function(e){
			e.stop();
			var req = new Request.HTML({
				method: 'get', 
				url: this.get('href'),
				update: $(to_update)
			}).send();
		});
	}
}


var enable_pop = function(div, link) {
	if ($chk($(div))) {
		var UPop = new Fx.Slide(div);
		UPop.hide();
		if ($chk($$('.'+link))) {
			$$('.'+link).addEvent('click', function(e){
				e.stop();
				var req = new Request.HTML({
					method: 'get', 
					url: this.get('href'),
					update: $(div),
					onRequest: function() {
						UPop.show().hide();
					},
					onComplete: function() {
						UPop.toggle();
						window.scrollTo(0, 0);
						pickerize();
						sweepize();
					}
				}).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, link) {
	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_pop(id_container, link)
		}
	}).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 submission = function(id_form) {
	f = document.getElementById(id_form);
	f.submit();
}

var close_pop = function(id_pop) {
	var Pop = new Fx.Slide(id_pop);
	Pop.toggle();
}

var open_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');
}


