//~ SuperBGImage by Andreas Eberhard (http://andreaseberhard.de) 
//~ licensed under a Creative Commons Attribution 3.0 Unported License.
//~ Based on a work at http://dev.andreaseberhard.de/projects/superbgimage/

var http_base
var mnupl = false
var bdmedpl = false
var bdinfpl = false
var bdmedpl = false
var menuizq = 321
var menuminder = 800
var handlemenuizq = 300
var handlebandainf = 254
var bandainfizq = 275
var slide_interval = 120
var promogap = 30
var gapcontenido = 50
var actual = '#ds'
var aactual = 'ds'
var tinicial = 0
//~ var mapa = 'http://maps.google.es/maps?f=q&source=s_q&hl=es&geocode=&q=César Pastor Llopis,6 Madrid 28031&output=embed'
//&sll=40.393632,-3.771303&sspn=0.00889,0.01929&ie=UTF8&hq=&hnear=Calle+de+C%C3%A9sar+Pastor+Llop%C3%ADs,+6,+28031+Madrid,+Comunidad+de+Madrid&ll=40.383163,-3.615396&spn=0.022883,0.036478&z=14&iwloc=A&output=embed'

$(function(){
     dimension_menu()
     dimension_bandainf()
     dimension_bandamed() // Importa el orden
     $('#aux').width(136 * $('a.thumba').size() +'px'); 
     modulo = ($('img.plano').size()%3)
     $('div.planocont').width(197 * (modulo < 3 ? 3 : modulo) +'px'); 
    
   
     
     // Options for SuperBGImage
     $.fn.superbgimage.options = {
          id: 'superbgimage', // id for the containter
          z_index: 0, // z-index for the container
          inlineMode: 0, // 0-resize to browser size, 1-do not resize to browser-size
          showimage: 1, // number of first image to display
          vertical_center: 1, // 0-align top, 1-center vertical
          transition: tinicial, // 0-none, 1-fade, 2-slide down, 3-slide left, 4-slide top, 5-slide right, 6-blind horizontal, 7-blind vertical, 90-slide right/left, 91-slide top/down
          transitionout: 1, // 0-no transition for previous image, 1-transition for previous image
          randomtransition: 0, // 0-none, 1-use random transition (0-7)
          showtitle: 1, // 0-none, 1-show title
          slideshow: 0, // 0-none, 1-autostart slideshow
          slide_interval: 5000, // interval for the slideshow
          randomimage: 0, // 0-none, 1-random image
          speed: 'slow', // animation speed
          preload: 1, // 0-none, 1-preload images
          onShow: superbgimage_show, // function-callback show image
          onClick: superbgimage_click, // function-callback click image
          onHide: superbgimage_hide, // function-callback hide image
          onMouseenter: superbgimage_mouseenter, // function-callback mouseenter
          onMouseleave: superbgimage_mouseleave, // function-callback mouseleave
          onMousemove: superbgimage_mousemove // function-callback mousemove
     };

     // initialize SuperBGImage
     $('#aux').superbgimage();
     
     // Fancyzoom en planos
     $('a.zoom').fancyZoom({scaleImg: true, closeOnClick: true, directory: http_base + 'site_media/img'}) 

     // Slider
     try {
          $('#slider').nivoSlider({effect: 'fade',pauseTime:5000});
     }
     catch(e){
     }
    
    // Ocultar menú superior
    $('#gmenu').click(function() {
          var $content = $('ul.topnav');
          if(!mnupl) {
               $(this).animate({left: $(window).width() - 80})
               $content.animate({left: $(window).width()})
               $(this).attr('src', http_base + 'site_media/img/inimenur.png')
               mnupl=true
          } else {               
               $content.width($(window).width() - menuizq)
               $(this).animate({left: handlemenuizq})
               $content.animate({left: menuizq})
               $(this).attr('src', http_base + 'site_media/img/inimenu.png')
               mnupl=false
               dimension_menu()
          }
     });
     
     // Ocultar cinta media
     $('#inibanmed').click(function() {
          var $content = $('#promo');
          if(!bdmedpl) {
               $(this).animate({left: $(window).width() - 80})
               $content.animate({left: $(window).width()})
               $(this).attr('src', http_base + 'site_media/img/inibanmedr.png')
               bdmedpl=true
          } else {
               $content.width($(window).width() - bandainfizq)
               mapas()
               var espacio = $content.width() - $('div.contenido').width() - 20
               $('#navegador').css({'margin-left': $('div.contenido').width() + 20, 'width': espacio})
               $(this).animate({left: handlebandainf})
               $content.animate({left: bandainfizq});
               $(this).attr('src', http_base + 'site_media/img/inibanmed.png')
               $('div.contenido span#inds').width($('div.contenido').width() - $('div#fotos').width() - 25)
               bdmedpl=false
          }
     });
          
     // Ocultar cinta inferior
     $('#iniband').click(function() {
          var $content = $('#contentframe');
          if(!bdinfpl) {
               $(this).animate({left: $(window).width() - 100})
               $content.animate({left: $(window).width()})
               $(this).attr('src', http_base + 'site_media/img/inibandar.png')
               bdinfpl=true
          } else {               
               $content.width($(window).width() - bandainfizq)
               $(this).animate({left: handlebandainf})
               $content.animate({left: bandainfizq});
               $(this).attr('src', http_base + 'site_media/img/inibandad.png')               
               bdinfpl=false
          }
     });
     
                
     $('#prev').hover(function() {
               $(this).addClass('prev_bg');
          },
          function() {
               $(this).removeClass('prev_bg');
          }
     )        

     $('#prev').click(function() {     
               $.fn.superbgimage.options = {transition: 5}
               $('#aux').prevSlide()
               $.fn.superbgimage.options = {transition: tinicial}
          }
     )          
               
     $('#next').hover(function() {
               $(this).addClass('next_bg');
          },
          function() {
               $(this).removeClass('next_bg');
          }
     )      
     
     $('#next').click(function() {     
               $.fn.superbgimage.options = {transition: 3}
               $('#aux').nextSlide()
               $.fn.superbgimage.options = {transition: tinicial}
          }
     )
     
     $('#rev').click(function() {
               var max = Math.round($('#scrollpane').offset().left) + $('#scrollpane').width()
               var cur = Math.round($('#aux').offset().left) + $('#aux').width()
               var gap = cur - max
               if (gap == 0) return false
               $('#aux').animate({'margin-left':  parseInt($('#aux').css('margin-left')) - (gap < slide_interval ? gap : slide_interval)})
          }
     )         
          
     $('#ffw').click(function() {
               var max = Math.round($('#scrollpane').offset().left) 
               var cur = Math.round($('#aux').offset().left) 
               var gap = max - cur
               if (gap == 0) return false
               $('#aux').animate({'margin-left':  parseInt($('#aux').css('margin-left')) + (gap < slide_interval ? gap : slide_interval)})
          }
     )

     $('ul.navi li a').click(function() {
               var id = $(this).attr('id')
               if (id == '#pd') return
               $(this).removeClass('inactiva')
               $('a.' + aactual).addClass('inactiva')
               $('div.wait').fadeIn()
               if (id == '#cl' || id == '#ds') {
                    $('#ds span').html($(id + 'c').html())
                    if (!$('#ds').is(':visible')) {
                         $(actual).fadeOut()
                         $('#ds').fadeIn()
                         $('#slider').data('nivo:vars').stop = false
                         actual = '#ds'
                    }
               } else {
                    $('#slider').data('nivo:vars').stop = true
                    $(actual).fadeOut()
                    $(id).fadeIn()
                    actual = id
                    mapas()
               }
               $('div.wait').fadeOut()
               aactual = id.substring(1)
          }
     )
          
     $('#shcor').click(
          function(){
               $('#cor').fadeIn()
               $('div.avoverlay').fadeIn()
          }
     )
          
     $('div.avoverlay').click(
          function(){
               $('#cor').fadeOut()
               $(this).fadeOut()
          }
     )

          
     $(window).resize(function(){
          if (mnupl) $('#gmenu').animate({left: $(window).width() - 80})
          dimension_menu()
          if (bdinfpl) $('#iniband').animate({left: $(window).width() - 100})
          dimension_bandainf()
          if (bdmedpl) $('#barramedia').animate({left: $(window).width() - 20})
          dimension_bandamed()   
     }) 
     
     $(window).keydown(function(e) {
          if (e.which == 39 ) {
               e.stopImmediatePropagation();
               return false
          }
     })
     
     $('ul#navegador').css('display', 'block')
     
     try{
          if(resultado == 'Correcto'){
               alert('Gracias por su interés. Responderemos a su consulta en el plazo más corto posible')
          }
          else if (resultado == 'Errores') {
               $('#cor').fadeIn()
               $('div.avoverlay').fadeIn()
          }
     }
     catch(e){
     }
     
})
         



