var isIe6 =(!window.XMLHttpRequest)? true : false;

jQuery(function(){
var lang=$('html').attr('lang');
	/*gestione dimensione font*/
	/*gestisce la dimensione del file e la tiene in cache pr 7 giorni*/
	var font = { a: "16px", aa: "18px" ,aaa: "20px"}; 
	var dimensioneAttuale=(( $.cookie("font") )!=undefined)? $.cookie("font"):'16px';
	$('html').css('font-size', dimensioneAttuale);
	removeClass();
	$.each(font,function (key,val){
		if(val==dimensioneAttuale) $($('.'+key)).addClass('on');
		$($('.'+key)).click(function(){
			removeClass();
			$(this).addClass('on');
			$('html').css('font-size', val);
			$.cookie("font", $('html').css('font-size'), { expires: 7 ,path: '/Corporate'});
			return false;
		});
	});
	/*end gestione dimensione font*/

Shadowbox.init({
	handleOversize: "drag",
	modal: true
});
	
/*gestione bookmark*/
if(window.opera) {
		if ($("a.preferiti").attr("rel") != ""){ // don't overwrite the rel attrib if already set
				$("a.preferiti").attr("rel","sidebar");
		}
}
$("a.preferiti").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = location.href;
var title = document.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
				window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
				window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
				return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
				 alert('Unfortunately, this browser does not support the requested action,'
				 + ' please bookmark this page manually.');
		}
});
/*end gestione bookmark*/
	
	/*gestione stampa*/
	$("a.stampa").click(function(){
	print();
	return false;
	});
	/*end gestione stampa*/
	
	/*gestione invia ad un amico*/
	$("a.invia").click(function(){
	var url = location.href;
	var title = document.title;
	var body= ((lang=="IT") ? "Salve, penso che questo argomento possa interessarti. L'URL e': " : "Hi, I thought this topic could interest you, have a look at ")+url;
	location.href='mailto:?subject='+title+'&body='+body;
	return false;
	});
	/*end gestione invia ad un amico*/
	
	/*gestione didascalia immagine*/
	/*ridimensiona la didascalia dell'immagine in caso di ie6*/
	if(isIe6){
	if($(".dida").prev('img')){
	var dimensione=$(".dida").prev('img').width();
	$(".dida").width(dimensione);
	}}
	/*end gestione didascalia immagine*/
	
	/*gestione link esterni*/
	/*tutti i link di class 'external' aprono un target '_blank'*/
	$("a.external:not([rel])").click(function(){
	var external=window.open(this,'external');
	return false;
	});
	/*gestione link esterni*/
	
	/*gestione png*/
	/*fissa le png per ie6*/
	$('*').pngFix( );
	/*end gestione png*/
	
	var iPhoneCode = (lang=='IT') ? '<img src="/Static/images/corporate/iphone_hp_it.jpg" width="880" height="265" style="display:block"/><img src="/Static/images/corporate/iphone_hp_menu_it.png" width="880" height="30" usemap="#iphoneMenu"/><map name="iphoneMenu"><area href="/Corporate/IT/Corporate/Settori/Aeronautica/index.sdo" shape="rect" coords="0,0,107,28" /><area href="/Corporate/IT/Corporate/Settori/Elicotteri/index.sdo" shape="rect" coords="109,0,204,28" /><area href="/Corporate/IT/Corporate/Settori/Spazio/index.sdo" shape="rect" coords="207,0,281,28" /><area shape="rect" coords="284,0,562,28" href="/Corporate/IT/Corporate/Settori/Elettronica_per_la_Difesa_e_Sicurezza/index.sdo" /><area shape="rect" coords="564,0,697,28" href="/Corporate/IT/Corporate/Settori/Sistemi_di_Difesa/index.sdo" /><area shape="rect" coords="700,0,782,28" href="/Corporate/IT/Corporate/Settori/Energia/index.sdo" /><area shape="rect" coords="786,0,880,28" href="/Corporate/IT/Corporate/Settori/Trasporti/index.sdo" /></map>' : '<img src="/Static/images/corporate/iphone_hp_en.jpg" width="880" height="265" style="display:block"/><img src="/Static/images/corporate/iphone_hp_menu_en.png" width="880" height="30" usemap="#iphoneMenu"/><map name="iphoneMenu"><area href="/Corporate/EN/Corporate/Settori/Aeronautica/index.sdo" shape="rect" coords="0,0,107,28" /><area href="/Corporate/EN/Corporate/Settori/Elicotteri/index.sdo" shape="rect" coords="109,0,204,28" /><area href="/Corporate/EN/Corporate/Settori/Spazio/index.sdo" shape="rect" coords="207,0,281,28" /><area shape="rect" coords="284,0,562,28" href="/Corporate/EN/Corporate/Settori/Elettronica_per_la_Difesa_e_Sicurezza/index.sdo" /><area shape="rect" coords="563,0,696,28" href="/Corporate/EN/Corporate/Settori/Sistemi_di_Difesa/index.sdo" /><area shape="rect" coords="700,0,765,28" href="/Corporate/EN/Corporate/Settori/Energia/index.sdo" /><area shape="rect" coords="768,0,880,28" href="/Corporate/EN/Corporate/Settori/Trasporti/index.sdo" /></map>'
	
	if (navigator.platform=="iPhone" || navigator.platform=="iPad"){
		$('#flashHomepage').html(iPhoneCode)
	} else {
		$('#flashHomepage').flash({
			src: '/Static/emb/Corporate/FlashHome_'+lang.toLowerCase()+'.swf',
			width:880,
			height:294
		});
	}

	/*
	$('#flashPhotogallery').flash({
    src: '/Static/emb/Corporate/FlashPhotogallery.swf',
    width: 537,
    height: 153
	});
	*/

	/*gestione bordi input al focus*/
	$(':input[not(:checkbox)][not(:radio)]').focus(function() {
	$(this).css({'border-color':'#e31818'})
	});
	
	$(':input[not(:checkbox)][not(:radio)]').blur(function() {
	$(this).css({'border-color':'#d5d5d5'})
	});
	/*end gestione bordi input al focus*/
	
	/*gestione controllo form al submit*/
	/*tutti i campi che hanno la label associata contenente '*' diventano obbligatori */
	$('form').submit(function() {
		var invia=true;
		var index=0;
		var primo=0;
		$(this).find(':input').each(function() {
			index++;	
			var label=null;
			var idElemento=$(this).attr('id');
			var form=$(this).parents('form');
			var label=$(form).find("label:contains('*')[for~='"+idElemento+"']");
			if($(label).html()){
				if($(this).val()==''){
					//alert('Compila il campo '+$(label).text());
					//$(this).focus();
					$(this).css({'border-color':'#e31818'});
					$(this).css({'background-color':'#eac2c2'});
					$(label).css({'color':'#e31818'});
					invia=false;
					if(primo==0)primo=index;
				}else{
					$(this).css({'border-color':'#d5d5d5'});
					$(this).css({'background-color':'#fff'});
					$(label).css({'color':'#484e51'});
				}
			}
			
		});
		if(!invia){
		var message=(lang=='IT')?'I campi in rosso sono obbligatori':'The red fields are mandatory';
		alert(message);
		$(this).find(':input')[(primo-1)].focus();
		return false;
		}
	});
	/*end gestione controllo form al submit*/
	
	/*gestione link submit*/
	/*tutti i link che hanno il class 'submit' sottomettono la form*/
	$("a.submit").click(function(){
		var form=$(this).parents('form');
		$(form).submit();
		return false;
	});
	/*end gestione link submit*/
	
	$('.change').change(function(){$(this).parents('form').submit();});
	
	$("tr *").mouseover(function(){$(this).parent().find('*').addClass('hover');});
	$("tr *").mouseout(function(){$(this).parent().find('*').removeClass('hover');});
});

function writeFlash(id,src,w,h){
	$("#"+id).flash({
		src: src,
		width: w,
		height: h
	});
}

function removeClass(){
$(".font").removeClass('on');
}

function openPhoto(file,position){
var dominio=location.protocol+'//'+location.host
var url='/Corporate/applications/photogallery/popupPhoto.jsp?file='+file+'&posizione='+position+'&dominio='+dominio;
window.open(url,'PHOTOGALLERY','width=578,height=449,location=0,menubar=0,personalbar=0,resizable=yes,toolbar=0,status=0,scrollbars=auto');
}

var arrStoria=new Array("s_1948","s_primi_anni","s_60","s_70","s_1973","s_80","s_90","s_1992","s_2000_2004","s_2000_2008","s_oggi")
function swapStoria(who){
for(i=0;i<arrStoria.length;i++){
if(who!=arrStoria[i]){document.getElementById(arrStoria[i]).style.display="none"}
else{document.getElementById(arrStoria[i]).style.display="block"}
}}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

