var DH = {};
var DH2 = {};

(DH2.buildNavigation = function () {
	if ($.browser.msie) {
		$("#content-navigation .navigation-tongue").hover(
			function () {
				$(this).find("ol").toggle("fast");
				$(this).addClass("selected");
			},
			function () {
				$(this).find("ol").toggle("slow");
				$(this).removeClass("selected");
			}
		)
	}
	else {
		$("#content-navigation .navigation-tongue").hover(
			function () {
				$(this).find("ol").slideToggle("fast");
				$(this).addClass("selected");
			},
			function () {
				$(this).find("ol").slideToggle("slow");
				$(this).removeClass("selected");
			}
		)
	}
})();

DH2.init = function (){
	DH2.link_augment();
	DH2.form_augment();
	DH2.accordion_augment();
	DH2.panels_augment();
	DH2.list_augment();
	DH2.console_augment();
}

DH2.console_augment = function () {
	DH2.fontsizer(true);

	$("#content-article-console .text").click(function () {
		DH2.fontsizer();
	});
}

DH2.link_augment = function () {
	$("a[rel='advertise']").click(function () {
		window.open(this.href,"DarkHorizons-GNM","scrollbars=yes,width=550,height=600");
		return false;
	});
	$("a[rel='external']").click(function () {
		window.open(this.href);
		return false;
	});
	$("a[rel='share']").click(function () {
		window.open(this.href);
		return false;
	});
	$("a[rel='contact']").click(function () {
		window.open(this.href,"DarkHorizons-contact","scrollbars=yes,width=540,height=750");
		return false;
	});
	$("a[rel='search']").click(function () {
		var url = "http://www.google.com/custom?sitesearch=darkhorizons.com&cof=GIMP%3A%23777777%3BT%3A%23cecece%3BLW%3A300%3BALC%3A%2366CC99%3BL%3Ahttp%3A%2F%2Fwww.darkhorizons.com%2Fdhlogo.jpg%3BGFNT%3A%2336629E%3BLC%3A%23538687%3BLH%3A30%3BBGC%3A%23000000%3BAH%3Acenter%3BVLC%3A%23538687%3BGL%3A2%3BS%3Ahttp%3A%2F%2Fwww.darkhorizons.com%3BGALT%3A%2336629E%3BAWFID%3A6083c4a181ce0112%3B&domains=darkhorizons.com";
		window.open(url);
		return false;
	});
}

DH2.form_augment = function () {
	$("form[rel='external']").attr("target","_blank");

	$("form input").attr("default","Search").focus(function(){
		$(this).attr("value","").addClass("generated");
	});

	$("form input").attr("default","Search").blur(function(){
		if ($(this).attr("value").match(/^\s*$/)) { //use the default if only have empty string or space
			$(this).attr("value", $(this).attr("defaultValue")).removeClass("generated");
		}
	});
};

DH2.accordion_augment = function () {
	$(".accordion").accordion({
		header: 'a.accordion-toggle',
		clearStyle: true,
		autoHeight: false,
		collapsible: true,
		active: false
	});

	$(".accordion-toggle").mouseleave(function(){
		if( !$(this).hasClass("ui-state-active") ) {
			$(this).removeClass("ui-state-focus");
		};
	});
};

DH2.pageJump = function (which) {
	if (which != "") {
		location = which;
	}
}

DH2.advertisements = ["tergiversate-banner","tergiversate-skyscraper","tergiversate-island"];
DH2.contents = ["content","content-column","content-list","content-billboard","content-breakout"];

DH2.advertSwap = function (id) {
	var destination = document.getElementById("advertising-" + (id.split("-")[1]));
	var advertisement = document.getElementById(id);
	destination.appendChild(advertisement);
};

DH2.advertHold = function () {
	var oDHisland = document.getElementById("content-island");
	var oDHislandHolder = document.getElementById("content-island-holder");
	var sHeight = $(".tergiversate-island").height();
	
	$(".tergiversate-island").css({height:sHeight});
	$(".tergiversate-island-holder").css({height:sHeight});
};

