﻿/*
* @Author Sequence Ltd, http://www.sequence.co.uk
* @Copyright Sequence Ltd
* @ July 2010
* @Nadhim Orfali
*
* INIT GLOBAL JAVASCRIPT FUNCTIONS
*/

//Cufon.now();

$(document).ready(function() {
    formEffects();
    diabloToolbar();

    // scroll smoothly
    $('#lnk_backtotop').click(function() {
        $('html, body').animate({ scrollTop: 0 }, 'slow');
        return false;
    });


    //$('#searchblock').hide();
    $('#btn_resume').stop(true, true).fadeTo('slow', 0.5);
    $('.btn_logos').hover(function() {
        $(this).find('img').stop(true, true).fadeTo('slow', 0.5);
    }, function() {
        $(this).find('img').stop(true, true).fadeTo('slow', 1);
    });

    $('#btn_search').click(function() {
        $('#searchblock').animate({ width: 'toggle' }, 'slow');
        return false;
    });
    $('#btn_pause').click(function() {
        $('#mainpic').cycle('pause');
        $(this).fadeTo('slow', 0.5);
        $('#btn_resume').fadeTo('slow', 1);
        return false;
    });
    $('#btn_resume').click(function() {
        $('#mainpic').cycle('resume');
        $(this).fadeTo('slow', 0.5);
        $('#btn_pause').fadeTo('slow', 1);
        return false;
    });
    $('#mainpic').cycle({ fx: 'turnDown', timeout: 7000 });

    $('#homepage-events li')
        .click(function() {
            if ($(this).hasClass('active')) return;

            $(this)
                .siblings().not($(this))
                    .animate({ width: '86px' })
                    .removeClass('active')
                    .find('.homepageevent-thumbnail')
                        .fadeOut('fast');

            $(this)
                .animate({ width: '345px' }, function() {
                    $(this).addClass('active')
                        .find('.homepageevent-thumbnail')
                            .fadeIn('slow');
                });
        })
        .filter(":first")
            .click();

    var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) $(document).pngFix();

    /************* Forum **************/
    $('.forum tr:nth-child(even)').addClass('alt');

    /************* Tabbed content **************/
    $(".tabs").each(function() {
        $(this)
            .find("a[href^='#']")
                .click(function() {
                    $(".tab_content").hide();
                    $($(this)[0].hash).show();
                    $(this).parents(".tabs").find("a").removeClass("active");
                    $(this).addClass("active");
                    return false;
                })
                .filter(":first")
                    .click();
    });

    /************* Vertical Toggle Headers **************/
    $(".toggle_header")
        .after('<div class="toggle_tag" />')
        .filter(":not(:first)")
            .parent(".toggle_group")
                .addClass("collapsed")
                    .find(".toggle_content")
                        .hide();

    $(".toggle_header, .toggle_tag")
        .click(function() {
            var parent = $(this).parent(".toggle_group");
            if (parent.hasClass("collapsed")) {
                parent.removeClass("collapsed");
                parent.find(".toggle_content")
                    .slideDown();
            } else {
                parent.find(".toggle_content")
                    .slideUp();
                parent.addClass("collapsed");
            }
        })
        .css({ cursor: "pointer" });

    /************* Input:number, increase, decrease buttons **************/
    $("input[type='number'],input.number").each(function() {
        $(this)
            .before('<div class="number_decrease" />')
            .after('<div class="number_increase" />')
            .keypress(function(event) {
                if (event.which >= 48 && event.which <= 57) return true // 0 - 9;
                else if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 46) return true;
                else if (event.keyCode == 38) $(this).siblings(".number_increase").click();
                else if (event.keyCode == 40) $(this).siblings(".number_decrease").click();
                return false;
            })
            .keyup(function() {
                $(this).trigger("checkDigit");
            })
            .bind('checkDigit', function() {
                if (parseInt($(this).attr("value")) > 0) $(this).siblings("label").addClass("active");
                else $(this).siblings("label").removeClass("active");
            })
            .trigger("checkDigit");
    });
    $(".number_decrease")
        .live('click', function() {
            input = $(this).next();
            if ($(this).attr("type") === 'number') {
                input.stepDown();
            } else {
                if (input.val() > 0) input.attr('value', parseInt(input.val()) - 1);
            }
            input.trigger("checkDigit");
        })
        .css({ cursor: "pointer" });
    $(".number_increase")
        .live('click', function() {
            input = $(this).prev();
            if ($(this).attr("type") === 'number') {
                input.stepUp();
            } else {
                input.attr('value', parseInt(input.val()) + 1);
            }
            input.trigger("checkDigit");
        })
        .css({ cursor: "pointer" });

    $(".instrument-other input[type='text']").each(function() {
        $(this).keyup(function() {
            if ($(this).attr("value") != "") $(this).parents(".instrument-other").find("label").addClass("active");
            else $(this).parents(".instrument-other").find("label").removeClass("active");
        });
    });
});


/************* General Form Functions **************/
function formEffects() {
    // homepage search textboxes
    $('.textfield').focus(function() {
        $(this).addClass('textfield-focused');
        var defaultValue = $(this).attr('placeholder');
        if ($(this).val() == defaultValue) $(this).val('');
    });
    // Footer newsletter sign-up textbox
    $('.textfield').blur(function() {
        $(this).removeClass('textfield-focused');
        var defaultValue = $(this).attr('placeholder');
        if ($(this).val() == '') $(this).val(defaultValue);
    });

    $('.form .textbox').focus(function() {
        $(this).addClass('focused');
    });
    $('.form .textbox').blur(function() {
        $(this).removeClass('focused');
    });

    $('.form input[type=text].textbox, .form input[type=password].textbox').parents('.control').addClass('textbox-control');
    $('.form input[type=file].textbox').removeClass('textbox');
}
/************* Diablo Toolbar **************/
function diabloToolbar() {
    $('#toolbar')
        .bind('show', function() {
            $('#wrapper').css('padding-top', '104px');
        })
        .bind('hide', function() {
            $('#wrapper').css('padding-top', '');
        });

    if ($('#toolbar:visible').is('*'))
        $('#toolbar').trigger('show');
}
