$(function(){

	$('#BTNOFFER').click(function() {
		$('#homeColumns').slideToggle(400);
		return false;
	});

  var lang = $('html').attr('lang');
  switch (lang)
  {
    case 'pl': break;
    case 'en': break;
  }
	
	// ===== COLORBOX =====
	$("a[rel='lightbox']").colorbox();
	$("a[rel='LightFrame']").click(function() {
		window.open($(this).attr('href'),"ToDo",'width=800,height=460,resizable=no,scrollbars=no,menubar=no');
		return false;
	});
	
  $('a[rel*=external]').click( function() {
    window.open(this.href);
    return false;
  });
  $('a.email').each(function() {
    e = this.rel.replace('[at]','@');
    this.href = 'mailto:' + e;
    $(this).text(e);
  });
	
	var searchField = $('.search-phrase input'),
    searchFieldValue = $('.search-phrase input').val();
  searchField.val(searchFieldValue);
  searchField.focus(function() { if ($(this).val() == 'Wpisz szukany tekst...') $(this).val(''); })
    .blur(function() { if ($(this).val()=='') $(this).val(searchFieldValue); });
	
  // v 2.0
  $('form:has(input[type=submit])').each(function(){
    var thisForm = $(this);
    var submitButton = thisForm.find('input[type=submit]');
    var link = $('<a href="#" class="submit" title="' + submitButton.attr('value') +'">' + submitButton.attr('value') + '</a>');
    link.insertAfter(submitButton).click(function(){
      thisForm.submit();
      return false;
    });
    submitButton.remove();
  });
	
  // INTRO
  if ($('#index').hasClass('intro'))
  {
		alert('ass');
    $('#flashContainer').flash({swf: './flash/flasz.swf', width: 935, height: 465, flashvars: {wmode: 'transparent'}});
  }

  Cufon.replace('#home .news .header h2, #home .bottom h2,#contact #form h2,#contact #mapcontainer h2',{fontFamily: 'HelveticaNeueLight'});
  Cufon.replace('#sub #sidebar h2, #sub #container .header h2',{fontFamily: 'HelveticaNeueLight'});
  Cufon.replace('#home .news .header h2 strong',{fontFamily: 'HelveticaNeueMedium'});
	Cufon.replace('.menu li a',{fontFamily: 'HelveticaNeueLightCondensed', hover: true});
	Cufon.replace('#home .columns > div .inner h3',{fontFamily: 'HelveticaNeueBoldCondensed', textShadow: '1px 1px #5c5c5c'});
	Cufon.replace('#nav li a',{fontFamily: 'HelveticaNeueLight', hover: true});
	
	
  Cufon.replace('#header .left p, #slideshow h2', {fontFamily: 'HelveticaNeueLightCondensed', textShadow: '1px 1px #5c5c5c'});
  Cufon.replace('#header .left p strong, #slideshow h2 strong', {fontFamily: 'HelveticaNeueBoldCondensed', textShadow: '1px 1px #5c5c5c'});

	Cufon.replace('.sub_header',{fontFamily: 'HelveticaNeueBoldCondensed'});


	$('#kd1').click(function () {
		$('.div-kd2').slideUp(200);
		$('.div-kd1').slideDown(200);
		$('.kd-menu a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	
	$('#kd2').click(function () {
		$('.div-kd1').slideUp(200);
		$('.div-kd2').slideDown(200);
		$('.kd-menu a').removeClass('active');
		$(this).addClass('active');
		return false;
	});

  // nav item width fix

  var actualWidth = 987;
  var totalLIWidth = 0;
  var lis = $('#nav ul li');
  lis.each(function(){
      totalLIWidth += $(this).width();
  });
  var requiredPadding = Math.round(((actualWidth-totalLIWidth)/lis.length)/2);
  var roundingErrorFix = (requiredPadding*lis.length*2)+totalLIWidth-actualWidth;
  var isFirst = true;
  lis.each(function() {
      if(isFirst) {
          $(this).css('width', $(this).width() + 2*(requiredPadding-roundingErrorFix)+'px');
          isFirst = false;
      }
      else
          $(this).css('width', $(this).width() + 2*requiredPadding+'px');
    $(this).children('a').css('width', $(this).width() - 40 + 'px');
  });
  totalLIWidth = 0;
  lis.each(function(){
      totalLIWidth += $(this).width();
  });
  var roznica = actualWidth - totalLIWidth;
  $('#nav li:last').css('width', $('#nav li:last').width() + roznica + 'px');
  $('#nav li:last a').css('width', $('#nav li:last').width() - 40 + 'px');



  // HOME

  if ($('#home').length > 0)
  {
    $('#home #newsletter_email').placeholder();
    $('#carousel').jcarousel({auto:15, animation:800, wrap: 'last'});
    $('#homeColumns >div').hoverIntent({ over:
			function() {
      if ($(this).hasClass('active'))
      {
        $('#homeColumns >div').animate({width: 319}).children('.inner').animate({width: 319});
        $(this).removeClass('active');
      }
      else
      {
        $(this).addClass('active');
        $('#homeColumns >div').not(this).removeClass('active');
        $('#homeColumns >div').not(this).animate({width: 219});
        $('#homeColumns >div .inner').animate({width: 219});
        $(this).animate({width: 519});
      }
    }, out: function() {}, timeout: 500, sensitivity: 7, interval: 100 }
    );


    $('#homeColumns .col1 .slides').cycle({delay: 0, timeout: 6000});
    $('#homeColumns .col2 .slides').cycle({delay: 2000, timeout: 6000});
    $('#homeColumns .col3 .slides').cycle({delay: 4000, timeout: 6000});
  }

  /// SUBPAGE

  if ($('#sub').length > 0)
  {
    $('#slideshow').cycle({slideExpr: 'img'});
  }

	$('#newsletter_form').submit(function() {
		$('#cl > div').html('');
		$('#cl').hide();
		$('#cl > div').load("./zapisz.html?e="+$('#newsletter_email').val());
		$('#cl').show(300);
		return false;
	});
	
	$('#newsletter_form .delete').click(function() {
		$('#cl > div').html('');
		$('#cl').hide();
		$('#cl > div').load("./wypisz.html?e="+$('#newsletter_email').val());
		$('#cl').show(300);
		return false;
	});
	
	$('#cl .cl-close').click(function() {
		$('#cl').hide(300);
		return false;
	});
	
	
	
	$('#sidebar:has(#SContact)').addClass('siderbar-kontakt');
	var HEIGHT = $('#SMenu').height() + 120;
	$('#sidebar:has(#SMenu)').attr('style','background-position: 100% '+HEIGHT+'px !important;');
	
	
});


function show_confirm(url)
{
	var r=confirm("Czy na pewno usun±æ?");
	if (r==true)
		{
		window.location=url;
		}
	else
		{
		
		}
}
