/* IE FIX */
      if(typeof(DOMParser) == 'undefined') {
       DOMParser = function() {}
       DOMParser.prototype.parseFromString = function(str, contentType) {
        if(typeof(ActiveXObject) != 'undefined') {
         var xmldata = new ActiveXObject('MSXML.DomDocument');
         xmldata.async = false;
         xmldata.loadXML(str);
         return xmldata;
        } else if(typeof(XMLHttpRequest) != 'undefined') {
         var xmldata = new XMLHttpRequest;
         if(!contentType) {
          contentType = 'application/xml';
         }
         xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
         if(xmldata.overrideMimeType) {
          xmldata.overrideMimeType(contentType);
         }
         xmldata.send(null);
         return xmldata.responseXML;
        }
       }
      }


/*
    AJAX related functions
*/
var damimageshop_xmlhttp;
function damimageshop_ajaxCall(session, method, params, pid, price, title) {
    damimageshop_xmlhttp=damimageshop_GetXmlHttpObject();
    if (damimageshop_xmlhttp==null) {
        alert ("Your browser does not support XMLHTTP!");
        return;
    }
    
    var url="typo3conf/ext/pxa_damimageshop/pi2/class.tx_pxadamimageshop_ajax.php";
    if (method == 'addProduct') {
        url=url+"?session="+session+"&m=addProductToBasket&params="+params+"&pid="+pid;
    }
    else if (method == 'getProductsInBasket') {
        url=url+"?session="+session+"&m=getProductsInBasket&pid="+pid;
    }
    else if (method == 'deleteProduct') {
        url=url+"?session="+session+"&m=deleteProduct&params="+params+"&pid="+pid;
    }
	else if (method == 'deleteCollection') {
        url=url+"?session="+session+"&m=deleteCollectionFromBasket&params="+params+"&pid="+pid;
    }
	else if(method == 'addCollectionToBasket') {
		url=url+"?session="+session+"&m=addCollectionToBasket&params="+params+"&pid="+pid+"&price="+price+"&title="+title;
	}
    damimageshop_xmlhttp.onreadystatechange=damimageshop_stateChanged;
    damimageshop_xmlhttp.open("GET",url,true);
    damimageshop_xmlhttp.send(null);
}

function damimageshop_stateChanged() {
    if (damimageshop_xmlhttp.readyState==4) {
    	if (damimageshop_xmlhttp.responseText) {
	        var xmldoc = (new DOMParser()).parseFromString(damimageshop_xmlhttp.responseText, "text/xml");
		}
        //var xmldoc = damimageshop_xmlhttp.responseXML;
        if (xmldoc) {
            updateBasket(xmldoc);
            showFeedback();
        }
    }
}

function damimageshop_GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject) {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

/*
    Add a product to the basket
*/
function addToBasket(session, imgId, pid) {
    damimageshop_ajaxCall(session, 'addProduct', imgId, pid, '','');
    document.getElementById('damimageshop_basket_buttons').style.display = 'block';
    //updateBasket(xmlDoc);
}

/*
    Update the basket inventory
*/              
function updateBasket(pid) {
    damimageshop_ajaxCall('', 'updateBasket', '', pid, '','');
}

/*
    delete product from basket inventory
*/              
function deleteProduct(session, imgId) {
    damimageshop_ajaxCall(session, 'deleteProduct', imgId, '', '','');
}
function deleteCollection(session, imgId) {
    damimageshop_ajaxCall(session, 'deleteCollection', imgId, '', '','');
}

function addCollectionToBasket(session, imgList, pid, price, title) {
	damimageshop_ajaxCall(session, 'addCollectionToBasket', imgList, pid, price, title);	
	document.getElementById('damimageshop_basket_buttons').style.display = 'block';
}

/*
    refresh the basket
*/
function updateBasket(xmlDoc) {
    var select = document.getElementById('damimageshop_basketCase');
    var i;
    //alert(xmlDoc);
    for(i=select.options.length-1;i>=0;i--)
    {
        select.remove(i);
    }
    
    if(xmlDoc.getElementsByTagName('products')[0].getElementsByTagName('product').length > 0) {
	    for (i = 0; i < xmlDoc.getElementsByTagName('products')[0].getElementsByTagName('product').length; i++)
	    {
	        var option = document.createElement("OPTION");
	        //alert(xmlDoc.getElementsByTagName('products')[0].getElementsByTagName('product')[i].firstChild.nodeValue);
	        option.text = xmlDoc.getElementsByTagName('products')[0].getElementsByTagName('product')[i].firstChild.nodeValue;
	        option.value = '';
	        select.options.add(option);
	    }
    } else {
    	var option = document.createElement("OPTION");
    	option.text = 'Din kundvagn är tom';
    	select.options.add(option);
    	document.getElementById('damimageshop_basket_buttons').style.display = 'none';
    }
    
}

/*
    Other misc functions
*/
function updateSumTotal(price) {
	price = parseInt(price);
	sumtotal = document.getElementById('damimageshop_price_sumtotal');
	sumtotal.innerHTML = parseInt(sumtotal.innerHTML)-price;
}

function showimage(url){
    document.getElementById('imageContainer').innerHTML = '<img src="'+url+'" />';
    document.getElementById('imageContainer').style.display = "block";
    document.getElementById('imageContainer').style.top = damimageshop_getY()+"px";
}

var damimageshop_IE = document.all?true:false;
if (!damimageshop_IE) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = damimageshop_getMouseXY;
var damimageshop_tempX = 0;
var damimageshop_tempY = 0;

function damimageshop_getY() {
    return damimageshop_tempY-150;
}
function damimageshop_getMouseXY(e) {
    if (damimageshop_IE) {
    //alert(document.compatMode);
    //alert(document.body.scrollTop);
    //alert(document.documentElement.scrollTop);
        damimageshop_tempX = window.event.clientX + document.documentElement.scrollLeft;
        damimageshop_tempY = window.event.clientY + document.documentElement.scrollTop;
    } else { 
        damimageshop_tempX = e.pageX;
        damimageshop_tempY = e.pageY;
    }  
    if (damimageshop_tempX < 0){damimageshop_tempX = 0;}
    if (damimageshop_tempY < 0){damimageshop_tempY = 0;}                
    return true;
}

/*

*/
function showFeedback() {
    displayFeedback();
    setTimeout ( "hideFeedback()", 1000 );

}
function displayFeedback() {
    document.getElementById('damimageshop_feedback').style.top = damimageshop_getY()+"px";
    document.getElementById('damimageshop_feedback').style.display = "block";
    document.getElementById('damimageshop_feedback').style.opacity = 1;
    document.getElementById('damimageshop_feedback').style.filter = 'alpha(opacity=100)';
}
function hideFeedback() {
    //while (setTimeout ( "lowerOpacity()", 100 )){}
    document.getElementById('damimageshop_feedback').style.display = "none";
    //setTimeout ( "lowerOpacity()", 300 );
    //setTimeout ( "lowerOpacity()", 300 );
    //setTimeout ( "lowerOpacity()", 300 );
    
}
function lowerOpacity() {
    var opa = parseInt(document.getElementById('damimageshop_feedback').style.opacity)-0.2;
    if (opa <= 0.0) {
        return false;
    }
    alert(opa);
    document.getElementById('damimageshop_feedback').style.opacity = opa;
	document.getElementById('damimageshop_feedback').style.filter = 'alpha(opacity=' + opa*100 + ')';
	return true;
    //setTimeout ( "lowerOpacity()", 100 );
}
