$.fn.log = function(msg) {
	console.log("%s: %o", msg, this);
	return this;
}
$.fn.exists = function() {
	return this.length>0;
}
$(document).ready (
	function() {
		/* Init
		--------------------------------------------------------------------------------------- */
		var speed = 800;
		var init = function() {
			fixMenuLastItem();
			greennews();
			bookmark();
			//services();
		};
		
		/* Bookmark
		--------------------------------------------------------------------------------------- */
		var bookmark = function() {
			$("a.bookmark").jBrowserBookmark();			
		}
		
		/* Animate scroll to
		--------------------------------------------------------------------------------------- */
		var animateScrollTo = function(ancre) {
			$("html,body").stop().animate({
				scrollTop: $(ancre).offset().top
			}, speed, "easeInOutExpo", function() {
	            if(ancre != "body") {
					window.location.hash = ancre;
				} else {
					window.location.hash = "#";
				}
	            $(ancre).attr("tabindex", "-1");
	            $(ancre).focus();
	            $(ancre).removeAttr("tabindex");
	        });
	    }
	
		/* Default text
		--------------------------------------------------------------------------------------- */
		$(".defaultText").each(function() {
			var fld_current = this;
			var defaultValue = $(this).attr("default");
									
			if(this.value == "") {
				this.value = defaultValue;
			}
			
			$(this).focus(function() {
				if (this.value == defaultValue) {
					this.value = "";
				}
			}).blur(function() {
				if (this.value == "") {
					this.value = defaultValue;
				}
			}).parents("form").each(function() {
				$(this).submit(function() {
					if (fld_current.value == defaultValue) {
						fld_current.value = "";
					}
				});
				$(this).find("a.submit").each(function() {
					$(this).click(function() {
						if (fld_current.value == defaultValue) {
							fld_current.value = "";
						}
					});
				});
			});
		});
		
	
		/* Fix menu pages last
		--------------------------------------------------------------------------------------- */
		var fixMenuLastItem = function() {
			$(".menu li:last").addClass("last_item");
			if($(".services").exists()) {
				$(".services li:last").addClass("last_item");
			}	
	    }
	
		/* Services
		--------------------------------------------------------------------------------------- */
		var services = function() {
			if($(".serviceslist").exists()) {
				var ajax_file = $(".serviceslist").attr("wpurl") + "/wp-admin/admin-ajax.php";
	
				$(".serviceslist .services a").click(
					function() {
						var currentItem = $(this).parent();
						var service_match = currentItem.attr("class").match(/page-item-([0-9]+)/);
												
						$.ajax({
							type: "POST",
							url: ajax_file,
							data: {
								action: "load_service",
								service_id: service_match[1]
							},
							success: function(data) {
								data = eval("(" + data + "})");
								if(!data.response["error"]) {
									$(".serviceslist .current_page_item").removeClass("current_page_item");
									currentItem.addClass("current_page_item");
									$(".serviceslist .body").html(data.response["service_data"]);	
								}
								else {
									$(".serviceslist .body").html('<p class="error">' + data.response["error"] + '</p>');						
								}				
							}
						});	
											
						return false;
					}					
				);
			}
		}
		
		/* Greennews
		--------------------------------------------------------------------------------------- */
		var loadfeed = function(ajax_file, feed_id, feed_src, items) {
			$.ajax({
				type: "POST",
				url: ajax_file,
				data: {
					action: "load_feed",
					feed_id: feed_id,
					feed_url: feed_src
				},
				success: function(data) {
					data = eval("(" + data + "})");
					if(!data.response["error"]) {
						$("#" + data.response["feed_id"]).attr("offset", items);
						$("#" + data.response["feed_id"] + " .feeddata").html(data.response["feed_data"]);
						$("#" + data.response["feed_id"] + " h5 a").html($("#" + data.response["feed_id"]).attr("nicename"));
						$("#" + data.response["feed_id"]).attr("offset", items);
						if($("#" + data.response["feed_id"]).find(".feeddata li").size() > items) {
							$("#" + data.response["feed_id"]).find(".nav").show();				
						}						
						$("#" + data.response["feed_id"] + " .feeddata li:lt(" + items + ")").show("fast");
					}
					else {
						$(this).replaceWith('<p class="error">' + data.response["error"] + '</p>');						
					}				
				}
			});
		}
		
		var greennews = function() {
			if($(".greennews").exists()) {
				var ajax_file = $(".greennews").attr("wpurl") + "/wp-admin/admin-ajax.php";
				var items = 3;
				
				$("div.feed").each(
					function() {
						// $(this).log($(this).attr("src"));
						loadfeed(ajax_file, $(this).attr("id"), $(this).attr("src"), items);						
					}
				);
				
				$(".nextfeed").click(
					function() {
						var currentFeed = $(this).parent().parent();
						var currentOffset = parseInt(currentFeed.attr("offset"));
				
						currentFeed.find(".feeddata li").slice((currentOffset - items), currentOffset).hide("fast");
						currentFeed.find(".feeddata li").slice(currentOffset, (currentOffset + items)).show("fast");
						currentOffset += items;
						currentFeed.attr("offset", currentOffset);
						
						if((currentOffset+1) > currentFeed.find(".feeddata li").size()) {
							currentFeed.find(".nextfeed").hide();							
						}
						
						currentFeed.find(".prevfeed").show();
						
						return false;
					}
				);
				
				$(".prevfeed").click(
					function() {
						var currentFeed = $(this).parent().parent();
						var currentOffset = parseInt(currentFeed.attr("offset"));
						
						currentFeed.find(".feeddata li").slice((currentOffset - items), currentOffset).hide("fast");
						currentFeed.find(".feeddata li").slice((currentOffset - items * 2), (currentOffset - items)).show("fast");
						currentOffset -= items;
						currentFeed.attr("offset", currentOffset);
						
						if((currentOffset-1) < items) {
							 currentFeed.find(".prevfeed").hide();	
						}
						
						currentFeed.find(".nextfeed").show();
						
						return false;
					}
				);				
				
				$("ul>li").hover(
					function() {
						$(this).addClass("hover");
					}, function() {
						$(this).removeClass("hover");
					}
				);	
			}
		}
			
		init();
	}
);
