//hoverFlow
(function($){$.fn.hoverFlow=function(c,d,e,f,g){if($.inArray(c,['mouseover','mouseenter','mouseout','mouseleave'])==-1){return this}var h=typeof e==='object'?e:{complete:g||!g&&f||$.isFunction(e)&&e,duration:e,easing:g&&f||f&&!$.isFunction(f)&&f};h.queue=false;var i=h.complete;h.complete=function(){$(this).dequeue();if($.isFunction(i)){i.call(this)}};return this.each(function(){var b=$(this);if(c=='mouseover'||c=='mouseenter'){b.data('jQuery.hoverFlow',true)}else{b.removeData('jQuery.hoverFlow')}b.queue(function(){var a=(c=='mouseover'||c=='mouseenter')?b.data('jQuery.hoverFlow')!==undefined:b.data('jQuery.hoverFlow')===undefined;if(a){b.animate(d,h)}else{b.queue([])}})})}})(jQuery);
//highlight
jQuery.fn.highlight=function(b){function a(e,j){var l=0;if(e.nodeType==3){var k=e.data.toUpperCase().indexOf(j);if(k>=0){var h=document.createElement("span");h.className="highlight";var f=e.splitText(k);var c=f.splitText(j.length);var d=f.cloneNode(true);h.appendChild(d);f.parentNode.replaceChild(h,f);l=1}}else{if(e.nodeType==1&&e.childNodes&&!/(script|style)/i.test(e.tagName)){for(var g=0;g<e.childNodes.length;++g){g+=a(e.childNodes[g],j)}}}return l}return this.each(function(){a(this,b.toUpperCase())})};jQuery.fn.removeHighlight=function(){return this.find("span.highlight").each(function(){this.parentNode.firstChild.nodeName;with(this.parentNode){replaceChild(this.firstChild,this);normalize()}}).end()};
//trim
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

$(document).ready(function() {

	// animacja obrazka na glownej
	var main_image = $('#photo>a>img');
	if (main_image.length > 0) { //$.support.opacity && 
		main_image.removeClass('noJsHover').parent().hover(function(e) {
					main_image.hoverFlow(e.type, { opacity: 'hide' }, 'slow');
				}, function(e) {
					main_image.hoverFlow(e.type, { opacity: "show" }, 'normal');
				});
	}
	
	// slideshow
    var images = $('#gallery ul a');    
    if (images.length > 0) {
    	var big_photo = $('#gallery div.bigPhoto img');
    	
    	big_photo.parent().prettyPhoto({allowresize: true});
    	
    	if(images.length > 3){
        	$('#carousel_top').show().addClass('disabled');
        	$('#carousel_bottom').show();
        	big_photo.parent().parent().addClass('bigPhotoMargins');
        	$('#gallery>div.minis').jCarouselLite({
               btnPrev:'#carousel_top',
               btnNext:'#carousel_bottom',
               visible: 3,
               circular:false,
               vertical: true,
               mouseWheel: true
             });
    	}
    	
    	images.click(function(){
            var _this=$(this);
            if(_this.attr('href') != big_photo.attr('src')){
            	$('#gallery li.current').removeClass('current');
            	_this.parent().addClass('current');
            	
            	big_photo.parent().attr('href',  _this.children('span').eq(0).text());
            	big_photo.parent().attr('title', _this.attr('title'));
            	big_photo.attr('alt', _this.attr('title'));
            	
            	big_photo.fadeOut(150, function(){
	            	$(new Image()).load(function(){
	            		big_photo.fadeIn(150).attr('src', _this.attr('href'));
	            	}).attr('src', _this.attr('href'));
	            });
            }
            return false;
        });
    }
    
    // search
    var searched_phrase = $('#input_search_id');
    if(searched_phrase.length > 0){
    	var phrases = searched_phrase.val().split(" ");
    	var to_highlight = $('#offer .offerItem .description');
    	for(i in phrases){    		
    		phrases[i] = phrases[i].trim();    		
    		if(phrases[i].length > 0){
    			to_highlight.highlight(phrases[i]);
    		}
    	}    	
    }
    
    //tree menu: offers
    var lis = $('.wrapRealizacje #navSide ul.subSubNav li.first');
    if(lis.length > 0){
    	lis.click(function() {
			$(this).siblings('.rest').toggle('normal');
			return false;
		})
    }
    
});