YAHOO.namespace('itcraft');

var DOM = YAHOO.util.Dom;

var wait, error, errorMsg;
var dialogW = 230;
var dialogH = 205;

YAHOO.itcraft.DDApp = function(image_id, image_width, image_height){
	this.image_id = image_id;
	this.loader();
}

YAHOO.itcraft.DDApp.prototype = {
	loader : function() {
		this.notesArr 		= new Array;
		this.image 			= DOM.get("imageDiv");
		this.wrapperScript 	= "query_wrapper.php";
		this.productDIV 	= DOM.get("productDIV");
		this.note 			= null;
		this.getNotes(this.image_id);
	},
	
	getDetails : function (obj) {
		var callback = {
			success : function(obj){
				var response = eval('new Object(' + obj.responseText + ')');
				newADD.loadDetails(response.product);
			},
			failure : function () { alert("Connection error."); }
		};
		this.exec({nid:obj.id,action:'getProductDetails'}, callback);
	},
	
	loadDetails : function (productData)
	{
		var costTTD 	= DOM.get("costTTD"); 		costTTD.innerHTML = '';
		var costTD 		= DOM.get("costTD"); 		costTD.innerHTML = '';
		var costTR 		= DOM.get("costTR");		costTR.style.display = 'none';
		
		var tagsTD 		= DOM.get("tagsTD"); 		tagsTD.innerHTML = '';
		var tagsTR 		= DOM.get("tagsTR");		tagsTR.style.display = 'none';
		
		var linkHREF 	= DOM.get("linkHREF");		linkHREF.style.display = ''; linkHREF.innerHTML = '';
		var linkSpan 	= DOM.get("linkSpan");		linkSpan.style.display = 'none'; linkSpan.innerHTML = '';
		
		var yahooHREF 	= DOM.get("yahooHREF");
		var detailsTD 	= DOM.get("detailsTD");
		var detailsHREF = DOM.get("detailsHREF");
		
		var merchantTTD = DOM.get('merchantTTD');	merchantTTD.innerHTML = '';
		var merchantTD 	= DOM.get('merchantTD');	merchantTD.innerHTML = '';
		var merchantTR 	= DOM.get('merchantTR');	merchantTR.style.display = 'none';
		
		if(productData.link != '' || (diOwNeR == 0 && productData.im_selling == 1)){
			this.updateEl(linkHREF, productData.name);
		}else{
			this.updateEl(linkSpan, productData.name);
			linkHREF.style.display = 'none';
			linkSpan.style.display = '';
		}
		
		if(productData.cost > 0){
			if(productData.im_selling == 0){
				costTTD.innerHTML = 'I paid:';
			}else{
				costTTD.innerHTML = 'Selling for:';
			}
			costTR.style.display = ''; 
			this.updateEl(costTD, "$" + productData.cost);
		}
		else{costTR.style.display = 'none';}
		
		if(productData.im_selling == 0){
			linkHREF.href = productData.link;
			linkHREF.onclick = function(){};
			if(productData.where_to_buy != ''){
				merchantTTD.innerHTML = 'I bought this at:';
				merchantTD.innerHTML = productData.where_to_buy;
				merchantTR.style.display = '';
			}
		}
		else{
			if(diOwNeR == 0){
				linkHREF.onclick = function(){
					if(USER_ID != ''){
						ImSED.show(productData.product_id, '');
					}
					else{
						LD.show('im-selling-link' + productData.product_id, 'details.php?id='+IDEA_ID);
					}
					return;
				};
			}else{
				linkHREF.onclick = function(){};
			}
			
			linkHREF.href = "#";
			linkHREF.target = "_self";
			
			if(productData.where_to_buy != ''){
				merchantTR.style.display = '';
				merchantTTD.innerHTML = 'Buy this from:';
				merchantTD.innerHTML = productData.where_to_buy;
			}
			else{merchantTR.style.display = 'none';}
		}
		
		if(productData.tags){
			for(i=0; i < productData.tags.length; i++){
				var tagLink = document.createElement('A');
					tagLink.href = 'listing.php?cmd=reset&keyword=' + escape(productData.tags[i]);
					tagLink.target = '_self';
					tagLink.className = "TextBlueBody";
					tagLink.innerHTML = productData.tags[i];
				this.updateEl(tagsTD, tagLink, 1, 1, '&nbsp;');
							//obj, val, isObject, clearNot
			}
			tagsTR.style.display = '';
		}
		else{	
			tagsTR.style.display = 'none';
		}
		
		yahooHREF.href = "yahoo_search_results.php?pid="+productData.product_id+"&di_id="+IDEA_ID;
		detailsHREF.onclick = function(){
			displaySection(productData.product_id, 1);
			newADD.hideDetails();
		};
		this.showDetails(DOM.get(productData.note_id));
		//DOM.get("product-wait").style.display = "none";
	},

	showDetails : function (obj) {
		var pos = DOM.getXY(obj);
		//DOM.get("product-wait").style.display = "";
		//this.productDIV.style.display = "";
		getEl("productDIV").show(obj);
		DOM.setXY(this.productDIV, [pos[0]-60,pos[1]-61]);
	},
	
	hideDetails : function (obj) {
		getEl("productDIV").hide(true);
	},
	
	updateEl : function (obj, val, isObject, clearNot, separator) {

		if(!clearNot)
			this.clearEl(obj);

		if(isObject == 1){
			obj.appendChild(val);
			obj.appendChild(document.createTextNode(' '));
		}
		else{
			obj.appendChild(document.createTextNode(val));
			obj.appendChild(document.createTextNode(' '));
		}
	},
	
	clearEl : function (obj) {
		while(obj.firstChild)
			obj.removeChild(obj.firstChild);
	},
	
	hideDetails : function () {
		this.productDIV.style.display = "none";
	},
	
	getNotes : function () {
		this.hideDetails();
		var callback = {
			success : function(obj){
//			alert(obj.responseText);
				var response = eval('new Object(' + obj.responseText + ')');
				var err = '';
				if(response.errors.length > 0){
					for(var i in response.errors)
						err += response.errors[i] + "\n";
					return;
				}else{ newADD.loadNotes(response.notes); }
			}
		};
		this.exec({image_id:this.image_id,action:'get_notes', scaling: 1}, callback);
	},
	
	loadNotes : function (notes) {
		for(var i in notes){
			this.addNote(notes[i], 'load');
		}
		Gallery.isChangeAvail = 1;
	},
	
	addNote : function (result, type) {
		this.notesArr[result.id] = result.id;

		newImage = document.createElement("IMG");
		newImage.id = result.id + "_pinImage";
		newImage.src = "images/Point_Normal.gif";
		newImage.width = 10;
		newImage.height = 10;
		
		var note = document.createElement("DIV");
			note.style.width = newImage.width + "px";
			note.style.height = newImage.height + "px";
			note.style.cursor = "pointer";
			note.appendChild(newImage);
			note.id = result.id;
			note.setAttribute("objType", "note");
			note.className = "comment_over_all";
			
			note.onmouseover = function(ev, el){
							if(!el) var el = this;
							//newADD.showDetails(el);
							//newADD.note = el;
							//DOM.get(el.id + "_pinImage").src = "images/Point_MouseOver.gif";
							newADD.getDetails(el);
							//setTimeout("show5()", 2000);
						};
						
		var imCoor = DOM.getXY(this.image);
		this.image.appendChild(note);
		note.setAttribute("startPos", [Number(imCoor[0])+Number(result.coord[0]), Number(imCoor[1])+Number(result.coord[1])]);
		//note.style.border = "solid 2px red";
		DOM.setXY(note, [Number(imCoor[0])+Number(result.coord[0]), Number(imCoor[1])+Number(result.coord[1])]);
	},
	
	destroyNotes : function (){
		for(noteC in this.notesArr){
			this.image.removeChild(DOM.get(this.notesArr[noteC]));
		}
	},
	
	exec : function(data, callback, targetURL){
		var request = "";
		for(i in data){
			if(request != "") request += "&";
			request += i + "=" + data[i];
		}
		if(!targetURL) var targetURL = this.wrapperScript;
		YAHOO.util.Connect.asyncRequest('POST', targetURL, callback, request);
	},
	
	rePositionPins : function () {
		this.destroyNotes();
		this.getNotes();
	}
}

window.onresize = function () { newADD.rePositionPins(); /*Gallery.resetLayout();*/ };

function show5 ()
{
	var el = newADD.note;
	newADD.showDetails(el);
	DOM.get(el.id + "_pinImage").src = "images/Point_Normal.gif";
}

