$j = jQuery.noConflict();

$j(function () {
    $j('#coda-slider-1').codaSlider();
});

//Accordion List
$j(function () {
    $j('.accordion').each(function () {
        $j(this).children('dt').eq(0).addClass('open');
        $j(this).children('dt').click(function () {
            $j(this).parent().children('dt').removeClass('open');
            $j(this).addClass('open');
            $j(this).siblings('dd').slideUp(200);
            $j(this).nextUntil('dt').slideDown(200);
            return false;
        });
    });
});

$j(document).ready(function () {

    // Contact form textboxes
    $j('.contact-form dl').each(function () {
        var textBox = $j(this).find('dd input');
        if (textBox.length < 1) textBox = $j(this).find('dd textarea');
        if (textBox.length > 0) {
            var originalText = $j(this).find('dt label').html();
            textBox.val(originalText);
            textBox.focusin(function () {
                $j(this).addClass('focused');
                if ($j(this).val() === originalText)
                    $j(this).val('');
            });
            textBox.focusout(function () {
                $j(this).removeClass('focused');
                if ($j(this).val().length == 0)
                    $j(this).val(originalText);
            });
            $j('.contact-submit-button').click(function () {
                if (textBox.val() == originalText && !(textBox.next('.field-required').length > 0)) textBox.val('');
            });
        }
    });

    // Portfolio boxes
    $j(function () {
        $j('.hoverImg').css({ "top": "174px" });
        $j('.portfolio-item').hover(function () {
            $j(this).children('.hoverImg').stop()
	            .animate({ "top": "0" }, 700);
        }, function () {
            $j(this).children('.hoverImg').stop()
	            .animate({ "top": "174px" }, 700);
        });
    });

    // Code block styling
    $j('code, pre').each(function () {
        if (!($j(this).parent().hasClass('code-block'))) {
            $j(this).wrap('<div class="code-block" />');
            $j(this).parent().wrap('<div class="code-block-wrapper" />');
            $j(this).parent().before('<div class="code-block-header">' +
                                    '<h3>Code Block</h3>' +
                                    '<span class="code-block-tools">' +
                                        '<a href="#" title="Select All" class="select-code first last">Select All</a>' +
                                    '</span>' +
                                '</div>');
        }
    });

    $j('.code-block-tools .select-code').click(function () {
        $j(this).closest('.code-block-wrapper').find('pre, code').each(function () {
            selectElementText(this);
        });
        return false;
    });

    function selectElementText(el, win) {
        win = win || window;
        var doc = win.document, sel, range;
        if (win.getSelection && doc.createRange) {
            sel = win.getSelection();
            range = doc.createRange();
            range.selectNodeContents(el);
            sel.removeAllRanges();
            sel.addRange(range);
            return range;
        } else if (doc.body.createTextRange) {
            range = doc.body.createTextRange();
            range.moveToElementText(el);
            range.select();
            return range;
        }
    }
    // End Code block styling
});


