$(document).ready(function(){

	$('input[placeholder], textarea[placeholder]').placeholder();
	
	$('span.more_btn a').wrapInner('<span></span>');
	
	$('#footer ul li:not(":first")').prepend('<span>&nbsp;&bull;&nbsp;</span>');
	
	if( $('body.single-post').size() ) {

			$('#main span.date').prependTo($('#main p:first'));

		};
	
	if( $('#featured_slider').size() ) {
		
		$('#featured_slider').slides({
			effect: 'fade'
		})
	
		$('.col a').on('click', function() {
		
			var this_el = $(this);
			var this_parent = this_el.parent();
			var this_el_index = this_parent.index() + 1;

			// this_el.parent().addClass('active');

			$('#featured_slider .pagination li:eq(' + this_el_index + ') a').trigger('click');
			
			if( $('.col_holder .col.active').size() ) {

				var active_col = $('.col_holder .col.active');

				active_col.removeClass('active').css({ 'top': '10px' });
				active_col.animate({ 'top': 0 }, 250);
				this_parent.addClass('active');
				
			} else {
				this_parent.addClass('active');
			}

			return false;
			
		});

		$('.col').on('mouseover', function() {
			
			if( !$(this).is('.active') ) {
				$(this).stop().animate({ 'top': '10px' }, 250)
			};

		}).on('mouseout', function() {
			
			if( !$(this).is('.active') ) {
				$(this).stop().animate({ 'top': '0' }, 250)
			};

		})
		
	}
	
	$("body.single-produse .product_list li .figure a").fancybox({
	    helpers:  {
	        title:  {
	            type : 'inside'
	        }
	    }
	});
	
	$('#show_form').fancybox({
		'type': 'inline',
		'content': $('#for_fancybox .form_holder')
	})
	
});

$(window).load(function(){
	
	$('#f_send').on('click', function() {
	
		var cf = $('#contact_form');
		var fb = $('#form_feedback');

		if( $('html.lt-ie10').size() ) {

		$('input[type="text"], texarea', cf).each(function() {
			
			if( $(this).val() == $(this).attr('placeholder') ) {
				$(this).val('')
			}

		})

		}

		$.ajax({
			type : 'POST',
			url : cf.attr('action'),
			dataType : 'json',
			data: cf.serialize(),
			success : function(data){

				if( data.yesno === true ) {

					fb.html('<ul class="error"></ul>');

					$.each(data.msg, function(key, value) { 
						$('ul', fb).append('<li>' + value + '</li>');
						// alert(key + ': ' + value); 
					});

					if( $('html.lt-ie10').size() ) {

					$('input[type="text"], texarea', cf).each(function() {
						
						if( $(this).val() == "" ) {
							$(this).val($(this).attr('placeholder'))
						}

					})

					}
					
					if( $('.fancybox-wrap').is(':visible') ) {
						$.fancybox.update()
					}

				} else {

					fb.html('<p class="success">' + data.msg + '</p>');
					// cf.hide();
					
					if( $('.fancybox-wrap').is(':visible') ) {
						$.fancybox.update()
					}

				}

			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert('Ne pare rau dar emailul dumnevoastra nu a fost trimis. Va rugam sa incercati din nou mai tarziu.');
				console.log(XMLHttpRequest, textStatus, errorThrown);
			}
		});
		
		return false;
		
	});
	
});
