/**
 * @author ZuLuS
 */
function searchForm() {
	var input = $('#searchForm input');
	input.attr('defaultText', input.attr('value'));

	input.focus(function() {
		if($(this).attr('value')==$(this).attr('defaultText')) {
			$(this).attr('value', '');
		}
	});
	input.blur(function() {
		if($(this).attr('value')=='') {
			$(this).attr('value', $(this).attr('defaultText'));
		}
	});
	
}
$(searchForm);

function topLoader() {
	this.base = '/img/top/';
	this.images = 6;
	this.active	= Math.ceil(Math.random() * (this.images));
	this.animate = function() {
		if(top.active == top.images) {
			top.active = 1;
		} else {
			top.active++;
		}
		$('#top .active').append('<div style="background-image: url(' + top.base + top.active + '.jpg)" class="newImg"></div>');
		var img = $('#top .newImg');
		img.hide().css('top', '0px').slideDown(500, top.clean);	
	}
	this.clean = function() {
		$('#top .oldImg').remove();
		$('#top .newImg').removeClass('newImg').addClass('oldImg');
	}
	this.init = function() {
		$('#top .active').css('background-image', 'url(' + top.base + top.active + '.jpg)');
		var arr = new Array(top.images);
		for(var i = 1; i <= top.images; i++) {
			arr[i-1] = '' + i;
		}
		$.preload(arr, { base: top.base, ext: '.jpg', onFinish: function() { setInterval(top.animate, 3000); } });
	}
}
var top = new topLoader();
$(top.init);

//----------------------------PROJECTS-----------------------------------

function projectsLoader() {
	this.base = '/img/projects/';
	this.images = 7;
	this.active	= 2;
	this.duration = 400;
	this.animate = function() {
		if(projects.active == projects.images - 1) {
			projects.active = 1;
		} else {
			projects.active++;
		}
		$('#projects .wrap').append('<div style="background-image: url(' + projects.base + projects.active + '.jpg)" class="img newImg"></div>');
		var newImg = $('#projects .newImg').animate({ left: '335px'}, projects.duration, "swing");
		var img1 = $('#projects .img2').animate({ left: '37px'}, projects.duration, "swing");
		var img2 = $('#projects .img1').animate({ top: '200px'}, projects.duration, "swing", projects.clean);
		//var img2 = $('#projects .img1').fadeOut(projects.duration, projects.clean);
	}
	this.clean = function() {
		$('#projects .img1').remove();
		var img2 = $('#projects .img2').removeClass('img2').addClass('img1');
		$('#projects .newImg').removeClass('newImg').addClass('img2');
		
		
	}
	this.init = function() {
		var arr = new Array(projects.images);
		for(var i = 1; i <= projects.images; i++) {
			arr[i-1] = '' + i;
		}
		$('#projects .img2').css('background-image', $('#projects .img2').css('background-image'));
		$.preload(arr, { base: projects.base, ext: '.jpg', onFinish: function() { setInterval(projects.animate, 8000); } });
	}
}
var projects = new projectsLoader();
$(projects.init);