/* main entry point, called when the DOM is loaded */

var panosol_initialize = function() {
    // ie6 png transparency
    if ($.browser.msie && $.browser.version == '6.0') {
        DD_belatedPNG.fix('img', '*[style*=background-image:(/media/)]');
    }

    // remove link functionality from menu items that have children
    $('#main-navigation > li:has(ul) > a').click(function(e) { return false; });

    // cufon
    Cufon.replace('h2', { fontFamily: 'lubalin' });
    Cufon.replace('h3', { fontFamily: 'lubalin' });
    Cufon.replace('.product-content .avantages h4', { fontFamily: 'lubalin' });
    Cufon.replace('.block-contrat h3', { fontFamily: 'lubalin' });
    Cufon.replace('.block-contrat h4', { fontFamily: 'lubalin' });
    Cufon.replace('.avantages h4', { fontFamily: 'lubalin' });
    Cufon.replace('#header h1', { fontFamily: 'lubalin' });
    Cufon.replace('.who-are-we p', { fontFamily: 'lubalin' });
    Cufon.replace('.club-customer-comment', { fontFamily: 'desyrel' });
    
    var colorbox_opacity = 0.8;

    // lightboxes
    $(".colorbox").each(function() {
        $(this).colorbox({opacity: colorbox_opacity});
    });
    $(".colorbox-inline").each(function() {
        $(this).colorbox({opacity: colorbox_opacity, inline: true, href: $(this).attr('href') }, function() {});
    });
    $(".colorbox-auto-open").each(function() {
        $(this).wrap('<div style="display:none"/>');
        $(this).colorbox({
            opacity: colorbox_opacity,
            href: $(this).attr('href'),
            inline: true,
            open: true
        });
    });

    // blog rss entries
    var blog_rss = $('#content-right li.blog-rss ul');
    if (blog_rss.length) {
        $.get(blog_rss.attr('rel'), {}, function(response) {
            $.each(response, function(i, item) {
                blog_rss.append(
                    '<li><a href="' + item.link + '" target="blank" title="' + item.title + '">'
                    + item.title + '</span></a></li>'
                );
            });
        });
    }

    // study form

    // testimonials
    var testimonials = $('#content-right li.testimonials ul');
    if (testimonials.length) {
        testimonials.addClass('clearfix');
        $.get(testimonials.attr('rel'), {}, function(response) {
            $.each(response, function(i, item) {
                var img = '<img src="' + item.image + '"/>';
                testimonials.append(
                    '<li>' + img + '<h3><strong>' 
                    + item.first_name + '</strong></h3><div class="age-city">' 
                    + item.city + '</div><p>' + item.comment + '</p></li>' 
                );
            });
        });
    }

    $('.club-customer h3 a').click(function() {
        $(this).parent().parent().toggleClass($(this).attr('rel')).find('.show-hide').toggle();
        $(this).toggleClass('close');
    });

    // Flickr stream
    // http://api.flickr.com/services/feeds/photoset.gne?set=72157625253724970&nsid=50072109@N05&lang=fr-fr
    $('.flickr').jflickrfeed({
            limit: 9,
            qstrings: {
                set: "72157625253724970",
                id: "50072109@N05",
                lang: "fr-fr"
            },
            itemTemplate: 
                '<li>' +
                    '<a href="{{image}}" rel="photos"><img src="{{image_s}}" alt="{{title}}" /></a>' +
                '</li>'
        }, function(data) {
            $('.flickr li a[rel="photos"]').colorbox({
                opacity: colorbox_opacity,
                slideshow: true,
                slideshowAuto: false,
                current: 'photo {current} sur {total}',
                previous: 'précédente',
                next: 'suivante'
        });
    });
}

$(document).ready(function() {
    try {
        panosol_initialize();
    } catch (e) {
        if (typeof(console) != 'undefined') {
            console.log(e);
        }
    }
    if ($.browser.msie && $('.fixed-width-select').length) {
        var width = $('.fixed-width-select').css('width');
        $('.fixed-width-select').live('mousedown', function() {
            $(this).css('width', 'auto');
        });
        $('.fixed-width-select').live('blur', function() {
            $(this).css('width', width);
        });
        $('.fixed-width-select').change(function() {
            $(this).css('width', width);
        });
    }
});

