// publisher.js
var player_chrome_width = 20;
var player_chrome_height = 126;
var min_player_height = 380 - player_chrome_height;

var MagnifyPublisher = Class.create();

MagnifyPublisher.id_hash = {};

MagnifyPublisher.prototype = {
	
	//// Setup 
	
  initialize: function () {
		return;
	},
	
	initialize_interface: function () {
		this.initialize_tooltabs();
		this.initialize_image_search_tool();
		this.initialize_search_tool();
	},
	
	initialize_tooltabs: function () {
		this.tooltabs_tabs_seq = new Sequence ( 'mvp-embedder-toolbar' );
		this.tooltabs_tabs_seq.active_class = 'mvp-widget-toolbtn-active';
		
		this.tooltabs_tool_seq = new Sequence ( 'mvp-embedder-tool-area' );
		this.tooltabs_tool_seq.active_class = 'mvp-embedder-tool-panel-active';
		
		this.selected_tool = 'search';
		
		this.tooltabs_tabs_seq.clickable_observe_events();
		var this_publisher = this;
		this.tooltabs_tabs_seq.onactivate = function ( seq, child, spec, index, trailing ) {
			this_publisher.tooltabs_tool_seq.activate( index );
			this_publisher.selected_tool = trailing;
			this_publisher.toggle_splash_msg();
		};
	},

	toggle_splash_msg: function ( show_splash ) {
		var class_type = this.selected_tool.gsub('_', '-');
		if ( $$('#mvp-embedder-' + class_type + '-tips .mvp-embedder-splash-area') != '') {
			$$('#mvp-embedder-' + class_type + '-tips .mvp-embedder-splash-area').first().style.display = show_splash ? 'block' : 'none';
			$$('#mvp-embedder-' + class_type + '-tips .mvp-embedder-help-area').first().style.display = show_splash ? 'none' : 'block';
		}
	},

	
	//// Search
	
	initialize_search_tool: function () {
		this.search_services_seq = new Sequence (
			$$('div.mvp-embedder-search-services').first() 
		);
		this.search_services_seq.active_class = 'mvp-embedder-search-service-active';
		this.search_display_seq = new Sequence (
			$$('div.mvp-embedder-search-display').first()  
		);
		this.search_display_seq.active_class = 'mvp-embedder-search-panel-active';
		this.search_input_field = $('mvp_search_query_input');
	},
	
	search_service_activate: function ( service ) {
		this.search_services_seq.activate(
			 '#mvp-embedder-search-service-' + service 
		)
	},
	
	search_display_id_or_ajax: function ( trailing_id, ajax_url, ajax_params ) {
		this.search_display_seq.activate_by_id_or_ajax(
			'mvp-embedder-search-' + trailing_id, 
			ajax_url, 
			ajax_params
		);
	},
	
	search_display_results: function ( query, service, page ) {
		if ( this.selected_tool == 'image_search' ) {
			return this.image_search_display_results(query, service, page);
		}
		service = service || 'youtube';
		page = page || 1;
		
		this.search_query_value = query;
		this.search_service_value = service;
		
		this.search_service_activate( service );
		
		if ( ! query || ! query.match( /\S/ ) ) {
			this.search_display_id_or_ajax( 
				'tips',
				'/embed/publisher_tools/search_tips' 
			);
		} 
		else {
			this.search_display_id_or_ajax( 
				'panel-' + service + '-' + escape(query) + '-' + page, 
				'/embed/publisher_tools/search_results', 
				{ 
					search: query, 
					service: service, 
					page: page 
				} 
			);
		}
	},
	search_display_index: function ( item_index ) {
		var query = this.search_query_value;
		var service = this.search_service_value;
		
		this.search_display_id_or_ajax( 
			'details-' + service + '-' + escape(query) + '-' + item_index, 
			'/embed/publisher_tools/search_item', 
			{ 
				search: query, 
				service: service, 
				index: item_index
			} 
		);
	},
	search_display_item: function ( media_cid, content_cid, search_total, search_index ) {
		if ( this.selected_tool == 'image_search' ) {
			this.image_search_total = search_total;
			return this.image_search_display_index( search_index );
		}
		this.search_display_id_or_ajax( 
			'details-' + media_cid + '-' + content_cid, 
			'/embed/publisher_tools/search_detail', 
			{
				media: media_cid,
				content: content_cid,
				search: this.search_query_value, 
				service: this.search_service_value, 
				search_index: search_index,
				search_total: search_total
			}
		);
	},
	
	//// Image Search
		
	initialize_image_search_tool: function () {
		this.image_search_services_seq = new Sequence (
			$$('div.mvp-embedder-image-search-services').first() 
		);
		this.image_search_services_seq.active_class = 'mvp-embedder-image-search-service-active';
		this.image_search_display_seq = new Sequence (
			$$('div.mvp-embedder-image-search-display').first()  
		);
		this.image_search_display_seq.active_class = 'mvp-embedder-image-search-panel-active';
		this.image_search_input_field = $('mvp_image_search_query_input');
	},
		
	image_search_service_activate: function ( service ) {
		this.image_search_services_seq.activate(
			 '#mvp-embedder-search-service-' + service 
		)
	},
	
	image_search_display_id_or_ajax: function ( trailing_id, ajax_url, ajax_params ) {
		this.image_search_display_seq.activate_by_id_or_ajax(
			'mvp-embedder-image-search-' + trailing_id, 
			ajax_url, 
			ajax_params
		);
	},
	
	image_search_display_results: function ( query, service, page ) {
		service = service || 'flickr';
		page = page || 1;
		this.image_search_query_value = query;
		this.image_search_service_value = service;
		
		this.image_search_service_activate( service );
		
		if ( ! query || ! query.match( /\S/ ) ) {
			this.image_search_display_id_or_ajax( 
				'tips',
				'/embed/publisher_tools/search_tips' 
			);
		} 
		else {
			this.image_search_display_id_or_ajax( 
				'panel-' + service + '-' + escape(query) + '-' + page, 
				'/embed/publisher_tools/image_search_results', 
				{ 
					search: query, 
					service: service, 
					page: page 
				} 
			);
		}
	},
	image_search_display_index: function ( item_index ) {
		var query = this.image_search_query_value;
		var service = this.image_search_service_value;
		
		this.image_search_display_id_or_ajax( 
			'details-' + service + '-' + escape(query) + '-' + item_index, 
			'/embed/publisher_tools/image_search_item', 
			{ 
				search: query, 
				service: service, 
				index: item_index,
				search_total: this.image_search_total
			} 
		);
	},
	image_search_display_item: function ( photo_id, secret, search_total, search_index ) {
		this.image_search_display_id_or_ajax( 
			'details-' + photo_id + '-' + secret, 
			'/embed/publisher_tools/image_search_detail', 
			{
				media: photo_id,
				content: secret,
				search: this.image_search_query_value, 
				service: this.image_search_service_value, 
				search_index: search_index,
				search_total: search_total
			}
		);
	},
	
	
	
	
	//// Media Values
	
	media_values: function ( publish_type ) {
		this.media_value_hash = 
		this.media_value_hash || {};
		return ( this.media_value_hash[ publish_type ] = 
		         this.media_value_hash[ publish_type ] || {} );
	},
	
	media_update: function ( publish_type, value_hash ) {
		var media_display_div = $$('#mvp-embedder-create-' + publish_type).first();
		
		var media_values = this.media_values( publish_type );
		var new_value;
		if ( new_value = value_hash.content_cid ) {
			media_values.content_cid = new_value;
		}
		if ( new_value = value_hash.media_cid ) {
			media_values.media_cid = new_value;
		}
		if ( new_value = value_hash.description ) {
			media_values.description = new_value;
		}
		if ( new_value = value_hash.tags ) {
			media_values.tags = new_value;
		}
		if ( new_value = value_hash.embed_align ) {
			media_values.embed_align = new_value;
		}
		if ( new_value = value_hash.embed_height ) {
			media_values.embed_height = new_value;
		}
		if ( new_value = value_hash.widget_template_cid ) {
			media_values.widget_template_cid = new_value;
		}
		if ( new_value = value_hash.embed_width ) {
			media_values.embed_width = new_value;
		}
		if ( new_value = value_hash.embed_color ) {
			media_values.embed_color = new_value;
		}
		if ( new_value = value_hash.license ) {
			media_values.license = new_value;
		}
		if ( new_value = value_hash.attribution ) {
			media_values.attribution = new_value;
		}
		if ( new_value = value_hash.title ) {
			media_values.title = new_value;
			if ( media_display_div ) {
				var title_el = media_display_div.select('.mvp-embedder-details-title').first();
				title_el.innerHTML = new_value;
			}
		}
		if ( new_value = value_hash.thumbnail ) {
			media_values.thumbnail = new_value;
			if ( media_display_div ) {
				if ( new_value == '-' ) {
					new_value = 'http://s3.amazonaws.com/magnifythumbs/no_thumbnail.gif';
				}
				if ( new_value == ' ' ) {
					new_value = 'http://decor.magnify.net/decor/live/transparent.gif';
				}
				var img_el = media_display_div.select('.mvp-embedder-details-thumb img').first();
				img_el.src = new_value;
			}
		}
		if ( new_value = value_hash.status ) {
			media_values.status = new_value;
			if ( media_display_div ) {
				var status_el = media_display_div.select('.mvp-embedder-details-status').first();
				status_el.innerHTML = new_value;
			}
		}
		if ( new_value = value_hash.ready ) {
			media_values.ready = new_value;
			if ( media_display_div && (value_hash.ready == true) ) {
				var submit_div = media_display_div.select('.mvp-embedder-details-action').first();
				var submit_btn = submit_div.select('.mvp-embedder-submit-button').first();
				submit_btn.disabled = ! new_value;
			}
		}
	},
	
	
	//// Callback
	
	configure_callback: function ( type, callback ) {
		this.callback_type = type;
		this.callback_value = callback;
	},

	display_embed: function ( result_hash ) {
		this.do_callback( result_hash ); 
	},
	
	do_callback: function ( result_hash ) {
		var calltype = this.callback_type;
		var callback = this.callback_value;
		
		if ( calltype == 'ScriptFunc' ) {
			var func = eval(callback);
			func( result_hash );
			
		} else if ( calltype == 'GetFrame' ) {
		  var dest_url = callback;
			dest_url += ( dest_url.match( /[?]/ ) ? '&' : '?' );
			dest_url += $H( result_hash ).toQueryString();
			setTimeout( 'window.location = "' + dest_url + '"', 100 );
			
		} else if ( calltype == 'GetPage' ) {
		  var dest_url = callback;
			dest_url += ( dest_url.match( /[?]/ ) ? '&' : '?' );
			dest_url += $H( result_hash ).toQueryString();
			setTimeout( 'window.top.location = "' + dest_url + '"', 100 );
			
		} else {
			alert( "I don't know how to handle a callback of type '" + calltype + '"!')
		}
	},
	
	set_player_height: function() {
		var media_values = this.media_values( this.selected_tool );
		if ( typeof media_values.embed_width == 'undefined' ) {
			media_values.embed_width = '';
		}
		var url = '/embed/publisher_tools/player_height?media_item_cid=' + media_values.media_cid + '&width=' + media_values.embed_width;
		var this_o = this;
		new Ajax.Request(url, { 
			onSuccess: function(transport) { 
				var regex = /(media item|width)/;
				var failure = regex.test(transport.responseText);
				if ( ! failure ) {
					embed_height = transport.responseText;
					if ( embed_height < min_player_height ) {
						embed_height = min_player_height;
					}
					this_o.media_update(this_o.selected_tool, {
						embed_height: embed_height
					});
					this_o.complete_callback();
				} else {
					alert('Could not get height for player.');
				}
			},
			onError: function(transport) { 
				alert('Could not get height for player.');
			} 
		});	
	},
	
	get_embed_code: function () {
		var media_values = this.media_values( this.selected_tool );
		var item_embed;
		var align_start = "";
		var align_end = "";
		var attribution = "";
		if ( media_values.content_cid ) {
			if ( media_values.embed_align ) {
				align_start = '<div style="text-align: ' + media_values.embed_align + ';">';
				align_end = '</div>';
			}
			if ( media_values.attribution ) {
				attribution = media_values.attribution;
			}
			if ( /image/.test(this.selected_tool) ) {
				item_embed = align_start + '<img src="' + media_values.thumbnail + '" />' + align_end + attribution;
			} else if ( ! media_values.embed_height ) {
				item_embed = ''
			} else {
				item_embed = align_start + '<iframe src="http://' + location.hostname + '/embed/player/?content=' + media_values.content_cid + '&widget_type_cid=svp&widget_template_cid=' + media_values.widget_template_cid + '" width="' + ( Number(media_values.embed_width)+player_chrome_width ) + '" height="' + ( Number(media_values.embed_height)+player_chrome_height ) + '" frameborder="0" scrolling="no" allowtransparency="true"></iframe>' + align_end;

			
			}
		} 
		return item_embed;
	},

	get_embed_placeholder: function () {
		var media_values = this.media_values( this.selected_tool );
		var embed = this.get_embed_code();

		if ( media_values.content_cid ) {
			if ( media_values.embed_align ) {
				align_start = '<div style="text-align: ' + media_values.embed_align + ';">';
				align_end = '</div>';
			}
			if ( /image/.test(this.selected_tool) ) {
			} else {
				var embed_code = escape( embed );
				var bottom_padding = 40;
				var top_padding = 71;
				embed = align_start + '<img src="' + media_values.thumbnail + '" width="' + ( Number(media_values.embed_width) ) + '" height="' + (Number(media_values.embed_width)*.75) + '" class="mvp-embedder-placeholder" style="position: relative; background: ' + media_values.widget_template_cid + ' url(/decor/publisher/placeholders/embed_placeholder_' + (Number(media_values.embed_width)) + '_' + media_values.widget_template_cid + '.jpg) no-repeat; padding: ' + top_padding + 'px 10px ' + bottom_padding + 'px 10px;" rel="' + embed_code + '" />' + align_end;
				
			}
		} 
		return embed;
	},

	complete_callback: function () {
		var media_values = this.media_values( this.selected_tool );
		if ( media_values.content_cid ) {
			media_values.embed = this.get_embed_code();
			media_values.placeholder = this.get_embed_placeholder();
			if ( ! media_values.embed ) {
				alert("Could not generate embed code for " + media_values.title);
			} else {
				this.display_embed( media_values );
			}
		} 
	},
	

	create_content_item_if_needed: function ( on_success_func, on_fail_func ) {
	
		var media_values = this.media_values( this.selected_tool );
		var content_item_form = 'mvp-embedder-create-form-' + (this.selected_tool).gsub('_','-');	
		if (  $(content_item_form) ) {
			var content_item_params = Form.serialize(content_item_form);
			var new_media_values = content_item_params.toQueryParams();
			this.media_update(this.selected_tool, new_media_values);
		}
		
		if ( this.selected_tool == 'image_search' ) {
			var embed_item_form = 'mvp-embedder-embed-form-' + (this.selected_tool).gsub('_','-');	
			var embed_item_params = Form.serialize(embed_item_form);
			new_media_values = embed_item_params.toQueryParams();
			this.media_update(this.selected_tool, new_media_values);
			return this.complete_callback();
		}
		
// the search_details panel has a separate form
		if ( this.selected_tool == 'search' ) {
			var embed_item_form = 'mvp-embedder-embed-form-' + (this.selected_tool).gsub('_','-');	
			var embed_item_params = Form.serialize(embed_item_form);
			new_media_values = embed_item_params.toQueryParams();
			this.media_update(this.selected_tool, new_media_values);
		}
		
		if ( media_values.content_cid ) {
			on_success_func();
		} else {
			var url = '/embed/publisher_tools/item?media_item_cid=' + media_values.media_cid + '&' + content_item_params;
			var this_o = this;
			new Ajax.Request(url, { 
				onSuccess: function(transport) { 
					var regex = /(media item|content item)/;
					var failure = regex.test(transport.responseText);
					if ( ! failure ) {
						content_cid = transport.responseText;
						this_o.media_update(this_o.selected_tool, {
							content_cid: content_cid
						});
						on_success_func();
					} else {
						if ( on_fail_func ) {
							on_fail_func();
						} else {
							alert('No content item to embed.');
						}
					}
				},
				onError: function(transport) { 
					if ( on_fail_func ) {
						on_fail_func();
					} else {
						alert('Could not create content item.');
					}
				} 
			});	
		}
	},


	do_detail_callback: function () {
		var this_o = this;
		this.create_content_item_if_needed( 
			function () { 
				if ( /image/.test(this.selected_tool) ) {
					this_o.complete_callback(); 
				} else {
					this_o.set_player_height();
				}
			} 
		);
	}
	
}

