/*
 * 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).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);
                }, 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);
                }, 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);
								}, settings.timeout);
								$(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
				}
    };

    $.innerfade.next = function(elements, settings, current, last) {
        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);
        }), 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: 7000,
    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() {
	$('#movie01').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "1"}
		},
		{version: 8}
	);
	$('#movie02').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "2"}
		},
		{version: 8}
	);
	$('#movie03').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "3"}
		},
		{version: 8}
	);
	$('#movie04').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "4"}
		},
		{version: 8}
	);
	$('#movie05').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "5"}
		},
		{version: 8}
	);
	$('#movie06').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "6"}
		},
		{version: 8}
	);
	$('#movie07').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "7"}
		},
		{version: 8}
	);
  $('#movie08').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "8"}
		},
		{version: 8}
	);
	$('#movie09').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "9"}
		},
		{version: 8}
	);
	$('#movie10').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "10"}
		},
		{version: 8}
	);
	$('#movie11').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "11"}
		},
		{version: 8}
	);
	$('#movie12').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "12"}
		},
		{version: 8}
	);
	$('#movie13').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "13"}
		},
		{version: 8}
	);
	$('#movie14').flash({
		src: '_swf/main.swf',
			width: 760,
			height: 535,
			flashvars: {loadmovie_no: "14"}
		},
		{version: 8}
	);

	try {
		$("a#goMovie01, a#goMovie02, a#goMovie03, a#goMovie04, a#goMovie05, a#goMovie06, a#goMovie07, a#goMovie08, a#goMovie09, a#goMovie10, a#goMovie11, a#goMovie12, a#goMovie13, a#goMovie14").fancybox({
			'frameWidth' : 760,
			'frameHeight' : 535,
			'overlayOpacity' : 0.7
		});
	} catch (e) {}
});

$(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) {}
});


/*
 * 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,
    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.slides.width();
    this.current = this.options.startIndex;
    this.timeout = null;

    this.slider.width(this.slideWidth * this.slides.length);
  }

  Frontcarousel.prototype = {

    start: function(autochangeTime) {
      this.slider.css('left', -this.current * 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 next = this.current + 1;
      if (next >= this.slides.length) {
        next = 0;
      }
      this._change(next, time);
    },

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

    _startTimer: function(time) {
      var self = this;
      this.timeout = setTimeout(function() {
        self.next.call(self, time);
      }, time);
    },

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

    _change: function(index, time) {
      this._clearTimer();
      this.caruserlElement.trigger('frontcarousel:change', {
        toIndex: index,
        fromIndex: this.current
      });
      this.slider.animate(
        {left: -index * this.slideWidth + 'px'},
        this.options.animationTime,
        this.options.animationEasing
      );
      this.current = index;
      if (time) {
        this._startTimer(time);
      }
    }

  };

  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) {
    return;
  }

  carousel = new Frontcarousel(carouselElement, {
    startIndex: 0,
    animationTime: 700,
    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);


  // 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)] + ')');

});