$(document).ready(function(){    
    $('#NewsLeft a').addClass('out');
    jQuery('#NewsCarousel, .jcarousel-list').jcarousel();
    
    Cufon.replace('.menu ul li', {
        textShadow: '1px 1px #202020',
        fontFamily: 'Segoe UI Semibold'
    });
    
    Cufon.replace('.page_block h1', {
        color: '#000000',
        fontFamily: 'Segoe UI'
    });
    
    Cufon.replace('#NewsArchiwum h3, #CarsList h3', {
        color: '#000000',
        fontFamily: 'Segoe UI'
    });
    
    Cufon.replace('.left_column_news h2, .left_column_cars h2', {
        color: '#ffffff',
        fontFamily: 'Segoe UI'
    });
    
    Cufon.replace('.page_block h2, .page_block h3, .page_block h4, .page_block h5', {
        color: '#03137f',
        fontFamily: 'Segoe UI Semibold'
    });
    
    Cufon.replace('.page_block h1 span', {
        color: '#03137f',
        fontFamily: 'Segoe UI Semibold'
    });
    
    Cufon.replace('h2.news_head, #NewsPage h2', {
        color: '#03137f',
        fontFamily: 'Typodermic'
    });
    
    Cufon.replace('#NewsCarousel h2, .zajawka h1', {
        color: '#ffffff',
        fontFamily: 'Typodermic'
    });
    
    Cufon.replace('.zajawka h2', {
        color: '#ffffff',
        fontFamily: 'Kims Hand'
    });
    
    $('#CarPageGallery_Thumb').jScrollPane(
        {
            horizontalDragMinWidth: 150,
            horizontalDragMaxWidth: 150
        }
    );
        
    if($('#NewsRight').is('div')) {
        $('#NewsRight img:last').load(function() {
            var height = $('#NewsRight img').height();
            height += $('#NewsRight img').next('img').height();
            height += $('#NewsRight img').next('img').next('img').height();
            height += $('#NewsRight img').next('img').next('img').next('img').height();
            height += $('#NewsRight img').next('img').next('img').next('img').next('img').height();
            height += $('#NewsRight img').next('img').next('img').next('img').next('img').next('img').height();
            $('#NewsRightPhoto').css('height', (height+20));
            $('#NewsRight').jScrollPane(
                {
                    verticalDragMinHeight: 150,
                    verticalDragMaxHeight: 150
                }
            );
        });
    }
    
    $('#NewsLeft').jScrollPane(
        {
            verticalDragMinHeight: 150,
            verticalDragMaxHeight: 150
        }
    );
    
    $('#NP_link').click(function() {
        $('#Zajawka').show();
        $('#NP').hide().animate();
        $('#NB').show().animate();
    });
    
    $('#NB_link').click(function() {
        $('#Zajawka').hide();
        $('#NB').hide().animate();
        $('#NP').show().animate();
        
    });
    
    $('.CarColor_ViewPhoto').hide();
    $('#CarColor_0_select').fadeIn(1000);
    
    $('.CarColor_Select ul li a').click(function() {
        $('.CarColor_ViewPhoto').hide();
        var id = $(this).attr('id');
        $('#'+id+'_select').fadeIn(1000);
        return false;
    });
    
    $('#CarPageTab_Select li a').click(function() {
        $('#CarPageTab_Select li a').removeClass('hover');
        $(this).addClass('hover');
        
        $('.TabContent').removeClass('visible');
        var id = $(this).parent().attr('id');
        $('#'+id+'_content').addClass('visible');
    });
    
    $('#PrintPage img').click(function() {
        $('#background').attr('class', '');
        $('body, .background').css('background', 'white')
        $('.top').hide();
        $('#CarPageTab_Content ul li').show();
        $('#CarPageTab_Select, .left_column_cars').hide();
        return false;
    });
    
    $('#CarPageGallery_Thumb ul li a').click(function() {
        var file = $(this).children('img').attr('src').slice(23);
        $('#CarPageGallery_Big').css('background', 'url("/image-view/car/724x362/'+file+'") no-repeat center');
        return false;
    });
    
    if($('.shadow').is('div')) {
        var bkgImgs = ([
            '',
            '/template/images/bg_priora.jpg',
            '/template/images/bg_kalina.jpg',
            '/template/images/bg_44.jpg'
         ]);
         
         var defbg = $.cookie('background');

         if($('#Zajawka').is('ul')) {
            var max = $('#Zajawka li:last').attr('id').slice(8);
            var i = 0;
            var car = null;
            var newpic;
            
            next = function(){
                if(i>max) i = 0;
                if(defbg != null) {
                    car = defbg;
                    i = $('#Zajawka li.car_'+defbg).attr('id').slice(8)
                    newpic = bkgImgs[car];
                } else {
                    car = $('#Zajawka li#Zajawka_'+i).attr('class').slice(4);
                    newpic = bkgImgs[car];
                }

                $('.shadow').animate({opacity: 1.0}, 2500, function() {
                    $('body').css('background', 'url("'+newpic+'") no-repeat top center');
                    i++;
                });

                var prev = i-1;
                if (prev < 0) prev = max;
                
                $.cookie('background', car);

                if(defbg == null) $('#Zajawka_'+prev).fadeOut(1250);
                $('.shadow').animate({opacity: 0.0}, 2500);
                $('#Zajawka_'+i).fadeIn(1250);


                $('.shadow').css('background', 'url("'+newpic+'") no-repeat top center');

                if(defbg != null) defbg = null;
                setTimeout( function(){next()}, 12000 );
             };
        } else {
            var max = 3;
            var i = 1;
            
            if(defbg != null) i = defbg;
            
            next = function() {
                if(i>max) i =1;
                var newpic = bkgImgs[i];
                $.cookie('background', i);
                $('.shadow').animate({opacity: 1.0}, 2500, function() {
                    $('body').css('background', 'url("'+newpic+'") no-repeat top center');
                    i++;
                });

                var prev = i-1;
                if (prev < 1) prev = max;

                $('.shadow').animate({opacity: 0.0}, 2500);
                $('.shadow').css('background', 'url("'+newpic+'") no-repeat top center');
                
                setTimeout( function(){next()}, 12000 );
            };
        }
         next();
    }
     
     // add a "rel" attrib if Opera 7+
    if(window.opera) {
            if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
                    $("a.jqbookmark").attr("rel","sidebar");
            }
    }

    $("a.jqbookmark").click(function(event){
            event.preventDefault(); // prevent the anchor tag from sending the user off to the link
            var url = this.href;
            var title = this.title;

            if (window.sidebar) { // Mozilla Firefox Bookmark
                    window.sidebar.addPanel(title, url,"");
            } else if( window.external ) { // IE Favorite
                    window.external.AddFavorite( url, title);
            } else if(window.opera) { // Opera 7+
                    return false; // do nothing - the rel="sidebar" should do the trick
            } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
                     alert('Unfortunately, this browser does not support the requested action,'
                     + ' please bookmark this page manually.');
            }

    });

    $('.top_navigation img').ifixpng();

    $('a.out').click(function() {
        window.open(this.href);
        return false;
    });
    
    $('ul.menu_ico li a img').hover(function() {
        $(this).parent().children('span').fadeIn(1000);
    }, function() {
        $(this).parent().children('span').fadeOut(1000);
    });
    
    $('.car_table tr td:first-child').addClass('first');
    $('.car_table tr:odd').addClass('line_1');
    $('.car_table tr:even').addClass('line_2');
        
});
