/**
 * ShopCart handling - Ajax style
 *
 * Requires: jQuery.js
 * Requires: jQuery.transfer.js
 * Author: tan@enonic.com
 */

// List page
MY_LIST_KEY = 3517; 


// Element which contains the list
LIST_ELEMENT = '#memo-list'; 
LIST_ELEMENT_INNER = '#my-list'; 


function loadMyList() {
    jQuery(LIST_ELEMENT).load('page?id='+MY_LIST_KEY);
}

function addToMyList(key,price,count,title,trigger)  {
    var myPrice = 0; // Noen priser er i et format som ikke shop takler (eg. 1 000). Ideelt sett burde det være en regexp her.
    var urlstring = 'userservices?handler=order&op=cart_add&productid='+key+'&price='+myPrice+'&count='+count;
    var parrent = '#parrent_'+key;

	jQuery(parrent).TransferTo({to:'my-list',className:'transferer', duration: 600, complete: sendRequest(urlstring)});
}

function sendRequest(urlstring) {
    jQuery.ajax({ url: urlstring, type: "GET", complete: addToListDone, success: addToListSuccess });
}

function addToListSuccess() {
    loadMyList();
}

function addToListDone() {
    //$(LIST_ELEMENT).fadeIn('slow');
}   

function IsImageOk(img) {

    // During the onload event, IE correctly identifies
   // any images that
    // weren’t downloaded as not complete. Others should too. Gecko-based
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete) {
    return false;
    }

    // However, they do have two very useful properties:
   // naturalWidth and
    // naturalHeight. These give the true size of the image. If it failed
    // to load, either of these should be zero.
    if (typeof img.naturalWidth
    != "undefined" && img.naturalWidth
    == 0) {
    return false;
    }

    // No other way of checking: assume it’s ok.
    return true;

}

//Call this function onLoad of body tag
function checkImages() {

 /*   for (var i = 0; i < document.images.length; i++) {
    if (!IsImageOk(document.images[i])) {
 
    document.images[i].src = "/images/loaderror.jpg";
    }
    }
*/
} 

