var background = new Image();
var ar;
var done=0;

$(document).ready(function(){

	//alert($(window).width());
	$('#content').css({background: 'transparent', border: '0px'});
	$('#room_over').css({'margin-top': '300'});
  resizeBg();
	
  var isiPad = navigator.userAgent.match(/iPad/i) != null;
  if(isiPad==true){
	//fix per ipad
	$('#top1, #top2').css({width:'980px'});
	$('#bgfoto').css({width: $(window).width()});
    $('#bgfoto').css({height: $(window).height()});
    $('#step1_left, #content_left, #calendar, #h_calendar, .ui-datepicker').css({width: '500px'});
    $('.video').css({width: '500px'});
    $('#iframedove').css({width: '580px'});
    $('#room_foto').css({width: '500px'});
    $('#step1_left, #content_left, #content_right,#pack_left, #pack_right').css({"font-size": '120%','line-height':'1.6em'});
    $('#step1_left').css({'padding':'0px 10px'});
    $('#step1_right').css({"font-size": '130%','padding':'0px 10px'});
    $('#step1_left h2 , #step1_right h2 ').css({"font-size": '130%'});
    $('#content_right h2 ').css({"font-size": '130%'});
    $('#menu-content, #menu-content2').css({"font-size": '130%','top':'250px'});
    $('#w_picker').val('500')

  }	
		
  $(window).resize(function(){
  		if(isiPad==false){
  			resizeBg();
  		}	
	});
	
	function resizeBg(){
	  
	contentSizeX=890;
	contentSizeY=460;
	
    imgWidth=1024;
    imgHeight=640;
    
    ratioImg=imgWidth/imgHeight;
    ratioScreen=$(window).width()/$(window).height();
    
    
    if ( ratioScreen >= ratioImg ) {
      
      if ( $(window).height() <= $('body').height()){
        
        if ( $(window).width() >= imgWidth){
          if ($('body').height()<=$(window).width()*imgHeight/imgWidth){
            endWidth=$(window).width();
            endHeight=$(window).width()*imgHeight/imgWidth;
          } else {
              endWidth=$('body').height()*imgWidth/imgHeight;
              endHeight=$('body').height();
            }
        } else {
          endWidth=$('body').height()*imgWidth/imgHeight;
          endHeight=$('body').height();
        }
      
      } else {
          endWidth=$(window).width();
          endHeight=$(window).width()*imgHeight/imgWidth;
        } 
            
    } 
    
    if ( ratioScreen < ratioImg){
      
      if ( $(window).height() >= $('body').height()){
        endWidth=$(window).height()*imgWidth/imgHeight;
        endHeight=$(window).height();
      } else {  
          endWidth=$('body').height()*imgWidth/imgHeight;
          endHeight=$('body').height();
        }
        
    }
   
    
    //if((endWidth<970)){endWidth=1024;endHeight=768;}

    $('#bgfoto').css({width: endWidth});
    $('#bgfoto').css({height: endHeight});
	
    
    if (( $(window).width() >= 970)||(done==0)){
    	
    	done=1;
    	
    	//setup delle dimensioni
    	resize_top2=endHeight/100*8; // 8% dell'altezza finestra 
   		if(resize_top2>81)resize_top2=81; //massimo px altezza
  		
  		resize_top1=endHeight/100*2; // 8% dell'altezza finestra 
   		if(resize_top1>25)resize_top1=25; //massimo px altezza
   		if(resize_top1<18)resize_top1=18;
   		
   		resize_menu=endWidth/100*2;
   		if(resize_menu<30)resize_menu=30;
   		if(resize_menu>40)resize_menu=40;
   		
   		resize_bottom=$(window).height()/100*70;
   		
   		resize_imgbig=$(window).width()/100*50;
		if(resize_imgbig<500)resize_imgbig=500;
   		if(resize_imgbig>850)resize_imgbig=850;
   		
   		resize_picker=$(window).width()/100*50;
   		if(resize_picker<650)resize_picker=650;
   		if(resize_picker>850)resize_picker=850;
   		$('#w_picker').val(resize_picker);
   		
   		resize_topbtn =resize_imgbig/1.9;
   		
   		resize_package=$(window).width()/100*61;
   		if(resize_package<600)resize_package=600;
   		//if(resize_package>850)resize_package=850;
   		
   		resize_iframe=$(window).width()/100*60;
   		resize_iframe_h=$(window).height()/100*60;
		if(resize_iframe<650)resize_iframe=650;
   		if(resize_iframe_h<350)resize_iframe_h=350;
    	
    	resize_imgthb=$(window).width()/100*4;
    	//
    	if(resize_imgthb<45.6)resize_imgthb=45.6;
   		if(resize_imgthb>70.3)resize_imgthb=70.3;
   		
   		content_right_side=resize_imgbig*0.8;
    	if(content_right_side<260)content_right_side=260;
    	
    	//ridimensiono i font
    	fontSize = endWidth*20/1680;
    	if (fontSize < 16) {fontSize=16;}
    	if (fontSize > 22) {fontSize=22;}
    	$("body").css("font-size", fontSize);
		top1_font=fontSize-5;
    	if(top1_font<12)top1_font=12;


		//Gestione Top
/*
    	$("#top1").css("width",endWidth);
    	$("#top2").css("width",endWidth);	
    	$('#top2').css({height:resize_top2});
    	$('#top2 img').css({height: resize_top2});
	
    	$('#top1').css({height: resize_top1,'font-size':top1_font});*/
    	
    	//gestione menu
    	$('#absmenu').css({height:60});
    	/*$('.items').css({height:resize_menu});
   		$('#absmenu').css({height:(resize_menu*2.05)});
  		var resize_menu=resize_menu+1;
    	$('.ul-menu').css({height:resize_menu});
    	*/
    	
    	//gestione spacer home
    	$('#spacer').css({height:resize_bottom});
    	
    	//resize tabella area pacchetti
    	$('.packages').css({width:resize_package});
    	    	
    	//gestione foto
    	$('#room_foto,.room_foto,#slider').css({width: resize_imgbig,'overflow':'hidden'});
    	$('#room_over').css({width: resize_imgbig-35});
    	$('#masq-gallery').css({'width': resize_imgbig});
    	if(resize_imgbig>640){
    	$('.video').css({'width': resize_imgbig,'height': ((resize_imgbig*385)/640)});
    	}
    	var top_menu=resize_imgbig*250/580;
    	if(top_menu<290)top_menu=290;
    	/*$('#menu-c').html(top_menu);*/
    	$('#menu-content').css({'top': top_menu});
    	$('#menu-content2').css({'top': top_menu});
    	$('#menu-left, #menu-right').css({'margin-left': (resize_imgbig*45/580),'width': (resize_imgbig*1.2)/2 });
    	
    	$('#menu_foto_small').css({width: (resize_imgbig/2),'overflow':'hidden'});
    	$('#menu_foto_big').css({width: (resize_imgbig*1.2),'overflow':'hidden'});

    	
    	$('#room_foto1').css({width: resize_imgbig/2});
    	$('#room_foto2').css({width: resize_imgbig/2});
    	$('.area_foto').css({width: resize_imgbig/2});
    	
    	//Iframe mappa 
    	$('#iframedove').css({width: resize_iframe,height: resize_iframe_h});
    	
    	$('[id^=thumb-] img').css({width: resize_imgthb,height:resize_imgthb});
    	
    	$('#content_left,#step1_left').css({width: resize_imgbig});
    	$('#content_middle').css({width: resize_imgthb+2});
    	$('#content_right,#step1_right').css({width: content_right_side});
    	$('#show_camera').css({width: resize_imgbig+resize_imgthb+100});
    	$('#riemp').css({width: resize_imgbig});
    	$('#riemp').css({height: ($('#room_foto').height()*1.007)});
    	$('#room_over').css({'margin-top': (resize_imgbig*0.50)});
    	$('#content_left2').css({'width': resize_imgbig+resize_imgthb +30 });
    	
    	$('.ui-datepicker').css({width: resize_picker});
    	//$('#spanc').html(resize_picker);
    }
    
    // ELIMINO SCROLLBAR SE FINESTRA MAGGIORE DI BODY
    if ( $(window).height() >= contentSizeY){
      //$("body").css("overflow-y", "hidden");
    } else {
        //$("body").css("overflow-y", "visible");
      }
    if ( $(window).width() >= contentSizeX){
      $("body").css("overflow-x", "hidden");
    } else {
        $("body").css("overflow-x", "visible");
      }
       
     
	}	
		
	
});
