/*
 * jQuery Cycle Lite Plugin
 * http://malsup.com/jquery/cycle/lite/
 * Copyright (c) 2008 M. Alsup
 * Version: 1.0 (06/08/2008)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.2.3 or later
 */
;(function($) {

var ver = 'Lite-1.0';

$.fn.cycle = function(options) {
    return this.each(function() {
        options = options || {};

        if (this.cycleTimeout) clearTimeout(this.cycleTimeout);
        this.cycleTimeout = 0;
        this.cyclePause = 0;

        var $cont = $(this);
        var $slides = options.slideExpr ? $(options.slideExpr, this) : $cont.children();
        var els = $slides.get();
        if (els.length < 2) {
            if (window.console && window.console.log)
                window.console.log('terminating; too few slides: ' + els.length);
            return; // don't bother
        }

        var opts = $.extend({}, $.fn.cycle.defaults, options || {}, ($.metadata ? $cont.metadata() : $.meta ? $cont.data() : {}));

        opts.before = opts.before ? [opts.before] : [];
        opts.after = opts.after ? [opts.after] : [];
        opts.after.unshift(function(){opts.busy=0;});

        var cls = this.className;
        opts.width = parseInt((cls.match(/w:(\d+)/)||[])[1]) || opts.width;
        opts.height = parseInt((cls.match(/h:(\d+)/)||[])[1]) || opts.height;
        opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1]) || opts.timeout;

        if ($cont.css('position') == 'static')
            $cont.css('position', 'relative');
        if (opts.width)
            $cont.width(opts.width);
        if (opts.height && opts.height != 'auto')
            $cont.height(opts.height);

        var first = 0;
        $slides.css({position: 'absolute', top:0, left:0}).hide().each(function(i) {
            $(this).css('z-index', els.length-i)
        });

        $(els[first]).css('opacity',1).show(); // opacity bit needed to handle reinit case
        if ($.browser.msie) els[first].style.removeAttribute('filter');

        if (opts.fit && opts.width)
            $slides.width(opts.width);
        if (opts.fit && opts.height && opts.height != 'auto')
            $slides.height(opts.height);
        if (opts.pause)
            $cont.hover(function(){this.cyclePause=1;}, function(){this.cyclePause=0;});

        $.fn.cycle.transitions.fade($cont, $slides, opts);

        $slides.each(function() {
            var $el = $(this);
            this.cycleH = (opts.fit && opts.height) ? opts.height : $el.height();
            this.cycleW = (opts.fit && opts.width) ? opts.width : $el.width();
        });

        $slides.not(':eq('+first+')').css({opacity:0});
        if (opts.cssFirst)
            $($slides[first]).css(opts.cssFirst);

        if (opts.timeout) {
            if (opts.speed.constructor == String)
                opts.speed = {slow: 600, fast: 200}[opts.speed] || 400;
            if (!opts.sync)
                opts.speed = opts.speed / 2;
            while((opts.timeout - opts.speed) < 250)
                opts.timeout += opts.speed;
        }
        opts.speedIn = opts.speed;
        opts.speedOut = opts.speed;

        opts.slideCount = els.length;
        opts.currSlide = first;
        opts.nextSlide = 1;

        var e0 = $slides[first];
        if (opts.before.length)
            opts.before[0].apply(e0, [e0, e0, opts, true]);
        if (opts.after.length > 1)
            opts.after[1].apply(e0, [e0, e0, opts, true]);

        if (opts.click && !opts.next)
            opts.next = opts.click;
        if (opts.next)
            $(opts.next).bind('click', function(){return advance(els,opts,opts.rev?-1:1)});
        if (opts.prev)
            $(opts.prev).bind('click', function(){return advance(els,opts,opts.rev?1:-1)});

        if (opts.timeout)
            this.cycleTimeout = setTimeout(function() {
                go(els,opts,0,!opts.rev)
            }, opts.timeout + (opts.delay||0));
    });
};

