f={
	hovered: function(what) {
		$(what).mouseout(function() {
			if(!$(this).hasClass('hovered')) {
				$(this).addClass('hovered');
				} else {
				$(this).removeClass('hovered');
				}
		});
	},
	focused: function(what) {
		$(what).focus(function() {
			var tmp = $(this).val();
			var tmpp = $(this).attr('alt');
			if(tmp == tmpp || tmp == '')
			$(this).val('');
			$(this).select();
		});
		$(what).blur(function() {
			var tmp = $(this).val();
			var tmpp = $(this).attr('alt');
			if(tmp == tmpp || tmp == '')
			$(this).val(tmpp);
			
		});
	},
	nextPresentation: function(what) {
		$(what).click(function() {
			if($('.portfolio-page #foo li[class=active]').next('li:not(:last-child)').size() == 0) {
				$('.portfolio-page #foo li:nth-child(2)').children('a').trigger('click');
				return false;
			}
			$('.portfolio-page #foo li[class=active]').next().find('a').trigger('click');
			
		});
	},
	sendMail: function() {
		var mail = $('#contact-form input[name=input-email]').val();
		var name = $('#contact-form input[name=input-name]').val();
		var message = $('#contact-form textarea[name=input-message]').val();
		var lang = $('#contact-form input[name=input-lang]').val();
		$('#contact-form').addClass('load').children().css({visibility:'hidden'});;
		if(lang == 'eng') {
			msg = '\nSending failed! \n\nFill the form accordingly and try again.';
			msg2 = '<h5>Mail sent successfuly!</h5><p><br/>We\'ll contact you back as soon as possible.</p>';
		} else if(lang == 'pl') {
			msg = '\nWysyłanie nie powiodło się! \n\nWypełnij prawidłowo formularz i spróbuj ponownie.';
			msg2 = '<h5>Wiadomość została wysłana!</h5><p><br/>Postaramy się z Państwem skontaktować tak szybko jak to możliwe.</p>';
		}
		
		$.ajax({
			type: "POST",
			url: "mail.php",
			data: "input-email="+mail+"&input-name="+name+"&input-message="+message+"&input-lang="+lang,
			success: function(data){
				if(data == '0') alert(msg);
				else $('#contact-form').removeClass('load').html(msg2);
				$('#contact-form').removeClass('load').children().css({visibility:'visible'});
			}
		});
	}
};
$(function(){
	f.hovered('#body li a');
	f.focused('#body input[type=text], #body textarea');
	f.nextPresentation('#img-holder img');
	
	var response;
	var txt = $('#foo-text').html();
	
	$('#img-holder img').hide().fadeIn();
	
	$('.portfolio-page #foo li a:not(:first)').click(function() {
		if($(this).parent().hasClass('active')) return false;
		$('#img-holder img').hide();
		$(this).parents('ul').find('li').removeClass('active');
		$(this).parent().addClass('active');
		var src = $(this).attr('href');
		$('#img-holder img').attr('src',src);
		$.ajax({ 
			type: "get",
			url: src,
			success: function(img){
				$('#img-holder img').fadeIn();
			}
		});
		var tmp2 = $(this).attr('rel');
		if(!tmp2) tmp2 = txt;
		$('#foo-text').html(tmp2);
		return false;
	});
	$('#contact-form').submit(function(){
		f.sendMail();
		return false;
	});
});


