Cufon.set('fontFamily', 'Gnuolane Free');

Cufon.replace('.cufon').replace('#subnav li a,#footer .footer-links a',{
    hover:true
});

Cufon.replace('.cufon').replace('#tooltip_list li a',{
    hover:true
});

$().ready(function(){
    cssFix();
    imgRounder();
    jqtransform();
    tooltip();
    sgallery();
    logoslider();
    customer();

    $('div.thumbnail').live('click', function() {
        window.location = $(this).parent().next().children('a:first').attr('href');
    })

    $('div.installations .block-gallery .more .rounded_wrapper').live('click', function() {
        window.location = $(this).parent('a').attr('href');
    //alert($(this).parent('a').attr('href'));
    });

    $('div.installations .block-gallery > a .rounded_wrapper').live('click', function() {        
        window.location = $(this).parent('a').attr('href');
    //alert($(this).parent('a').attr('href'));
    });

    $('a.fancybox').fancybox();
    $('.external').attr('target', 'blank');
    $('a.submit').click(function() {
        $(this).parents('form').submit();
    });    
});

function logoslider(){
    var current = 0;
    var min = 0;
    var max = $('#logoslider .holder img').length -1;
    $('#logoslider .prev').hover(function(){
        //console.log(min);
        //console.log(current);
        $('#logoslider .holder img').animate({
            left:'+=3000'
        },14000);
    },function(){
        $('#logoslider .holder img').stop()
    });
    $('#logoslider .next').hover(function(){
        $('#logoslider .holder img').animate({
            left:'-=3000'
        },14000);
    },function(){
        $('#logoslider .holder img').stop()
    });
}

function sgallery(){
    $('.sgallery .sgallery-big > a:gt(0)').hide();
    $('.sgallery .sgallery-small > a').click(function(){
        var index = $('.sgallery .sgallery-small > a').index($(this));
        $('.sgallery .sgallery-big > a').hide().eq(index).show();
        return false;
    });
}
function cssFix(){
    $('#subnav li a:last').css('border-style','none');
    $('#sidebar .block:first h3').css('margin-top', '10px');
    $('#sidebar .block-gallery .more a:nth-child(3n)').css('margin-right','0px');
    $('.sgallery .sgallery-small a:nth-child(3n)').css('margin-right','0px');
//$('#main .product-preview:odd').css('margin-right','0px');
//$('#mslider .imagelist a:nth-child(6n)').css('margin-right','0px');

}
function tooltip(){
    $('.block-gallery a img, .sgallery img').tooltip({
        track:true,
        delay: 0,
        fade:250 ,
        showURL :false,
        bodyHandler: function() {
            return $(this).attr("alt");
        }
    });
}
function jqtransform(){
    $('#quick-browser, #form-product-finder').jqTransform();
}
function imgRounder(){
    $('img.rounded').one('load',function () {
        var img = $(this);
        var img_width = img.width();
        var img_height = img.height();

        // build wrapper
        var wrapper = $('<div class="rounded_wrapper"></div>');
        wrapper.width(img_width);
        wrapper.height(img_height);

        // move CSS properties from img to wrapper
        wrapper.css('float', img.css('float'));
        img.css('float', 'none')

        wrapper.css('margin-right', img.css('margin-right'));
        img.css('margin-right', '0')

        wrapper.css('margin-left', img.css('margin-left'));
        img.css('margin-left', '0')

        wrapper.css('margin-bottom', img.css('margin-bottom'));
        img.css('margin-bottom', '0')

        wrapper.css('margin-top', img.css('margin-top'));
        img.css('margin-top', '0')

        wrapper.css('display', 'block');
        img.css('display', 'block')

        // IE6 fix (when image height or width is odd)
        if ($.browser.msie && $.browser.version == '6.0')
        {
            if(img_width % 2 != 0)
            {
                wrapper.addClass('ie6_width')
            }
            if(img_height % 2 != 0)
            {
                wrapper.addClass('ie6_height')
            }
        }

        // wrap image
        img.wrap(wrapper);

        // add rounded corners
        img.after('<div class="tl"></div>');
        img.after('<div class="tr"></div>');
        img.after('<div class="bl"></div>');
        img.after('<div class="br"></div>');
    }).each(function(){
        if(this.complete) $(this).trigger("load");
    });
}

function customer() {
    $("#customer div.jqTransformSelectWrapper ul li a").click(function(){
        var value = $("#customer div.jqTransformSelectWrapper span").attr('data-value');
        $('div.customer').hide();
        if (value == '')
            $('div.customer').fadeIn();
        else
            $('#customer-' + value).fadeIn();
        return false; //prevent default browser action
    });

}

function rpiBlockUI() {
    $.blockUI({
        css: {
            padding: '20px 45px',
            color: '#fff',
            'font-style': 'italic',
            'font-weight': 'bold',
            'background-color': '#006bb3',
            '-moz-border-radius': '6px',
            '-webkit-border-radius': '6px',
            'border-radius': '6px',
            'top':            '43%' 
        },
        message: 'Loading...',
        overlayCSS:  {
            backgroundColor: '#fff'
        //opacity:         0.6
        }
    });

}

function delayExecutionForIE() {
    if ( typeof document.all == "object" &&
        (document.readyState != "loaded"
            && document.readyState != "complete")
        ) {
        try {
            //If IE is used, use the trick by Diego Perini
            //http://javascript.nwbox.com/IEContentLoaded/
            document.documentElement.doScroll("left");
            rpiBlockUI();
        } catch(error) {
            setTimeout(delayExecutionForIE, 200);
        }
    } else {
        rpiBlockUI();
    }
}

