///<reference path="jquery.js" />
var serverUrl = location.host;

function addToCart(element) {
    element = $(element);
    var variantId = element.attr('variantId');
    if (variantId == '' || variantId == undefined) {
        variantId = 1;
    }
    var productId = element.attr('productId');
    var catalogName = 'DCC Tru';
    var quantity = 1;
	var url = 'http://' + serverUrl + '/store/pages/addtocart.aspx?ProductId=' + productId + '&VariantId=' + variantId + '&Quantity=' + quantity + '&Catalog=DCC Tru';
	$.get(url, null, onResult);
	return false;
   /* var method = 'http://www.davidccook.com/AddToCart';
    var data = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><AddToCart xmlns="http://www.davidccook.com/"><productId>' + productId + '</productId><variantId>' + variantId + '</variantId><quantity>' + quantity + '</quantity><catalogName>' + catalogName + '</catalogName></AddToCart></soap:Body></soap:Envelope>';
    var request = new XMLHttpRequest();
    request.open("POST", window['L_Menu_BaseUrl'] + '/_layouts/dcc/shoppingservice.asmx', false);
    request.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    request.setRequestHeader("SOAPAction", method);
    request.onreadystatechange = itemAdded;
	request.send(data); */
}

function onResult(data, textStatus) {
	window.location = window.location.href;
}


function itemAdded(result) {
    if (this.readyState == 4) {
    }
}

function submitOrder() {
    var shoppingWsUrl = 'http://' + serverUrl + '/store/_layouts/dcc/shoppingservice.asmx';
    var method = 'http://www.davidccook.com/GetAddress';
    var data = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <CheckOut xmlns="http://www.davidccook.com/" />  </soap:Body></soap:Envelope>';
    var request = new XMLHttpRequest();
    request.open("POST", shoppingWsUrl, false);
    request.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    request.setRequestHeader("SOAPAction", method);
    request.send(data);
    alert(request.responseText);
}


function getAddress(element) {
    element = $(element);
    var id = element.val();
    if (id == '') {
        showAddress({ responseXML: 'nocontrol', element: element });
        return;
    }
    var userId = element.attr('userId');
    var method = 'http://www.davidccook.com/GetAddress';
    var data = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  <soap:Body>    <GetAddress xmlns="http://www.davidccook.com/">      <addressId>' + id + '</addressId><userId>' + userId + '</userId></GetAddress>  </soap:Body></soap:Envelope>';
    var request = new XMLHttpRequest();
    request.open("POST", window['L_Menu_BaseUrl'] + '/_layouts/dcc/profileService.asmx', true);
    request.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    request.setRequestHeader("SOAPAction", method);
    request.element = element;
    request.onreadystatechange = function() {
        if (this.readyState == 4) {
            showAddress(this);
        }
    };
    request.send(data);
}

function showAddress(result) {
    var element = result.element.parents('div').slice(0, 1);
    var result = $(result.responseXML);
    element.find('#Line1').val(result.find('Line1').text());
    element.find('#Line2').val(result.find('Line2').text());
    element.find('#FirstName').val(result.find('FirstName').text());
    element.find('#LastName').val(result.find('LastName').text());
    element.find('#City').val(result.find('City').text());
    element.find('#StateProvinceCode').val(result.find('StateProvinceCode').text());
    element.find('#StateProvinceName').val(result.find('StateProvinceName').text());
    element.find('#ZipPostalCode').val(result.find('ZipPostalCode').text());
    element.find('#CountryRegionCode').val(result.find('CountryRegionCode').text());
    element.find('#Extension').val(result.find('TelephoneExtension').text());
	element.find('#PhoneNumber').val(result.find('PhoneNumber').text());
}






//Information Boxes
showInformationBox = function() {

    if ($(".information_bar")[0]) {
        $(".information_bar").each(function(i) {
            //Hide all info bars currently showing
            $(".information_bar div").hide();

            var currentInfoBar = $(this);
            $(this).unbind();
            $(this).bind("click", function(e) {

                //Grab Anchor object & name
                var anchorObj = $(this).find("a");
                var anchorName = $(anchorObj).attr("name");                

                //Get element position for info box positioning
                var anchPos = $(anchorObj).offset();
                var anchWidth = 20;
                var infoDivPos = $(this).offset();

                if ($(this).find("div").length > 0) {
                    $(this).find("div").show();
                }
                else {
                    //Create Div element to paste content and append
                    var divEle = document.createElement("div");
                    $(this).append(divEle);

                    //Grab content in anchorName variable from dataview and insert the text
                    $(divEle).text(eval(anchorName));

                    //$(divEle).mouseout(function() { $(divEle).hide(); })
                    $(divEle).show();
                    $(divEle).css({ "left": (anchPos.left + anchWidth) + "px", "top": anchPos.top + "px" });
                    $().mousemove(function() { checkCursorPos(infoDivPos, currentInfoBar) });
                }
            });
        });
    }
}

function checkCursorPos(divPos, divObj) {
    $().mousemove(function(e) {
        if (e.pageX < divPos.left || e.pageY < divPos.top || e.pageX > (divPos.left + 225))
            $(divObj).find("div").hide();
    });
}

var showInfo = new showInformationBox();      