function css() {

	/* go on for IE in generall */
	if($.browser.msie){
		
		/* take care IE 6 only */
		if($.browser.version <= 6) {
		
		}
	}
	
	
	$("#banner").slider({slideAnimate:true,slideInterval: 4000});
		
	$('#portfolio li').each(function(){
	
		var 
		item = $(this).children('a');
		itemImg = $(item).children('img').attr('src');
		
		
		str = itemImg.match(/(\/img-)/g); 
		str = new String(str);
		str = itemImg.replace(str,'/bg-');
		
		
		$(item).css('background', 'url('+str+') no-repeat 1px 1px');
		
	
	});
	
	
	
	$('#projects').ready(function(){
	
		$this = $('#projects');
		$l = $this.find('.pr-list');
		$p = $l.find('.p');
		$t = $p.find('.t');
		$v = $p.find('.v');
		$i = $v.find('.i');
		$img = $i.find('img');
		
		
		$i.each(function(){
			$(this).children('img').wrap('<span class="img"></span>');
		});
		
		if($t.size() > 0){
			$t.each(function(t){
				$(this).attr('callback',t);
			});
		}
		else
			$l.addClass('empty-t');


		checkAct = true;
		$p.each(function(t){
			if($(this).is('.act'))
				checkAct = false;
		});

		if(checkAct)
			$p.eq(0).addClass('act');
		
		$l.before('<ul class="pr-nav"></ul>');
		setNew();
		
		
		function setNew(){
			$a = $l.find('.p.act').find('.i');
			$a.hide().eq(0).show();
			
			buildProject();		
		}
		
				
		
		function buildProject(){
		
			$('.pr-nav').empty();
			
			cnt = $a.size();
			for(i=0;i<cnt;i++){
				j = i+1;
				if(i==0){
					act = ' class="a"';
					x = 0;
				}
				else{
					act = '';
					x = '100%';
				}
				y = -i*21+'px';
				
				html = '<li'+act+'><a href="#" callback="'+i+'" style="background-position: '+x+' '+y+';">'+j+'</a></li>';
				
				if($l.is('.news-list'))
					$('.pr-nav').prepend(html);
				else
					$('.pr-nav').append(html);
			}
			
			
			navigateProject();
			
		}
		
		$t.unbind().bind('click',function(){
			
			t = parseInt($(this).attr('callback'));
			
			$p.removeClass('act').eq(t).addClass('act');
			setNew();
		});			
		
		
		$img.unbind().bind('click',function(){
			
			$item = $(this).parent().parent();
			$prevLink = $('.pr-nav li.a a');
			
			if($item.is(':last-child')){
				$item.hide().siblings().eq(0).show();
				$link = $prevLink.parent().siblings().eq(0).children();
			}
			else{
				$item.hide().next('li').show();
				$link = $prevLink.parent().next().children();
			}
			
				
			prev_i = parseInt($prevLink.attr('callback'));
			prev_x = '100% ';
			prev_y = -prev_i*21+'px';
			
			i = parseInt($link.attr('callback'));
			x = '0 ';
			y = -i*21+'px';
			
			$prevLink.css('background-position',prev_x + prev_y).parent().removeClass('a');
			$link.css('background-position',x + y).parent().addClass('a');
				
		});			
		
		
		function navigateProject(){
		
			$('.pr-nav li a').unbind().bind('click',function(e){
				e.preventDefault();
				
				prev_i = parseInt($('.pr-nav li.a a').attr('callback'));
				
				prev_x = '100% ';
				prev_y = -prev_i*21+'px';
				
				
				
				i = parseInt($(this).attr('callback'));
				
				x = '0 ';
				y = -i*21+'px';
				
				$('.pr-nav li.a a').css('background-position',prev_x + prev_y).parent().removeClass('a');
				$(this).css('background-position',x + y).parent().addClass('a');
				
				$i.hide();
				
				$('.pr-list .p.act .i').eq(i).show();
			});
			
			$('.pr-nav li a').hover(function(){
				i = parseInt($(this).attr('callback'));
				
				x = '0 ';
				y = -i*21+'px';
				$(this).css('background-position',x + y);
				
			}, function(){
				i = parseInt($(this).attr('callback'));
				
				if($(this).parent().is('.a'))
					x = '0 ';
				else
					x = '100% ';
				y = -i*21+'px';
				$(this).css('background-position',x + y);
			});
				
		}	
		
	});
	
	
}

$(document).ready(css);

jQuery.fn.slider = function(option) {
	option = jQuery.extend({
		slideAnimate: false,
		slideInterval: 4000
	}, option);	
	
	return this.each(function(i){	

		$canvas = $(this);
		$key = $canvas.find('.i');
		$frames = $key.size();
		$desc = $key.find('.d');
		
		buildSteps();
		
		$canvas.attr('frame',0);
		
		$trigger = $canvas.find('.trigger');
		

		$key.hide().eq(0).show().addClass('act');
		$trigger.eq(0).addClass('active');
		


		$trigger.children('a').click(function(e){
			e.preventDefault();
			i = $(this).attr('callback');
			animSlider(i);
		});

		$trigger.children('a').hover(function(){
			i = parseInt($(this).attr('callback'));
			
			x = '0 ';
			y = -i*21+'px';
			$(this).css('background-position',x + y);
			
		}, function(){
			i = parseInt($(this).attr('callback'));
			
			if($(this).parent().is('.active'))
				x = '0 ';
			else
				x = '100% ';
			y = -i*21+'px';
			$(this).css('background-position',x + y);
		});	
		
		
		function setAnimation(){
			i = parseInt($canvas.attr('frame'))+1;
			animSlider(i);		
		}
		
		function buildSteps(){
		
			html = '';
			
			for(i=0;i<$frames;i++){
				j = i+1;
				if(i== 0)
					x = 0;
				else
					x = '100%';
					
				y = -i*21+'px';
				
				html += '<li class="trigger"><a href="#" callback="'+i+'" style="background-position: '+x+' '+y+';">'+j+'</a></li>';
				
			}
			
			$canvas.append('<ul class="nav">'+html+'</ul>');
			$desc.css('right',(($frames+1)*22+36)+'px');
		
		}
				
				
		function animSlider(i){

			if(i == $frames)
				i = 0;
			
			i = parseInt(i);
			
			$canvas.attr('frame',i);
			$canvas.find('.act').removeClass('act').hide();
			$key.eq(i).show().addClass('act');
			
			
			$linkOut = $canvas.find('.active');
			$linkIn = $trigger.eq(i);
			
			j = parseInt($linkOut.children('a').attr('callback'));
			x = '100% ';
			y = -j*21+'px';			
			$linkOut.removeClass('active').children('a').css('background-position',x + y);
			
			j = parseInt($linkIn.children('a').attr('callback'));
			x = '0 ';
			y = -j*21+'px';
			$linkIn.addClass('active').children('a').css('background-position',x + y);
			
		} // function
		
		if(option.slideAnimate){
			si = setInterval(setAnimation, option.slideInterval);
		} // if(option.slideAnimate)
		
		if(si){
			$canvas.mouseover(function(){
				clearInterval(si);
			});
			
			$canvas.mouseout(function(){
				si = setInterval(setAnimation, option.slideInterval);
			});
		}// if(si)
	}); // each
}
