//getElementById() + cache
function getter(){
	getter_cache = {};

	//Get element by id
    this.get = function(id){
		if(getter_cache[id]){
			return getter_cache[id];
		}
		getter_cache[id] = document.getElementById( id );
		return getter_cache[id];
	}

	//Clear cache
	this.clear_cache = function(){
		for(var id in getter_cache)
			getter_cache[id] = undefined;
	}
}

function toggle_display(id, flag, getter_obj){
	var el = getter_obj && getter_obj.get ? getter_obj.get( id ) : document.getElementById( id );
	if(!el)
		return false;
	el.style.display = flag ? '' : 'none';
	return true;
}

//Input length tracker
function set_track(el, ml){
	if(el.addEventListener){
		el.addEventListener('change', get_track_func(el, ml), true);
		el.addEventListener('keyup', get_track_func(el, ml), true);
	}
	else if(el.attachEvent){
		el.attachEvent('onchange', get_track_func(el, ml));
		el.attachEvent('onkeyup', get_track_func(el, ml));
	}
	(get_track_func(el, ml))();
}
function get_track_func(el, ml){
	return function(){
		if(el.value == undefined)
			return;
		var len = el.value.length;
		var track = _.get(el.id + '_track');
		track.getElementsByTagName('span')[0].innerHTML = len;
		track.getElementsByTagName('div')[0].style.width = (len > ml ? 100 : Math.round(100 * len / ml)) + '%';
		if(len > ml)
			track.className = 'tracker overflow';
		else
			track.className = 'tracker';
	}
}

//Color table rows if table class="zebra"
function zebra(){
	var tables = document.getElementsByTagName('TABLE');
	var i = 0, l = tables.length;
    var trs, j, l2, cur;
	for(; i < l; i++){
		if(/\bzebra\b/.test(tables[i].className)){
			trs = tables[i].getElementsByTagName('tr');
			cur = 0;
			for(j = 0, l2 = trs.length; j < l2; j++){
				if(/*trs[j].parentNode.tagName == 'THEAD' || */trs[j].parentNode.tagName == 'TFOOT')
					continue;
				trs[j].className += ' zebra_line' + ((cur % 2) + 1)
				cur++;
			}
		}
	}
}

//Make links with class="external" open in new window
function externals(){
	var i, l, as = document.getElementsByTagName('A');
	for(i = 0, l = as.length; i < l; i++){
		if(as[i].className.indexOf('external') != -1)
			as[i].onclick = function(){ window.open(this.href); return false; }
	}
}

function p_ajax(url, el_id){
	if(!_.get(el_id))
		return;
	url += (url.indexOf('?') == -1 ? '?' : '&') + 'r=' + Math.random();
	var span = _.get(el_id).appendChild(document.createElement('SPAN'));
	var scrpt = span.appendChild(document.createElement('SCRIPT'));
	scrpt.src = url;
}

function popup(src, width, height, title, add_html){
	var html = '<html><head><title>' + title + '</title><link rel="stylesheet" type="text/css" href="/styles/popup.css" /></head><body style="margin:0;padding:0;"><img src="' + src + '" width="' + width + '" height="' + height + '" alt="" style="display:block;cursor:pointer;" onclick="window.close()" /><div id="caption"><p>' + title + (add_html ? ' ' + add_html : '') + '</p></div></body></html>';
	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	var wnd = window.open('', '', 'toolbar=0,location=1,directories=0,menuBar=0,scrollbars=0,resizable=1,width=' + width + ',height=' + height + ',left=' + x + ',top=' + y + ',screenX=' + x + ',screenY=' + y);
	wnd.document.open();
	wnd.document.write(html);
	wnd.document.close();
}

function NavigateMe(event){
	var link;
	if(!event && window.event) event = window.event;
	if(!event) return;
	switch (event.keyCode ? event.keyCode : event.which ? event.which : null){
		case 0x25:
			link = _.get('PrevLink');
		break;
		case 0x27:
			link = _.get('NextLink');
		break;
	}
	if (link && link.href) document.location = link.href;	
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function align_height(ids, correction){
	var max_height = 0, i = 0, l = ids.length, el;
	if(!correction)
		correction = 0;

	//First pass - determine max height
	for(; i < l; i++){
		if((el = _.get(ids[i])) && (el.offsetHeight > max_height))
			max_height = el.offsetHeight;
	}

	//Second pass - align heights
	for(i = 0; i < l; i++){
		if((el = _.get(ids[i])))
			el.style.height = (max_height + correction) + 'px';
	}
}

function bm_add(t, url, title){
	var goto;
	switch(t){
		case 'favourites':
			if(window.sidebar)
				window.sidebar.addPanel(title, url, "");
			else if(window.opera && window.print){
				var elem = document.createElement('a');
				elem.setAttribute('href', url);
				elem.setAttribute('title', title);
				elem.setAttribute('rel', 'sidebar');
				elem.click();
			} 
			else if(document.all)
				window.external.AddFavorite(url, title);
			return;
		break;
		case 'google':
			goto = 'http://www.google.com/bookmarks/mark?op=add&bkmk=' + url + '&title=' + title;
		break;
		case 'yahoo':
			goto = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' + url + '&t=' + title + '&d=&ei=UTF-8';
		break;
		case 'facebook':
			goto = 'http://www.facebook.com/sharer.php?u=' + url + '&t=' + title;
		break;
		case 'myspace':
			goto = 'http://www.myspace.com/Modules/PostTo/Pages/?u=' + url + '&t=' + title + '&c=';
		break;
		case 'twitter':
			goto = 'http://twitter.com/home?status=' + url + ' via @addthis';
		break;
		case 'live':
			goto = 'https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=' + url + '&title=' + title + '&top=1';
		break;
		case 'linkedin':
			goto = 'http://www.linkedin.com/shareArticle?mini=true&url=' + url + '&title=' + title + '&ro=false&summary=&source=';
		break;
	}
	window.open(goto);
}

function format_price(str){
	var pr = parseFloat(str);
	var p1 = Math.floor(pr);
	var p2 = pr - p1;
	var p2_str;
	if(p2 == 0)
		p2_str = '00';
	else{
		p2 = Math.floor(p2 * 100);
		p2_str = (p2 < 10 ? '0' : '') + p2;
	}

	var p1_r = [], j = 0;
	for(i = p1.length - 1; i >= 0; i--){
		j++;
		p1_r.unshift(p1.charAt(i));
		if(j % 3 == 0 && i != 0)
			p1_r.unshift(',');
	}
	return p1_r.join('') + '.' + p2_str;
}