
var flyingSpeed = 21;
var url_addProductToBasket = 'aj_add_exer_to_prog.php';
var url_removeProductFromBasket = 'aj_remove_exer_from_prog.php';
var txt_totalPrice = '<b>Total: ';
var valuta = ' SEK</b>';


var training_prog_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var training_prog_x = false;
var training_prog_y = false;

var slide_xFactor = false;
var slide_yFactor = false;

var diffX = false;
var diffY = false;

var currentXPos = false;
var currentYPos = false;

var ajaxObjects = new Array();


function training_prog_getTopPos(inputObj)
{
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
          if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function training_prog_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
          if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}


function addTotraining_prog2(productId,day)
{

        if(!training_prog_div)training_prog_div = document.getElementById('training_prog');
        if(!flyingDiv){
                flyingDiv = document.createElement('DIV');
                flyingDiv.style.position = 'absolute';
                document.body.appendChild(flyingDiv);
        }

        training_prog_x = training_prog_getLeftPos(training_prog_div);
        training_prog_y = training_prog_getTopPos(training_prog_div);

        currentProductDiv = document.getElementById('slidingExer' + productId);

        currentXPos = training_prog_getLeftPos(currentProductDiv);
        currentYPos = training_prog_getTopPos(currentProductDiv);

        diffX = training_prog_x - currentXPos;
        diffY = training_prog_y - currentYPos;



        var training_progContentCopy = currentProductDiv.cloneNode(true);
        training_progContentCopy.id='';
        flyingDiv.innerHTML = '';
        flyingDiv.style.left = currentXPos + 'px';
        flyingDiv.style.top = currentYPos + 'px';
        flyingDiv.appendChild(training_progContentCopy);
        flyingDiv.style.display='block';
        flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
        flyTotraining_prog(productId);

}


function flyTotraining_prog(productId)
{
        var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
        var moveX = (diffX / maxDiff) * flyingSpeed;;
        var moveY = (diffY / maxDiff) * flyingSpeed;

        currentXPos = currentXPos + moveX;
        currentYPos = currentYPos + moveY;

        flyingDiv.style.left = Math.round(currentXPos) + 'px';
        flyingDiv.style.top = Math.round(currentYPos) + 'px';


        if(moveX>0 && currentXPos > training_prog_x){
                flyingDiv.style.display='none';
        }
        if(moveX<0 && currentXPos < training_prog_x){
                flyingDiv.style.display='none';
        }

        if(flyingDiv.style.display=='block')setTimeout('flyTotraining_prog("' + productId + '")',10); else ajaxAddProduct(productId);
}

function showAjaxtraining_progContent(ajaxIndex)
{

        //ajaxIndex='1|||2|||3';
        //alert(ajaxIndex);
        // Getting a reference to the shopping cart items table


        var productItems = ajaxObjects[ajaxIndex].response.split('|||');        // Breaking response from Ajax into tokens
        var itemBox = document.getElementById('training_prog_exer' + productItems[1]);

        //alert(' training_prog_' + productItems[1]+ ' training_prog_exer'+ productItems[1] + '' + productItems[0]);
        if(document.getElementById('training_prog_exer'+ productItems[1] + '' + productItems[0])){        // A product with this id is allready in the basket - just add number items
                //var row = document.getElementById('training_prog_exer'+ productItems[1] + '' + productItems[0]);
                //var items = row.cells[0].innerHTML /1;
                //items = items + 1;
                //row.cells[0].innerHTML = items;

        }else{        // Product isn't allready in the basket - add a new row
                var tr = itemBox.insertRow(-1);
                tr.id = 'training_prog_exer' + productItems[1] + '' + productItems[0]

                //var td = tr.insertCell(-1);
                //td.innerHTML = '1';         // Number of items

                //var td = tr.insertCell(-1);
                //td.innerHTML = 'x';         // x

                var td = tr.insertCell(-1);
                td.innerHTML = productItems[3];         // name

                //var td = tr.insertCell(-1);
                //td.style.textAlign = 'right';
                //td.innerHTML = productItems[2];         // set

                //var td = tr.insertCell(-1);
                //td.style.textAlign = 'right';
                //td.innerHTML = "";         // empty

                var td = tr.insertCell(-1);
                var a = document.createElement('A');
                //var text = document.createTextNode('-');

                td.appendChild(a);
                a.href = '#';
                a.onclick = function(){ removeProductFromBasket(productItems[1] + '' +productItems[0]); };
                var img = document.createElement('IMG');
                img.src = 'simg/remove.png';
                img.width = "12";
                img.height = "12";
                img.border = "0";
                a.appendChild(img);
                //a.appendChild('-');
                //td.innerHTML = '<a href="#" onclick="removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';


        }


        //updateTotalPrice();

        ajaxObjects[ajaxIndex] = false;

}

function updateTotalPrice()
{
        var itemBox = document.getElementById('shopping_cart_items');
        // Calculating total price and showing it below the table with basket items
        var totalPrice = 0;
        if(document.getElementById('shopping_cart_totalprice')){
                for(var no=1;no<itemBox.rows.length;no++){
                        //alert(itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/g));
                        totalPrice = totalPrice + (itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[3].innerHTML);

                }
                document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + totalPrice.toFixed(2) + valuta;

        }

}

function removeProductFromBasket(productId)
{
        //alert(productId);
        var productRow = document.getElementById('training_prog_exer' + productId);

        var numberOfItemCell = productRow.cells[0];
        if(numberOfItemCell.innerHTML){
                productRow.parentNode.removeChild(productRow);
        }else{
                //numberOfItemCell.innerHTML = numberOfItemCell.innerHTML/1 - 1;
        }
        //updateTotalPrice();
        ajaxRemoveProduct(productId);
}

function ajaxValidateRemovedProduct(ajaxIndex)
{
        if(ajaxObjects[ajaxIndex].response!='OK')alert('Error while removing product from the database');

}

function ajaxRemoveProduct(productId)
{
        //alert(productId);
        var ajaxIndex = ajaxObjects.length;
        ajaxObjects[ajaxIndex] = new sack();
        ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket + '?exerId=' + productId;        // Saving product in this file
        ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };        // Specify function that will be executed after file has been found
        ajaxObjects[ajaxIndex].runAJAX();                // Execute AJAX function
}

function addTotraining_prog(productId,day)
{
        //alert("1 "+productId+" "+day);
        var ajaxIndex = ajaxObjects.length;
        ajaxObjects[ajaxIndex] = new sack();
        //alert("2");
        ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket + '?exerId=' + productId + '&day=' + day;        // Saving product in this file
        //alert("3");
        ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxtraining_progContent(ajaxIndex); };        // Specify function that will be executed after file has been found
        //alert("4");
        ajaxObjects[ajaxIndex].runAJAX();                // Execute AJAX function
}
