/**
 * scripts.js
 *
 * @author Martin Bean <martin@mcbwebdesign.co.uk>
 * @version 2.0
 */
(function($) {
    $(document).ready(function() {
        $('a[rel="external"]').click(function(e) {
            e.preventDefault();
            window.open(this.href);
        });/*
        $('.carousel .slides').jCarouselLite({
            btnNext: '.next',
            btnPrev: '.prev',
            visible: 1
        });*/
        $('.carousel').each(function() {
            var carousel = this;
            $('.slides ul', carousel).cycle({
                fx: 'fade',
                pager: '.pages',
                pagerAnchorBuilder: function(idx, slide) {
                    return '.carousel .pages li:eq(' + idx + ') a';
                }    
            }); 
        });
        $('.slides').slides({
            container: 'slides-container',
            crossfade: true,
            effect: 'fade',
            generateNextPrev: true,
            play: 5000,
            pause: 2500
        });
        $('.tabs').tabs({
            event: 'mouseover'
        }).tabs('rotate', 5000);
        if ($(window).width() > 768) {
            $('#subscribe').hide().css({
                'width': '100%',
                'position': 'fixed',
                'top': 0,
                'left': 0,
                'z-index': 999
            });
            $('#subscribe h2').after('<a href="#" title="Close" class="close">Close</a>');
            $('#subscribe a.close').click(function(e) {
                e.preventDefault();
                $('#subscribe').slideUp('fast');
            });
            $('#header .email a').click(function(e) {
                e.preventDefault();
                $('#subscribe').slideDown('fast');
            });
        }
        
        $('.insLink').click(function(){
            if($(this).attr('rel')){
                var textArea = $('#' + $(this).attr('rel'));
                var ctrl = document.getElementById($(this).attr('rel'));
                var pos = doGetCaretPosition (ctrl);
                var link = '[[link text|/link_url]]';
                var val = textArea.val();
                var parts = [val.substring(0, pos),val.substring(pos)];
                textArea.val(parts[0] + link + parts[1]);
                setCaretPosition(ctrl, pos + link.length);
            }       
            return false;
        });
    });
})(jQuery);

function insertLink(){
    
}

function doGetCaretPosition (ctrl) {
    var CaretPos = 0;	// IE Support
    if (document.selection) {
        ctrl.focus ();
        var Sel = document.selection.createRange ();
        Sel.moveStart ('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
        CaretPos = ctrl.selectionStart;
    return (CaretPos);
}
function setCaretPosition(ctrl, pos){
    if(ctrl.setSelectionRange)
    {
        ctrl.focus();
        ctrl.setSelectionRange(pos,pos);
    }
    else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}
