Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};
$.fn.highlight = function() {
	$(this).css('backgroundColor', "#faa")
	$(this).animate({'backgroundColor': '#fff'}, 800, function() {
		$(this).css('backgroundColor', 'none');
	})	
}
$(document).ready(function() {  //Sķšan er loaduš en t.d. myndir enn aš loadast
    $('a.lightbox, a[rel=lightbox], a[rel=gallery], a.fancybox').live('click', function() {
		$('a.lightbox, a[rel=lightbox], a[rel=gallery], a.fancybox').fancybox({
			centerOnScroll: false
		})
		$(this).click();
		return false;
    })

    $('.hover').hover(function() {
			$(this).addClass('hoverSel')
	}, function() {
		$(this).removeClass('hoverSel')
	})
    $.fn.vAlign = function() {
        return this.each(function(i){
        var ah = $(this).height();
        var ph = $(this).parent().height();
        var mh = (ph - ah) / 2;
        //$('#opnunartimi2').html(ah + " " + ph)
        if(ph>0) {
            $(this).css('margin-top', mh);
        }
        });
    };		
})

$(window).load(function() {  //Sķšan alveg loaduš, m.a. myndir
    //$('.v_center').vAlign();
})

var timeout_return = function() {
		var timeout;
		var obj = this
		var objs = $(this).find('input, textarea, select, radio').andSelf()
		var time = $(obj).attr('timeout') ? $(obj).attr('timeout') : 500;
		objs.each(function(i, item) {
			//$(item).trigger('hasChanged')
			//alert("b "+$(item).attr('type'))
			if($(item).attr('type')=="checkbox" || $(item).is("select") || $(item).is("radio")) {
				$(item).bind('change', function() {
					$(item).trigger('hasChanged')
				})
				return;
			}
			if($(item).attr('type')=="text" || $(item).is("textarea")) {
				$(item).keydown(function(e) {
					$(this).attr('oldval', $(this).val())
					//$("h3").text($(this).val())
				})
				$(item).keyup(function(e) {
					var ov = $(item).attr('oldval')
					var val = $(item).val()
					if(ov != val) {
						$(item).attr('oldval', val)
						if(timeout) {
							clearTimeout(timeout);
							timeout = null;
						}
						timeout = setTimeout(function() { $(item).trigger('hasChanged') }, time)
					}
				})
				$(item).blur(function() { 					
					if($(this).attr('oldval') && $(this).attr('oldval')!=$(this).val()) { $(item).trigger('hasChanged') }  });		
			}
		})
		return false;
	}
$.fn.timeout = timeout_return;
	
var ajax_return = function() {
		var rel = $(this).attr('rel');
		rel = rel ? rel : $(this)
		var url = $(this).attr('href');
		var target = $(this).attr('target');
		var is_input = $(rel).is('input, textbox, select, option')
		var value = is_input ? $(rel).val() : $(rel).text();
		var item = this
		$.ajax({
			url: url,
			data: {'val': value},
			success: function(data) {
				$(rel).trigger('ajax', [data])
				$(item).trigger('ajax', [data])
				if(target) {
					$(target).append(data)
					$(target).trigger('ajax', [data])
				}
				if(is_input) {
					$(item).highlight()
				}
			}
		})
		return false;
	}		
$(function() {
	$(document).click(function(e) {
		var t = $(e.target)
		if(!t.hasClass('noclick') && !t.closest('.noclick').length) {
			if(window._uploading) {
				return;
			}
			var hideble = t.closest('.hideble')
			$('.hideble').not(hideble).hide().trigger('cancel');
		}
	})	
	$(document).keydown(function(e) {
		var t = $(e.target)
		if(!t.hasClass('keyboard') && t.closest('.keyboard').length) {
			t = t.closest('.keyboard');
		}
		if(window.hasOwnProperty('_keyEvent')) {
			window._keyEvent.call(t, e);
		}
		if(t.hasClass('keyboard') || t.closest('.hideble').length ) {
			switch(e.keyCode) {
				case 27:
					t.closest('.hideble').length ? t.closest('.hideble').hide() : t.hide();
					break;
				case 13:
					t.find('.save').click();
					break;
			}
		}
	})
	
	$('.hide').live('click', function() {
		$(this).closest('.hideble').hide();
	})
	
	$('.del').live('click',function() {
		var r = $(this).attr('del');
		var rel = $(this).attr('rel');
		var url = $(this).attr('href') ? $(this).attr('href') : $(r).attr('href');
		if(!url) return
		var value = $(r).is('input, textbox, select, option') ? $(r).val() : $(r).text();
		if($(this).hasClass('confirm')) {
			if(!confirm('Ertu viss?')) {
				return false
			}
		}
		$.ajax({
			url: url,
			data: { val : value },
			success: function(data) {
				$(r).remove();
				$(rel).trigger('delete', [data])
			}
		})
		return false;
	})

	$('a.submit').live('click', function() {
		$(this).closest('form').submit();
		//alert($(this).closest('form').html());
	})
	
	/*	Öll input eša textarea undir hlut meš classan "timeout" fęr eventiš "hasChanged" 500 ms eftir aš seinasta breyting er gerš	*/
	$('.timeout').livequery(timeout_return)
	//timeout_return.call()
	$('.timeout select').live('change', function() {
		$(this).trigger('hasChanged');
	})
	/*$("#ajax-loading").ajaxStart(function(){
	   $(this).show();
	 });	
	$("#ajax-loading").ajaxStop(function(){
	   $(this).hide();
	 });	*/
	$('.ajax_changed').live('hasChanged', ajax_return)
	$('.ajax').live('click', ajax_return)
})

function format_price(nStr)
{
	nStr = Math.round(nStr+0.0001);
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

