/*global jQuery, window, configDomain, $*/
/*jslint white: true, onevar: true, browser: true, devel: true, widget: false, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: false, strict: true, newcap: true, immed: true */
"use strict";

var Boostturku = {};

/* Blog widget */
Boostturku.blogWidget = function () {
	
	/* Set the basic variable for creating the blog widget || HARD CODED || TODO: Move as parameters */
	var blogAddress = "http://blog.boostturku.com/api/read/json",
	blogStart = 0,
	blogNum = 10,
	blogType = '',
	blogId = '',
	blogFilter = '',
	blogArticlesObject = [];
		
	/* Blog articles */
	function blogArticles() {
		
		/* For documentation see http://www.tumblr.com/docs/en/api#api_read */
		this.loadArticle = function (blogStart, blogNum, blogType, blogId, blogFilter, blogTagged, blogSearch) {
			//Load articles (fetch JSON data)
			$.ajax({
				url: blogAddress + "?start=" + blogStart +"&num=" + blogNum +"&type=" + blogType +"&id=" + blogId +"&filter=" + blogFilter +"&blogTagged=" + blogTagged +"&search=" + blogSearch,
				dataType: "jsonp",
				success: function (data) {
				var i;
				for (i = 0; i < data.posts.length; i = i + 1) {
										
						//TODO: Make a decent MVC implementation out of this clusterfuck
						$("#boostturku-blog").append("");
												
						$("#boostturku-blog").append("<div class='blog-entry' id='blog-" + data.posts[i]['id'] + "'><h1>" + data.posts[i]['regular-title'] + "</h1>" + data.posts[i]['regular-body'] + "<footer><!--<p>Tagged: " + data.posts[i]['tags'] + "</p>--><p><a href=\"" + data.posts[i]['url-with-slug'] + "\">Comments</a></p></footer><p class=\"blog-entry-more\"><a href=\"#\">Read more &raquo;</a></p></div>");
					}	
						
					//Functions for creating the widget from loaded articles
					
					$(".blog-entry-more").click(function(event){
						event.preventDefault();
						$(this).siblings().show(400);
						$(this).hide();
					});
					
				}
			})	
						
		};
					
	}
	
	blogArticlesObject[0] = new blogArticles();
	blogArticlesObject[0].loadArticle(0, 10, '', '', '', '', '', '');	
	
	return true;
}

/* Trigger on pageload */
$(function () {
	
	/* Loads blog */
	Boostturku.blogWidget();

	/* Ad-hoc show-hide */
	$("#cushy-sidebar img[class!='showLessImg']").click(function(){
		$(this).parents("p").siblings("p").find(".showMore").parents("p").hide().siblings(".companyDescription").show().siblings(".showLess").show();
		
		$(this).bind('click.showLessImg', function(){
			$(this).parents("p").siblings(".showLess").hide().siblings(".companyDescription").hide().siblings("p").find(".showMore").parents("p").show();
			$(this).unbind('click.showLessImg');
		});
		
	});

	$("#cushy-sidebar .showMore").click(function(event){
		event.preventDefault();
		$(this).parents("p").hide().siblings(".companyDescription").show().siblings(".showLess").show();
		
		$(this).parents("p").siblings("p").find("img").bind('click.showLessImg', function(){
			$(this).parents("p").siblings(".showLess").hide().siblings(".companyDescription").hide().siblings("p").find(".showMore").parents("p").show();
			$(this).unbind('click.showLessImg');
		});

	});
	
	$("#cushy-sidebar .showLess a").click(function(event){
		event.preventDefault();
		$(this).parents("p").hide().siblings(".companyDescription").hide().siblings("p").find(".showMore").parents("p").show();
		$(this).parents("p").siblings("p").find("img").unbind('click.showLessImg');
	});

	$("#form-loader").hide();
	$("#entryform").show();
});




