function isTouchDevice() {
    try {
        document.createEvent("TouchEvent");
        return true;
    } catch (e) {
        return false;
    }
    //if(document.body.ontouchstart) { / yay touch device / }
}

var isTouch = isTouchDevice();

var IS_DRAGGING = false;
var CUR_LEFT = 0;
var START_X = 0
var END_X = 0
var CUR;

// ------------------------ MOUSE / TOUCH EVENTS ------------------------

var onmousedown = function(e) {
    //if (!IS_DRAGGING) {
        e = e || window.event;
        var touch;
        if (e.touches) { e = e.targetTouches[0]; }
        END_X = 0;
        CUR = e.target.parentNode.parentNode.parentNode;
        CUR_LEFT = parseInt($(CUR).css('left'));
        START_X = e.clientX;
        IS_DRAGGING = true;
    //}
    return false;
}

var onmouseup = function(e) {
    if (CUR.parentNode.parentNode == null) {
        //alert("no node parent");
        return false;
    }
    /*if (!IS_DRAGGING) {
        return false;
    }*/
    IS_DRAGGING = false;
    var LEFT_NEW = -10000;
    var length = (END_X == 0 || isNaN(END_X)) ? 0 : START_X - END_X;
    //alert('length = '+length);
    if (CUR_LEFT - length > 0) {
        LEFT_NEW = 0;
    }
    else if (CUR_LEFT - length < -1638) {
        LEFT_NEW = -1638;
    }
    else if (Math.abs(length) > 350) {
        // Slide left
        if (length > 0) {
            LEFT_NEW = CUR_LEFT > -819 ? -819 : -1638;

        }
        //slide right
        else {
            LEFT_NEW = CUR_LEFT < -819 ? -819 : 0;
        }
    }
    else {
        LEFT_NEW = CUR_LEFT;
    }

    if (LEFT_NEW != -10000 && length != 0) {

        $(CUR).animate({ left: LEFT_NEW });

        /* === Setup of tiny pagination spots  === */
        try {
            var className = CUR.parentNode.parentNode.className;
            if (className == '')
            { return false; }
            var cur_li = '.' + className + ' .tiny_pagination>li';
            var rel = '0';
            if (LEFT_NEW > -819) { rel = '0'; }
            else if (LEFT_NEW < -1637) { rel = '2'; }
            else { rel = '1'; }

            $(cur_li).each(function() {
                if ($(this).children().attr('rel') == rel) {
                    $(this).addClass('current');
                }
                else {
                    $(this).removeClass('current');
                }
            });
        }
        catch (e) {
            console.log('el: ' + cur_li + ' : ' + e);
        }
        /* ===== */
    }
}

var oncancel = function(e) {
    //console.log('CANCEL');
    IS_DRAGGING = false;
    $(CUR).animate({ left: CUR_LEFT });
    return false;
}

var onmousemove = function(e) {
    if (IS_DRAGGING) {
        e.preventDefault();
        e = e || window.event;
        if (e.touches) { e = e.targetTouches[0]; }

        END_X = e.clientX;
        $(CUR).css({ left: CUR_LEFT - (START_X - END_X) });
    }
    return false;
}

function initSwipe(){
if (isTouch == true){
    document.getElementById('content').addEventListener('touchstart', onmousedown, false);
    document.getElementById('content').addEventListener('touchend', onmouseup, false);
    document.getElementById('content').addEventListener('touchcancel', oncancel, false);
    document.getElementById('content').addEventListener('touchmove', onmousemove, false);
    document.getElementById('content').addEventListener('click', function() { return false; }, false);
} 
/*else {
    $('#multimedia .tiny')
    .live('mousedown', onmousedown)
    .live('mouseup', onmouseup)
    .live('mousemove', onmousemove);
}*/

}
