/* utils */
$(function() {
	$('.counter a:last-child').css('background', 'none');
	$('.news li:last-child').css('borderBottom', 'none');
	$('.textfield .counter a:last-child ').css('background', 'none');
	
	
	$('#facebook').hover(function(){
				$(this).animate({right:'0px'},{queue:false,duration:500});
			}, function(){
				$(this).animate({right:'-292px'},{queue:false,duration:500});
			});
			

	$('.arrow.prev').css('opacity', '0.90');
	$('.arrow.prev').hover(function() {
	$(this).animate({opacity: '1'},{ queue: false, duration: 300 });
	}, function() {
	$(this).animate({opacity: '0.90'},{ queue: false, duration: 300 });
	});
	$('.arrow.next').css('opacity', '0.90');
	$('.arrow.next').hover(function() {
	$(this).animate({opacity: '1'},{ queue: false, duration: 300 });
	}, function() {
	$(this).animate({opacity: '0.90'},{ queue: false, duration: 300 });
	});


});

//SLIDESHOW
jQuery.fn.slideshow = function() {
	//if this is the current slideshow - ignore
	if ($(this).css('display') == 'block') return;
	//hide any cycle containers
	$('.slideshow').fadeOut('fast');
	//show only the chosen container
	$(this).fadeIn('fast');
	//destroy previous cycle
	$(this).find('.holder').cycle('destroy');
	
	//start the cycle
	$(this).find('.holder').cycle({
		fx: 'fade',
		cleartype: true,
		cleartypeNoBg: true,
		speedIn:  500, 
		speedOut: 500, 
		pause: 1,
		timeout: 7000,
		pager: $(this).find('.counter'),
		next: $(this).find('.holder .slide, .next'),
		prev: $(this).find('.prev')
	});
	
	//activate the corresponding module card
	$('.card[rel="'+$(this).attr('id')+'"]').addClass('active').siblings('.card').removeClass('active');
	
	//save the information to session
	$.ajax(getRoot('/session/'+$(this).attr('id')+'/true'));
}

jQuery.fn.clickToSet = function() {
	$(this).click(function(e) {
		e.preventDefault();
		//save the information to session
		$.ajax({
			url: getRoot('/session/'+$(this).attr('rel')),
			dataType: 'json',
			success: function () { window.location = getRoot(''); }
		});
	});
}

jQuery.fn.clickClose = function() {
	 var args = arguments[0] || {}; // It's your object of arguments
	
	$(this).click(function(e) {
		e.preventDefault();
		//save the information to session
		$.ajax({
			url: getRoot('/session/'+args),
			dataType: 'json',
			success: function () { window.location = getRoot(''); }
		});
	});
}

//CONTACT FORM
jQuery.fn.ajaxify = function() {
	this.submit(function() {
		var error = false;
		var form = jQuery(this);
		form.find(':input').each(function(index, element) {
			var elementObj = jQuery(element);
			if (elementObj.attr('name').length == 0 || elementObj.attr('id').length == 0) return;
			
			var selector = '';
			if (elementObj.attr('type') == 'text') {
				selector = 'div.input.text';
			} else if (elementObj.attr('type') == 'textarea') {
				selector = 'div.input.textarea';
			} else if (elementObj.attr('type') == 'checkbox') {
				selector = 'div.input.checkbox';
			}
			if (!(elementObj.attr('type') == 'checkbox' || elementObj.attr('type') == 'hidden' || elementObj.attr('type') == 'select-multiple')) {
				if ((elementObj.val().length == 0 || elementObj.val() == elementObj.attr('alt')) && elementObj.hasClass('required') == true) {
					elementObj.closest(selector).addClass('error');
					error = true;
				} else {
					elementObj.closest(selector).removeClass('error');

				}
			}

		});
		if (error == true) return;
		
		form.find('button').hide();
		form.find('.loader').show();
		
		jQuery.ajax({
			type: 'POST',
			url: form.attr('action'),
			data: form.serializeArray(),
			success: ajaxifyCallback,
			dataType: 'json',
			context: form
		});
	});
	
	return this.each(function(){});
};

function ajaxifyCallback(data, txt) {
	var form = $(this.context);
	if(data.errors == 'incorrectId') form.parent().addClass('error');
	if (data.errors == null) {
		form.closest('div').html(form.siblings('.form-replacement').html());
		return;
	}
		
	form.find('.loader').hide();
	form.find('button').show();
	
	jQuery.each(data.errors,function(index,value) {
		var inputObj = form.find(":input[name='data[" + index + "]']");
		
		var selector = '';
		if (inputObj.attr('type') == 'text') {
			selector = 'div.input.text';
		} else if (inputObj.attr('type') == 'textarea') {
			selector = 'div.input.textarea';
		}
		
		inputObj.closest(selector).addClass('error');
	});
};
