
function transparent(im) {
   if (!im.transparented && (/\.png/.test(im.src))) {
    im.transparented = 1;
    var picture = im.src; var w = im.width; var h = im.height;
    im.src = "/img/pixel.gif";
    im.style.filter = "progid:DXImagetransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='scale', src='"+picture+"');";
    im.width = w; im.height = h;
   }
   return "transparent";
}

/**
 * JQuery get form values plugin
 */
$.fn.getFormData = function(assoc) {
    var a = assoc ? {} : [];
    $('input,textarea,select,button', this).each(function() {
        var n = this.name;
        var t = this.type;
        if ( !n || this.disabled || t == 'reset' ||
            (t == 'checkbox' || t == 'radio') && !this.checked ||
            (t == 'submit' || t == 'image' || t == 'button') && this.form.clicked != this ||
            this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
            return;
        if (t == 'image' && this.form.clicked_x)
            return a.push(
                {name: n+'_x', value: this.form.clicked_x},
                {name: n+'_y', value: this.form.clicked_y}
            );
        if (t == 'select-multiple') {
            $('option:selected', this).each( function() {
                a.push({name: n, value: this.value});
            });
            return;
        }
        if (assoc) {
            a[n] = this.value;
        }
        else {
            a.push({name: n, value: this.value});
        }
    });
    return a;
};

/**
 * Gets values from all elements
 */
$.fn.getAll = function(what)
{
	var arr = [];
	this.each(function(){
		arr.push(eval('($(this).' + what + ')'));
	});
	return arr;
}

$.fn.enter = function(handler) {
    this.each(function(){
        var th = $(this);
        th.keypress(function(e){
            var key;
            if(window.event) key = window.event.keyCode;
            else if(e.which) key = e.which;
            if (key == 13) handler(th);
        });
    });
}

$.anchor = function(value) {
    var m = document.location.href.match(/#(.+)$/);
    var anchor = m && m[1];
    if (typeof(value) != 'undefined') {
        if (anchor) {
            document.location = document.location.href.replace(/#(.+)$/, '#' + value);
        }
        else {
	        document.location = document.location.href + '#' + value;   
        }
    }
    return anchor;
}

// Fix all pngs
$(function(){
	//$.ifixpng('/img/pixel.gif');
	//$('*[@src$=.png]').ifixpng();
	var form = $('.top form');
	if (form.length) {
	    var inputs = $('input', form);
	    var submit = function(){
	        inputs.removeClass('error');
            $.post('/login/ajax', form.getFormData(), function(state){
                if (state) {
                    document.location.href = form.attr('action') || document.location.href;
                }
                else {
	                inputs.addClass('error');
                }
            }, 'json');
        }
	    form.enter(submit);
	    $('input:submit', form).click(submit);
	    
	    /*var showLogin = function(href) {
	        form.attr('action', href);
	        var loginArrow = $('div.login-arrow');
	        if (!loginArrow.length) {
	            loginArrow = $('<div>').addClass('login-arrow iePNG').appendTo('div.top');
	        }
	        $('input:text:eq(0)').focus();
	        loginArrow.show('slow');
	    }
		$('a.authRequired').each(function(){
		    var a = $(this);
		    var v = 'javascript:void(0)';
		    var href = a.attr('href');
		    a.attr('href', v);
		    a.click(function(){
		        showLogin(href == v ? null : href);
		    });
		});*/
	}
});