$(document).ready(function() {
    //courierJparallax();
    //homepage cycle
    $('#home-slides').cycle({ 
        fx:     'scrollHorz', 
        speed:  'fast', 
        timeout: 0,
        pagerAnchorBuilder: function(idx, slide) { 
            // return selector string for existing anchor 
            return '.slide-switch li:eq(' + idx + ')'; 
        }
    });
    $('.switch-link').click(function() {
        $(this).parents('li').siblings().find('.switch-link').removeClass('switch-active');
        $(this).addClass('switch-active');
    })
    //contact page
    $('.download-form').hover(function(){
        $(this).css('background-position', '0 -429px');
        $(this).children().css('background-position', '100% -429px');}, function() {
        $(this).css('background-position', '0 -391px');
        $(this).children().css('background-position', '100% -391px');
    });
    //padding for the page with image
    $('.image-page').each(function(){
        var width = $(this).prev().outerWidth();
        if (width > 0) {
            width = width + 20;
            $(this).css('padding-left', width + 'px');
        }
    });
    displaySettings();
    //ajax for services pages
    $('.service-title').click(function() {
        //if it's already open, just close it
        if ($(this).parents('.service-item').hasClass('service-active')) {
            $(this).parents('.service-item').removeClass('service-active');
            $(this).parent().siblings('.service-main').fadeOut('fast');
        } else {
            var cTitle = $(this).text();
            var cLink = $(this).attr('href')
            //parent container
            var licont = $(this).parents('.service-item').attr('id');
            //item number
            itemnum = licont.split('-');
            itemnum = itemnum[1];
            var item = $('#item-' + itemnum);
            if (item.length > 0) {
                //remove old box
                $('#' + licont).addClass('service-active').siblings().removeClass('service-active').children('.service-main').css('display', 'none');
                //display the box
                $('#item-' + itemnum).fadeIn('fast');
                //set title tag
                document.title = cTitle;
                displaySettings();     
            } else {
                // link id: link-x, parent container id: feature-x, place in container: item-x
                $.ajax({
                    url:        cLink + '&ajax=true',
                    dataType:   'html',
                    error:      function() {
                        window.location = cLink;                
                    },
                    success:    function(data) {
                        //remove old box
                        $('#' + licont).siblings().removeClass('service-active').children('.service-main').css('display', 'none');
                        //store it in a box
                        $('#' + licont).append('<div id="item-' + itemnum + '" style="display: none;" class="service-main" />').addClass('service-active');
                            //display the box
                        $('#item-' + itemnum).html(data).fadeIn('fast');
                        //set title tag
                        document.title = cTitle;
                        displaySettings();
                    }
                });
            }            
        }
        return false;
    });
});

function displaySettings() {
    $('.grid-item, .vertical-item').each(function() {
        if (!($(this).hasClass('fixed-padding'))) {    
            var vheight = $(this).height();
            var vcontent = $(this).children('.vertical').height();
            if (vheight > vcontent) {
                var toppadding = Math.floor((vheight - vcontent)/2);
                $(this).children('.vertical').css('padding-top', toppadding);
            }
        }
        //mark it as seen, need it for advanced dental tools with ajax loading - otherwise it calculates twice
        $(this).addClass('fixed-padding');
    });
    //rounded corners
    $('.grid-image-box, .image-box, .service-item, .service-image').each(function() {
        $(this).addClass('box-rounded');
        $(this).append('<span class="box-tl"></span><span class="box-tr"></span><span class="box-bl"></span><span class="box-br"></span>');
    });
}

//cufon replace
Cufon('.menu-link', {
        hover: {color: 'white'}
});
Cufon('.switch-link', {
        hover: {color: 'white'}
});
Cufon('.download-form', {
        hover: {color: '#569d03'}
});

Cufon.replace('.menu-link, .page-title, .footer-title, .account-order-link, .switch-span, .account-title, .contact-title, .download-span, .home-title', { fontFamily: 'TeXGyreHerosCondensed' });
Cufon.replace('.phone-message, .phone-number, .contact-phone', { fontFamily: 'TitilliumText22L Rg' });
