$(document).ready(function () {

    // ColorBox
    $('.colorbox a, a.colorbox').colorbox({
        current: "{current} of {total}",
        initialHeight: '150px',
        initialWidth: '150px',
        maxHeight: '95%',
        maxWidth: '95%',
        opacity: 0.25,
        speed: 250,
        transition: "elastic"
    });

    // Set up placeholder support for older browsers
    // Check if browser supports HTML5 input placeholder
    function supports_input_placeholder() {
        var i = document.createElement('input');
        return 'placeholder' in i;
    }

    // Change input text on focus
    if (!supports_input_placeholder()) {
        $(':text').focus(function () {
            var self = $(this);
            if (self.val() == self.attr('placeholder')) self.val('');
        }).blur(function () {
            var self = $(this), value = $.trim(self.val());
            //if (val == '') self.val(self.attr('placeholder'));
        });
    } else {

    }

    $('div.news_engineer img').parent('a').colorbox({
        current: "{current} of {total}",
        initialHeight: '150px',
        initialWidth: '150px',
        maxHeight: '90%',
        maxWidth: '950px',
        opacity: 0.25,
        speed: 250,
        transition: "elastic"
    });

    // Drop-down menus
    leave_rollover_open = 'no';
    $('div.header ul.navigation li ul').css({
        'left': '0',
        'top': '34px'
    });
    $('div.header ul.navigation li ul').hide();
    $('div.header ul.navigation li').hover(function () {
        $(this).children('ul').fadeIn(0);
        if ($(this).find('ul').length != 0) {
            leave_rollover_open = 'yes';
        }
    }, function () {
        $(this).children('ul').fadeOut(0);
        if ($(this).find('li').length != 0) {
            $('div.header ul.navigation img').fadeTo(0, 1);
            leave_rollover_open = 'no';
        }
    });

    // Home page associations
    //$('div.main_area_home_page li.associations img.association_2').everyTime(1500, function() {
    //	$(this).fadeTo(1500, 0);
    //	$(this).fadeTo(1500, 0);
    //	$(this).fadeTo(1500, 1);
    //	$(this).fadeTo(1500, 1);
    //});

    // Remove focus outline on click
    $('a, input').mousedown(function () {
        this.blur();
        this.hideFocus = true;
        this.style.outline = 'none';
    });

    // Show bubble next to Facebook 'Like' button even when zero
    $("span.fb_share_no_count").each(function () {
        $(this).removeClass("fb_share_no_count");
        $(".fb_share_count_inner", this).html("0");
    });

    // Image transition rollovers
    rollover_triggered = 'no';
    $('img.rollover_transition, img.rollover_transition_slow, .rollover_transition img, .rollover_transition_slow img').parent('a').focus(function () {
        if ($(this).hasClass('rollover_transition_slow') || $(this).parents().hasClass('rollover_transition_slow')) {
            var fade_speed = 0;
        }
        else {
            var fade_speed = 0;
        }
        var source_image = $(this).children('img').attr('src');
        var rollover_image = source_image.replace(/\_off./gi, '_on.');
        $(this).css({
            'background': 'url("' + rollover_image + '") no-repeat',
            'display': 'block'
        });
        $(this).children('img').stop().fadeTo(fade_speed, 0);
        rollover_triggered = 'yes';
    });
    $('img.rollover_transition, img.rollover_transition_slow, .rollover_transition img, .rollover_transition_slow img').parent('a').blur(function () {
        $(this).children('img').stop().fadeTo(0, 1);
        rollover_triggered = 'no';
    });
    $('img.rollover_transition, img.rollover_transition_slow, .rollover_transition img, .rollover_transition_slow img').hover(function () {
        if ($(this).hasClass('rollover_transition_slow') || $(this).parents().hasClass('rollover_transition_slow')) {
            var fade_speed = 0;
        }
        else {
            var fade_speed = 0;
        }
        var source_image = $(this).attr('src');
        var rollover_image = source_image.replace(/\_off./gi, '_on.');
        $(this).parent('a').css({
            'background': 'url("' + rollover_image + '") no-repeat',
            'display': 'block'
        });
        $(this).stop().fadeTo(fade_speed, 0);
    }, function () {
        if (rollover_triggered != 'yes' && leave_rollover_open != 'yes') {
            $(this).stop().fadeTo(0, 1);
        }
    });

    // Instant rollovers
    $('.rollover_instant img, .rollover_instant input:image, img.rollover_instant, input.rollover_instant:image').parent('a').focus(function () {
        $(this).children('img').attr('src', $(this).children('img').attr('src').replace(/\_off./gi, '_on.'));
        rollover_triggered = 'yes';
    });
    $('.rollover_instant img, .rollover_instant input:image, img.rollover_instant, input.rollover_instant:image').parent('a').blur(function () {
        $(this).children('img').attr('src', $(this).children('img').attr('src').replace(/\_on./gi, '_off.'));
        rollover_triggered = 'no';
    });
    $('.rollover_instant img, .rollover_instant input:image, img.rollover_instant, input.rollover_instant:image').hover(function () {
        $(this).attr('src', $(this).attr('src').replace(/\_off./gi, '_on.'));
    },
	function () {
	    if (rollover_triggered != 'yes') {
	        $(this).attr('src', $(this).attr('src').replace(/\_on./gi, '_off.'));
	    }
	});

    // Opacity fade rollovers
    $('.rollover_fade img, .rollover_fade_slow img, img.rollover_fade, img.rollover_fade_slow').parent('a').focus(function () {
        if ($(this).hasClass('rollover_fade_slow') || $(this).parents().hasClass('rollover_fade_slow')) {
            var fade_speed = 125;
        }
        else {
            var fade_speed = 0;
        }
        $(this).children('img').stop().fadeTo(fade_speed, 0.85);
        rollover_triggered = 'yes';
    });
    $('.rollover_fade img, .rollover_fade_slow img, img.rollover_fade, img.rollover_fade_slow').parent('a').blur(function () {
        $(this).children('img').stop().fadeTo(350, 1);
        rollover_triggered = 'no';
    });
    $('.rollover_fade input:image, .rollover_fade_slow input:image, input.rollover_fade:image, input.rollover_fade_slow:image').focus(function () {
        if ($(this).hasClass('rollover_fade_slow') || $(this).parents().hasClass('rollover_fade_slow')) {
            var fade_speed = 125;
        }
        else {
            var fade_speed = 0;
        }
        $(this).stop().fadeTo(fade_speed, 0.85);
        rollover_triggered = 'yes';
    });
    $('.rollover_fade input:image, .rollover_fade_slow input:image, input.rollover_fade:image, input.rollover_fade_slow:image').blur(function () {
        $(this).stop().fadeTo(350, 1);
        rollover_triggered = 'no';
    });
    $('.rollover_fade img, .rollover_fade_slow img, .rollover_fade input:image, .rollover_fade_slow input:image, img.rollover_fade, img.rollover_fade_slow, input.rollover_fade:image, input.rollover_fade_slow:image').hover(function () {
        if ($(this).hasClass('rollover_fade_slow') || $(this).parents().hasClass('rollover_fade_slow')) {
            var fade_speed = 125;
        }
        else {
            var fade_speed = 0;
        }
        $(this).stop().fadeTo(fade_speed, 0.85);
    }, function () {
        if (rollover_triggered != 'yes') {
            $(this).stop().fadeTo(350, 1);
        }
    });
    $('img.rollover_transition, img.rollover_transition_slow, .rollover_transition img, .rollover_transition_slow img, .rollover_instant img, .rollover_instant input:image, img.rollover_instant, input.rollover_instant:image').each(function () {
        var source_image = $(this).attr('src');
        var rollover_image = source_image.replace(/\_off./gi, '_on.');
        $('<img>').attr('src', rollover_image);
    });

    function mycarousel_initCallback(carousel) {
        // Disable autoscrolling if the user clicks the prev or next button.
        carousel.buttonNext.bind('click', function () {
            carousel.startAuto(0);
        });

        carousel.buttonPrev.bind('click', function () {
            carousel.startAuto(0);
        });

        // Pause autoscrolling if the user moves with the cursor over the clip.
        carousel.clip.hover(function () {
            carousel.stopAuto();
        }, function () {
            carousel.startAuto();
        });
    };

    $('.first-and-second-carousel').jcarousel({
        auto: 20,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });



    $(function () {
        $.extend($.fn.pulse = function () {
            var minOpacity = .0;
            var fadeOutDuration = 3000;
            var fadeInDuration = 3000;
            $(this).attr('pulsing', 'y');
            $(this).animate({
                opacity: minOpacity
            }, fadeOutDuration, function () {
                $(this).animate({
                    opacity: 1
                }, fadeInDuration, function () {
                    if ($(this).attr('pulsing') == 'y') $(this).pulse();
                })
            });
            return $(this);
        });
        $.extend($.fn.stopPulse = function () {
            $(this).attr('pulsing', '').stop(true, true).animate({ opacity: 1 });
        });

        $('.pulse').pulse();


        // View Product: Show more
        if ($('.customise_order_para').find('tr').length > 5) {
            $('.customise_order_showmore').show();
            $('.customise_order_para').find('tr').hide().slice(0, 5).show();
            $('.customise_order_show').click(function () {
                $('.customise_order_show').hide();
                var s = $(this),
                t = $('.customise_order_para').find('tr');
                if (s.hasClass('customise_order_hide')) {
                    t.show();
                    s.removeClass('customise_order_hide');
                } else {
                    t.hide().slice(0, 5).show();
                    s.addClass('customise_order_hide');
                }
                return false;
            });
        }

        // View Product: Change price and additional description
        $('.customise_order_para').find('input').change(function () {
            var input = $('#product_hidden_' + $(this).val()).find('input');
            $('#product_price').html(input.eq(0).val());
            //        var val = input.eq(1).val();
            //        if ($('#product_additionaldescription').length === 0) {
            //            if (val) {
            //                $('#product_contractingservices').before('<div id="product_additionaldescription">' + input.eq(1).val() + '</div>');
            //            }
            //        } else {
            //            if (val) {
            //                $('#product_additionaldescription').empty().html(input.eq(1).val());
            //            } else {
            //                $('#product_additionaldescription').empty();
            //            }
            //        }
        }); //.eq(0).click();
    });
});
