/**
 * Executed when document is finished loading elements into DOM
 */
$(document).ready(function() {
	// display confirmation dialog whenever link with className "confirm" is slicked
	$(".confirm").click(function(){
		return confirm("Vai tieÅ?Ä?m vÄ“laties veikt Å?o darbÄ«bu?");
	});

	if (!(($.browser.msie && jQuery.browser.version <= "6.0"))) {
		$("form").jqTransform();
	}

	$("#active-area, #closed-area").sortable(
	{
		connectWith: ".sort-group",
		items: "div",
		placeholder: "placeholder",
		tolerance : "pointer",
		scroll: false,
		opacity: 0.9,
		handle: "p",
		revert:true,
		update: function(event, ui) {
			// sort active
			var old_ids = $("#active-area").sortable('toArray');
			var new_ids = [];

			for (var n=0; n<old_ids.length; n++) {
				new_ids[n] = old_ids[n].substring(9);
			}

			$.ajax({
				url: webroot+"start/sort_categories/"+new_ids.join(";"),
				cache: false
			});
		}
	});

	$("#w-active-area, #w-closed-area").sortable(
	{
		connectWith: ".sort-group",
		items: "div",
		placeholder: "placeholder",
		tolerance : "pointer",
		scroll: false,
		opacity: 0.9,
		handle: "p",
		revert:true,
		update: function(event, ui) {
			// sort active
			var new_ids = $("#w-active-area").sortable('toArray');

			$.ajax({
				url: webroot+"start/sort_widgets/"+new_ids.join(";"),
				cache: false
			});
		}
	});

	$("#znewsemail").html("<a href='mailto:info"+"@"+"znews.lv'>info"+"@"+"znews.lv</a>");

	$('.pageTracker').click(function() {
	    var linkval = $(this).attr("href");
	    //pageTracker._trackPageview(''+linkval+'');
	    _gaq.push(['_trackPageview', ''+linkval+''])
	    //alert(linkval);
	});
});

/**
 * Print email link (protection from spambots)
 * If javascript is disabled, nothing is visible
 *
 * @param string p1 Part 1 - this is username
 * @param string p2 Part 2 - this is domain and TLD
 * @param string name (optional) Display value
 */
function writemail(p1, p2, name) {
	if (typeof(name) == "undefined") {
		name = p1+"@"+p2;
	}

	document.write("<a href='mailto:"+p1+"@"+p2+"'>"+name+"</a>");
}

/**
 * Hide specified category block from first page
 *
 * @param int id Category ID
 * @return void
 */
function hideCategory(id) {
	$("#fp-category-block-"+id).hide();

	$.ajax({
		url: webroot+"categories/hide/"+id,
		cache: false
	});
}

function showCustomizeDialog() {
	$("#customize").fadeIn("fast");
}

function hideCustomizeDialog() {
	$("#customize").fadeOut("fast");

	setTimeout("location.reload(true)", 500);
}

function showWCustomizeDialog() {
	$("#wcustomize").fadeIn("fast");
}

function hideWCustomizeDialog() {
	$("#wcustomize").fadeOut("fast");

	setTimeout("location.reload(true)", 500);
}

function selectWatherCity(city_id) {
	$(".weather-city").hide();
	$("#weather-city-"+city_id).show();
}

function hideHelpbox() {
	$("#helpbox").slideUp("fast");

	$.ajax({
		url: webroot+"start/hide_helpbox",
		cache: false
	});
}

/**
 * Read cookie
 *
 * @return string
 */
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
