function readCookie(name) {
	var cookiename = name + "=";
	var ca = document.cookie.split(';');
	for ( var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
		if (c.indexOf(cookiename) == 0)
			return c.substring(cookiename.length, c.length);
	}
	return null;
}

function setCookie(c_name, value, expiredays) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000);

	document.cookie = c_name + "=" + escape(value) + ";expires="
			+ expire.toGMTString() + ";path=/";
}

//
// Martin Saulis for BALSAS.LT
//

$(function() {

	// adding rounded corner containers

	// v2

	$sel_items = $(".sub_cols");
	$top_pos = 0;
	$last_changed = 0;
	for ( var i = 0; i < $sel_items.length; i++) {
		$h1 = $($sel_items[i]).find('.sub_col1').height();
		$h2 = $($sel_items[i]).find('.sub_col2').height();
		if ($h2 > $h1)
			$($sel_items[i]).find('.sub_col1').height($h2);
	}

	$(".tabmenu li").click(function() {
		var index = $(".tabmenu li").index(this);
		$(this).parents(".tabs").find(".tab").hide();
		$(this).parents(".tabmenu").find("li").removeClass("active");
		$(this).addClass("active");
		var tab = $(this).parents(".tabs").find(".tab").get(index);
		$(tab).show();
		return false;
	});

	$(".photoswitcher td").hover(function() {
		if (!($(this).hasClass(".image"))) {
			// mouse over
			$(this).parents("table").find("td").removeClass("current");
			$(this).addClass("current");

			// change video / foto img
			$(this).parents("table").find("td").find(".video_ico").attr("src",
					"/img/video_grey_bg.gif");
			$(this).parents("table").find("td").find(".foto_ico").attr("src",
					"/img/foto_grey_bg.gif");
			// $(this).find("a").find("img").attr("src",
			// "/img/video_red_bg.gif");
			$(this).find(".video_ico").attr("src", "/img/video_red_bg.gif");
			$(this).find(".foto_ico").attr("src", "/img/foto_red_bg.gif");

			var image_src = $(this).children("img");
			var image_link = image_src[0].src;
			var table_bg = $(this).parents("table");

			$(table_bg).css( {
				background : "url('" + image_link + "') no-repeat"
			})

			var p_src = $(this).find("p");
			$(this).parents(".inside").find(".content").find("p").empty();
			$(this).parents(".inside").find(".content").find("p").append(
					p_src.html());
		}
	});
	$(".photoswitcher td").click(function() {
		var a = $(this).find("a");
		if (a) {
			location.href = a[0].href;
		}
	});

	// end v2

	/*
	 * var rounded = new Array( "#top_menu li a", "#main_menu td > a",
	 * "#main_menu div > a", "#sub_menu", "#search_menu form", ".box", "#media
	 * .menu a", "#orai .menu a", "#files .menu a", "#videos .menu a", "#archive
	 * .menu a", "#top .menu a", ".video .button", ".column_button", ".play" );
	 * rounded = Array();
	 * 
	 * 
	 * for(i = 0; i < rounded.length; i++){
	 * 
	 * var corners = "<span class='corner tl'>&nbsp;</span><span
	 * class='corner tr'>&nbsp;</span><span class='corner bl'>&nbsp;</span><span
	 * class='corner br'>&nbsp;</span>";
	 * 
	 * for(j=0;j<$(rounded[i]).length;j++){ x = $($(rounded[i])[j]);
	 * $(x).append(corners); } }
	 */

	// adding comment counter containers
	// $(".comments a:empty").remove();
	// $(".comments:empty").remove();
	// $(".comments").wrap("<span class='comments2'></span>");
	// $(".comments").before("<span class='comments3'>&nbsp;</span>");
	$(".advertisment").show()

	// setting text size

	$("a").filter(function() {
		if (this.hostname == 'balsuotojas.balsas.lt')
			return !(this.hostname && this.hostname !== location.hostname);
		else
			return this.hostname && this.hostname !== location.hostname;
	}).attr('target', '_blank');

	size = get_cookie("balsas_text_size");

	if (size < 151 && size > 99)
		$("#content *").css( {
			"font-size" : size + "%"
		});

	// dropdown menu

	$("#main_menu td > div").hover(function() {
		$(this).css( {
			"z-index" : "10"
		});
		$(this).addClass("current");
		$(this).addClass("hover");
		$("a:first", this).css( {
			"z-index" : "50"
		});
		$("ul", this).show();
	}, function() {
		$("ul", this).hide();
		$(this).removeClass("current");
		$(this).removeClass("hover");
	});

	// media

	$("#media_left").click(function() {
		var z = 0;
		if ($(".videos:visible .item:visible").length > 6) {
			$($(".videos:visible .item:visible")[0]).hide("fast");
		}
		return false;

	});

	$("#media_right").click(function() {
		var z = 0;
		if ($(".videos .item:hidden:last").length > 0) {
			$($(".videos .item:hidden:last")[0]).show("fast");
		}
		return false;

	});

	// orai

	/*
	 * $("#orai_left").click(function(){ var z = 0; if($(".videos:visible
	 * .item:visible").length>6){ $($(".videos:visible
	 * .item:visible")[0]).hide("fast"); } return false;
	 * 
	 * });
	 * 
	 * $("#orai_right").click(function(){ var z = 0; if($(".videos
	 * .item:hidden:last").length>0){ $($(".videos
	 * .item:hidden:last")[0]).show("fast"); } return false;
	 * 
	 * });
	 */

	// $("#top > div").hide(); // not needed now
	var size = 110;
	$(".plus").click(function() {
		size += 10;
		size = size > 150 ? 150 : size;
		document.cookie = "balsas_text_size=" + size + "; path=/;";
		$("#content *").css( {
			"font-size" : size + "%"
		});
	});

	$(".minus").click(function() {
		size -= 10;
		size = size < 100 ? 100 : size;
		document.cookie = "balsas_text_size=" + size + "; path=/;";
		$("#content *").css( {
			"font-size" : size + "%"
		});
	});

	$("#top > ul li a").click(function() {
		/*
		 * id = $(this).attr('href').replace(/#/g, '');
		 * 
		 * $(this).parents("div:first").find("div").hide();
		 * $(this).parents("div:first").find("*").removeClass("current");
		 * 
		 * $(this).parent().addClass("current");
		 * 
		 * $(this).parents("div:first").find("div[class='"+id+"']").show();
		 */
		tab(this);
		return false;
	});

	$("#media > ul li a").click(function() {
		tab(this);
		return false;
	});

	$("#videos > ul li a").click(function() {
		tab(this);
		return false;
	});

	$("#orai > ul li a").click(function() {
		tab(this);
		return false;
	});

});

