var cornerSize = '5px';
$(document).ready(function(){

    jQuery('#carousel').jcarousel({
        scroll: 1,
        auto: 8,
        wrap: 'circular'
    });

    /**                            CORNERS                                    */
    $('#login, #c-infos h2, #sec-menu .title, #sub-prices h3').corner(cornerSize);
    $('#tab td:not(".placeholder")').corner('5px')
    
    if($.browser.msie){
        $('.search-input').corner(cornerSize);
    } else {
        $('.search-input input').corner(cornerSize);
    }

    /**                            CUFON                                      */
    $('#menu, #home-news h1, #lc-box .main, #sub-menu a, #news h1').each(function(){
        Cufon.replace(this, {
            hover: true,
            fontFamily: 'cg'
        });
        $(this).css({
            visibility: "visible"
        });
    });

    $('#home-news h2, #c-infos h2, #login h2, #body h1, #body h2, #news h2, .formbuilder .submit input, #sub-prices h3, #tab h3, #tab h4, #tab .button a').each(function(){
        Cufon.replace(this, {
            hover: true,
            fontFamily: 'garag'
        });
        $(this).css({
            visibility: "visible"
        });
    });

    /**                           SEARCH FORM                                 */
    $('.search-input input').bind({
        focus: function(){
            $(this).addClass('focus');
        },

        blur: function(){
            $(this).removeClass('focus');
        }
    })

    /**                      HOMEPAGE NEWS LAST BOX                           */
    $('#home-news .last').find('a:last').addClass('bottom');

    /**                           SUB MENU                                    */
    $('#sub-menu .child').bind({
        mouseover: function (){
            $(this).css('cursor','pointer');
            $(this).addClass('hover');
            Cufon.refresh();
        },

        mouseleave: function(){
            $(this).removeClass('hover');
            Cufon.refresh();
        },

        click: function(){
            $(location).attr('href', $(this).find('a').attr('href'));
        }
    })

    /**                             BOTTOM MENU                               */
    var bmDuration = 150;
    $("#sec-menu .title").bind({
        mouseenter: function(){
            $(this).css('cursor','pointer').animate({
                backgroundColor: '#c81a21'
            }, bmDuration);
            $(this).find('a').css({
                'color':'white'
            });
        },

        mouseleave: function(){
            $(this).animate({
                backgroundColor: '#27272B'
            }, bmDuration);
            $(this).find('a').css({
                'color':oFontColor
            });
        },

        click: function(){
            $(location).attr('href', $(this).find('a:first').attr('href'));
        }
    })

    /*                             LOGIN BOX                                  */
    $("#login").bind({
        mouseenter: function(){
            $(this).css('cursor','pointer').animate({
                backgroundColor: '#00AEEF'
            }, 200);
        },

        mouseleave: function(){
            $(this).animate({
                backgroundColor: '#ED1C24'
            }, 200);
        },

        click: function(){
            $(location).attr('href', $(this).find('a:first').attr('href'));
        }
    })

    /**                             CONTACT BOX                               */
    $(".h-blue").bind({
        mouseenter: function(){
            $(this).css('cursor','pointer').animate({
                backgroundColor: '#c81a21'
            }, 200);
        },

        mouseleave: function(){
            $(this).animate({
                backgroundColor: '#00AEEF'
            }, 200);
        },

        click: function(){
            $(location).attr('href', $(this).find('a:first').attr('href'));
        }
    })

    /**                             SEARCH FORM                               */

    $('.search-button input').bind({
        mouseover: function (){
            $(this).css('cursor','pointer').addClass('hover');
        },

        mouseleave: function(){
            $(this).removeClass('hover');
        }
    })

    /**                           NEWS NAVIGATION                             */
    $('#n-next, #n-prev, #news-detail .back').bind({
        mouseenter: function(){
            $(this).css('cursor','pointer').addClass('hover');
        },

        mouseleave: function(){
            $(this).removeClass('hover');
        },

        click: function(event){
            event.preventDefault();
            $(location).attr('href', $(this).find('a').attr('href'));
        }
    })

    /**                          NEWS BOX EVENTS                              */
    var oColor = $('#box-content').css('backgroundColor');
    var aDuration = 100;

    $('#newslist .item, #home-news .news').css('backgroundColor',oColor).corner('5px').bind({
        mouseenter: function(){
            $(this).css('cursor','pointer').animate({
                backgroundColor: '#e4e4e4'
            }, aDuration );
        //$(this).find('div').css('text-decoration','underline');
        },

        mouseleave: function(){
            $(this).animate({
                backgroundColor: '#F2F1EF'
            }, aDuration );
        //$(this).find('div').css('text-decoration','none');
        },

        click: function(event){
            $(location).attr('href', $(this).find('a').attr('href'));
        }
    })

    /**                            NEWS LAST BOX EVENTS                       */
    $('#home-news .last').bind({
        mouseenter: function(){
            $(this).find('h2').css({
                'background-image': 'url(images/home_news_arrow_hover.png)'
            });
            $(this).css('cursor','pointer');
        },

        mouseleave: function(){
            $(this).find('h2').css({
                'background-image': 'url(images/home_news_arrow.png)'
            });
        },

        click: function(event){
            event.preventDefault();
            $(location).attr('href', $(this).find('a:first').attr('href'));
        }

    })

    /**                              FAQ EVENTS                               */
    $("#faq > h3:not(active)").bind({
        mouseover: function(){
            $(this).css({
                'cursor':'pointer'
            });
        },

        click: function(){
            $('#faq .answer').slideUp('normal');
            $('#faq > h3').removeClass('active');
            $(this).next('.answer').slideDown('normal', submenuBorder);
            $(this).addClass('active');
        }
    })

        /**                           OBJEDNAVKA FORM                             */
        if($.browser.msie){
            $('.formbuilder > div').corner(cornerSize);
        } else {
            $('.formbuilder div input, .formbuilder div textarea').corner(cornerSize);
        }

        if ($('.formbuilder select').size() > 0) $('.formbuilder select').sSelect();

        $('.formbuilder div input, .formbuilder div textarea').each( function(cnt, el){
            $(this).example( $(this).siblings('label').text(), { className: 'c_silver' });
        })

        $(".formbuilder .submit input").bind({
            mouseenter: function(){
                $(this).css('cursor','pointer').animate({
                    backgroundColor: '#c81a21'
                }, bmDuration);
            },

            mouseleave: function(){
                $(this).animate({
                    backgroundColor: '#00AEEF'
                }, bmDuration);

            }
        });

        if ( $('.formbuilder').size() > 0 ){
            var sPack = $.url.attr('anchor');
            if ( sPack ){
                var $text = $('.formbuilder select option').eq( sPack ).text();
                $('.formbuilder select').val( sPack );
                $('.selectedTxt').text( $text );
                $('.newList li a').removeClass('hiLite');
                $('.newList li').eq( sPack ).find('a').addClass('hiLite');
            }
        }

    /**                            CENNIK                                     */
    $('#tab td:not(".placeholder")').bind({
        mouseover: function(){
            $(this).css('cursor','pointer');
            $(this).find('a').css({
                backgroundImage: 'url(images/arrow_right_hover.png)'
            });
        },
        mouseleave: function(){
            $(this).find('a').css({
                backgroundImage: 'url(images/arrow_right.png)'
            });
        },
        click: function(){
            $(location).attr('href', $(this).find('a').attr('href'));
        }
    })

})

function submenuBorder(){
    $('#sub-menu-border').height($('#box-content').height());
}

$(window).bind('load', function(){
    /**                            SUBMENU BORDER                             */
    submenuBorder();

    /**                           TWO BLOCKS BORDER                           */
    $('.center-line').each(function(){
        var parentBlock = $(this).parents('.two-blocks');
        var fHeight = parentBlock.find('.first').height();
        var sHeight = parentBlock.find('.second').height();
        var mHeight = Math.max(fHeight, sHeight);

        $(this).height( mHeight );
    })

    /**                         SOLVE PRICES BOXES HEIGHT                     */
    var mHeight = 0;
    $('.td-wrapper').each(function(){
        mHeight = ( mHeight < $(this).height() ) ? $(this).height() : mHeight;
    })
    $('.td-wrapper').height( mHeight );

})