function go(els, opts, manual, fwd) {
    if (opts.busy) return;
    var p = els[0].parentNode, curr = els[opts.currSlide], next = els[opts.nextSlide];
    if (p.cycleTimeout === 0 && !manual)
        return;

    if (manual || !p.cyclePause) {
        if (opts.before.length)
            $.each(opts.before, function(i,o) {o.apply(next, [curr, next, opts, fwd]);});
        var after = function() {
            if ($.browser.msie)
                this.style.removeAttribute('filter');
            $.each(opts.after, function(i,o) {o.apply(next, [curr, next, opts, fwd]);});
        };

        if (opts.nextSlide != opts.currSlide) {
            opts.busy = 1;
            $.fn.cycle.custom(curr, next, opts, after);
        }
        var roll = (opts.nextSlide + 1) == els.length;
        opts.nextSlide = roll ? 0 : opts.nextSlide+1;
        opts.currSlide = roll ? els.length-1 : opts.nextSlide-1;
    }
    if (opts.timeout)
        p.cycleTimeout = setTimeout(function() {go(els,opts,0,!opts.rev)}, opts.timeout);
};

function advance(els, opts, val) {
    var p = els[0].parentNode, timeout = p.cycleTimeout;
    if (timeout) {
        clearTimeout(timeout);
        p.cycleTimeout = 0;
    }
    opts.nextSlide = opts.currSlide + val;
    if (opts.nextSlide < 0) {
        opts.nextSlide = els.length - 1;
    }
    else if (opts.nextSlide >= els.length) {
        opts.nextSlide = 0;
    }
    go(els, opts, 1, val>=0);
    return false;
};

$.fn.cycle.custom = function(curr, next, opts, cb) {
    var $l = $(curr), $n = $(next);
    $n.css({opacity:0});
    var fn = function() {$n.animate({opacity:1}, opts.speedIn, opts.easeIn, cb)};
    $l.animate({opacity:0}, opts.speedOut, opts.easeOut, function() {
        $l.css({display:'none'});
        if (!opts.sync) fn();
    });
    if (opts.sync) fn();
};

$.fn.cycle.transitions = {
    fade: function($cont, $slides, opts) {
        $slides.not(':eq(0)').css('opacity',0);
        opts.before.push(function() {$(this).show()});
    }
};

$.fn.cycle.ver = function() {return ver;};

$.fn.cycle.defaults = {
    timeout:       1,
    speed:         500,
    next:          null,
    prev:          null,
    before:        null,
    after:         null,
    height:       'auto',
    sync:          1,
    fit:           0,
    pause:         0,
    delay:         0,
    slideExpr:     null
};

})(jQuery);


// jquery.innerfade.js

// Datum: 2008-02-14
// Firma: Medienfreunde Hofmann & Baldes GbR
// Author: Torsten Baldes
// Mail: t.baldes@medienfreunde.com
// Web: http://medienfreunde.com

// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
// and Ralf S. Engelschall http://trainofthoughts.org/

// ========================================================= */