function tab(e) {
	id = $(e).attr('href').replace(/#/g, '');

	// $(e).parents("div:first").find("div").hide();
	first = $(e).parents("div:first")[0];
	$("> div", first).hide();

	$(e).parents("div:first").find("*").removeClass("current");

	$(e).parent().addClass("current");

	$(e).parents("div:first").find("div[class*='" + id + "']").show();
	return false;
}

function viewport() {
	var x, y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	} else if (document.documentElement
			&& document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [ x, y ];
}

function scrollsize() {
	var x, y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop)
	// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	} else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return [ x, y ];
}
var v = {
	w : null,
	h : null,
	x : null,
	y : null
}
function video_maximize() {
	m = document.getElementById("video");
	/*
	 * m.style.marginBottom = "-22em"; m.style.height = "56em";
	 * m.style.marginRight = "-25.5em"; m.style.width = "65.5em"; m.style.top =
	 * "-10em"; m.style.left = "-1em";
	 */
	v.w = m.offsetWidth;
	v.h = m.offsetHeight;
	// console.log(v);

	m.style.marginLeft = "-1px";
	m.style.padding = "0";
	m.style.position = "absolute";
	m.style.left = "-10px";
	m.style.top = "-2px";
	m.style.width = "1000px";
	m.style.height = viewport()[1] - 20 + "px";

	// $(m).parent().css({"position":"static"});
}
function video_minimize() {
	// console.log(v);
	m = document.getElementById("video");
	m.style.width = v.w / 10 + "px";
	m.style.height = v.h / 10 + "px";
	m.style.position = "relative";
	m.style.top = "0";
	m.style.left = "0";
}

function popup(href, title, width, height, center) {
	var left = 100;
	var top = 100;
	if (center) {
		left = (screen.width - width) / 2;
		top = (screen.height - height) / 2;
	}
	window
			.open(
					href,
					title,
					'width='
							+ width
							+ ', height='
							+ height
							+ ', left='
							+ left
							+ ', top='
							+ top
							+ ', directories=no, location=no, resizable=no, menubar=no, toolbar=no, scrloobars=no, status=no');
	return false;
}

function findPos(obj) { // quirksmode
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [ curleft, curtop ];
}

function get_cookie(cookie_name) {
	var results = document.cookie
			.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
	if (results)
		return (unescape(results[2]));
	else
		return null;
}