/*********************************/
function dimension_menu() {     
     var der
     var sitio
     if (mnupl) {          
          $('ul.topnav').css('left', $(window).width())
     } else {
          sitio = $(window).width() - menuizq
          if (sitio > menuminder) { // Hay sitio               
               $('ul.topnav').css('font-size', '10pt')
               der = sitio
          } else {
               $('ul.topnav').css('font-size', '9pt')
               der = menuminder
          }
          $('ul.topnav').css('width', der + 'px')
     }
}

function dimension_bandamed() {     
     var topesup = parseInt($('#promo').css('top'))
     var topeinf =  parseInt($('#contentframe').position().top)
     $('#promo').width($(window).width() - parseInt($('#promo').css('left')))
     //~ $('#promo').height(topeinf -topesup - promogap)
     //~ $('div.contenido').height($('#promo').height() - gapcontenido)
     //~ $('#barramedia').height($('#promo').height())
     //~ $('#barra').height($('#promo').height() - 40)
     //var espacio = $('#promo').width() - $('div.contenido').width() - 20
     $('#navegador').css({'margin-left': $('div.contenido').width() + 30}) //, 'width': espacio})
     $('div.contenido span#inds').width($('div.contenido').width() - $('div#fotos').width() - 25)
     mapas()
     if (bdmedpl) {
          $('#promo').css('left', $(window).width())
     }
}

