$(document).ready(function(){
	/* slimbox */
	jQuery(function($) {
		$("a[rel^='lightbox']").slimbox({counterText:img_full_txt}, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
	});
});

/*************************/
$(document).ready(function(){
	var forms = $('form').get();
	for (var i in forms) {
		var form = forms[i];
		$("input#applicationform", form).bind('click', function(e){
			submitForm(this);
		});
	}
 
/*	$("input#catalogue_s_b").bind("click", function(e) {
		var frm = document.catalogForm;
		var q = frm.q.value;
		var condition = frm.condition.value;
		var catal_lang = frm.catal_lang.value;
		if ((q != '') && (q != '') && (q != '')) {
			$('#searchResults').html('');
				$('#searchResults').load($(frm).attr('action'), {'q': q, 'condition': condition, 'catal_lang' : catal_lang}, function() {});
		}
	});*/

        $('a').each(function() {
                href = $(this).attr('href');
                
                if (href.match(/^javascript/)) {
                        $(this).removeAttr('target');
                }
                else if (!href) {
                        $(this).removeAttr('target').removeAttr('href');
                }
        });
        
        $('div.right_col div.info_news div.list ul li').each(function() {
                if (!$(this).text()) {
                        $(this).remove();
                }
        });

        runRightBunner(4000);

});

function runRightBunner (time) {
        images = new Array();
        size = new Array();
        num = new Array();
        window.setInterval('changeImage()', time);
}

function changeImage() {
        $('div.right_col div.info_news div.list ul li').each(function(i) {
                images[i] = $(this).find('a:hidden');
                size[i] = images[i].size();
                if (!num[i]) {
                        num[i] = 1;
                }
                
                if (size[i]) {
                        var img = $(this).find('a');
                        
                        if (num[i] > size[i])
                                num[i] = 0;
                        img.hide().eq(num[i]).show();
                        num[i]++;
                }
        });
        
}

function resizeImage(where) {
        var row = $(where);
        var img = row.find('img');
        var height_row = row.height();
        
        img.each(function() {
                var height = $(this).height();
                var width = $(this).width();
                var koef = height/height_row;
                var new_height = height/koef;
                var new_width = width/koef;

                $(this).css('max-height', height_row);
                $(this).height(new_height).width(new_width);
        });        
}

function submitForm(a) {
	var form = $(a).parents('form');
		$('input, textarea, select', form).removeClass('error');
	if (form) {
			//console.log($(form).attr('action'));
		//$(a).hide().after('<img class="waiting" src="images/loading.gif" alt="uploading..." />');
		$.postJSON($(form).attr('action'), $(form).serialize(), function(data){
			if (data.result != true) {
				$(a).show().next().remove();
				if (data.inc_fields){
					for (var i in data.inc_fields) {
						$('input[name=' + data.inc_fields[i] + '], select[name=' + data.inc_fields[i] + '], textarea[name=' + data.inc_fields[i] + ']', form).addClass('error');
					}
				}
				$('div#message_not_send').show();
			} else {
				$('div#message_not_send').hide();
				//$(a).show().next().remove();
				$('div#message_send').show();
				document.forms['application'].reset();
			}
		})
	}
	return false;
}

$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};
