(function() {

    var glow, $, addListener;

    gloader.load(
        ["glow", "1", "glow.dom", "glow.anim","glow.events", "glow.widgets", "glow.widgets.AutoSuggest","glow.widgets.InfoPanel"],
        {
            async: true,
            onLoad: function(g) {
                glow = g;
                $ = glow.dom.get;
                addListener = glow.events.addListener;
                glow.ready(init);
            }
        }
    );
	
	function init() {
		$("body").addClass("js");
	
		// SHOW/HIDE QUOTE-CAROUSSEL
		var imgCaroussel = $(".blog-caroussel li p.img");
		var fistLi = glow.dom.get(".blog-caroussel li").slice(0,1);
		
		$(imgCaroussel).css({
			display: "block",
			cursor: "pointer"
		});
		
		$(fistLi).addClass("current");

		imgCaroussel.each(function(i) {
			var quoteUrl = $(this).prev("div").get("p a").attr("href");
		
			addListener(this, "mouseover", function() {
				if (!$(this).parent().hasClass("current")) {
					$(".blog-caroussel li.current").removeClass("current");
					$(this).parent().addClass("current");
				}
			});
			
			//addListener(this, "mouseout", function() {
				//$(this).parent().removeClass("current");
				//fistLi.addClass("current");
			//});
			
			addListener(this, "click", function() {
				document.location.href = quoteUrl;
			});
        });
	
		// SHOW/HIDE LATEST POST/COMMENTS
		$(".blog-lcomments").addClass("none");
			
		var currentlatest = $(".blog-latest h3");
		$(currentlatest).css("cursor", "pointer");

		glow.anim.fadeOut(".blog-lcomments", 0.5);
		
		addListener(currentlatest, "click", function() {
			var whichDiv = $(this).attr("id");
			var div = $("div." + whichDiv);

			if ($(div).hasClass("none")) {
				glow.anim.fadeOut(".blog-latest div", 0.5);
				$(".blog-latest div").addClass("none");
				glow.anim.fadeIn(div, 0.5);
				$(div).removeClass("none");
			} else{
				glow.anim.fadeIn(div, 0.5);
			}
			
			if (! $(this).hasClass("current")) {
				$(".blog-latest h3").removeClass("current");
				$(this).addClass("current");
			}
		});
		
		// SHOW/HIDE MY_LATEST BLOGS/COMMENTS
		$(".blog-mlcomments").addClass("none");

		var currentMlatest = $(".blog-mlatest h3");
		$(currentMlatest).css("cursor", "pointer");

		glow.anim.fadeOut(".blog-mlcomments", 0.5);
		
		addListener(currentMlatest, "click", function() {
			var whichDiv = $(this).attr("id");
			var div = $("div." + whichDiv);

			if ($(div).hasClass("none")) {
				glow.anim.fadeOut(".blog-mlatest div", 0.5);
				$(".blog-mlatest div").addClass("none");
				glow.anim.fadeIn(div, 0.5);
				$(div).removeClass("none");
			} else{
				glow.anim.fadeIn(div, 0.5);
			}
			
			if (! $(this).hasClass("current")) {
				$(".blog-mlatest h3").removeClass("current");
				$(this).addClass("current");
			}
		});
		
		//BLOG FEATURES : HEIGHT:
		$(".blog-latest").height("37em");
		$(".blog-read").height("37em");
		$(".blog-mlatest").height("37em");
		
		
		//MY_LATEST COMMENTS
		var myLatestComments = $(".blog-mlcomments");
		
		//identity.user.isSignedIn() = true|false
		//identity.user.getId() = string
		
		var request = glow.net.get("script/text.xml", {
			onLoad: function(response) {
				var xmlNodeList = $( response.xml() );
				var commentsList = xmlNodeList.get("contribution");
				commentsList = commentsList.slice(0,4);
				
				var html = '<ul>';
				
				commentsList.each(function(i) {
					var dateTime = ($(this).get("timestamp").children().text()) ;
					var dDate = dateTime.substring(0,2);
					var dMonth = dateTime.substring(3,5);
					var whichMonth;
					if (dMonth == "01"){
						whichMonth = "Jan";
					}else if (dMonth == "02"){
						whichMonth = "Feb";
					}else if (dMonth == "03"){
						whichMonth = "Mar";
					}else if (dMonth == "04"){
						whichMonth = "Apr";
					}else if (dMonth == "05"){
						whichMonth = "May";
					}else if (dMonth == "06"){
						whichMonth = "Jun";
					}else if (dMonth == "07"){
						whichMonth = "Jul";
					}else if (dMonth == "08"){
						whichMonth = "Aug";
					}else if (dMonth == "09"){
						whichMonth = "Sep";
					}else if (dMonth == "10"){
						whichMonth = "Oct";
					}else if (dMonth == "11"){
						whichMonth = "Nov";
					}else {
						whichMonth = "Dec";
					}
					var dYear = dateTime.substring(6,10);
					var dHour = dateTime.substring(11,13);
					var dMin = dateTime.substring(14,16);
				
					i==3 ? ifLast = "last" : ifLast = "";

					
					var bodyText = $(this).get("body").text();
					if(bodyText.length > 200 ){bodyText = bodyText.slice(0,200) + ' &#8230;';}

					
					html += '<li class='+ ifLast +'><p>' + bodyText + '</p>';
					html += '<p>on <a href="' + $(this).get("commentForumUrl").text() + '">' + $(this).get("title").text() + '</a></p>';
					html += '<p>at <span>' + dHour + ':' + dMin + ' on ' + dDate + ' ' + whichMonth + ' ' + dYear +'</span></p></li>';
				});
					
				html += '</ul>';
				myLatestComments.append(html);
			}
        });
		
	
		// QUICKFIND
		var quickFindHolder = $("#blog-listing");
		 
		quickFindHolder.before(
			'<div id="quickfind">' +
			'<form action="#">' +
			'<h3><label for="quickFindInput">Quick Find</label></h3>' +
			'<p><input type="text" id="quickFindInput" value="e.g. EastEnders" /></p>' +
			'</form>' +
			'</div>'
		);
		
		addListener("#quickFindInput",  "focus", function () {
			$(this).attr("value","");
		});

		var myAutoSuggest = new glow.widgets.AutoSuggest("#quickFindInput","script/quickfind.js",{
			anim: "roll",
			width: 202,
			id: "quickFindList",
			index: ["blogName", "blogAuthors"],
			
			isMatch: function(indexedWord, inputWord) {
				return (
					// only try to match when we have more than 1 character
					inputWord.length > 4
					// match anywhere in the word
					&& indexedWord.indexOf(inputWord) > -1
				);
			},

			formatItem: function(data) {
				var matchedAuthors = [],
					regex = new RegExp($('#quickFindInput').val(), "i"),
					authorIndex;
					
				for(authorIndex in data.blogAuthors){
					if(data.blogAuthors[authorIndex].match(regex)){
						matchedAuthors[matchedAuthors.length] = data.blogAuthors[authorIndex];
					}
				}
			
				var html = '<p> ';
				html += '<img src="' + data.blogImg + '" alt="" />';
				html += '<span class="author">' + matchedAuthors.join(', ') + '</span>';
				html += '<span class="blog">' + data.blogName + '</span>';
				html += '</p>';
				return html;
			},

			onItemSelect: function(event){
				window.location.href = event.selectedItem.blogUrl;
			}
		});
		
		
		// SHOW/HIDE BLOG CATEGORIES
		$("#blog-listing").children("ul").addClass("blog-categories");
		
		var curSubNav = $(".blog-categories h3");
		var curCatList = $(".blog-categories h4");
		var listHeight = ""
		
		$(curSubNav).get("span").wrap("<a href='#'></a>");
		$(curCatList).get("span").wrap("<a href='#'></a>");
			
		
		curSubNav.each (function(i) {
			if (! $(this).hasClass("current")) {
				$(this).next("ul").addClass("none");
			}
		});
		
		curCatList.each (function(i) {
			if (! $(this).hasClass("current")) {
				$(this).next("div").addClass("none");
			}
		});
		
		
		$("#blog-all").height(285);
	
		addListener(curSubNav, "click", function() {
		
		
			if  (!$(this).hasClass("current")) {
				$("#blog-listing h3").removeClass("current");
				$("#blog-listing h3").next().addClass("none");
				$(this).addClass("current");
				$(this).next("ul").removeClass("none");
			}
			
			listHeight = ($(".blog-categories").height() + $(this).parent().get("ul").height() + $(this).parent().get("h4.current").next().height() + 39);
			$("#blog-all").height(listHeight);
			
			return false;
		
			
		});
		
		addListener(curCatList, "click", function() {
			$(this).parent("li").parent("ul").addClass("parent");
			
			if (! $(this).hasClass("current")) {
				$("ul.parent li h4").removeClass("current");
				$("ul.parent li h4").next("div").addClass("none");
				$(this).addClass("current");
				$(this).next("div").removeClass("none");
				$(this).parent("li").parent().removeClass("parent");
			} 
			
		
			listHeight = ($(".blog-categories").height() + $(this).parent().height() + $(this).parent().get("div").height() + 39);
			$("#blog-all").height(listHeight);
			
			return false;
		});	
		
		// INFOPANEL
		var showInfoPanel = $(".blog-categories div a");

		$("span.blog-desc").addClass("blq-hide");
		
		var myInfoPanel = new glow.widgets.InfoPanel(
			glow.dom.create('<p></p>'),
			
			{
				width: 350,
				//pointerPosition: "t",
				//offsetInContext: {x: "105%", y: "100%"},
				id: "restyled"
			}
		);
		
		showInfoPanel.each(function(i) {
			var blogName = $(this).text();
			var blogDesc = $(this).next("span").text();
			var blogImg = $(this).attr("name");
			
			addListener(this, "mouseover", function() {
				myInfoPanel.body.empty().append('<div id="simpleInfoPanel">'
				+ '<h2><img src="images/blogs/' + blogImg +'.jpg" alt="" />' + blogName + '</h2>'
				+ '<p>' + blogDesc + '</p>'
				+ '</div>');
				myInfoPanel.setContext(this);
				myInfoPanel.show();
			});
			
			addListener(this, "mouseout", function() {
				myInfoPanel.hide();
			});
        });
    }
})();