(function($) {

    $.fn.innerfade = function(options) {
        return this.each(function() {
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
        var settings = {
        	'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
        	$(container).mouseover(function(){
                $(container).attr('pause', 'true');
            });
        	$(container).mouseout(function(){
                $(container).attr('pause', 'false');
            });
            $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };
            if (settings.type == "sequence") {
                setTimeout(function() {
                    $.innerfade.next(elements, settings, 1, 0, container);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
            		var last = Math.floor ( Math.random () * ( elements.length ) );
                setTimeout(function() {
                    do {
												current = Math.floor ( Math.random ( ) * ( elements.length ) );
										} while (last == current );
										$.innerfade.next(elements, settings, current, last, container);
                }, settings.timeout);
                $(elements[last]).show();
						} else if ( settings.type == 'random_start' ) {
								settings.type = 'sequence';
								var current = Math.floor ( Math.random () * ( elements.length ) );
								setTimeout(function(){
									$.innerfade.next(elements, settings, (current + 1) %  elements.length, current, container);
								}, settings.timeout);
								$(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
				}
    };

    $.innerfade.next = function(elements, settings, current, last, container) {
    	if ($(container).attr('pause') != 'true') {
	        if (settings.animationtype == 'slide') {
    	        $(elements[last]).slideUp(settings.speed);
        	    $(elements[current]).slideDown(settings.speed);
        	}else if (settings.animationtype == 'fade') {
            	$(elements[last]).fadeOut(settings.speed);
            	$(elements[current]).fadeIn(settings.speed, function() {
								removeFilter($(this)[0]);
							});
        	} else
	            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
    	    if (settings.type == "sequence") {
            	if ((current + 1) < elements.length) {
	                current = current + 1;
                	last = current - 1;
            	} else {
                	current = 0;
                	last = elements.length - 1;
            	}
        	} else if (settings.type == "random") {
            	last = current;
            	while (current == last)
                	current = Math.floor(Math.random() * elements.length);
        	} else
            	alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
    	}
        setTimeout((function() {
            $.innerfade.next(elements, settings, current, last, container);
        }), settings.timeout);
    };

})(jQuery);

function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

function textReplacement(input){
  var originalvalue = input.val();
  input.focus( function() {
    if($.trim(input.val()) == originalvalue) {
      input.val('');
    }
  });

  input.blur( function() {
    if($.trim(input.val()) == '') {
      input.val(originalvalue);
    }
  });
}

jQuery.fn.outerHTML = function(s) {
	return (s)
	? this.before(s).remove()
	: jQuery("<p>").append(this.eq(0).clone()).html();
}

$(document).ready(function(){
	textReplacement($('#searchWhat'));
  textReplacement($('#searchWhere'));

  $('#searchSubmit').hover(function(){
    $(this).attr({src: '/images/_img/search_submit_1.png'});},
    function(){
    $(this).attr({src: '/images/_img/search_submit_0.png'});}
  );


//  var s1 = $('.showcaseWizytowki').outerHTML();
//  var s2 = $('.showcaseUslugi').outerHTML();
//  var s3 = $('.showcaseZlecenia').outerHTML();
//  switch(Math.round(Math.random()*2)) {
//    case 0:$('#news').html(s1 + s2 + s3);break;
//    case 1:$('#news').html(s3 + s1 + s2);break;
//    case 2:$('#news').html(s2 + s3 + s1);break;
//  }
  $('#news').show();
  $('#news').innerfade({
    animationtype: 'fade',
    speed: 1000,
    timeout: 5000,
    containerheight: '144px'
  });

//  $('#seeDiv').flash(
//    {
//  	src: '/_swf/button.swf',
//    width: 60,
//    height: 36
//    },
//    { version: 9 }
//  );

//  var slideAllowed = true;
//
//  jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 8000);
//
//  $('#uslugiBtn a').click(function() {
//    slideAllowed = false;
//  });
//
//  $("#uslugiContent, #wykonawcyContent, #zleceniaContent, #dyskusjeContent").bind("mouseenter", function() {
//    jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//  }).bind("mouseleave", function() {
//    if (slideAllowed) {
//      jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 8000);
//    }
//  });
//
//  $('a.arrowLeft').click(function() {
//    var selected = $('#carouselDiv').tabs('option', 'selected');
//    switch (selected) {
//      case 0:
//        jQuery('#carouselDiv').tabs('select', 3);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//      case 1:
//        jQuery('#carouselDiv').tabs('select', 0);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//      case 2:
//        jQuery('#carouselDiv').tabs('select', 1);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//      case 3:
//        jQuery('#carouselDiv').tabs('select', 2);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//      default :
//        jQuery('#carouselDiv').tabs('select', 0);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//    }
//  });
//
//  $('a.arrowRight').click(function() {
//    var selected = $('#carouselDiv').tabs('option', 'selected');
//    switch (selected) {
//      case 0:
//        jQuery('#carouselDiv').tabs('select', 1);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//      case 1:
//        jQuery('#carouselDiv').tabs('select', 2);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//      case 2:
//        jQuery('#carouselDiv').tabs('select', 3);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//      case 3:
//        jQuery('#carouselDiv').tabs('select', 0);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//      default :
//        jQuery('#carouselDiv').tabs('select', 0);
//        jQuery('#carouselDiv').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 0);
//        break;
//    }
//  });
});


$(document).ready(function() {
	try {
		$("a#goDopasowanie").fancybox({
			'frameWidth' : 700,
			'frameHeight' : 380,
			'overlayOpacity' : 0.7
		});
	} catch (e) {}
	try {
		$("a#goWysoko").fancybox({
			'frameWidth' : 980,
			'frameHeight' : 620,
			'overlayOpacity' : 0.7
		});
	} catch (e) {}
	try {
		$("a#goWyroznienie").fancybox({
			'frameWidth' : 850,
			'frameHeight' : 485,
			'overlayOpacity' : 0.7
		});
	} catch (e) {}
	try {
		$("a.iframe").fancybox({
			'frameWidth' : 1020,
			'frameHeight' : 535,
			'overlayOpacity' : 0.7
		});
	} catch (e) {}
  try {
    jQuery('.help_movie_link').fancybox({
      'frameWidth': 760,
      'frameHeight': 570,
      'callbackOnShow': function() {
        jQuery('#fancy_frame').css({ 'height': 535 }).attr('scrolling', 'no');
      },
      'callbackOnClose': function() {
        jQuery('#fancy_frame').remove();
      }
    });
  } catch (e) {}
  try {
    jQuery('.rapid_fb').fancybox({
      'frameWidth': 995,
      'frameHeight': 570,
      'type': 'iframe',
      'callbackOnShow': function() {
        jQuery('#fancy_frame').css({ 'height': 535 }).attr('scrolling', 'yes');
        jQuery('#fancy_bg').css({ 'background-color': '#36B9C3' });
      },
      'callbackOnClose': function() {
        jQuery('#fancy_frame').remove();
        location.reload();
      }
    });
  } catch (e) {}
});


/*
 * Klasa do prostej karuzeli. Robi samą logikę i efekt przejścia, reszte
 * (interfejs) nalezy zrobic przy uruchamianiu.
 * @param element - dom element, dodawany jest do niego event: frontcarousel:change
 * @param options - opcje: startIndex, animationTime, animationEasing...
 * @author: sos
 */
var Frontcarousel = (function($) {

  var defaultOptions = {
    startIndex: 0,
    slideWidth: null,
    animationTime: 750,
    animationEasing: 'linear',
    slideSelector: '.slide',
    sliderSelector: '.slider'
  };

  function Frontcarousel(element, options) {
    this.caruserlElement = $(element);
    this.options = $.extend(defaultOptions, options || {});

    this.slider = this.caruserlElement.find(this.options.sliderSelector);
    this.slides = this.slider.find(this.options.slideSelector);
    this.slideWidth = this.options.slideWidth || this.slides.width();
    this.slidesLength = this.slides.length;
    this.current = this.options.startIndex;
    this.timeout = null;
    this.isAnimating = false;
    this.queuedMove = null;

    var
      first = $(this.slides[0]),
      last = $(this.slides[this.slidesLength - 1]);

    first.before(last.clone());
    last.after(first.clone());
    this.slider.width(this.slideWidth * (this.slidesLength + 2));
    this.slider.attr('over', 'false');
    this.slider.mouseover(function() {
      $(this).attr('over', 'true');
    });
    this.slider.mouseout(function() {
    	$(this).attr('over', 'false');
    });
  }

  Frontcarousel.prototype = {

    start: function(autochangeTime) {
      this.slider.css('left', -(this.current + 1) * this.slideWidth);
      this.caruserlElement.trigger('frontcarousel:change', {
        toIndex: this.current
      });

      if (autochangeTime && !isNaN(autochangeTime)) {
        this._startTimer(autochangeTime);
      }
    },

    go: function(index) {
      if (index != this.current) {
        this._change(index);
      }
    },

    next: function(time) {
      var current = this.queuedMove || this.current;
      var next = current + 1;
      if (next >= this.slidesLength) {
        next = 0;
      }
      this._change(next, time);
    },

    previous: function() {
      var current = this.queuedMove || this.current;
      var prev = current - 1;
      if (prev < 0) {
        prev = this.slidesLength - 1;
      }
      this._change(prev);
    },

    _change: function(index, time) {

      if (this.isAnimating) {
        this.queuedMove = index;
        return;
      }

      var self = this, callback, distance;

      this._clearTimer();
      this.isAnimating = true;
      this.caruserlElement.trigger('frontcarousel:change', {
        toIndex: index,
        fromIndex: this.current
      });

      if (this.current == this.slidesLength - 1 && index == 0) {
        // z ostatniego przechodzimy na pierwszy
        distance = (this.current + 2) * this.slideWidth;
        callback = function() {
          self.slider.css('left', -1 * self.slideWidth);
          self._afterAnimate();
        };
      } else if (this.current == 0 && index == this.slidesLength - 1) {
        // z pierwszego na ostatni
        distance = 0;
        callback = (function(index) {
          return function() {
            self.slider.css('left', -(index + 1) * self.slideWidth);
            self._afterAnimate();
          }
        })(index);
      } else {
        distance = (index + 1) * this.slideWidth;
        callback = function () {
          self._afterAnimate();
        }
      }

      this._animate(distance, callback);
      this.current = index;
      if (time) {
        this._startTimer(time);
      }
    },

    _startTimer: function(time) {
      var self = this;
      this.timeout = setTimeout(function() {
    	if (self.slider.attr('over') == 'false') {
          self.next.call(self, time);
    	} else {
    	  self._startTimer(time);
    	}
      }, time);
    },

    _clearTimer: function() {
      if (this.timeout) {
        clearTimeout(this.timeout);
        this.timeout = null;
      }
    },

    _animate: function(distance, callback) {
      callback = callback || function() {};
      this.slider.animate(
        {left: -distance + 'px'},
        this.options.animationTime,
        this.options.animationEasing,
        callback
      );
    },

    _afterAnimate: function() {
      this.isAnimating = false;
      if (this.queuedMove !== null) {
        this._change(this.queuedMove);
        this.queuedMove = null;
      }
    }
  };

  return Frontcarousel;

})(jQuery);


/*
 * Uruchomienie karuzeli
 */
jQuery(function() {

  var carouselElement = jQuery('#carouselDiv'),
      tabsAnchors = jQuery('#mainTabs a'),
      bottomAnchors = jQuery('#carouselDiv .bottomBar a'),
      arrowleft = jQuery('#arrows .arrowLeft'),
      arrowright = jQuery('#arrows .arrowRight'),
      autochangeTime = 5000,
      carousel;

  if (!carouselElement.length) {
    return;
  }

  // ustawienie losowego obrazka tla dla dyskusji
  var images = ['pic_dyskusje.gif', 'pic_dyskusje2.gif', 'pic_dyskusje3.gif'];
  function random(min, max){
    return Math.floor(Math.random() * (max - min + 1) + min);
  }
  jQuery('#dyskusjeContent')
      .css('backgroundImage', 'url(/images/_img/carousel/' + images[random(0, images.length-1)] + ')');

  carousel = new Frontcarousel(carouselElement, {
    startIndex: 0,
    animationTime: 700,
    slideWidth: 717,
    animationEasing: 'easeOutCubic',
    slideSelector: '.slide',
    sliderSelector: '#carouselSlider'
  });

  carouselElement.bind('frontcarousel:change', function(event, data) {
    if (data.fromIndex !== null) {
      jQuery(tabsAnchors.get(data.fromIndex)).parent('li').removeClass('selected');
      jQuery(bottomAnchors.get(data.fromIndex)).css('display', '');
    }
    jQuery(tabsAnchors.get(data.toIndex)).parent('li').addClass('selected');
    jQuery(bottomAnchors.get(data.toIndex)).css('display', 'block');
  });

  tabsAnchors.bind('click', function(event) {
    event.preventDefault();
    event.stopPropagation();
    carousel.go(tabsAnchors.index(this));
  });

  arrowleft.bind('click', function(event) {
    event.preventDefault();
    carousel.previous();
  });

  arrowright.bind('click', function(event) {
    event.preventDefault();
    carousel.next();
  });

  carousel.start(autochangeTime);

});

/*
 * Flash JavaScript functions
 * help baners - strona glowna, pomoc
 */
function fancyBoxWindow(url) {
  var movieTvContainer = $('<a>').addClass('movieTvContainer iframe').attr({
    'id':'movieTvLink',
    'href' : url
  }).text('link');
  $('body').append(movieTvContainer);


  $("a.movieTvContainer").fancybox({
    "frameWidth"  :  760,
    "frameHeight"  :  570,
    'callbackOnShow' : function(){$('#fancy_content').height(575).width(760)},
    'callbackOnClose': function(){$('#fancy_content').empty();}

  });
  $("a#movieTvLink").click().css('display','none').remove();

}




/*
 * Flash JavaScript functions
 * help baners - strona glowna, pomoc
 */
function fancyBoxWindow(url) {
  var movieTvContainer = $('<a>').addClass('movieTvContainer iframe').attr({
    'id':'movieTvLink',
    'href' : url
  }).text('link');
  $('body').append(movieTvContainer);


  $("a.movieTvContainer").fancybox({
    "centerOnScroll": false,
    "frameWidth"  :  780,
    "frameHeight"  :  900,
    'callbackOnShow' : function(){$('#fancy_content').height(900).width(780)},
    'callbackOnClose': function(){$('#fancy_content').empty();}

  });
  $("a#movieTvLink").click().css('display','none').remove();

}



 

