// Global jquery script
$(document).ready(function() {
	
	/*---------------------------------
		AJAX
	---------------------------------*/
	$.ajaxSetup({
		type: "POST",
		url: "/_konstrukt/ajax.gateway.php",
		dataType: "json",
		global: false
	});
	
	/*---------------------------------
		Banner
	---------------------------------*/
	if ($('#site_banner').attr('bannerscroll') == 1) {
		var scroll_images = $('#site_banner').attr('bannercount');
		var current_image = $('#site_banner').attr('bannercurrent');
		var name = $('#site_banner').attr('name');
		var next = ((Number(current_image) + 1) >= scroll_images) ? 1 : (Number(current_image) + 1);
		function animateHomeFeature() {
			$('#site_banner img:eq(0)').before("<img src=\"/images/banner_" + name + next + ".jpg\" width=\"874\" height=\"220\" alt=\"\" />");
			$('#site_banner img:eq(1)').delay(2000).animate({"opacity": "0"}, 2000, function(){
				$('#site_banner img:eq(1)').remove();
				next = ((Number(next) + 1) >= scroll_images) ? 1 : (Number(next) + 1);
				animateHomeFeature();
			});
		};
		animateHomeFeature();
	}
	
	/*---------------------------------
		jGrowl
	---------------------------------*/
	$.jGrowl.defaults.position = 'center';
	
	/*---------------------------------
		prettyPhoto
	---------------------------------*/
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.35, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_square', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});
	
	/*---------------------------------
		Defuscate emails
	---------------------------------*/
	$('.defuscate').defuscate();
	
	$('.img_hover').hover(
		function () {
			$(this).attr('src', $(this).attr('src').replace(/.gif/, '_on.gif'));
		},
		function () {
			$(this).attr('src', $(this).attr('src').replace(/_on.gif/, '.gif'));
		}
	);
	
	/*---------------------------------
		Contact Form
	---------------------------------*/
	$('#form_contact').submit(function(index, domEle) {
		var thisObj = this;
		$.ajax({
			data: "func=emailContact&"+$(this).serialize(),
			beforeSend: function(result) {
				$('#form_loading').addClass('loading');
				$(thisObj).find('.form_error').removeClass('form_error');
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){	
				$.jGrowl('#form_contact; error: ' + textStatus + ', errorThrown:' + errorThrown, { 
					life: 5000
				});
			},
			success: function(response){	
				var resp_arr = new Array(); eval("resp_arr="+response);
				if (resp_arr.result == 'true') {
					$.jGrowl(resp_arr.msg, { 
						life: 2000,
						close: function(e,m,o) {
						}
					});
				} else if (resp_arr.result == 'msg') {
					$.jGrowl(resp_arr.msg, { 
						life: 5000
					});
				} else if (resp_arr.result == 'false') {
					var arr = resp_arr.data;
					for (var i in arr) {
						$(thisObj).find(':input[name=\''+i+'\']').addClass('form_error');
					}
					var error_msg = errorMsg(arr, resp_arr.msg);
					$.jGrowl(error_msg, { 
							 life: 5000
					});
				} // end if
			},
			complete: function(response){
				$('#form_loading').removeClass('loading');
			}
		});
        return false;
	});	
	
});

/* Email Defuscator */
(function($){
	jQuery.fn.defuscate = function( settings ) {
		settings = jQuery.extend({
			link: true,
			find: /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi,
			replace: '$1@$2'
		}, settings);
		return this.each(function() {
			if ( $(this).is('a[@href]') ) {
				$(this).attr('href', $(this).attr('href').replace(settings.find, settings.replace));
				var is_link = true;
			}
			$(this).html($(this).html().replace(settings.find, (settings.link && !is_link ? '<a href="mailto:' + settings.replace + '">' + settings.replace + '</a>' : settings.replace)));
		});
	};
})(jQuery);