function addCL() {

	$('#dolacz-do-nas ul li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	$('#form-kontakt-submit').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	$('.progressWrapper').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	$('#header h1').hover(function() {
		$('#wrapper').addClass('hover');
		$('#wrapper a').addClass('hover');
	}, function() {
		$('#wrapper').removeClass('hover');
		$('#wrapper a').removeClass('hover');
	});
//	function validate(formData, jqForm, options) { 
//    // jqForm is a jQuery object which wraps the form DOM element 
//    // 
//    // To validate, we can access the DOM elements directly and return true 
//    // only if the values of both the username and password fields evaluate 
//    // to true 
// 
//    var form = jqForm[0]; 
//    if (!form.kontakt_imie.value || !form.kontakt_firma.value) { 
//        alert('Please enter a value for both Username and Password'); 
//        return false; 
//    } 
//    alert('Both fields contain values.'); 
//}
//	
//	
//	var options = { 
//    target:     '#on', 
//    url:        'send.php', 
//	beforeSubmit: validate,
//    success:    function() { 
//		$('#form-kontakt').fadeTo(1000,0).remove();
//        $('#on').css({'display':'block'}).fadeTo(1000,1);
//		return false;
//    } 
//}; 
//	$('#form-kontakt').ajaxForm(options);
	
}

function initContactForm(init) {
	var target = $('div#form');
	var message = $('#on');
	if (init) message.css('opacity', 0);
	
	initInputLabelFromLabel("#form-kontakt-imie", "#form-kontakt-imie-l");
	initInputLabelFromLabel("#form-kontakt-firma", "#form-kontakt-firma-l");
	initInputLabelFromLabel("#form-kontakt-email", "#form-kontakt-email-l");
	initInputLabelFromLabel("#form-kontakt-telefon", "#form-kontakt-telefon-l");
	//initInputLabelFromLabel("#form-kontakt-tresc", "#form-kontakt-tresc-l");
	
	swfuploadInit();
	
	var replaceTarget = function(content) {
		target.stop().fadeTo(500, 0, function() {
			target.html(content);
			if (target.find('form').length) {
				initContactForm();
			}
			target.fadeTo(500, 1);
		});
	};
	
	var processFormResponce = function(resp, status) {
		var content = resp.find('#content');
		var msg = resp.find('#message');

		if (status == 'ok') {
			try{
				pageTracker._trackPageview('/form/sent');
			} catch(err) {}
			if (msg.length) {
				replaceTarget(msg.html());
			}
			if (content.length) {
				if (msg.length) {
					setTimeout(function() {
						replaceTarget(content.html());
					}, 4000);
				} else {
					replaceTarget(content.html());
				}
			}
		} else {
			try{
				pageTracker._trackPageview('/form/notvalid');
			} catch(err) {}
			if (msg.length) {
				message.html(msg.html());
				message.fadeTo(500, 1);
			}
			if (content.length) {
				replaceTarget(content.html());
			}
		}
	};
	
	$('#form-kontakt').submit(function(event) {
		event.preventDefault();
		var _this = $(this);

		try {
			pageTracker._trackPageview('/form/submit');
		} catch(err) {}

		message.fadeTo(500, 0);
		message.html('');
		
		target.find('input').add('textarea').each(function() {
			var _this = $(this);
			if (_this.attr('emptyText') && _this.val() == _this.attr('emptyText')) {
				_this.val('');
			}
		});
		
		_this.ajaxSubmit({
			success: function(resp) {
				resp = $(resp);
				if (resp.hasClass('ok')) {
					processFormResponce(resp, 'ok');
				} else if (resp.hasClass('error')) {
					processFormResponce(resp, 'error');
				} else {
					//alert('nieznany blad');
				}
			}
		});
	});
}

function sAn() {
	$('#realizacje #real1 ul').animate( {
		"left" : "+=930px"
	}, 1000);


}

function sHeightd() {

	var Hei1 = $('#tab-c1 div.active').height();

	if (Hei1 < 330) {
		var Hei1 = 330;
	} else
		var Hei1 = Hei1;

	$('#tab-c1').css( {
		'height' : Hei1
	});
	$('#real1').animate( {
		'height' : Hei1
	}, 300);
	
}

function sTarget(){
	$(".online").attr("target","_blank");
	$(".facebook").attr("target","_blank");
	$("#co-z-projektem").attr("target","_blank");
	
}

function sClick() {
	$('#realizacje #real1 ul li').click(function(event) {
		event.preventDefault();
		$('#realizacje #real1 ul').find('li.active').removeClass('active');
		$(this).addClass('active');
		$("#realizacje #real1 ul li.active").attr("disable", "disable");
		var currTi = $('#nav-local').find('li.active a').attr('title');
		$('#realizacje #tab-c1 div.active').fadeOut('slow', function() {
			$(this).removeClass('active');
			var currentId2 = $('#realizacje ul li.active a').attr('title');
			var cID2 = "#tabc" + currentId2;
			loadXML('xml/xml' + currTi + '/' + currentId2 + '.xml');
			$(cID2).fadeIn('slow');
			$(cID2).addClass('active');
			sHeightd();
			var Hei = $(cID2).height();
			if (Hei < 360) {
				var Hei = 360;
			} else
				var Hei = Hei;
			$('#realizacje .active div div.active').css('height', Hei);
		});
	});
}
function initInputLabelFromLabel(input_id, label_id) {
	var text = $(label_id).text();
	var input = $(input_id);
	input.attr('emptyText', text);
	
	if (!input.val()) {
		input.val(text);
	}

	input.focus(function() {
		if ($(this).val() == text) {
			$(this).val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val(text);
		}
	});
}

