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("#search", "#search_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");

	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') {
			if (msg.length) {
				replaceTarget(msg.html());
			}
			if (content.length) {
				if (msg.length) {
					setTimeout(function() {
						replaceTarget(content.html());
					}, 4000);
				} else {
					replaceTarget(content.html());
				}
			}
		} else {
			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);

				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 tabFrontpage() {
	var tm = null;
	var change = function() {		
		if (tm != null)
			clearTimeout(tm);
		if ($('.t_polecamy').length < 2) return;
		tm = setTimeout(function() {
			tm = null;
			change();
		}, 13000);
		var d_polec = $('.t_polecamy.active');
		var d_next = d_polec.next('.t_polecamy');
		var slDa = d_next.length;
		if (slDa > 0) {
			d_next.addClass('active');
		} else {
			$('.t_polecamy:first').addClass('active');
		}
		d_polec.removeClass('active');
	};
	var a_next = $('.a_nastepne').click(function(event) {
		event.preventDefault();
		change();
	});
	tm = setTimeout(function() {
		tm = null;
		change();
	}, 13000);

}

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");
	$(".target_blank").attr("target", "_blank");

}

function sTab() {
	var getHash = function(lnk) {
		var href = lnk.attr('href');
		var pos = href.indexOf('#');
		if (pos > -1 && href.length > pos + 1) {
			return href.substr(pos + 1);
		}
		return '';
	};

	var selectTab = function(tab, noHash) {
		var _this = $(tab);
		$('.tab-menu > li.active').add('.tab-content > div.active').removeClass('active');
		_this.addClass('active');
		var tabid = _this.attr('tab');
		var cla = ".tab-content > div[tab=" + tabid + "]";
		$(cla).addClass('active');

		if (!noHash) {
			var lnk = _this.find('a');
			var hash = getHash(lnk);
			if (hash.length > 0)
				document.location.hash = hash;
		}
	};

	$('.tab-menu li').each(function(i) {
		$(this).attr('tab', i);
	}).click(function(event) {
		event.preventDefault();
		selectTab(this);
	});
	
	$('.tab-content > div').each(function(i) {
		$(this).attr('tab', i);
	});

	var tmp = $(".tab-menu li.active");
	if (tmp.length) {
		tmp.attr('defaultTab', '1');
	} else {
		tmp = $(".tab-menu li").eq(0).attr('defaultTab', '1');
	}
	//selectTab(tmp, true);

	var currentHash = '';
	var selectTabByHash = function() {
		currentHash = document.location.hash;
		var hash = currentHash;
		if (hash == '') {
			var tab = $(".tab-menu li[defaultTab=1]");
			//if (!tab.hasClass('active'))
				selectTab(tab, true);
		}
		if (hash.charAt(0) == '#')
			hash = hash.substr(1);
		if (hash.length > 0) {
			$(".tab-menu li").each(function() {
				var _this = $(this);
				var lnk = _this.find('a');
				var _hash = getHash(lnk);
				if (_hash.length > 0 && _hash == hash)
					selectTab(_this, true);
			});
		}
	};

	setInterval(function() {
		if (currentHash != document.location.hash) {
			selectTabByHash();
		}
	}, 500);

	selectTabByHash();
}

function nMuzyka() {
	$('#nav-global li').mouseenter(function() {
		$(this).find('a:first').addClass('active');
		var lf = $(this).find('ul');
		if (lf.length)
			lf.stop().show().fadeTo("slow", 1);
	}).mouseleave(function() {
		$(this).find('a:first').removeClass('active');
		$(this).find('ul').stop().fadeTo("slow", 0, function() {
			$(this).hide();
		});
	});
	$('.copyright ul li').mouseenter(function() {
		$(this).find('a:first').addClass('active');
		var lf = $(this).find('ul');
		if (lf.length)
			lf.stop().show().fadeTo("slow", 1);
	}).mouseleave(function() {
		$(this).find('a:first').removeClass('active');
		$(this).find('ul').stop().fadeTo("slow", 0, function() {
			$(this).hide();
		});
	});
}

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 initNewwindow() {
	$('#h_newlink-foot a').click(function(){window.open(this.href);
	
	
	return false;
	});
	}
function sHoverlf() {
	$('#nav-kimjestesmy li').click(function() {
		$('.kim-jestesmy').find('div.active').fadeTo(100, 0).css( {
			'display' : 'none'
		});
		$('#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 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 lght() {
	$('.film-l > a').lightBox();
	$('.film-lc > a').lightBox();
	$('.ct3 .gallery a').lightBox();
	$('.box-bor .gal-atrakcje a').lightBox();
	$('.lightb').lightBox();
}

function initialize() {
	nMuzyka();
	addCL();
	sTarget();
	lght();
	sTab();
	initContactForm(true);
	initNavLocal();
	sHoverlf();
	initInputLabelFromInput("#search", "#search_l");
	tabFrontpage();
	initNewwindow();
	// sHover();
	// sAn();
	// sClick();
	// sHeightd();
	// resizeText();
	// bind 'myForm' and provide a simple callback function

}

$(document).ready(initialize);