function dimension_bandainf() {
     if (bdinfpl) {
          $('#contentframe').css('left', $(window).width())
     } else {
          $('#contentframe').width($(window).width() - parseInt($('#contentframe').css('left')))
          $('#ffw').css('left', $(window).width() - handlemenuizq - 15)
     }
}

function mapas() {
     $('#map').attr({'width':  $('div.contenido').width() , 'height': $('div.contenido').height() })
     //~ if (actual == '#mp' && $('#map').attr('src') == '') $('#map').attr('src', mapa)
     if (actual == '#mp') $('#map').attr('src', mapa)
}

// function callback on hiding image
function superbgimage_hide(img) {
     $('#showtitle').hide();
}

// function callback on showing image
// get title and display it
function superbgimage_show(img) {
     $('#superbgimage').css('background', 'none')
     //~ $('#superbgimage').append($('#showtitle'));
     //~ $('#showtitle p.imagecount').html('image ' + img + ' of ' + $.superbg_imgIndex);
     //~ if ($('#thumbs1').css('display') == 'block') {
          //~ $('#showtitle p.title').html($('#thumbs1 a' + "[rel='" + img + "']").attr('title'));
     //~ } else {
     $('#showtitle').html($('#aux a' + "[rel='" + img + "']").attr('title'))
     //~ }
     if($('#showtitle').html() == '') {
          $('#showtitle').css('display', 'none')
     } else {
          $('#showtitle').fadeIn('fast')
     }
}

// function callback on clicking image, show next slide
function superbgimage_click(img) {
     //$('#thumbs').nextSlide();
}

my_slideshowActive = false;

// function callback onmouseenter, stop slideshow, show pause-indicator
function superbgimage_mouseenter(img) {
     if ($.superbg_slideshowActive) {
          my_slideshowActive = true;
          if ($('#pause').length == 0) { 
               $('body').prepend('<div id="pause"><img src="' + http_base + 'site_media/img/pause.png" \/><\/div>');
          }
          $('#pause').css('position', 'absolute').css('z-index', 3).show();
          return $('#aux').stopSlideShow();
     }
}

// function callback onmouseleave, start slideshow, hide pause-indicator
function superbgimage_mouseleave(img) {
     if (my_slideshowActive && ($('#pause').length > 0) && ($('#pause').css('display') == 'block'))  { 
          $('#pause').hide();
          return $('#aux').startSlideShow();
     }	
}

// function callback onmousemove, show and move pause-indicator
function superbgimage_mousemove(img, e) {
     if (my_slideshowActive && ($('#pause').length > 0)) { 
          $("#pause").css("top",(e.pageY + 20) + "px").css("left",(e.pageX + 20) + "px").show();
     }
}

