// <![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
        get_article: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				insertBeforeTarget:		'#project-items',
				containerObject:		'#container-article',
				containerHTML:			'<div id="container-article"></div>',
				articleObject:			'#article',
				galleryObject:			'#viewer-gallery',
				exitObject:				'.close-article',
				scrollTopSpeed:			'slow',
				scrollTopMarginOnLoad:	110,
				scrollTopMarginOnExit:	110,
				durationAnimate:		1000
            }
			
			// Always leave this line here.
			var options = $.extend(defaults, options);
			var o = options;
                 
			// Attach click function.
			//var $cm = this.click(function(e){
			// Atttach hashchange function.
			$(window).hashchange(function() {

				//var $cm = this.each(function(index,element){
				// "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.ready(function(e){
					
				//var object = $(this); // always return #document if you have: var $cm = this.ready(function(e){}).
				
				// Keep the lines below for checking.
				//alert(object.get(0).nodeName);
				//alert(object.selector);
				//alert($cm.selector);
				//alert(o.insertBeforeTarget);
				//alert(o.durationAnimate);
					
				var hash = location.hash;
				//alert(hash);
				
				// Check if the fragment/ hash doesn't exit, then slide up and remove the container.
				if(hash == "" || hash == "#/all/") if( $(o.containerObject).length > 0 ) $(o.containerObject).slideUp(function(){ $(this).remove(); });
				
				// Attach the load article function.
				$.fn.load_article();
			
				return false;
			});
			
			// Function to load the article.
			$.fn.load_article = function(insertBeforeTarget,scrollTopMarginOnLoad) {
				
				var fragment = location.hash;
				//alert(fragment);
				
				// Check if fragment exists.
				if(fragment)
				{
					//if (/#/i.test(fragment)) alert('#');
					//if (fragment.indexOf("/") > 0) alert('/');
					//if (fragment.indexOf("#") > 0) alert('#');
					
					// Check if '/' exists in the fragment eg. #/article-1
					// Then, split the string with '/' to get the last item in the array eg. article-1
					if (fragment.indexOf("/") > 0)
					{
						//var url_next = window.location;
						// Get the last second item in the array.
						var array_fragment = fragment.split('/');
						var pg_url = array_fragment[array_fragment.length - 2];
						//var pg_url = $(array_fragment).last()[0];
					}
					// If '/' does not exisit, then check if '#' exists in the fragment eg. #article-1
					// Then, split the string with '#' to get the last item in the array eg. article-1
					else if (/#/i.test(fragment))
					{
						//var url_next = window.location;
						// Get the last second item in the array.
						var array_fragment = fragment.split('#');
						var pg_url = array_fragment[array_fragment.length - 2];
						//var pg_url = $(array_fragment).last()[0];
					}
					//alert(pg_url);
					
					// Check if the pg url is not 'all'.
					// Then load the ajax page.
					if(pg_url != 'all')
					{
					
						// Check if the container has already existed, remove it first if it does.
						if($(o.containerObject).length > 0){
					
							// Remove any previous ajax loaded page.
							$(o.containerObject).remove();
							
						};
				
						// Scroll the page to the top first.
						if(scrollTopMarginOnLoad) $('html, body').animate({scrollTop:scrollTopMarginOnLoad}, o.scrollTopSpeed);
						else $('html, body').animate({scrollTop:o.scrollTopMarginOnLoad}, o.scrollTopSpeed);
						
						// Insert the dive before the target.
						if(insertBeforeTarget) $(o.containerHTML).insertBefore($(insertBeforeTarget));
						else $(o.containerHTML).insertBefore($(o.insertBeforeTarget));
						
						// Set the div to a variable and set its css.
						//var container_article = $('#container-article').css({height:0});
						var container_article = $(o.containerObject).hide();
						
						// Load the article into container_article.
						container_article.load( http_root + rp_local_template + 'article.php?pg_url=' + pg_url, function(){
							
							// Not necessary.
							//container_article.css("height", container_article.height()).hide();
							
							// Must attach this plugin first to get the height of the first image in the gallery.
							$('#menu-gallery a').gallery();
							
							// Fade in and slide down the objects.
							$(o.articleObject).fadeIn('slow');
							
							// Check if it is IE first and it is older than version 9.
							if ($.browser.msie && parseInt($.browser.version) < 9) $(o.galleryObject).fadeIn('slow'); /* fix for IE < 9 */
							
							$(this).slideDown();
							
							// Attach other plugins and functions.
							$("a[rel='example1']").colorbox();
							$(".example6").colorbox({iframe:true, innerWidth:425, innerHeight:344});
							$('a[href=#articletop]').scroll_to_top({topMargin:o.scrollTopMarginOnLoad}); // Does not work on IE < 9!
							
							// Attach the close article function.
							$.fn.close_article();
							
						});
					
					}
				
				}
			}
			
			// Function to slide up the article and remove it.
			$.fn.close_article = function() {
				
				$(o.exitObject).click(function(e){
					
					//var target_object = $('#container-article');
					
					// Fade out the article and the gallery.
					$(o.articleObject).fadeOut('slow');
					$(o.galleryObject).fadeOut('slow'); /* fix for IE < 9 */
					
					// Slide up and remove it.
					// scroll the page to the top.
					$(o.containerObject).slideUp(function(){
						location.hash = '/all/';
						$(this).remove();
						//$('html, body').animate({scrollTop:o.scrollTopMarginOnExit}, o.scrollTopSpeed);
					});
					
					return false;
					
				});
			}

			
        }
    });
     
//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);

// ]]>
