$(document).ready(function(){
	
	/*
	|---------------------------------------------------------------
	| SET GLOBAL VARIABLES
	|---------------------------------------------------------------
	|
	| rp = relative path
	| ap = absolute path
	|
	*/
	http_root = $("body").attr("title");
	rp_cms = 'global/applications/CMS/';
	rp_public = 'global/applications/PUBLIC/';
	rp_local_layout = 'local/views/includes/layouts/';
	rp_local_template = 'local/views/includes/templates/';
	rp_local_image = 'local/views/styles/images/';
	rp_global_image = 'global/views/styles/images/';
	rp_local_style = 'local/views/styles/stylesheets/';
	rp_global_style = 'global/views/styles/stylesheets/';
	image_loader = 'loader-2b.gif';
	$('body, div').removeAttr('title');
	
	/*
	|---------------------------------------------------------------
	| LOAD THE SITE LOCAL FUNCTIONS
	|---------------------------------------------------------------
	|
	*/
	if($('#cms').length == 0)
	{
		/*
		|---------------------------------------------------------------
		| LOAD THE SITE GENERAL FUNCTIONS
		|---------------------------------------------------------------
		|
		*/
		
		// Bind load event first to work on Google Chrome.
		$(window).bind('load', function() {
			
			var timeout = setTimeout(function()
			{
				// Attach the grayscale plugin.
				$(".tile-shield").grayscale_tile_image();
				
				// Attach the image tile reveal plugin.
				$(".tile-shield").reveal_tile_image();
				
			}, 3000 ); 

		});
		
		// Attach the image tile reveal plugin.
		//$(".tile-shield").reveal_tile_image();
		
		// Attach the text tile reveal plugin.
		$(".tile-gray").reveal_tile_text({objectDelay:250});
		
		// Attach the slider function.
		init_scrollbar();
		
		// Attach the help plugin.
		$(".get-help").help();
		
		/*
		|---------------------------------------------------------------
		| HASHING AND MENU
		|---------------------------------------------------------------
		|
		*/
		
		/*
		var handler = function() {
			return false;
		};

		// Unbind the click event from the menu.
		$(".tile-tag > ul > li > a").bind('click', handler);
		
		// Bind the click event back to the menu.
		$(".tile-tag > ul > li > a").unbind('click', handler);
		*/
		
		// Attach the function to the tags navigation.
		$('.tile-tag > ul > li > a').click(function(){
			
			// Set the variable.
			var parent_object = $(this).parent(); // which is <li>
			var siblings_parent = parent_object.siblings(); // other <li>s
			//parent_object.css({background:'red'});
			
			// Clean all siblings' style.
			siblings_parent.removeClass('current-tag');
			//$('span:first',siblings_parent).html('&#43;');
			$('ul',siblings_parent).slideUp('fast');
			
			// Attach style to object's parent.
			parent_object.toggleClass('current-tag');
			$('ul',parent_object).slideToggle('fast');
			//if($('ul',parent_object).length > 0) alert('1');
			
			// Swap the object's parent's span html
			if(parent_object.hasClass('current-tag') && $('ul',parent_object).length > 0) $('span:first',parent_object).html('&#150;');
			else if($('ul',parent_object).length > 0) $('span:first',parent_object).html('&#43;');
			
			// you must comment this line out so that you get the url to change for hashing.
			//return false;
		});
		
		// Attach function of hashing with ajax.
		location_article();
		do_hashchange();
		do_fragment();
		
		/*
		|---------------------------------------------------------------
		| DRAGGABLE
		|---------------------------------------------------------------
		|
		*/
		
		// Attach the jquery.ui draggable.
		$( ".tile-tag" ).draggable({ 
			handle: 'h3',
			containment: $('body')
		});
		
		/*
		|---------------------------------------------------------------
		| LETTER REPLACEMENT AND REVERSE ORDER
		|---------------------------------------------------------------
		|
		*/
		
		// Set the object target.
		var object = $('.letter-replace-reverse');
		
		// Get the text from the object and split them into an array.
		// Must use split('') to work on IE versions below 9.
		var array_letter = object.text().split('');
		
		// Clear the html of h3 so we can change it .
		object.html('');
		
		// Loop it.
		for (var i = 0, length_string = array_letter.length; i < length_string; i++) {
		
			//$('h3').append('<span class="box-letter">' + array_letter[i] + '</span>');
			//$('h3').append('<span class="box-letter"><img src="letter-' + array_letter[i] + '.png" alt="letter-replacement"/></span>');
			object.append('<span class="box-letter">' + ((array_letter[i] == ' ') ? '&nbsp;' : '<img src="' + http_root + rp_local_image + 'letter-' + array_letter[i].toLowerCase() + '.png" alt="letter-replacement"/>') + '</span>');
		  
		}
		
		// Reverse the order with the plugin.
		$('.box-letter').reverseOrder();
		
		/*
		|---------------------------------------------------------------
		| SCROLLBAR REPLACEMENT
		|---------------------------------------------------------------
		|
		| A third party plugin. IE < 9 may have some problem with this website only. 
		| It can be caused by too many plugin I have used for this site.
		| This plugin works fine on other websites such as ener.
		|
		*/
		
		if (($.browser.msie && parseInt($.browser.version) >= 9) || !$.browser.msie)
		{
			$('ul.content-item-pulse').jScrollPane({
				showArrows:false, 
				scrollbarWidth: 10,
				reinitialiseOnImageLoad: true
			});
		}
		
		/*
		|---------------------------------------------------------------
		| ADDTHIS TO LOAD
		|---------------------------------------------------------------
		|
		| Asynchronous Loading.
		|
		*/
		
		// After the DOM has loaded...
		initAddThis();
		
		/*
		|---------------------------------------------------------------
		| FEEDING PULSE
		|---------------------------------------------------------------
		|
		*/
		
		$('div.item-pulse:has(form.feed-item)').each(function(){
		
			var object = $(this);
			var parameter = $('form',object).serialize();
			//var url = $('.url',object).text();
			//var limit = $('.limit',object).text();
			//alert(url);
			
			// Loading.
			$('.content-binder-pulse',object).html('<img src="'+http_root+rp_local_image+'ajax-loader-2.gif" style="float:none; border:0px solid #000; margin:10px 10px 0px 0px;" class="string-ajax-loader"/> loading');
			
			// Post the form data.
			$.post(http_root + rp_local_template + 'feed.php',parameter,function(html){
				
				// Now get the feed and print it on the targeted element.
				$('.content-binder-pulse',object).html(html);
				
				// Attach the scrollbar plugin.
				$('ul.content-item-pulse', object).jScrollPane({
					showArrows:false, 
					scrollbarWidth: 10,
					reinitialiseOnImageLoad: true
				});
			});

			
		});
		
		
		/*
		$('ul.content-item-pulse:has(li.url)').each(function(){
		
			var object = $(this);
			var title = $('.title',object).text();
			var url = $('.url',object).text();
			var limit = $('.limit',object).text();
			//alert(url);
			
			//if( url.search( /\?/ig ) ) alert(url);
			
			//if( url.indexOf( "?" ) !== -1 ) var parameter = '&url=' + url + '&limit=' + limit  + '&title=' + title;
			//	else var parameter = '?url=' + url + '&limit=' + limit  + '&title=' + title;
			
			alert(object.serialize());
			
			// Loading.
			object.html('<img src="'+http_root+rp_local_image+'ajax-loader-2.gif" style="float:none; border:0px solid #000; margin:10px 10px 0px 0px;" class="string-ajax-loader"/> loading');
			
			// Now load the feed.
			object.load( http_root + rp_local_template + 'feed.php?url=' + url + '&limit=' + limit  + '&title=' + title, function(){
						
				object.jScrollPane({
					showArrows:false, 
					scrollbarWidth: 10,
					reinitialiseOnImageLoad: true
				});
		
			});
			
		});
		*/
	
	}
	
	/*
	|---------------------------------------------------------------
	| LOAD THE CMS FUNCTIONS
	|---------------------------------------------------------------
	|
	*/
	
	if($('#cms').length > 0)
	{
		load_cms();
	}

});



/**/
function initAddThis() 
{
	addthis.init()
}

