//* outside values
var toSet = "";


//* on ready
$(document).ready(function(){

	//* hot news image & height    
        if($.trim($('.hnc-image').text()).length > 0)
        {
            
            var img = $.trim($('.hnc-image').text());
            
            var th = img.replace(/\.(jpg|png|gif)/i, '_204x125.$1');
            th = th.replace(/(.*)(\/)/i, '$1/thumb$2');
            
            $('.hnc-image').html('<a href="' + img + '" class="pirobox" title="&nbsp;"><img src="' + th + '" class="hot-news-image" width="204" height="125" alt="" /></a>');	
            
        }
        else
        {
            if($('#hotNewsFull').length > 0)
            {
                    
                    var img = $('#hotNewsFull img:first').parent().attr('href');

                    if(img != undefined)
                    {	
                            th = img.replace(/\.(jpg|png|gif)/i, '_204x125.$1');
                            th = th.replace(/(.*)(\/)/i, '$1/thumb$2');

                            $('.hnc-image').prepend('<a href="' + img + '" class="pirobox"><img src="' + th + '" class="hot-news-image" width="204" height="125" alt="" /></a>');	
                    }
            }
        }
        
	
	//** set hn height according to main page amount of content
	//$('.hot-news').height($('.menu-content').height());
	
        //** file li for knowledge
        $('.knowledge-doc li:first').addClass('first');
        $('.knowledge-doc').find('li').each(function(){
            
            $(this).css('background-image', $(this).find('a').css('background-image'));
            $(this).find('a').css('background-image', 'none');
            $(this).show();

        });
        //** opening text for knowledge
        if(window.location.href.search('baza_wiedzy.html') > 0)
        {
        }

	//* POSITION
	
	//** for chrome wait until all images will loaded
	$(window).load(function(){
	
		//** middle boxes (middle-top)	
		$( ".main .middle .middle-box .middle-img-container img" ).each(function() {
			
			
			$(this).css('width', $(this).width() + 'px').css('height', $(this).height() + 'px');
			
			$(this).position({
				of: $(this).parent(),
				my: "center middle",
				at: "center middle"
			});
	
		});
		
		//** pre-foot boxes
		$( ".main .pre-foot .pre-foot-box .pre-foot-img-container img" ).each(function() {
			
			$(this).css('width', $(this).width() + 'px').css('height', $(this).height() + 'px');
			
			$(this).position({
				of: $(this).parent(),
				my: "center middle",
				at: "center middle"
			});
		});	
		
	});	

	//** defuscate (mail protect)
	$(".main").find("p, div, span").each(function(){

		if($(this).find("object").size() == 0)
			$(this).defuscate();
	});
	
	
	setPiroboxClass('.menu-content-container-wrapper');
	setPiroboxClass('.news-details');
	//** autocolumn 
	
	///$('.menu-content-container-selected div').addClass("dontsplit"); 

	//$('.menu-content-container-wrapper').columnize({ columns: 2});
	
	//***************************************************
	var content = $('.menu-content-container-selected .menu-content-container-wrapper').html();

	var modules = new Array();
	var a = 0;
        
        

	
	$('.menu-content-container-selected .menu-content-container-wrapper .module').each(function(){

		var module = $(this).html();

		modules[a] = new Array(2);
		modules[a][0] = module;

		
		var opos = $(this).find('.module_position').val();
	
		
		
		if(opos != "")
		{
			if(opos == 'append' || opos == 'a')
			{
				modules[a][1] = 0;
				$(this).remove();

			}	
			else if(opos == 'here' || opos == 'h')
			{
				modules[a][1] = 2;
			}			
			else
			{
				modules[a][1] = 1;
				$(this).remove();
				
			}
		}
		else
		{
			
			var pos = content.lastIndexOf(module);
			var len = module.length;
			var pre = content.substring(0, pos).length;
			var app = content.substring(pos + len).length;
			
			
			if(pre > app)		
				modules[a][1] = 0;		
			else
				modules[a][1] = 1;			
		}

		a++;
	});
	
        
	content = $('.menu-content-container-selected .menu-content-container-wrapper').html();
	if(!content) content = "";


	if($.trim(content.replace(/<\/?[^>]+>/gi, '')).length > 10 && content.indexOf("map_") < 0)
		$('.menu-content-container-selected .menu-content-container-wrapper').html(jQuery('<div class="space">').html(content));
	else
	{
		if( content.indexOf("map_") >= 0)
                {
			$('.menu-content-container-selected .menu-content-container-wrapper').html('<div class="space">' + content + '</div>');
                        
                        
                }
                else
			$('.menu-content-container-selected .menu-content-container-wrapper').html('');
	}


	for (var i in modules)
	{
		if(modules[i][1] == 0)
			$('.menu-content-container-selected').append(modules[i][0]);		
		else if(modules[i][1] == 1)
			$('.menu-content-container-selected').prepend(modules[i][0]);	
	}



	//***************************************************
	
	//** garbage remove
	if($(".menu-content-container-selected .menu-content-container-wrapper").html() == "") 
		$(".menu-content-container-selected .menu-content-container-wrapper").remove();
	

	//** wiszace spojniki & hyphenate
	ws('.menu-content-container-selected');
        
        //** ikony w opening text dla bazy wiedzy i oferty
        $('#iconRecognize').addClass('opening-icon').css({'height': $('#iconRecognize').parent().find('.menu-content-container-wrapper').height() + 'px'});
	$('#iconRecognize img').vAlign();
	//$('.menu-content-container-selected').attr('lang', '{/literal}{$smarty.session.page.language}{literal}');
	
	//$('.menu-content-container-selected').addClass('hyphenate');
        
        if($('#Slideshow').length > 0)
        {
            var i = 0;
            $('#Slideshow').cycle({ 
                    fx:         'fade', 
                    speed:      400, 
                    timeout:    6000,            
                    fit:        1, 
                    pause:      1,
                    pagerEvent: 'mouseover',
                    before: function (currSlideElement, nextSlideElement, options, forwardFlag)
                    {    	    		    		    	
                            i = $(nextSlideElement).index();
                            
                            if(i == 0)
                            {
                                animateTo(0, 1000);
                                $('.scib-box-1').animate({backgroundColor: '#ffffff'}, 500);
                                $('.scib-box-3').animate({backgroundColor: '#f1f1f1'}, 500);
                                
                            }
                            else if(i == 1)
                            {
                                animateTo(1, 1000);
                                $('.scib-box-2').animate({backgroundColor: '#ffffff'}, 500);
                                $('.scib-box-1').animate({backgroundColor: '#f1f1f1'}, 500);
                            }
                            else if(i == 2)
                            {
                                animateTo(2, 1000);
                                $('.scib-box-3').animate({backgroundColor: '#ffffff'}, 500);
                                $('.scib-box-2').animate({backgroundColor: '#f1f1f1'}, 500);
                                
                            }
  
                    }
            });
            
            $('.scib-box-1').hover(
                function(){
                    $('#Slideshow').stop().cycle(0).cycle('pause');
                    animateTo(0, 800)
                    $(this).animate({backgroundColor: '#ffffff'}, 300); 
                    $('.sc-info-boxes .scib-box').not(this).animate({backgroundColor: '#f1f1f1'}, 300); 
                    return false;
                }, 
                function(){                   
                    //animateTo(i, 800);
                    ///$(this).animate({backgroundColor: '#fafafa'}, 300);
                    //whiteTo(i, 300);
                    $('#Slideshow').cycle('resume');
                }
            );
            $('.scib-box-2').hover(
                function(){
                    $('#Slideshow').stop().cycle(1).cycle('pause');
                    animateTo(1, 800)
                    $(this).animate({backgroundColor: '#ffffff'}, 300);
                    $('.sc-info-boxes .scib-box').not(this).animate({backgroundColor: '#f1f1f1'}, 300);
                    return false;
                }, 
                function(){                   
                    //animateTo(i, 800);
                    //$(this).animate({backgroundColor: '#fafafa'}, 300);
                    //whiteTo(i, 300);
                    $('#Slideshow').cycle('resume');
                }
            );
                
            $('.scib-box-3').hover(
                function(){
                    $('#Slideshow').stop().cycle(2).cycle('pause');
                    animateTo(2, 800)
                    $(this).animate({backgroundColor: '#ffffff'}, 300);
                    $('.sc-info-boxes .scib-box').not(this).animate({backgroundColor: '#fafafa'}, 300);
                    return false;
                }, 
                function(){                   
                    //animateTo(i, 800);
                    //$(this).animate({backgroundColor: '#fafafa'}, 300);
                    //whiteTo(i, 300);
                    $('#Slideshow').cycle('resume');
                }
            );
        }
        
        //** ah arrow
        if($('hr.arrow-right').length > 0)
        {
            
            $('hr.arrow-right').replaceWith('<img src="/i-hrarrow.gif" alt="" class="arrow-right" />');
            /*
            var har = $('hr.arrow-right');
            var hpos = har.position();
            
            alert(hpos.left + '-' + hpos.top);

            var arl = hpos.left + har.outerWidth() - 3;
            var art = hpos.top + parseInt(har.css('margin-top').replace('px', '')) - 4;



            $('<div class="arrow-right"></div>').css({'left': arl + 'px', 'top': art + 'px'}).appendTo(har.parent());
                */
        }
        
        //** min height issue removal
        
        
        if($(".menu-content-container-middle-top").length > 0)
        {
            var soPos = $(".menu-content-container-middle-top").position();

            if(soPos.top < 440)
                $(".menu-content-container-middle-top").css('margin-top', (440 - soPos.top) + 'px');
        }
        
        //** remove border from wide images
        $("img[src]").filter(function() {
            return this.src.match(/.*650x240.*/);
        }).css('border', '0px');
        
        //** offer products selection
        $('.offer-products .box').hover(function(){
           $(this).css({'background-color': '#f1f1f1'});

        }, function(){
            $(this).css({'background-color': '#ffffff'});
        });
	
	//** set pirobox gallery
	$().piroBox({
	      my_speed: 300, //animation speed
	      bg_alpha: 0.3, //background opacity
	      radius: 4, //caption rounded corner
	      scrollImage : false, // true == image follows the page _|_ false == image remains in the same open position
	                           // in some cases of very large images or long description could be useful.
	      slideShow : 'true', // true == slideshow on, false == slideshow off
	      slideSpeed : 3, //slideshow
	      pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
	      pirobox_prev : 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
	      close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox


	});	
	

	

});

function animateTo(lp, time)
{
    if(lp == 0)
        $('.s-triangle').stop().animate({'left': '98px'}, time);
    else if(lp == 1)
        $('.s-triangle').stop().animate({'left': '315px'}, time);
    else if(lp == 2)
        $('.s-triangle').stop().animate({'left': '532px'}, time);       

}

function whiteTo(lp, time)
{
    if(lp == 0)
        $('.scib-box-1').animate({backgroundColor: '#ffffff'}, time);
    else if(lp == 1)
        $('.scib-box-2').animate({backgroundColor: '#ffffff'}, time);
    else if(lp == 2)
        $('.scib-box-3').animate({backgroundColor: '#ffffff'}, time);  
    
}


