$(document).ready(function(){
	fixPNG();
	$(".imgbig").fancybox({
		'overlayShow': true,
		'hideOnOverlayClick': true,
		'hideOnContentClick': true,
		'autoDimensions': true,
		'autoScale': true,
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'titlePosition': 'inside',
		'centerOnScroll': true
	});
	
	$('.serv_extend').bind('click', function(e){
		$.fancybox(
			$($(this).attr('elm')).html(),
			{
				'overlayShow': true,
				'hideOnOverlayClick': true,
				'hideOnContentClick': true,
				'autoDimensions': false,
				'autoScale': false,
				'transitionIn': 'elastic',
				'transitionOut': 'elastic',
				'titlePosition': 'inside',
				'centerOnScroll': true,
				'width': 420,
				'height': 420,
				'padding': 0,
				'margin': 0
			}
		);
	});
	
});

function btMenu(texto, href, div, sel, width, vent, extra_path)
{
	$('#' + div).flash({
		src: extra_path + '/images/btMenu.swf',
		width: width,
		height: 20,
		wmode: "transparent",
		scale: "noscale",
		salign: "lt",
		flashvars: {
			texto: texto,
			selected: sel,
			ventana: vent,
			href: href
		}
	},
	null,
	null,
	function(htmlOptions){
		$('#' + div).html('<a style="font-family:arial; font-size:12px; text-decoration:none; color:#848484;display:block;float:left;margin-right:0px;" href="' + href + '">' + texto + '</a>&nbsp;&nbsp;|&nbsp;&nbsp;');
	});
}

function messageAndGo(str, onclick)
{
	$.alerts.okButton = "OK";
	jAlert(str, 'Mitsubishi', onclick);
}

function message(str)
{
	$.alerts.okButton = "OK";
	jAlert(str, 'Mitsubishi');
}

function messageConfirm(str, onclick)
{
	jConfirm(str, 'Mitsubishi', onclick);
}

/*
 * Arregla los PNGs en IE6 usando los filtros de MS
 */
function fixPNG()
{
	// Solo para IE6 y posteriores
	if($.browser.msie && parseInt($.browser.version.substr(0,1)) <= 6) {
		// Todas las imagenes
		$("img").each(function(){
			var image = $(this).attr('src');
			// Que tengan un PNG
			if (image.match(/.png/i)) {
				// Le ponemos por CSS con el filter de IE la imagen y le quitamos la actual
				$(this).css({
					'backgroundImage': 'none',
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='scale', src='" + image + "')"
				}).attr("src", "/images/pix.gif");
			}
		});
		// Para todos los tags
		$("*").each(function(){
			// Que tengan un backgroundImagen: url(*.png)
			var image = $(this).css('backgroundImage');
			if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
				image = RegExp.$1;
				// Le cambiamos el CSS
				$(this).css({
					'backgroundImage': 'none',
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + ($(this).css('backgroundRepeat') == 'no-repeat' ? 'crop' : 'scale') + ", src='" + image + "')"
				}).each(function () {
					var position = $(this).css('position');
					if (position != 'absolute' && position != 'relative')
						$(this).css('position', 'relative');
				});
			}
		});
	}
}

function loadJCombo(slt, file, val, callback)
{
	slt.options.length = 0;
	slt.disabled = true;
	$.getJSON(file, function(data) {	
		$.each(data.items, function(i, item) {
			if(item) {
				if(item.name) {
					slt.options.add(new Option(item.name, item.id));
				}
			}
		});
		if(parseInt(val) > 0) {
			slt.value = val;
		}
		if(parseInt(slt.options.length) > 1) {
			slt.disabled = false;
		}
		if(typeof callback == "function") {
			callback.apply(this);
		}
	});
}

function todo(str)
{
	message("@todo: " + str);
}

function zeroPad(num, width) {
	num = num.toString();
	while (num.length < width) {
		num = "0" + num;
	}
	return num;
}

function base64_encode(data)
{
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
    if (!data) {
        return data;
    }
    do {
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
        bits = o1<<16 | o2<<8 | o3;
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    enc = tmp_arr.join('');
    switch (data.length % 3) {
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
    return enc;
}