DH2.panels_augment = function () {
	$(".module-panel").each(function () {
		DH2.modulePanels(this.id);
	});
};
DH2.modulePanels = function (which,count) {
	var cookie_name = "dh_custom_" + document.getElementsByTagName("body")[0].id + "_" + which;//section based
	if(!$.cookie(cookie_name)) {
		$.cookie(cookie_name, count);
	}
	var tab = which + " .module-control a";
	$(tab).click(function () {
		var that = $(this).attr("className").split("-")[1];
		var panel = which + " .panel-" + that;

		if (that.length < 4) {
			$(tab).parent().removeClass("tab-active");
			$(this).parent().addClass("tab-active");
			$(which + " .module-body").hide();
			$(which + " .module-body .module-body").show();
			$(panel).show();
		}
		else {
			DH2.moduleLines(which, that)
		}
		return false;
	});
};

DH2.moduleLines = function (which, way) {
	var $one, $two, $tre;
	var last = false;
	var item = (which.indexOf("-trailers") != -1) ? " div.accordion>div" : " .module-headlines li";
	var cookie_name = "dh_custom_" + document.getElementsByTagName("body")[0].id + "_" + which;//section based
	var count;

	if ($(which + item).hasClass("panel-more")) {
		$one = $(which + " .panel-one" + item + ".panel-more:first");
		$two = $(which + " .panel-two" + item + ".panel-more:first");
		$tre = $(which + " .panel-tre" + item + ".panel-more:first");
		last = false;
	}
	else {
		$one = $(which + " .panel-one" + item + ":last");
		$two = $(which + " .panel-two" + item + ":last");
		$tre = $(which + " .panel-tre" + item + ":last");
		last = true;
	}

	if (way === "more") {
		$one.removeClass("panel-more");
		$two.removeClass("panel-more");
		$tre.removeClass("panel-more");
		count = last ? 0 : 1;
	}
	else {
		if (last){
			$one.addClass("panel-more");
			$two.addClass("panel-more");
			$tre.addClass("panel-more");
		}
		else if (!$one.prev().hasClass("panel-minimum")) {
			$one.prev().addClass("panel-more");
			$two.prev().addClass("panel-more");
			$tre.prev().addClass("panel-more");
		}
		count = $one.prev().hasClass("panel-minimum") ? 0 : -1;
	}

	if ($.cookie(cookie_name)) {
		var current_count = parseInt($.cookie(cookie_name));
		current_count += count;
		$.cookie(cookie_name, current_count);
	} else {
		$.cookie(cookie_name, 1);
	}
};

DH2.fontsizer = function (customised) {
	var target = "#content-article-body p";
	var size = parseFloat($(target).css("font-size"));
	var normal = 13.3833;
	var larger = 14.4;
	var smaller = 12;
	var scale, className;
	var cookie = "dh-custom";

	if (customised) {
		if ($.cookie(cookie)) {
			scale = $.cookie(cookie).split(",")[0] + "px";
			className = $.cookie(cookie).split(",")[1];
		}
	}
	else {
		if (size == larger) {//largest, make it smallest
			scale = smaller;
			className = "text-smaller";
		}
		else if (size == smaller) {//smallest, make it normal
			scale = normal;
			className = "text-normal";
		}
	 	else {//normal, make it larger
			scale = larger;
			className = "text-larger";
		}
		$.cookie(cookie, scale + "," + className);
	}
	
	$(target).css("font-size",scale);
	$("#content-article-console .text")
		.removeClass("text-smaller")
		.removeClass("text-normal")
		.removeClass("text-larger")
		.addClass(className);
};

DH2.list_augment = function () {
	$(".module-control a.tab-title").click(function () {
		if (!$.browser.msie) {
			$(".module-slide").slideToggle("slow");
		}
		else {
			$(".module-slide").toggle();
		}
		return false;
	});
};

$(document).ready(function () {
	DH2.init();
});