jQuery.noConflict();
jQuery(document).ready(function($) {
	function str_replace(search, replace, subject) {
        //return subject.split(search).join(replace);
        return subject.replace(new RegExp (search, 'g'), replace);
    }
    $("p#add_row a").click(function() {
        var num = $("table.goods_table tr:not(#row_append):last").attr("id");
		num = num.split('_');
        num = parseInt(num[1]) + 1;;
        $obj = $("table.goods_table tbody #row_append");
        //$obj.attr({"id": "row_" + num});
        var str = str_replace("num_append", num, $obj.html());
		var new_row = "<tr id=\"row_" + num + "\">" + str + "</tr>";
        $("table.goods_table tbody").append(new_row);
        $("table.goods_table tbody").append($obj);
        return false;
    });
	$("table.goods_table .del_row_js a").live("click", function() {
		$(this).parent().parent().remove();
		return false;	
	});
	
		$("a.modal_img").fancybox({
			padding : 30,
			margin : 40,
			opacity : false,
			modal : false,
			cyclic : false,
			scrolling : 'auto',	// 'auto', 'yes' or 'no'
	
			width : 710,
			//height : 340,
			//autoWidth: true,
			autoHeight: true,
	
			autoScale : true,
			autoDimensions : true,
			centerOnScroll : false,
	
			ajax : {},
			swf : { wmode: 'transparent' },
			type: 'image',
	
			hideOnOverlayClick : true,
			hideOnContentClick : false,
	
			overlayShow : true,
			overlayOpacity : 0.76,
			overlayColor : '#000',
	
			titleShow : true,
			titlePosition : 'top', // 'float', 'outside', 'inside', 'top' or 'over'
			titleFormat : null,
			titleFromAlt : false,
	
			transitionIn : 'fade', // 'elastic', 'fade' or 'none'
			transitionOut : 'fade', // 'elastic', 'fade' or 'none'
	
			speedIn : 300,
			speedOut : 300,
	
			changeSpeed : 300,
			changeFade : 'fast',
	
			easingIn : 'swing',
			easingOut : 'swing',
	
			showCloseButton	 : true,
			showNavArrows : true,
			enableEscapeButton : true,
			enableKeyboardNav : true,
	
			onStart : function(){},
			onCancel : function(){},
			onComplete : function(){
				setTimeout(function() {$.fancybox.center();}, '0');
				$("#fancybox-outer").resize(function() {
					$.fancybox.center();
				});
			},
			onCleanup : function(){},
			onClosed : function(){},
			onError : function(){}
		});
	
/*-----------AJAX модалка tiny_mce----------------"*/
	$("a.modal_edit").click(function() {
		var $this = $(this);
		var param = $(this).attr("href").split("?");
		var uri = param[0];
		if(param[1] != null) {
			param = param[1].split("&");
			for(var i = 0; i < param.length; i++) {
				param[i] = param[i].split("=");
			}
		}
		if((param[0] != null && param[0][0] == "node" && param[0][1]) && (param[1] && param[1][0] == "action" && param[1][1] == "edit#form") && (param[2] && param[2][0] == "mode" && param[2][1] == "modal")) {
			$.ajax({  
				url: "/ajax/",             // указываем URL 
			    type: "POST",
			    cache: false,
			    data: "uri="+uri+"&node="+param[0][1]+"&action=" + param[1][1] + "&mode=" + param[2][1] + "&modul=texter&item_id="+param[3][1],
			    complete: function(data, textStatus){
			        if(textStatus != "error") {
//						ui.item.find("em.load").css("display", "none");
						//alert(data.responseText+"end");
						$.fancybox({
							content: data.responseText,
							padding : 17,
							margin : 40,
							opacity : false,
							modal : false,
							cyclic : false,
							scrolling : 'auto',	// 'auto', 'yes' or 'no'
					
							//width : 560,
							//height : 340,
							autoWidth: true,
							autoHeight: true,
	
							autoScale : true,
							autoDimensions : true,
							centerOnScroll : false,
	
							ajax : {},
							swf : { wmode: 'transparent' },
		
							hideOnOverlayClick : true,
							hideOnContentClick : false,
			
							overlayShow : true,
							overlayOpacity : 0.76,
							overlayColor : '#000',
	
							titleShow : true,
							titlePosition : 'inside', // 'float', 'outside', 'inside' or 'over'
							titleFormat : null,
							titleFromAlt : false,
		
							transitionIn : 'fade', // 'elastic', 'fade' or 'none'
							transitionOut : 'fade', // 'elastic', 'fade' or 'none'
			
							speedIn : 300,
							speedOut : 300,
			
							changeSpeed : 300,
							changeFade : 'fast',
		
							easingIn : 'swing',
							easingOut : 'swing',
	
							showCloseButton	 : false,
							showNavArrows : true,
							enableEscapeButton : true,
							enableKeyboardNav : true,
		
							onStart : function(){},
							onCancel : function(){},
							onComplete : function(){
								tinyMCE.init({
									mode : "specific_textareas",
									editor_selector : "wysiwyg",
									elements : "ajaxfilemanager",
									convert_urls: false,
									language: "ru",
									theme: "advanced",
									width: "100%",
									plugins : "fullscreen,table,advimage,advlink,media,contextmenu",
									theme_advanced_buttons1_add : "fontsizeselect",
									theme_advanced_buttons2_add : "media,forecolor,backcolor,liststyle",
									theme_advanced_buttons3_add : "tablecontrols,|emotions,fullscreen,images",
									theme_advanced_toolbar_location : "top",
									theme_advanced_toolbar_align : "left",
									theme_advanced_path_location : "bottom",
									content_css : "/themes/styles/screen-tinymce.css",
									plugin_insertdate_dateFormat : "%d.%m.%Y",
									plugin_insertdate_timeFormat : "%H:%M:%S",
									extended_valid_elements : "hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
									external_link_list_url : "example_link_list.js",
									external_image_list_url : "example_image_list.js",
									flash_external_list_url : "example_flash_list.js",
									media_external_list_url : "example_media_list.js",
									template_external_list_url : "example_template_list.js",
									file_browser_callback : "ajaxfilemanager",
									paste_use_dialog : false,
									theme_advanced_resize_horizontal : true,
									theme_advanced_resizing : true,
									apply_source_formatting : true 
								});
								$.fancybox.center();
								$("#fancybox-outer .mceResize").mousedown(function() {
									$("#fancybox-outer .mcePlaceHolder").mouseup(function() {
										$.fancybox.center();
									});
								});
								$("#fancybox-outer").resize(function() {
									$.fancybox.center();
								});
							},
							onCleanup : function(){},
							onClosed : function(){},
							onError : function(){}
						});
			        }
				}
			});
		} 
		return false;
	});
});
