var bgnum = 0;
var imageMap = [];
var mapRatio = [
1.350438892640108,
1.3333333333333333,
1.5007012622720897,
2.002085505735141,
1.5003750937734435,
1.5026296018031555
];



function onBefore(){
    
    triggerNum(bgnum);
    bgnum+=1;
    if(bgnum>=6){
        bgnum = 0;
    }
}

function setCookie(c_name,value,exdays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
            return unescape(y);
        }
    }
}
function checkCookie(c_name)
{
    var c=getCookie(c_name);
    if (c!=null && c!="")
    {
        return true;
    }
    else
    {
        /*c=prompt("Please enter your name:","");
  if (c!=null && c!="")
    {*/
        return false;
    //}
    }
}


function triggerNum (id){
    
    $('#bg_containers').cycle('pause');
    $('#red-box-text').children('.red-box-title').html($('.bg-title')[id].innerHTML);
    $('#red-box-text').children('.red-box-content').html($('.bg-content')[id].innerHTML);

    $('.sqrt').removeClass('selected');
    $('#p'+(id+1)).addClass('selected');

    $('#bg_containers').cycle('resume');
}

var sqrtClick = function sqrtClick(ev){
    
    $('#bg_containers').cycle('pause');
    
    var id = parseInt(this.id.substr(1))-1;
    triggerNum(id);

    bgnum = id;
    $('#bg_containers').cycle(id);
    
    return false;
}
var sqrtOver = function sqrtOver(ev){

    if(cycleOpt===true){
        $('#bg_containers').cycle('toggle');
    }

}
var sqrtOut = function sqrtOut(ev){

    if(cycleOpt===true){
        $('#bg_containers').cycle('toggle');
    }

}
var cycle = null;
var cycleOpt = true;
$(document).ready(function() {

    $('li.item a, li.subitem a').each(
        function(index,element){

            var r = new RegExp("^"+element.href+"$");
            var isroot = false;

            if(r.test(document.URL)){
                isroot = $(element).parent().hasClass('item');
                if(!isroot){
                    $('item').removeClass('item-selected');
                    $(element).parent().parent().parent().addClass('item-selected');
                }else{
                    $('item').removeClass('item-selected');
                    $(element).parent().addClass('item-selected');
                }
                
            }
            

        }
        );


    $('#files').bind(
        'load',
        function(ev){
            
            if(/files\-all/.test(this.src)!=true){
                $(this).attr({
                    //src: 'files-all.html'
                    });
            }
        }
        );
    $('.pdf_list_item p img').each(
        function(index, element){

            if(element.alt.length>0){
                var content = '<a href="files-'+element.alt+'.html" target="files">'+
                '<img src="/layout/ico_pdf.gif" alt="_" />'+
                '</a>';
                var content2 = '<a href="files-'+element.alt+'.html" target="files">'+
                $(element).parent().children('span:last').text()+
                '</a>';
                $(element).parent().children('span:last').replaceWith(content2);
                $(element).replaceWith(content);
            }
        }
        );

    $('.sqrt').bind('click',sqrtClick);
    $('.sqrt').hover(sqrtOver, sqrtOut);

    if(cycleOpt===true){
        
        cycle = $('#bg_containers').cycle({
            fx: 'fade',
            fit: false,
            before: onBefore,
            startingSlide: 0,
            speed: 3000,
            speedIn: 1200,
            speedOut: 900,
            delay: 4000
        }
        );
    }else{

        $('#bg_containers').cycle('stop');
        $('#red-box').remove();
    }
    $('.item > a').hover(
        function(){
            if(!$(this).parent().hasClass('item-selected')){
                $(this).stop(true,true)
                .animate({
                    backgroundColor: '#b81021',
                    backgroundPosition: '0px -70px'
                },450);
            }
        },
        function(){
            if(!$(this).parent().hasClass('item-selected')){
                $(this).stop(true,true)
                .animate({
                    backgroundColor: '#000000',
                    backgroundPosition: '0px 20px'
                });
            }
        });

    $('a.disable').css({
        textDecoration:'none',
        cursor: 'default'
    }).click(function(ev){
        ev.preventDefault()
    });

    $('.subitem').hover(
        function(){
            if(!$(this).hasClass('subitem-selected') && !(this).hasClass('desub')){
                $(this).stop(true,true)
                .animate({
                    backgroundColor: '#b0091a',
                    backgroundPosition: 'center -70px'
                },450);
            }
        },
        function(){
            if(!$(this).hasClass('subitem-selected') && !(this).hasClass('desub')){
                $(this).stop(true,true)
                .animate({
                    backgroundColor: '#b81021',
                    backgroundPosition: 'center bottom'
                });
            }
        });

    $('span.email').each(function(index, option){

        var email = this.innerHTML.replace(/\s/,'').split(':');
            
        $(this).html(email[0]+': <a href="mailto:'+email[1]+'" >'+email[1]+'</a>');
    });
    $('span.www').each(function(index, option){

        $(this).html('<a title="'+this.innerHTML+'" href="http://'+this.innerHTML+'" target="_blank" >'+this.innerHTML+'</a>');
    });

    leftPipes();
    
    $('#word').bind({
        focus:function(){
            this.value = '';
        },
        blur: function(){
            if(this.innerHTML.length<1){
                this.value = 'WYSZUKAJ';
            }
        }
    });

    $('a[href="#"]').bind('click',function(ev){
        ev.preventDefault();
        $(this).css({
            cursor: 'default'
        });
    });

    $('#fSend').click(function(ev){
        ev.preventDefault();
        
        var form = this.form;
        var valid = true;
        var vE= {};

        if(form.elements.imie_nazwisko.value.length < 4){
            valid =false;
            vE['imie_nazwisko'] = form.elements.imie_nazwisko;
            vE['m_imie_nazwisko'] = 'Podaj imię i nazwisko';
        }

        if(form.elements.enquiry.value.length < 4 ){
            valid =false;
            vE['enquiry'] = form.email;
            vE['m_enquiry'] = 'Wpisz treść wiadomości';
        }

        if(form.elements.email.value.length < 4 || /([^@]{4,})@([^@]{4,})/i.test(form.elements.email.value)!==true){
            valid =false;
            vE['email'] = form.email;
            vE['m_email'] = 'Podaj email';
        }

        var i =0;
        var l =form.elements.length;

        for(i;i<l;i+=1){
            
            $(form.elements[i]).css({
                color:''
            });
        }
        i=0;
        $('#erm').html('');
        for(i;i<l;i+=1){


            if(vE[form.elements[i].name]){
                

                $(form.elements[i]).css({
                    color:'red'
                });
                
                $('#erm').append('<span>'+vE['m_'+form.elements[i].name]+'</span><br />');
            }

        }

        if(valid===true){
            form.submit();
        }
    });

    $("a.target_preview_a").fancybox();
    $('.target_preview_image').bind(
        'click',
        function(){

            var target = $('#target_preview');
            var a = $("a.target_preview_a");
            var t = this;

            $('html,body').stop(true,true).animate({
                scrollTop: ($($('.target_p')[0]).offset().top-10)+'px'
            }, {
                duration: 'slow',
                easing: 'swing'
            });

            $('.target_p').height((target.outerHeight())+'px');
            $('.target_p').width((target.outerWidth())+'px');
            
            target.fadeOut(250, function(){
                
                var src = t.alt.replace(/\?[0-9]{1,}$/,'');
                a.attr({
                    href: src
                });
                
                $(this).attr({
                    src: src+'?v='+new Date().getTime()
                }).bind('load',function(){
                    
                    $(this).fadeIn();
                    $('.target_p').height('');
                    $('.target_p').width('');
                    leftPipes();
                });
                 
            });
            
          
            
        }
        );

        
    bodyHeight();
    leftPipes();
    
    $('#bg_containers img').each(function(index, element){
        var im = new Image();
        im.src = element.src;
        var prop = 1;
        
        /*
        im.onload = function(){
            prop = im.width / im.height;
            console.log(prop);
        };
*/
        imageMap[index] = {
            dm: element,
            n:im,
            p: mapRatio[index]
        };
        element.id = "im_"+index;
    });

    $('#bg_containers img').bind('rest', function(ev){

        var th = $(this).height();
        var id = this.id.replace(/[im_]{1,}/,'');
        var ob = imageMap[id];
        var h = $('#bg_containers').height();
        var tw = $('#bg_containers').width();
        var w = h*ob.p;

        if(w < tw){
            p = tw/w;
            h = h*p;
            w = tw;
        }
        
        $(this).height(h);
        $(this).width(w);

    //console.log();

    });
    $('#bg_containers img').trigger('rest');
});

var leftPipes = function (){

    $('.content_right').height(($('.home_content').outerHeight()+0)+'px');
    $('.content_left').height(($('.home_content').outerHeight()+0)+'px');
}

var bodyHeight = function bodyHeight(){

    

    if(cycleOpt==false){
        
        var h = $('.home_content').height()+140;
        
        if( $('body').height()>=h){
            
            h = $('body').outerHeight()+10;
        }
        
        $('#body').css({

            height: (h)+'px'
        });
    }
    
}

var wtrig = function wtrigg(ev){

    leftPipes();

    bodyHeight();
    
    $('#bottom-pipe').stop(true,true).css({
        // top: ($(this).scrollTop()+$(window).height()-53)+'px'
        });

    $('#bg_containers img').trigger('rest');
}

$(window)
    .resize(
        wtrig
        );

$(document)
    .scroll(
        function(ev){

            leftPipes();
            bodyHeight();
            
            $('#bottom-pipe').stop(true,true).css({
                //top: ($(this).scrollTop()+$(window).height()-52)+'px'
                });
        }
        );
