var $tabbed_content_target;
var send_data;
$j = jQuery.noConflict();
jQuery(document).ready(function($) {
	if($('.contentbtn').length > 0){
		$('.contentbtn').button();
	}
	tabbed_content.init();	
	
	/*Image Uploader for admin*/
	var input_name;
	$('.upload_image_button').click(function() {
	 input_name = $(this).attr('target');
	 
	 formfield = jQuery('#' + input_name).attr('name');
	 tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
	 return false;
	});
	
	window.send_to_editor = function(html) {
		if(input_name != undefined){
		 imgurl = jQuery(html).attr('href');
		 jQuery('#' + input_name).val(imgurl);
		 tb_remove();
		}else{
					orig_send_to_editor(html);
			}
	}
	if($('#tabbed_select').length > 0){
		post_content.init();
	}
});
var post_content = {
	text : null,
	$text_container : null,
	regex :  /\[tabbed_content id=\"([0-9]+)\"\]/,
	exists : false,
	init : function(){
		this.$text_container = $j('textarea#content');
		this.text = this.$text_container.text();
		if(content_id = this.text.match(this.regex)){
			$j('#tabbed_select > option').each(function(){
				if($j(this).val() == content_id[1]){
					$j(this).attr('selected','selected');
					post_content.exists = true;
				}
			});
		}
		$j('textarea#content').keyup(function(){
			post_content.text = $j(this).val();									  
		});
		$j('#tabbed_select > option').click(function(){
			if(post_content.check_exists()){
				var str;
				str = ($j(this).val() == "")? "" : '[tabbed_content id="' + $j(this).val() + '"]';
				post_content.text = post_content.text.replace(post_content.regex, str);
			}else if($j(this).val() != ""){
				post_content.text = post_content.text + '[tabbed_content id="' + $j(this).val() + '"]';
			}
			post_content.$text_container.val(post_content.text);
			
		});
		
	},
	check_exists : function(){	
		this.exists = (this.regex.test(this.text))? true : false;
		return this.exists;
	}
};
function tabbed_content_ajax(send_data){
	//console.debug(send_data);
	$j.post(ajaxurl,send_data,
		function(data){
			var data = $j.parseJSON(data.substring(0,data.length - 1));
			switch(data.action){
				case 'add' :
					tabbed_content.add(data);
					break;
				case 'edit' :
					tabbed_content.edit(data);
					break;
				case 'delete' :
					tabbed_content.remove();
					break;
			}
			
		});
}
var tabbed_content = {
	edit_name : {},
	edit_value : {},
	edit_order : {},
	add : function(data){
		$tabbed_content_target.find('#content_name').val('Content Name').end()
			.find('#content_value').val('').end()
			.find('#content_order').val('0');
		var $add = $j('<tr content_id="' + data.content_id + '" class="tabbed_content_container">' +
                                    '<td class="tabbed_content_name">' + data.content_name + '</td>' +
                                    '<td class="tabbed_content_value">' + data.content_value + '</td>' +
									'<td class="tabbed_content_order">' + data.content_order + '</td>' +
                                    '<td class="tabbed_content_edit"><a href="#page=tabbed_content&amp;action=edit&amp;content_id=' + data.content_id + '" class="edit">Edit</a></td>' +
                                    '<td class="tabbed_content_delete"><a href="#page=tabbed_content&amp;action=delete&amp;content_id=' + data.content_id + '" class="delete">Delete</a></td>' +
                                '</tr>');
			
			
		//var $add = $tabbed_content_target.clone().toggleClass('alternate');
			
		//$add.find('.tabbed_content_name').html(data.content_name);
		//$add.find('.tabbed_content_value').html(data.content_value);
		
		//edit link
		$add.find('.tabbed_content_edit').find('a').unbind('click').click(function(){
			tabbed_content.edit_click($j(this));		
			return false;
		});
		//var edit_link = $add.find('.tabbed_content_edit').find('a').attr('href').replace(/tabbed_id=([0-9]+)/i,"tabbed_id=" + data.tabbed_id);
		//$add.find('.tabbed_content_edit').find('a').attr('href', edit_link);
		//delete link		
		$add.find('.tabbed_content_delete').find('a').unbind('click').click(function(){
			tabbed_content.delete_click($j(this));		
			return false;
		});
		//var delete_link = $add.find('.tabbed_content_delete').find('a').attr('href').replace(/tabbed_id=([0-9]+)/i,"tabbed_id=" + data.tabbed_id);
		//$add.find('.tabbed_content_delete').find('a').attr('href', delete_link);
		
		$add.insertBefore($tabbed_content_target.children().last());
		
		$add.children().css('background','#f6ff00').animate({backgroundColor: 'transparent'}, 500, function(){$j(this).css('background','transparent');});
	},
	remove : function(){
		$tabbed_content_target.fadeOut(function(){$j(this).remove();});
	},
	edit : function(data){
		tabbed_content.edit_name[data.content_id] = data.content_name;
		tabbed_content.edit_value[data.content_id] = data.content_value;
		tabbed_content.edit_order[data.content_id] = data.content_order;
		
		tabbed_content.edit_reset($tabbed_content_target);
		$tabbed_content_target.children().css('background','#f6ff00').animate({backgroundColor: 'transparent'}, 500, function(){$j(this).css('background','transparent');});
	},
	edit_click : function($elm){
		var $container = $elm.parent().parent();
		var content_id = $container.find('.tabbed_content_edit').find('a').attr('href').match(/content_id=([0-9]+)/)[1];

		tabbed_content.edit_name[content_id] = $container.find(".tabbed_content_name").html().trim();
		$container.find(".tabbed_content_name").html('<input type="text" value="' + tabbed_content.edit_name[content_id] + '" maxlength="60" size="30" class="input" id="content_name" name="content_name">');
		tabbed_content.edit_value[content_id] = $container.find(".tabbed_content_value").html().trim();
		$container.find(".tabbed_content_value").html('<textarea name="content_value" id="content_value" rows="10" cols="100" class="input" >' + tabbed_content.edit_value[content_id] + '</textarea>');
//'<input type="text" value="' + tabbed_content.edit_value[content_id] + '" maxlength="30" size="40" class="input" id="content_value" name="content_value">');
		tabbed_content.edit_order[content_id] = $container.find(".tabbed_content_order").html().trim();
		$container.find(".tabbed_content_order").html('<input type="text" value="' + tabbed_content.edit_order[content_id] + '" maxlength="10" size="1" class="input" id="content_order" name="content_order">');
		
		var save_link = $container.find('.tabbed_content_edit').find('a').attr('href').replace(/action=([A-Za-z]+)/i,"action=edit_save");
		$container.find('.tabbed_content_edit').find('a').attr('href', save_link).text('Save').unbind('click')
			.click(function(){
				tabbed_content.edit_save_click($j(this));
				return false;
			});
		var cancel_link = $container.find('.tabbed_content_delete').find('a').attr('href').replace(/action=([A-Za-z]]+)/i,"action=edit_cancel");
		$container.find('.tabbed_content_delete').find('a').attr('href', cancel_link).text('Cancel').unbind('click')
			.click(function(){
				tabbed_content.edit_reset($j(this).parent().parent());
				return false;
			});
		
	},
	edit_save_click : function($elm){
		var $container = $elm.parent().parent();
		var send_data = $container.find('#content_name,#content_value,#content_order').serialize() + "&tabbed_id=" + $container.attr('tabbed_id') + "&content_id=" + $elm.attr('href').match(/content_id=([0-9]+)/)[1] + "&edit_content=true&action=response_action";
		$tabbed_content_target = $container;
		
		tabbed_content_ajax(send_data);
	},
	edit_reset : function($elm){
		var $container = $elm;
		var content_id = $container.find('.tabbed_content_edit').find('a').attr('href').match(/content_id=([0-9]+)/)[1] + '';

		$container.find(".tabbed_content_name").html(tabbed_content.edit_name[content_id]);
		
		$container.find(".tabbed_content_value").html(tabbed_content.edit_value[content_id]);
		
		$container.find(".tabbed_content_order").html(tabbed_content.edit_order[content_id]);
		
		var edit_link = $container.find('.tabbed_content_edit').find('a').attr('href').replace(/action=([A-Za-z]+)/i,"action=edit");
		$container.find('.tabbed_content_edit').find('a').attr('href', edit_link).text('Edit').unbind('click')
			.click(function(){
				tabbed_content.edit_click($j(this));
				return false;
			});
		var delete_link = $container.find('.tabbed_content_delete').find('a').attr('href').replace(/action=([A-Za-z]]+)/i,"action=delete");
		$container.find('.tabbed_content_delete').find('a').attr('href', delete_link).text('Delete').unbind('click')
			.click(function(){
				tabbed_content.delete_click($j(this));
				return false;
			});
	},
	delete_click : function($elm){
		if(confirm('Are you sure you want to delete this tabbed_content')){
			var send_data = "content_id=" + $elm.attr('href').match(/content_id=([0-9]+)/)[1] + "&delete_content=true&action=response_action";
			$tabbed_content_target = $elm.parent().parent();
			tabbed_content_ajax(send_data);
		}
	},
	add_click : function($elm){
		var $container = $elm.parent().parent();
		send_data = $container.find('#content_name,#content_value,#content_order').serialize() + "&tabbed_id=" + $elm.attr('tabbed_id') + "&add_content=true&action=response_action";
		$tabbed_content_target = $container.parent();		
		tabbed_content_ajax(send_data);
	},
	init : function(){
		$j('.tabbed_content_add').unbind('click').click(function(){	
			tabbed_content.add_click($j(this));
			return false;
		});
		$j('.tabbed_content_edit').find('a.edit').unbind('click').click(function(){
			tabbed_content.edit_click($j(this));		
			return false;
		});
		$j('.tabbed_content_delete').find('a.delete').unbind('click').click(function(){ 
			tabbed_content.delete_click($j(this));		
			return false;
		});
		$j('.tabbed_content_wrapper').hide();
		$j('a.tabbed_toggle').toggle(function(){
				$j(this).parent().parent().next('.tabbed_content_wrapper').fadeIn();
				$j(this).text('Hide');
				return false;
			},function(){
				$j(this).parent().parent().next('.tabbed_content_wrapper').fadeOut();
				$j(this).text('View');
				return false;
		});
	}
}
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function orig_send_to_editor(h){
	var ed;

	if ( typeof tinyMCE != 'undefined' && ( ed = tinyMCE.activeEditor ) && !ed.isHidden() ) {
		ed.focus();
		if ( tinymce.isIE )
			ed.selection.moveToBookmark(tinymce.EditorManager.activeEditor.windowManager.bookmark);

		if ( h.indexOf('[caption') === 0 ) {
			if ( ed.plugins.wpeditimage )
				h = ed.plugins.wpeditimage._do_shcode(h);
		} else if ( h.indexOf('[gallery') === 0 ) {
			if ( ed.plugins.wpgallery )
				h = ed.plugins.wpgallery._do_gallery(h);
		} else if ( h.indexOf('[embed') === 0 ) {
			if ( ed.plugins.wordpress )
				h = ed.plugins.wordpress._setEmbed(h);
		}

		ed.execCommand('mceInsertContent', false, h);

	} else if ( typeof edInsertContent == 'function' ) {
		edInsertContent(edCanvas, h);
	} else {
		jQuery( edCanvas ).val( jQuery( edCanvas ).val() + h );
	}

	tb_remove();
}
