var cache = ['nav-first.gif', 'nav-last.gif'];
var path = 'css/images/';

$( document ).ready( function(){
	start_slides( '#slides img', 4500 );
	
	$( '.field' )
		.focus( function(){ $( this ).addClass('field-focus'); })
		.blur( function(){ $( this ).removeClass('field-focus'); });
		
	$( '.text-image a' ).lightBox();
	
	for (var i = 0; i < cache.length; i++ ) {
		var cache_image = new Image();
		cache_image.src = path + cache[i];
	}
});

function start_slides( item, time, callback ) {
	var all_items = $( item ).length;
	if ( all_items < 1 ) return;
	var zindex = all_items;
	var i=0;
	$( item ).each( function(){
		$( this ).css( { 'z-index': zindex } );
		if ( i > 0 ) $( this ).hide();
		zindex--;
		i++;
		var img = new Image();
		img.src = $( this ).attr('src');
	});
	
	var index = 0;
	var next;
	var time = time || 5000;
	var callback = callback || function(){};
	
	var interval = window.setInterval( function(){
		if ( index == all_items ) index = 0;
		next = index + 1;
		
		if ( next == all_items ) next = 0;
		
		$( item + ":eq(" + index + ")" ).fadeOut(1200);
		$( item + ":eq(" + next + ")" ).fadeIn(1200, function(){
			callback( next );
		});
		
		index++;
	}, time);
}



String.prototype.isValidEmail = function() { return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(this));};
$(function () {
    $('form.should-validate').submit(function () {
        reqs = $('.required', this);
        
        for (var i=0; i < reqs.length; i++) {
            var f = $(reqs[i]);
            var err = '';
            var choose = 'въведете';
            var tag = f.get(0).tagName.toLowerCase();
            
            if ( tag == 'select') choose = 'изберете';
            
            if (f.attr('value')=='') {
                err = "Моля, " + choose + " " + $('label[for=' + f.attr('id') + ']').text().replace(/:?\s*\*\s*$/, '').toLowerCase() + ".";
            }
            if (f.hasClass('valid-mail') && !f.attr('value').isValidEmail()) {
                err = "Моля, коректен e-mail адрес.";
            }
            if (err.length) {
                alert(err);
                f.focus();
                return false;
            }
        }
        
        return true;
    });
});