function initInputLabelFromInput(input_id) {
	var text = $(input_id).val();
	$(input_id).val(text);
	$(input_id).focus(function() {
		if ($(this).val() == text) {
			$(this).val("");
		}
	});
	$(input_id).blur(function() {
		if ($(this).val() == '') {
			$(this).val(text);
		}
	});
}

function sHoverlf() {
	$('#nav-kimjestesmy li').click(function() {
		$('#kim-jestesmy').find('div.active').css( {
			'display' : 'none'
		}).fadeTo(100, 0);
		$('#nav-kimjestesmy').find('li.active').removeClass('active');
		$(this).addClass('active');
		var currentId = $('#nav-kimjestesmy li.active a').attr('title');
		var cID = ".zsd_" + currentId;
		$(cID).addClass('active');
		$(cID).css( {
			'display' : 'block'
		}).fadeTo(1000, 1);
		return false;

	});

}
function initNewwindow() {
	$('.kim-jestesmy div a').click(function(){window.open(this.href);
	return false;
	});
	}
function initNavLocal() {
	var tabs = $('ul#nav-local > li');
	var sTabsCnt = $('ul#side-tabs');
	var container = $('#tab-c1');
	
	var hash = location.hash;
	var loc = null;
	
	var currentNL = null;
	var currentTab = 1;
	var forcedTab = null;
	var noHashOnInit = true;
	
	if (hash.charAt(0) == '#') {
		hash = hash.substring(1);
	}
	if (hash.length > 0) {
		loc = hash.split(',');
		if (loc.length > 0 && loc[0].length > 0 && tabs.find('a[rel='+loc[0]+']').length) {
			currentNL = loc[0];
			if (loc.length > 1) {
				var id = parseInt(loc[1]);
				if (!isNaN(id)) {
					forcedTab = loc[1];
				}
			}
			location.hash = 'przejdz-do-realizacji';
			noHashOnInit = false;
		}
	}
	
	tabs.each(function(idx) {
		var _nl = $(this);
		var _lnk = _nl.find('a');
		var _section = _lnk.attr('rel');
		var _href = _lnk.attr('href');
		
		_lnk.attr('href', '#' + _section + ',1');
		
		_lnk.click(function(event) {
			event.preventDefault();
			sTabsCnt.animate({'left': -50}, 500);
			container.fadeTo(500, 0, function() {
				$.ajax({
					url: _href,
					cache: false,
					success: function(data, status) {
						container.find('a').unbind();
						container.html(data);
						sTabsCnt.empty();
						sTarget();
						
						var _contents = container.children('div');
						
						if (forcedTab != null && forcedTab > _contents.length) forcedTab = null;
						
						_contents.each(function(idx2) {
							var _content = $(this);
							var ti = idx2 + 1;
							var _tab = $('<li>');
							
							var selectTab = function() {
								if (!noHashOnInit) {
									location.hash = _section+','+ti;
								}
								noHashOnInit = false;
								
								loadXML('realizacje/' + _section + '/' + ti + '.xml');
								
								sTabsCnt.find('li.active').removeClass('active');
								_tab.addClass('active');
								
								container.children('div.active').removeClass('active');
								_content.addClass('active');
								
								//document.title = 'Emarketing Experts :: ' + _lnk.text() + ' :: ' + _content.find('h3').text();
							};
							
							var _tabLnk = $('<a>').attr({'href': '#' + _section + ',' + ti, 'id': 'id'+ti})
							.text(ti).click(function(event) {
								event.preventDefault();
								if (ti == currentTab) return;
								currentTab = ti;
								
								container.fadeTo(500, 0, function() {
									selectTab();
									container.fadeTo(500, 1);
								});
							});
							
							_tab.append(_tabLnk);
							sTabsCnt.append(_tab);
							
							if ((forcedTab == null && ti == 1) || (forcedTab != null && ti == forcedTab)) {
								currentTab = ti;
								selectTab();
							}
						});
						forcedTab = null;
						tabs.filter('li.active').removeClass('active');
						_nl.addClass('active');
						
						container.fadeTo(500, 1);
						sTabsCnt.animate({'left': 0}, 500);
					},
					dataType: 'html'
				});
			});
		});
		
		if ((currentNL == _section) || (currentNL == null && idx == 0)) {
			_nl.addClass('active');
			_lnk.click();
		}
	});
}

function initialize() {
	addCL();
	sTarget();
	initNewwindow();
	initContactForm(true);
	initNavLocal();
	sHoverlf();
	//sHover();
	//sAn();
	// sClick();
	// sHeightd();
	// resizeText();
	// bind 'myForm' and provide a simple callback function

}

$(document).ready(initialize);

