/*
*/
var addIcon;
var add_wb_icons = new Array();
var workbook_logged_in = false;
function gallery_btn(img)
{
	addIcon = new StickyWin({
	  allowMultiple:false,
	  relativeTo:$(img),
	  offset: {x: 1, y: 1},
	  edge:'upperLeft',
	  position:'upperLeft',
	  content: StickyWin.ui('Add Image to Your Workbook', 'Click the Add button to save this image to your workbook.', {
	    css: "div.DefaultStickyWin div.body{font-family:verdana; font-size:11px; line-height: 13px;}"+
		"div.DefaultStickyWin div.top{display:none}"+
		"div.DefaultStickyWin div.top_ul{background:none; height:30px; width:15px; float:left}"+
		"div.DefaultStickyWin div.top_ur{position:relative; left:0px !important; left:-4px; background:none; height:30px; margin:0px 0px 0px 15px !important; margin-right:-4px; padding:0px}"+
		"div.DefaultStickyWin h1.caption{margin:0px 5px 0px 0px; overflow: hidden; padding:0; font-weight:bold; color:white; font-size:14px; position:relative; top:8px; left:5px; float: left; height: 22px;}"+
		"div.DefaultStickyWin div.middle, div.DefaultStickyWin div.closeBody {background:none; margin:0px 0px 0px 0px !important;	margin-bottom: -3px; position: relative;	top: 0px !important; top: -3px;}"+
		"div.DefaultStickyWin div.body{background:none; padding:8px 30px 8px 0px; margin-left:5px; position:relative; right:-20px}"+
		"div.DefaultStickyWin div.bottom{clear:both}"+
		"div.DefaultStickyWin div.bottom_ll{background:none; width:15px; height:15px; float:left}"+
		"div.DefaultStickyWin div.bottom_lr{background:none; position:relative; left:0px !important; left:-4px; margin:0px 0px 0px 15px !important; margin-right:-4px; height:15px}"+
		"div.DefaultStickyWin div.closeButtons{text-align: left; background:url({%baseHref%}body.png) top right repeat-y; padding: 0px 30px 8px 0px; margin-left:5px; position:relative; right:-20px}"+
		"div.DefaultStickyWin a.button:hover{background-color:white;/*background:url({%baseHref%}big_button_over.gif) repeat-x*/}"+
		"div.DefaultStickyWin a.button {background-color:white;/*background:url({%baseHref%}big_button.gif) repeat-x;*/ margin: 2px 8px 2px 8px; padding: 2px 5px; cursor:pointer; border: 1px solid #999 !important; text-decoration:none;font-size:10px; color: #000 !important;}"+
		"div.DefaultStickyWin div.closeButton{width:13px; height:13px; background:url({%baseHref%}closebtn.gif) no-repeat; position: absolute; right: 0px; margin:10px 15px 0px 0px !important; cursor:pointer}"+
		"div.DefaultStickyWin div.dragHandle {	width: 11px;	height: 25px;	position: relative;	top: 5px;	left: -3px;	cursor: move;	background: url({%baseHref%}drag_corner.gif); float: left;}",
	    width: '200px',
	    cssClass:'darkWin',
	    buttons: [
	      {
	        text: 'Close', 
	        onClick:function(){ addIcon.destroy();}
	      },
	      {
	        text: 'Add', 
	        onClick:function(){ send_item(img,this.getParent().getParent().getParent())},
	        properties: {'class': 'add_item_btn_ok'}
	      }
	    ]
	  })
	  
	});
	$(addIcon.id).addEvent('mouseleave',function(){
		addIcon.destroy();
	});
}

