var baseurl = "";
var w_px_1280 = 1230;
var w_ex_1280 = 130;
var side_1280 = 300;
var font_1280 = '12pt';
var w_px_1024 = 1000;
var w_ex_1024 = 108;
var side_1024 = 244;
var font_1024 = '10pt';
var h_flash_1024 = 195;
var w_px_800 = 770;
var w_ex_800 = 105;
var side_800 = 244;
var font_800 = '10pt';
var h_flash_800 = 150;

var current_width = w_px_800;
var h_flash = h_flash_800;

if ($.browser.msie && $.browser.version <= 6)
{
	msie6 = true;
} else {
	msie6 = false;
}
//alert(msie6);

var propswidth = ['margin-left','border-left-width','padding-left','padding-right','border-right-width','margin-right'];
var propsheight = ['margin-top','border-top-width','padding-top','padding-bottom','border-bottom-width','margin-bottom'];

function iguala_alt_columnes()
{
	maxh = 0;
	str = "";
	sh = (s = $("#sideNav")).height();
	ch = (c = $("#content")).height();
//	alert('side: ' + sh + '; content: '+ch);
	if (sh < ch)
	{
		s.css('bottom','0');
	} else {
		margins = 0;
		// calculem la suma de margins paddings i aixo
		for (i = 0; i < propsheight.length; i++)
		{
			x = parseInt(c.css(propsheight[i]));
			if (!isNaN(x))
			{
				margins += x;
			}
		}
		corrector = 0;
		while (c.height() + margins < sh)
		{
			new_height = max(0, sh - margins + corrector);
		//	alert('corregim el content: marges: '+margins+'; new_height: ' + new_height + '; (abans era ' + c.height()+ '); side: '+sh + 'condicio: ' + (c.height() + margins) + ' < ' + sh);
			c.height(max(0, new_height));
			corrector += 3;
		}
		s.css('bottom', '0');
	}
}

function old_estabilitzaDivsImatges()
{
	d = $("div.imgdiv");
	w = $("img", d).width();
	d.width(w);
}

function max(a,b)
{
	return a>b?a:b;
}

function flashitza_home()
{
	var header = $("#header");
	if (header.size() == 0)
	{
		return;
	}
	//alert(current_width);
	if (lg == 'ca')
	{
		so = new SWFObject(baseurl + "/img/capcalera_cat.swf?mipath=1", "flash_header", current_width, h_flash, "6.0.29", "#fff");
//		so = new SWFObject(baseurl + "/img/cat/capcalera_cat_nadal.swf", "flash_header", current_width, h_flash, "6.0.29", "#fff");
		//so = new SWFObject(baseurl + "/img/cat/2009-10ani_cat.swf", "flash_header", current_width, h_flash, "6.0.29", "#fff");
//		so.addVariable("mipath", 1);
	} 
	else if(lg == 'en')
	{
		so = new SWFObject(baseurl + "/img/capcalera_eng.swf?mipath=1", "flash_header", current_width, h_flash, "6.0.29", "#fff");
//		so = new SWFObject(baseurl + "/img/eng/capcalera_nadal.swf", "flash_header", current_width, h_flash, "6.0.29", "#fff");
		//so = new SWFObject(baseurl + "/img/eng/2009-10ani_eng.swf", "flash_header", current_width, h_flash, "6.0.29", "#fff");
//		so.addVariable("mipath", 1);
	}
	else {
		so = new SWFObject(baseurl + "/img/capcalera.swf?mipath=0", "flash_header", current_width, h_flash, "6.0.29", "#fff");
		//so = new SWFObject(baseurl + "/img/esp/2009-10ani.swf", "flash_header", current_width, h_flash, "6.0.29", "#fff");
//		so = new SWFObject(baseurl + "/img/esp/capcalera_nadal.swf", "flash_header", current_width, h_flash, "6.0.29", "#fff");
//		so.addVariable("mipath", 0);
	}
	so.addParam("menu", "false");
	so.write("header");
	header.addClass('flash');
}

function do_onload()
{
	if (!$.browser.msie)
	{
		window.onresize = resolucio_width;
	}
//	if ( ((im = $(".imgdiv img")).size() == 0) || msie6)
//	{
		resolucio_width();
//	} else {
//		im.load(resolucio_width);
//	}

	$("#footer").removeClass('marginat');
	flashitza_home();

	// obrim en popup
	$("a[rel=popup]").click(function(){
		return popup(this.href);
	});

	// posem enllacos a la home a tots els strong class anglatecnic
	//$("strong.anglatecnic").wrap("<a href=''></a>");
	//alex@20100201 Canviem el mètode per agafar les dues lletres de l'idioma de la url i anem a la pàgina home
	$("strong.anglatecnic").wrap("<a href='"+(window.location.pathname.substring(1,3))+"-2-Home.html'></a>");
}

