function openInFrame(url, closehandler){
	var frame = jQuery("<div id='flashFrame' class='frame-window'><div class='frame-container'><iframe id='t3frame' name='t3frame' src='"+url+"' scrolling='no'></iframe></div></div>");
	//<a href='#' class='close'></a>
	/*frame.find('a.close').click(function(){
		jQuery(this).parents().find('#flashFrame').remove();
		
		if(closehandler) closehandler();
		return false;
	});
	*/
	
	jQuery("div#main").append(frame);
	return frame;
}

function deletehotspot(hotspot){
	var url = "typo3/tce_db.php?cmd[tx_huseby_hotspots]["+hotspot.uid+"][delete]=1&redirect=../typo3conf/ext/tomaso_cms/modsub3/index.php&vC=a30f7d97da&prErr=1&uPT=1";
	var frm = openInFrame(url).find('iframe');
	var swfO = jQuery( '#kjokken')[0];
	
	frm.css("width","1px");
	frm.css("height","1px");
	frm.css("overflow","hidden");
	
	
	frm.load(function(e){
		var fdoc = jQuery(this.contentWindow.document);
		var docname = unescape(this.contentWindow.location.href);//.split("/").pop(); 
		var closed = docname.indexOf('&redirect=') == -1;
		
		if(closed){
			jQuery('#flashFrame').hide();
			setTimeout(function(){jQuery('#flashFrame').remove()},3000);
			swfO.deletehotspot(hotspot);
			return;
		}		
	});
}

function savehotspot(hotspot, autosave){
	autosave = autosave == true;
	var retURL = "../typo3conf/ext/tomaso_cms/modsub3/index.php";
	var url = "typo3/alt_doc.php?&returnUrl="+escape(retURL)+"&columnsOnly=xpos,ypos";
	if(!autosave) url += ",title,video,bodytext,product_link,imageindex,hidden";

	if(hotspot.uid == 0)
		url += "&edit[tx_huseby_hotspots][94]=new";
	else 
		url += "&edit[tx_huseby_hotspots]["+hotspot.uid+"]=edit";

	var hData = {xpos:hotspot.x,ypos:hotspot.y,product_link:"tt_content_"+hotspot.productid,imageindex:hotspot.index,hidden:0};
		
	urlData = "";
	urlData += "&overrideVals[tx_huseby_hotspots][xpos]="+hotspot.x;
	urlData += "&overrideVals[tx_huseby_hotspots][ypos]="+hotspot.y;
	if(!autosave){
		urlData += "&overrideVals[tx_huseby_hotspots][product_link]="+hotspot.productid;
		urlData += "&overrideVals[tx_huseby_hotspots][imageindex]="+hotspot.index;
		urlData += "&overrideVals[tx_huseby_hotspots][hidden]=0";
	}

	urlData += "&defVals[tx_huseby_hotspots][xpos]="+hotspot.x;
	urlData += "&defVals[tx_huseby_hotspots][ypos]="+hotspot.y;
	if(!autosave){
		urlData += "&defVals[tx_huseby_hotspots][product_link]="+hotspot.productid;
		urlData += "&defVals[tx_huseby_hotspots][imageindex]="+hotspot.index;
		urlData += "&defVals[tx_huseby_hotspots][hidden]=0";
	}
	
  	var swfO = jQuery('#kjokken')[0];
	
	var frm = openInFrame(url+urlData, swfO.savehotspot).find('iframe');
	if(autosave){
		frm.css("width","1px");
		frm.css("height","1px");
		frm.css("overflow","hidden");
	}
	
	frm.load(function(e){
		var fdoc = jQuery(this.contentWindow.document);
		var docname = unescape(this.contentWindow.location.href);//.split("/").pop(); 
		var closed = docname.indexOf('&returnUrl='+retURL) == -1;
		
		if(closed){
			jQuery('#flashFrame').hide();
			setTimeout(function(){jQuery('#flashFrame').remove()},3000);
			//console.debug(hotspot);
			try{
				swfO.savehotspot({hotspotdata:hotspot, autosave:(autosave == true)});
			}catch(e){
				
			}
			return;
		}
		
		fdoc.find("input[name^='data\[tx_huseby_hotspots\]']").each(function(index,item){
			var s = jQuery(item).attr('name').split(/\[|\]/);
			s.pop();
			var vname = s.pop();
		
			if(hData[vname] != undefined){
				item.value = hData[vname];
			} else {
				try{
					hotspot[vname] = item.value;
				} catch(e){}
			}
			
		});
		try{
			hotspot["details"] = fdoc.find('textarea[id^="RTEareadata\[tx_huseby_hotspots\]"]').html().replace(/&lt;/gi,"<").replace(/&gt;/gi,">").replace(/&nbsp;/gi," ");
		} catch(e){
			hotspot["details"] = "";
		}
		
		window.hotspotDebug = hotspot;
		
		fdoc.find("input[name='_savedokview']").remove();
		if(!autosave) 
			fdoc.find("input[name='_saveandclosedok']").remove();
		else 
			fdoc.find("input[name='_savedok']").remove();
		fdoc.find("input[name='_savedoknew']").remove();
		fdoc.find("div.buttonsright").remove();
		fdoc.find("div#typo3-docheader-row2").remove();
		
		if(autosave){
			fdoc.find("input[name='_saveandclosedok']").click();
			return;
		}
		
	});
}
