var WebService =  {
    stylesheet:null,
	outputNode:new Object(),
	moreLink: new String(),
	errorMsg: false,
	isBTOB: false,
	ean: false,
	hideMainMore: false,
    initialize:function(params) {
		if(params.outputNode.innerHTML == "" || params.outputNode.getElementsByTagName("img")) {
			if(params.outputNode) {
				this.outputNode = params.outputNode;
			}
			if(params.moreLink || params.moreLink != null || params.moreLink != false) {
				this.moreLink = params.moreLink;
			} else {
				this.moreLink = false;
			}
			if(params.isBTOB) {
				this.isBTOB = params.isBTOB;
			}
			if(params.ean) {
				this.ean = params.ean;
			}
			this.stylesheet = params.StyleSheet;
			
			if(params.hideMainMore) {
				this.hideMainMore = params.hideMainMore
			}
			
			this.loadScript(params.url);
			
			if(params.errorMsg) {
				this.errorMsg = params.errorMsg;
			}
		}
    },
    addEvent:function(element, type, handler) {
        if (element.addEventListener) {
            element.addEventListener(type, handler, false);
        } else if (element.attachEvent) {
            element.attachEvent("on" + type, handler);
        }
    },
    loadScript:function(url, id){
	    var script = document.getElementById(id);
	    if (script) {
		    script.parentNode.removeChild(script);
	    }
	    script = document.createElement("script");
	    script.type = "text/javascript";
	    script.id = id;	
	    script.src = url;
	    document.getElementsByTagName("head")[0].appendChild(script);
    },
    runXSL:function(JSONObj) {
		if (!(JSONObj.xml && JSONObj.xml.length > 0) || JSONObj.status == 1) { 
			this.outputNode.innerHTML = this.errorMsg; 
			return;
		}
		
		// code for IE
		var text = JSONObj.xml;
		
		if (window.ActiveXObject) {
		  	var doc=new ActiveXObject("Microsoft.XMLDOM");
		  	doc.async="false";
		  	doc.loadXML(text);
		 }
		// code for Mozilla, Firefox, Opera, etc.
		else {
		  var parser=new DOMParser();
		  var doc=parser.parseFromString(text,"text/xml");
		}

		this.outputNode.innerHTML = this.stylesheet({xml:doc.documentElement, ean:this.ean, isBTOB:this.isBTOB, moreLink:this.moreLink, hideMainMore:this.hideMainMore, errorMsg:this.errorMsg},this);
		this.displayMoreIndicator();

		/*if(true) {
			ProductPreview.initialize();
		}*/
    },
    displayMoreIndicator:function(){
	    // display indicator that more data exists
	    // consider removal from web service object. Separated from runXSL for reuse, and to ease removal.
	    if(this.hideMainMore == false && this.moreLink != false) {
			if(this.isBTOB == "true")
				this.moreLink = this.convertLinkForBTOB(this.moreLink);
			this.outputNode.innerHTML += "<div class='right'><a class='left-arrow-small' href='"+this.moreLink+"'>More</a></div>";
		}
    },
    convertLinkForBTOB:function(uri){
	    //used by displayMoreIndicator, but belongs in object-as BTOB modification may be commonplace. 
	    uri = uri.replace("http://search", "http://btobsearch");
		uri = uri.replace("http://video", "http://btobvideo");
		uri = uri.replace("http://music", "http://btobmusic");
		uri += "&btob=y";
		return uri;
    }
    ,callService:function(){
            WebService.loadScript(url, "webservice");
    }    
} // end 

function webservicecallback(response){
    if (response)
        WebService.runXSL(response);
}