function resolucio_width()
{
	ww = $(window).width();
	if (msie6)
	{
		prop_sid = 'padding-left';
	} else {
		prop_sid = 'margin-left';
	}
	if (ww < w_px_1024)
	{
		wiz = w_px_800;
		fon = font_800;
		sid = side_800;
		h_flash = h_flash_800;
		img_bigger = false;
		$("#home_ul").css('padding-left', '0').css('padding-right', '25px').css('width','179px');
		var rall_hom = $("#ralleta_home");
		if (rall_hom.size() > 0)
		{
			var pd_rll_hom = rall_hom.css('width');
			pd_rll_hom = parseInt(pd_rll_hom.substr(0, pd_rll_hom.length - 2)) - 28;
			rall_hom.css('width', pd_rll_hom + 'px');
		}

	} else { // if (ww < w_px_1280) {
		wiz = w_px_1024;
		fon = font_1024;
		sid = side_1024;
		h_flash = h_flash_1024;
		img_bigger = true;
	} 
// else {
//		wiz = w_px_1280 + 'px';
//		fon = font_1280;
//		sid = side_1280 + 'px';
//	}
	current_width = wiz;
	wiz = wiz + 'px';
	sid = sid + 'px';

	// ajustem les imatges
	divimg = $("#myContent .imgdiv");
	if (divimg.size() > 0 && ww >= w_px_1024)
	{
		wimg = parseInt(divimg.css('width'));
		himg = parseInt(divimg.css('height'));
		img = $("img", divimg);
		if (img.attr('src').indexOf('thumb1024/') == -1)
		{
			// si no l'haviem ajustat abans...
			// $("body").append(wimg).append("<br>").append(wimg * 1.5);
			divimg.css('width', '' + parseInt(wimg * 1.50) + 'px').css('height', '' + parseInt((himg - 30) * 1.50) + 'px');
			img.attr('src', img.attr('src').replace(/thumb\//, "thumb1024/"));
		}
	}

	$("body").css('width', wiz).css('font-size', fon);
	$("#sideNav").css('width', sid);
	$("#content").css(prop_sid, sid);
	ajusta_navigation_inside_content();
	iguala_alt_columnes();
	//alert(prop_sid);
	//alert(current_width);
}

function ajusta_navigation_inside_content()
{
	nav_div = $("#navigation-inside-content");
	if (nav_div.size() == 0) return;
	
	lis = $(".nav-inside-level-0 > li", nav_div);
	var amp = (parseInt(nav_div.width() / lis.size()) - 1) + 'px';
	//alert(nav_div.width() + ' / ' + lis.size() + ' = ' + amp);
	lis.css('width', amp);
	return;
/*
	tot_w = nav_div.width();
//	alert(tot_w);
	//$.browser.mozilla && console.log('total_width: ' + tot_w + '. Number of lis: ' + lis.size());
	sum_lis = 0;
	sum_margins = 0;
	margins = 0;
	lis.each(
		function(pos)
		{
			w = (me = $(this)).width();
			if (pos == 0)
			{
				for (i = 0; i < propswidth.length; i++)
				{
					x = parseInt(me.css(propswidth[i]));
					if (!isNaN(x))
					{
						margins += x;
					}
				}
			}
			//$.browser.mozilla && console.log('li ' + pos + ': ' + w + '; margins: ' + margins);
			sum_lis += w;
			sum_margins += margins;
		}
	);
	//$.browser.mozilla && console.log('done calculation. total sum: ' + sum_lis + '; total margins: ' + sum_margins);
	tot_w -= sum_margins;
	lis.each(
		function(pos)
		{
			me = $(this);
			before = me.width();
			if (pos == lis.size() - 1)
			{
	//			alert(before + me.text());
			}
			w = Math.floor(before * tot_w / sum_lis);
			//$.browser.mozilla && console.log('li ' + pos + ': before: ' + before + '; after: ' + w);
			me.width(w)
			if (pos == lis.size() - 1)
			{
				me.addClass('last');
			}
		}
	);
	//$.browser.mozilla && console.log('done manipulation');
*/
}

$(document).ready(do_onload);

function popup(url, w, h)
{
	picfile = new Image(); 
	picfile.src =(url); 
	fileCheck(url);

	return false;
	/*
	params = "toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=1";
	var extensio = url.substr(url.length - 4);
	switch (extensio.toLowerCase())
	{
		case ".jpg":
		case ".gif":
		case ".png":
			var tmp = url.split("/");
			var fn = tmp.pop();
			var id = fn.substr(0, fn.length - 4);
			id = parseInt(id);
			if (!isNaN(id))
			{
		//		url = baseurl + "/popupImg.php?img=" + id;
			}
			break;
		default: break;
	}
	win = window.open(url, 'popup', params);
	if (!win)
	{
		return true;
	} else {
		return false;
	}
	*/
}


function fileCheck(img)
{
	if( (picfile.width!=0) && (picfile.height!=0) )
	{ 
		makeWindow(img); 
	}else {
		funcio="fileCheck('"+img+"')"; 
		interval=setTimeout(funcio,50); 
	}
}
function makeWindow(img)
{ 	
    ht = picfile.height + 20;
	wd = picfile.width + 20; 
	var args= "height=" + ht + ",innerHeight=" + ht;
	args += ",width=" + wd + ",innerWidth=" + wd;
	if (window.screen) 
	{ 
		var avht = screen.availHeight; 
		var avwd = screen.availWidth;
		var xcen = (avwd - wd) / 2; 
		var ycen = (avht - ht) / 2;
		args += ",left=" + xcen + ",screenX=" + xcen;
		args += ",top=" + ycen + ",screenY=" + ycen + ",resizable=yes"; 	
	}
	return window.open(img, '', args); 
} 

