
var Cookie = {
    create: function(name, value, days) {
	    if(days) {
		    var date = new Date();
		    date.setTime(date.getTime()+(days*24*60*60*1000));
		    var expires = "; expires="+date.toGMTString();
	    }
	    else expires = "";
	    document.cookie = name+"="+value+expires+"; path=/";
    },
    read: function(name) {
	    var nameEQ = 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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
    }
};

var SiteFont = {
	init: function() {
		if(Cookie.read('siteFontSize') == 'larger') SiteFont.larger();
	},
	smaller: function() {
		var body = document.getElementsByTagName('body');
		if(body[0]) {
			body[0].style.fontSize = 'smaller';
			Cookie.create('siteFontSize', 'smaller', 7);
		}
		return false;
	},
	larger: function() {
		var body = document.getElementsByTagName('body');
		if(body[0]) {
			body[0].style.fontSize = 'larger';
			Cookie.create('siteFontSize', 'larger', 7);
		}
		return false;
	}
};

var Tag = {
    showSearchField: function(elem, id, default_size) {
        for (i=0; i<default_size; i++) Element.hide('search_tags_' + i);
        if (elem.value != '')
            Element.show('search_tags_' + id)
        else
            Element.hide('search_tags_' + id);
    },
    searchTag: function(div_id, field_id) {
        var v = $F(field_id);
        if (v != null && v != '') {
            var ajax = new Ajax.Request(
                '/home/tag-field-auto-complete/' + field_id,
                {evalScripts: true, parameters: 'div_id=' + div_id + '&tag=' + v}
            );
        }
    }
};

// Miscellaneous Functions

// findPostX & findPostY are from ... heck, where did I take these functions?
// (someone's blog, I remembered, but I lost my note)

function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
		while (1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function getElementStyle(elem, style) {
	if(document.getElementById(elem).currentStyle)
		v = document.getElementById(elem).currentStyle[style];
	else if(window.getComputedStyle) {
		var elstyle = window.getComputedStyle(document.getElementById(elem), "");
		v = elstyle.getPropertyValue(style);
	}
	return v;
}

function getMousePosition(e) {
	var posx = 0;
	var posy = 0;
    var e = (e) ? e : ((window.event) ? window.event : "");
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
    return {x: posx, y: posy}
}

function commentForm(url, height, width) {
    if (height == null) height = 550;
    if (width == null) width = 550;
//    left = (Geometry.getWindowX() + Geometry.getViewportWidth())/2 - width/2;
//    top = (Geometry.getWindowY() + Geometry.getViewportHeight())/2 - height/2 + 30;
//    window.open(url, 'height=' + height + ',width=' + width + ',left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top);
    window.open(url, "", 'scrollbars=yes,height=' + height + ',width=' + width);
}

function positionElementWithMouse(elem, e, width) {
    var width = (width) ? width : 200;
    mousePos = getMousePosition(e);
    $(elem).style.left = mousePos.x + 'px';
    $(elem).style.top = mousePos.y + 'px';
    $(elem).style.width = width + 'px';
}

function disableOtherElements(obj, elems) {
  if (obj.checked) {
    for (var i = 0; i < elems.length; i++) {
      $(elems[i]).disabled = true;
    }
  } else {
    for (var i = 0; i < elems.length; i++) {
      $(elems[i]).disabled = false;
    }
  }
}

// -------------------------------------------------------------------

Element.slideDown = function(elem) {
	$(elem).style.width = '100%';
	Effect.SlideDown(elem);
}

Element.slideUp = function(elem) {
	Effect.SlideUp(elem);
}

Element.toggle_menu = function(elem, ctl_to_hide, ctl_to_show, do_show) {
	do_show ? Element.show(elem) : Element.hide(elem);
	Element.hide(ctl_to_hide);
	Element.show(ctl_to_show);
}

Element.show_menu = function(elem, ctl_to_hide, ctl_to_show) {
	Element.toggle_menu(elem, ctl_to_hide, ctl_to_show, true);
}

Element.hide_menu = function(elem, ctl_to_hide, ctl_to_show) {
	Element.toggle_menu(elem, ctl_to_hide, ctl_to_show, false);
}

Effect.FlashAppear = function(elem, options) {
	Element.show(elem);
	new Effect.Highlight(elem, options);
}

// -------------------------------------------------------------------

var Loading = {
	show: function() {
		$('loading').style.visibility = 'hidden';
		Element.show('loading');
		$('loading').style.top = Geometry.getVerticalScroll() + 5 + 'px';
		$('loading').style.left = Geometry.getViewportWidth()/2 - $('loading').offsetWidth/2 + 'px';
		$('loading').style.visibility = 'visible';
		return false;
	},
	hide: function() {
		Element.hide('loading');
	}
};

// Register Actions

Ajax.Responders.register({
	onCreate: function(req) {
		req['timeoutId'] = window.setTimeout(
			function() {
				Loading.hide();
				req.transport.abort();
				alert("The network is down. Please try again later.");
			},
			50000
		);
		if (Ajax.activeRequestCount > 0) {
			Loading.show();
		}
	},
	onComplete: function(req) {
		window.clearTimeout(req['timeoutId']);
		if (Ajax.activeRequestCount == 0) {
			Loading.hide();
		}
	}
});

window.onload = function() {
	SiteFont.init();
}
