// <![CDATA[
/**
 * dependency of this plugin:
 *
 * //@var global string http_root
 * //@var global string rp_global_image
 * //@var global string img_loader
 * //@var global string rp_cms
 *
 * //@function close_popup()
 *
*/

// You need an anonymous function to wrap around your function to avoid conflict
(function($){
 
    // Attach this new method to jQuery
    $.fn.extend({ 
         
        // This is where you write your plugin's name
        photo_string: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				colorBackground:		'#ffffff',
				opacityBackground:		0.4,
				centraliseViewer:		true,
				widthViewer:			1000,
				topViewer:				0,
				borderViewer:			'1px solid #e7e7e7'
				
            }
                 
            var options =  $.extend(defaults, options);
			var o = options;
            
			// return this.click(function(){ // original
			// "this" is already a jQuery object: 
			// When you create the click function you can assign that element to a variable and reference it within:
			var $cm = this.click(function(e){
				
				// Check if it is IE first and is < 9.
				// Must change the body 'overflow' to 'hidden' above other lines or a bug on Chrome.
				// Must not use $('html')) here or a bug on firefox.
				if ($.browser.msie && parseInt($.browser.version) < 9) $('html').css('overflow', 'hidden');

				if($(document).height() > $(window).height()) $('body').css('overflow', 'hidden').css('padding-right','17px');
				else $('body').css('overflow', 'hidden');
				
				var object = $(this);
				var object_path = object.attr('href');
				var height_document = $(document).height();
				//alert(html_parents);
				//alert(html_remained);
				
				$(document.body).append('<div id="background-stringphoto"></div>');
				$(document.body).append('\
				<div id="container-stringphoto">\
					<ul id="items-stringphoto">\
					</ul>\
				</div>\
				');
				
				var object_container = $('div#container-stringphoto');
				var object_background = $('div#background-stringphoto');
				var object_viewer = $('ul#items-stringphoto');
				
				var width_viewer = o.widthViewer;
				var top_viewer = o.topViewer;
				var border_viewer = o.borderViewer;
				var margin_left_viewer = width_viewer/2;
				var index_div = $('div').get_topZindex();
				
				// Set css for the background.
				object_background.css({
					height:height_document + 'px',
					background: o.colorBackground,
					opacity: o.opacityBackground,
					zIndex: index_div+1
				});
				
				// Set css for the viewer.
				object_container.css({
					zIndex: index_div+2
				});
				
				// Set css for the viewer.
				object_viewer.css({
					width: width_viewer + 'px',
					marginLeft: '-' + margin_left_viewer + 'px',
					top: top_viewer + 'px',
					border: border_viewer
				});
				
				// If the centerise is set to be true.
				if(o.centraliseViewer) object_viewer.hide();
				
				// Display the loading.
				//object_viewer.html('<img src="'+http_root+rp_local_image+'ajax-loader-2.gif" style="float:none; border:0px solid #000; margin:4px 10px 0px 0px;" class="string-ajax-loader"/>');
				
				// Load the content.
				object_viewer.load(object_path, function(){
				
					// Get the image button.
					var object_button_image = $('li a',object_viewer);
					
					// Set css for the image button.
					object_button_image.css({
						width: width_viewer + 'px'
					});
				
					//alert(object_button_image.length);
			
					// If the centerise is set to be true.
					if(o.centraliseViewer) object_viewer.set_center({getWindowScrollTop:false});
					
					object_container.click(function(){
				
						// Check if it is IE first and is < 9.
						// Must change the body 'overflow' to 'hidden' above other lines or a bug on Chrome.
						// Must not use $('html')) here or a bug on firefox.
						if ($.browser.msie && parseInt($.browser.version) < 9) $('html').css('overflow', 'auto');
						
						// Undo.
						$('body').css('overflow', 'auto').css('padding-right', 0);
						
						
						object_background.remove();
						$(this).remove();
						return false;
						
					});
					
					$('a',object_container).click(function(){
					
						return false;
						
					});
				
				});
		
				
				// Keep the lines below for checking.
				//alert(o.popupEdit);
				//alert(path_rescale);
				//alert(path_edit);
				//alert(item_id);
				//alert(o.top);
				//alert($cm.selector);
					
				return false;
				
			});
			
			// Check if the property process_posted exists, and if not, creates it as a new empty object. The functions are then stored as properties of that new object.
			if(!$.fn.publish_string.process_publish_string) $.fn.publish_string.process_publish_string = {};
			
			// Callback function for proccesing the submitted form.
			//function process_update_uploadx(xml,object_parents,path_edit,path_delete,path_rescale,path_crop,path_createthumb)
			$.fn.publish_string.process_publish_string[$cm.selector] = function(xml,object_parents,html_remained)
			{
				//var target_element = object.parents(o.targetElement).css({background:'red'});
				//var target_slibing = target_element.next(o.targetSlibing);
				
				//alert(o.targetElement);
				
				// Append the popup.
				$(document.body).prepend("\
				<div id='popup-result' class='popup-outer'>\
					<div class='popup-inner'>\
						<div class='close'><a href='#' class='button-close'>x close</a></div>\
						<div class='respond-message'></div>\
					</div>\
				</div>\
				");
				
				// Set the target's value and set the target's width.
				var target_result = $('#popup-result').width(o.widthPopupResult);
				
				// Set the popup css and hide it first.
				target_result.css({
					display: 'none'
				});
				
				// If security stage, and confirmation stage have passed, print the result.
				if($(xml).find("result").length > 0)
				{
					// Set the local variable.
					var message = $(xml).find("result").attr('message');
					
					// Append the message to the .respond-message class.
					$(".respond-message", target_result).append("<img src='"+http_root+rp_global_image+"info.png' /> " + message);
					
					// Set the popup to the center of the page.
					//set_center(target_result);
					target_result.set_center();
					
					// Attach close popup plugin to fade out the popup.
					//close_popup(target_result);
					$('.close',target_result).close_popup({target:'#popup-result'});
					
					// Get the html content back.
					object_parents.html(html_remained);
					
					//$(".string-delete-needle").delete_string({targetElement:'.item-needle'});
					//$(".string-delete-thread").delete_string({targetElement:'.item-thread'});
					
					$.fn.micromenu_string();;
					
					// Fade out the popup.
					var timeout = setTimeout(function()
					{ 
						target_result.fadeOut('slow',function(){
							$(this).remove()
						});
						
					}, 1000 ); 
					
				}
			}

        }
    });
     
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )       
})(jQuery);

// ]]>
