﻿function update(TextToAddBefore, TextToAddAfter, TextAreaToEdit) {
    var o = document.getElementById(TextAreaToEdit).childNodes[2];
    var t = o.value, start = getSelectionStart(o), end = getSelectionEnd(o)
    o.focus();
    if (document.selection) {
        o.focus();

        /* Create a TextRange based on the document.selection
        This TextRanged can be used to replace the selected
        Text with the new one
        */
        var range = document.selection.createRange();

        /* If the range is not part of our Object (remember the
        textarea or input field), stop processing here
        */
        if (range.parentElement() != o) {
            return false;
        }

        /* Save the current value. We will need this value later
        to find out, where the text has been changed
        */
        var orig = o.value.replace(/rn/g, "n");

        /* Replace the Text */
        range.text = TextToAddBefore + range.text + TextToAddAfter;

        /* Now get the new content and save it into
        a temporary variable
        */
        var actual = tmp = o.value.replace(/rn/g, "n");

        /* Find the first occurance, where the original differs
        from the actual content. This could be the startposition
        of our text selection, but it has not to be. Think of the
        selection "ab" and replacing it with "ac". The first
        difference would be the "c", while the start position
        is the "a"
        */
        for (var diff = 0; diff < orig.length; diff++) {
            if (orig.charAt(diff) != actual.charAt(diff)) break;
        }

        /* To get the real start position, we iterate through
        the string searching for the whole replacement
        text - "abc", as long as the first difference is not
        reached. If you do not understand that logic - no
        blame to you, just copy & paste it ;)
        */
        for (var index = 0, start = 0;
        tmp.match(TextToAddBefore)
            && (tmp = tmp.replace(TextToAddBefore, ""))
            && index <= diff;
        index = start + text.length
        ) 
        {
            start = actual.indexOf(TextToAddBefore + range.text + TextToAddAfter, index);
        }
    } else if (o.selectionStart) {
            o.value = o.value.substr(0, start)
            + TextToAddBefore
            + o.value.substr(start, end - start)
            + TextToAddAfter
            + o.value.substr(end, o.value.length);
    } else {
        // Fallback for any other browser 
    } 
}

function getSelectionStart(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate()
        r.moveEnd('character', o.value.length)
        if (r.text == '') return o.value.length
        return o.value.lastIndexOf(r.text)
    } else return o.selectionStart
}

function getSelectionEnd(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate()
        r.moveStart('character', -o.value.length)
        return r.text.length
    } else return o.selectionEnd
}

function addURL(url, TextAreaToEdit) {
    var addURL = "";
    if (url.indexOf('dsale.co.il.com') > 0) {
        addURL = "[URL=" + url + "]";
    }
    else {
        addURL = "[URL=" + url + " target=blank]";
    }
    update(addURL, '[/url]', TextAreaToEdit);
}

var finnalOffset = 0;
//rowPanel - the container for the moving panel
//containerPanel - the moving panel
function movePreview(containerPanel, dir) {
    var btnNext = $('#btnNext');
    var btnPrev = $("#btnPrev");
    var containerWidth = $(containerPanel).width();
    if (dir == "left") {
        $(containerPanel).animate({ left: "+=550px" }, 1000);
        finnalOffset = finnalOffset + 550;
        if (finnalOffset > -500) {
            btnPrev.hide();
        }
        else {
            btnPrev.show();
        }
        btnNext.show();
    }
    if (dir == "right") {
        $(containerPanel).animate({ left: "-=550px" }, 1000);
        finnalOffset = finnalOffset - 550;
        if (finnalOffset < -(containerWidth - 600)) {
            btnNext.hide();
        }
        else {
            btnNext.show();
        }
        btnPrev.show();
    }
}

function setWidth(containerPanel,singleWidth) {
    var size = $(containerPanel + " > article").size();
    var divWidth = size * singleWidth;
    $(containerPanel).width(divWidth);
}

