1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * This file holds the main javascript functions needed for avia-media uploads
 *
 * @author		Christian "Kriesi" Budschedl
 * @copyright	Copyright ( c ) Christian Budschedl
 * @link		http://kriesi.at
 * @link		http://aviathemes.com
 * @since		Version 1.0
 * @package 	AviaFramework
 */

(function($)
{
	var avia_media = {
	
		aviaUseCustomEditor: false,
		aviaPostID: false,
		insertContainer : false,
		
		// bind the click event to all elements with the class avia_uploader 
		bind_click: function()
		{
			$('.avia_uploader').live('click', function()
			{
				var title  = this.title,
					idBased = "";
				this.title = "";
				
				
				avia_media.aviaPostID = this.hash.substring(1);
				avia_media.aviaUseCustomEditor = true;
				avia_media.insertContainer  = $(this).parents('.avia_upload_container');
				
				//
				if(avia_media.insertContainer.is('.avia_advanced_upload'))
				{
					idBased = "&avia_idbased="+ $('.avia_upload_input', avia_media.insertContainer).attr('name');
				}
				
				var label = $(this).parents('.avia_upload_container').find('.avia_upload_insert_label').trigger('change').val();
				
				tb_show( title, 'media-upload.php?post_id='+avia_media.aviaPostID+idBased+'&avia_label='+encodeURI(label)+"&TB_iframe=true" );
				
				
				return false;
			});
		},
		
		// bind the click event of the remove image links to the removing function
		bind_remove: function()
		{
			$('.avia_remove_image').live('click', function()
			{
				var container = $(this).parents('.avia_upload_container');
					
					container.find('.avia_upload_input').val('').trigger('change');
					container.find('.avia_preview_pic').hide(400, function(){ $(this).html("").css({display:"block"}); });
					
				return false;
			});
		},
		
		bind_blur: function()
		{
			$('.avia_upload_input').live('blur', function()
			{
				var input = $(this),
					value = input.val(),
					image = '<img src ="'+value+'" alt="" />',
					div = input.parents('.avia_upload_container:eq(0)').find('.avia_preview_pic');
					
					div.html('<a href="#" class="avia_remove_image">remove</a>' + image);
				
			});
		},
		
		//changes the label of the "insert into post" button to better reflect the current action and hides the use as post-thumb button
		change_label: function()
		{	
			avia_media.idBasedUpload();
			
			var newlabel = $('.avia_insert_button_label').val();
			
			if(newlabel != "" && typeof newlabel == 'string')
			{				
				var savesendContainer = $(".savesend");
				
				if(savesendContainer.length > 0)
				{		
					$(".button", savesendContainer).val(newlabel);
					$(".wp-post-thumbnail", savesendContainer).css('display','none');	
				}
			}
		},
		
		//hijack the original uploader and replace it if a user clicks on one an avia_uploader
		hijack_uploader: function()
		{			
			window.original_send_to_editor = window.send_to_editor;
     		window.send_to_editor = function(html)
     		{     			
     			if(avia_media.aviaUseCustomEditor)
				{
					var container = avia_media.insertContainer,
						returned = $(html),
						img = returned.attr('src') || returned.find('img').attr('src') || returned.attr('href');
					
					container.find('.avia_upload_input').val(img);
					container.find('.avia_preview_pic').html('<a href="#" class="avia_remove_image">remove</a><img src='+img+' alt="" />');
					
					tb_remove();
		     		avia_media.reset_uploader();
				}	
				else
				{
					window.original_send_to_editor(html);
				}
     		};
		},
		
			//id based advanced upload
		idBasedUpload: function()
		{
		
			var idbased = $('.avia_idbased');			
			
			if(idbased.length > 0)
			{
				idbased = idbased.val();
				
			
				var savesendContainer = $(".savesend"),
					insertInto = $(".button", savesendContainer),
					target =  $("input[name="+idbased+"]", parent.document),
					imageTarget = $("#div_"+idbased+"", parent.document),
					filter = $("#filter"), 
					label = $(".avia_insert_button_label");
					
				
				//add the id based and the insert name field as a form input so it gets sent in case the user uses the search or filter functions
				if(filter.length)
				{
					var filterInsert = filter.find("input[name=avia_idbased]"),
						labelInsert	 = filter.find("input[name=avia_label]");
					
					if(!filterInsert.length)
					{
						filter.prepend("<input type='hidden' name='avia_idbased' value='"+idbased+"'/>");
					}
					
					if(label.length && !labelInsert.length)
					{
						filter.prepend("<input type='hidden' name='avia_label' value='"+label.val()+"'/>");
					}
					
				}
				
				
				
				insertInto.unbind('click').bind('click', function()
				{
					var attachment_id = this.name.replace(/send\[/,"").replace(/\]/,"");	
								
					$.ajax({
			 		  type: "POST",
			 		  url: window.ajaxurl,
			 		  data: "action=avia_ajax_get_image&attachment_id="+attachment_id,
			 		  success: function(msg)
			 		  {
			 		  	target.val(attachment_id);
						imageTarget.html('<a href="#" class="avia_remove_image">remove</a>'+msg);
			 		  	parent.tb_remove();
			 		  	avia_media.reset_uploader();
			 		  }
			 		});
			 		
			 		return false;
				});	
			}
		},
		
		
		
		//reset values for the next upload
		reset_uploader: function()
		{
     		avia_media.aviaUseCustomEditor = false;
     		avia_media.aviaPostID = false;
     		avia_media.insertContainer = false;     		
		}
		
		
	};
	

	
	$(function()
	{
		$('#media-buttons a').click(avia_media.reset_uploader);
		avia_media.bind_click();
		avia_media.bind_blur();
		avia_media.bind_remove();
		avia_media.idBasedUpload();
		avia_media.hijack_uploader();
		avia_media.change_label();
		$(".slidetoggle").live('mouseenter',avia_media.change_label);
 	});

	
})(jQuery);