function add_item(img,add_icon)
{
	if ( $chk ( img ) )
	{
		coor = $(img).getCoordinates();
		//console.log(coor);
		var icon_id_str = 'add_wb_img_' + coor.left + '_' + coor.top;
		if ( !add_wb_icons.contains(icon_id_str) )
		{
			icon = new Element('img',{
				'src':'/_pdwms/workbook/support/gfx/add_wb_img.gif',
				styles:{
					left:coor.left + 5,
					top:coor.top + 5,
					position:'absolute',
					zIndex:9999,
					cursor:'pointer'
				},
				'relative':img.src,
				'class':'add_wb_img',
				events:{
					'mouseenter':function(){
						this.setStyle('display','block');
						addIcon = new StickyWin({
						  allowMultiple:false,
						  
						  relativeTo:$(img),
						  offset: {x: 1, y: 1},
						  edge:'upperLeft',
						  position:'upperLeft',
						  content: StickyWin.ui('Add Image to Your Workbook', 'Add this image to your workbook?<br/><br/>', {
						    css: "div.DefaultStickyWin div.body{font-family:verdana; font-size:11px; line-height: 13px;}"+
							"div.DefaultStickyWin div.top{display:none}"+
							"div.DefaultStickyWin div.top_ul{background:none; height:30px; width:15px; float:left}"+
							"div.DefaultStickyWin div.top_ur{position:relative; left:0px !important; left:-4px; background:none; height:30px; margin:0px 0px 0px 15px !important; margin-right:-4px; padding:0px}"+
							"div.DefaultStickyWin h1.caption{margin:0px 5px 0px 0px; overflow: hidden; padding:0; font-weight:bold; color:white; font-size:14px; position:relative; top:8px; left:5px; float: left; height: 22px;}"+
							"div.DefaultStickyWin div.middle, div.DefaultStickyWin div.closeBody {background:none; margin:0px 0px 0px 0px !important;	margin-bottom: -3px; position: relative;	top: 0px !important; top: -3px;}"+
							"div.DefaultStickyWin div.body{background:none; padding:8px 0px 0px 0px; margin-left:5px; position:relative; right:-20px}"+
							"div.DefaultStickyWin div.bottom{clear:both}"+
							"div.DefaultStickyWin div.bottom_ll{background:none; width:15px; height:15px; float:left}"+
							"div.DefaultStickyWin div.bottom_lr{background:none; position:relative; left:0px !important; left:-4px; margin:0px 0px 0px 15px !important; margin-right:-4px; height:15px}"+
							"div.DefaultStickyWin div.closeButtons{text-align: left; background:url({%baseHref%}body.png) top right repeat-y; padding: 0px 30px 8px 0px; margin-left:5px; position:relative; right:-20px}"+
							"div.DefaultStickyWin a.button:hover{background-color:white;/*background:url({%baseHref%}big_button_over.gif) repeat-x*/}"+
							"div.DefaultStickyWin a.button {background-color:white;/*background:url({%baseHref%}big_button.gif) repeat-x;*/ margin: 0px 5px 0px 0px; padding: 2px 5px; cursor:pointer; border: 1px solid #999 !important; text-decoration:none;font-size:10px; color: #000 !important;}"+
							"div.DefaultStickyWin div.closeButton{width:13px; height:13px; background:url({%baseHref%}closebtn.gif) no-repeat; position: absolute; right: 0px; margin:10px 15px 0px 0px !important; cursor:pointer}"+
							"div.DefaultStickyWin div.dragHandle {	width: 11px;	height: 25px;	position: relative;	top: 5px;	left: -3px;	cursor: move;	background: url({%baseHref%}drag_corner.gif); float: left;}",
						    width: '180px',
						    cssClass:'darkWin',
						    buttons: [
						      {
						        text: 'Add', 
						        onClick:function(){ send_item(img,this.getParent().getParent().getParent())},
						        properties: {'class': 'add_item_btn_ok'}
						      },
						      {
						        text: 'Close', 
						        onClick:function(){ addIcon.destroy();}
						      }
						    ]
						  })
						  
						});
						$(addIcon.id).addEvent('mouseleave',function(){
							addIcon.destroy();
						});
					},
					'mouseleave':function(){
						this.setStyle('display','none');

					}
				}
			});
			icon.id = icon_id_str;
			add_wb_icons.push(icon.id);
			
			$$('body').adopt(icon);
		}
		else
		{
			$(icon_id_str).setProperty('relative',img.src);
			$(icon_id_str).setStyle('display','block');
		}
	}
}
function add_gallery_item(img,add_icon,container)
{
	if ( $chk ( img ) )
	{
		coor = $(container).getCoordinates();
		//console.log(coor);
		var icon_id_str = 'add_wb_img_' + coor.left + '_' + coor.top;
		if ( !add_wb_icons.contains(icon_id_str) )
		{
			icon = new Element('img',{
				'src':'/_pdwms/workbook/support/gfx/add_wb_img.gif',
				styles:{
					left:coor.left + 5,
					top:coor.top + 5,
					position:'absolute',
					zIndex:9999,
					cursor:'pointer'
				},
				'relative':img.src,
				'class':'add_wb_img',
				events:{
					'mouseenter':function(){
						this.setStyle('display','block');
						addIcon = new StickyWin({
						  allowMultiple:false,
						  
						  relativeTo:$(container),
						  offset: {x: 1, y: 1},
						  edge:'upperLeft',
						  position:'upperLeft',
						  content: StickyWin.ui('Add Image to Your Workbook', 'Add this image to your workbook?<br/><br/>', {
						    css: "div.DefaultStickyWin div.body{font-family:verdana; font-size:11px; line-height: 13px;}"+
							"div.DefaultStickyWin div.top{display:none}"+
							"div.DefaultStickyWin div.top_ul{background:none; height:30px; width:15px; float:left}"+
							"div.DefaultStickyWin div.top_ur{position:relative; left:0px !important; left:-4px; background:none; height:30px; margin:0px 0px 0px 15px !important; margin-right:-4px; padding:0px}"+
							"div.DefaultStickyWin h1.caption{margin:0px 5px 0px 0px; overflow: hidden; padding:0; font-weight:bold; color:white; font-size:14px; position:relative; top:8px; left:5px; float: left; height: 22px;}"+
							"div.DefaultStickyWin div.middle, div.DefaultStickyWin div.closeBody {background:none; margin:0px 0px 0px 0px !important;	margin-bottom: -3px; position: relative;	top: 0px !important; top: -3px;}"+
							"div.DefaultStickyWin div.body{background:none; padding:8px 0px 0px 0px; margin-left:5px; position:relative; right:-20px}"+
							"div.DefaultStickyWin div.bottom{clear:both}"+
							"div.DefaultStickyWin div.bottom_ll{background:none; width:15px; height:15px; float:left}"+
							"div.DefaultStickyWin div.bottom_lr{background:none; position:relative; left:0px !important; left:-4px; margin:0px 0px 0px 15px !important; margin-right:-4px; height:15px}"+
							"div.DefaultStickyWin div.closeButtons{text-align: left; background:url({%baseHref%}body.png) top right repeat-y; padding: 0px 30px 8px 0px; margin-left:5px; position:relative; right:-20px}"+
							"div.DefaultStickyWin a.button:hover{background-color:white;/*background:url({%baseHref%}big_button_over.gif) repeat-x*/}"+
							"div.DefaultStickyWin a.button {background-color:white;/*background:url({%baseHref%}big_button.gif) repeat-x;*/ margin: 0px 5px 0px 0px; padding: 2px 5px; cursor:pointer; border: 1px solid #999 !important; text-decoration:none;font-size:10px; color: #000 !important;}"+
							"div.DefaultStickyWin div.closeButton{width:13px; height:13px; background:url({%baseHref%}closebtn.gif) no-repeat; position: absolute; right: 0px; margin:10px 15px 0px 0px !important; cursor:pointer}"+
							"div.DefaultStickyWin div.dragHandle {	width: 11px;	height: 25px;	position: relative;	top: 5px;	left: -3px;	cursor: move;	background: url({%baseHref%}drag_corner.gif); float: left;}",
						    width: '180px',
						    cssClass:'darkWin',
						    buttons: [
						      {
						        text: 'Add', 
						        onClick:function(){ send_item(img,this.getParent().getParent().getParent())},
						        properties: {'class': 'add_item_btn_ok'}
						      },
						      {
						        text: 'Close', 
						        onClick:function(){ addIcon.destroy();}
						      }
						    ]
						  })
						  
						});
						$(addIcon.id).addEvent('mouseleave',function(){
							addIcon.destroy();
						});
					},
					'mouseleave':function(){
						this.setStyle('display','none');

					}
				}
			});
			icon.id = icon_id_str;
			add_wb_icons.push(icon.id);
			
			$$('body').adopt(icon);
		}
		else
		{
			$(icon_id_str).setProperty('relative',img.src);
			$(icon_id_str).setStyle('display','block');
		}
	}
}
function send_item(img)
{
	//var add_icon_body = add_icon.getElement('.body');
	//add_icon_body.set('text','Attempting to add image. Please wait.');
	workbook_id = '';
	// ajax submit.
	url = BASE_URL + "/_pdwms/workbook/support/php/workbook_ajax.php?rand=1259862409";
	new Request( {
		url: url,
		method: 'post',
		data:{
			'action':'add_image',
			'ak':'5eb3f7c977724304cf8cb05aa159b478',
			'wbi_filepath':img.src,
			'workbook_id':workbook_id
		},
		onComplete: function(resp){
			ajaxResult = JSON.decode(this.response.text);
			addIcon.hide();
			var msg;
			if ( ajaxResult.result != 'success' )
			{
				msg = ajaxResult.result;
			}
			else
			{
				msg =  '<p>The image was successfully added.</p>'
			} 
			
			new StickyWin({
			  allowMultiple:false,
			  relativeTo:$(img),
			  offset: {x: 0, y: 0},
			  edge:'upperLeft',
			  position:'upperLeft',
			  content: StickyWin.ui('Add Image',msg, {
			    width: '400px',
			    cssClass:'darkWin',
			    buttons: [{
			      text: 'Continue Browsing', 
			      onClick: function(){}
			    },
			    {
			      text:'Go to workbook',
			      onClick: function(){ window.location.href=URL_ADJUSTMENT + '/project/index.php';}
			    }]
			  })
			});
			
		}				
	}).send();
}

function set_gallery_action(imgs,container)
{
	
	imgs.each(function(img){
		if (!img.src.test('pxl'))
		{
			img.addEvents({
				'mouseenter':function(){
					add_gallery_item(img,this.getParent().getParent().getParent(),container);
				},
				'mouseleave':function(){
					$$('.add_wb_img').setStyle('display','none');
				}
			
			});
		}
	});
}
function set_workbook_action(imgs)
{
	
	imgs.each(function(img){
		if (!img.src.test('pxl'))
		{
			img.addEvents({
				'mouseenter':function(){
					add_item(img,this.getParent().getParent().getParent());
				},
				'mouseleave':function(){
					$$('.add_wb_img').setStyle('display','none');
				}
			
			});
		}
	});
}

window.addEvent('domready',function(){
	
	});

