$(window).load(function(){
        init_navigation()
        init_gallery();
        init_subcategory_toggler();
        init_product_gallery();
        $(".zoomer").colorbox({ opacity:0.5});
        $(".zoomer-movies").colorbox({ opacity:0.5, iframe: true, width:380, height:290 });
        var max = '';
        $('.component-container').each(function(){
           max = ($(this).height()>max) ? $(this).height() : max;
        })
        $('.component-container').css('height',max+10);
        Cufon.replace('.product-heading .title', { fontFamily: 'Elektra Light Bold' });
        Cufon.replace('.products-list li .summary h3', { fontFamily: 'Elektra Light Bold' });
        Cufon.replace('.files .products li .thumb', { fontFamily: 'Elektra Light Bold' });
        Cufon.replace('.product-heading .description', { fontFamily: 'Elektra Light Pro' });
        //Cufon.replace('.product-features',{ fontFamily: 'Elektra Light Pro' });
        //Cufon.replace('.content .text p', { fontFamily: 'Elektra Light Pro' });
        Cufon.replace('h1', { fontFamily: 'Elektra Light Bold',fontSize: 33 });
        Cufon.replace('.content .big', { fontFamily: 'Elektra Light Bold',fontSize: 33 });
        //Cufon.replace('.subcategory-top h2', { fontFamily: 'Elektra Light Bold', fonSize:21, hover: true });
        
         $('#search').click(function(){
            if($(this).val()==window.search_str) $(this).val('');
         }).blur(function(){
            if($(this).val()=='') $(this).val(window.search_str);
         })
         
         $('ul.months').each(function(){ $(this).attr('default_height',$(this).height()) })
         $('.year-expand').each(function(){ if($(this).hasClass('selected')==false) $(this).next().css('height',0) })
         $('.year-expand').click(function(){
            $(this).toggleClass('selected')            
            $(this).hasClass('selected') ? $(this).text($(this).text().replace('+','-')) : $(this).text($(this).text().replace('-','+'));
            $(this).next().animate({'height': $(this).next().height() ? 0 : $(this).next().attr('default_height') },200)
         })
})
function init_product_gallery(){
   var thumbnails = $('#thumbs').children();
   var large_image = $('#large_image');
   var currently_selected = 0;
   var image_url = '/uploads/product_images/';
   thumbnails.each(function(index,el){
     $(this).click(function(e){
        e.preventDefault();
         if(index!=currently_selected){
            var image_name = $(this).children(":first").attr('src').replace(/\/uploads\/product_images\/.*\/(.*)/,'$1');
            $(large_image).attr('href',image_url+'large/'+image_name);
            $(large_image).animate({'opacity':0},200,'linear',function(){$(this).children(':first').attr('src',image_url+'medium/'+image_name).parent().animate({'opacity':1},200) });            
            /*$(large_image).fadeOut(200,function(){
              $(this).children(':first').attr('src',image_url+'medium/'+image_name).parent().fadeIn(200);
            }) */
            $(this).addClass('selected')
            $(thumbnails[currently_selected]).removeClass('selected')            
            if($('#text_'+$(this).attr('id').replace('thumb_','')).length){
               $('#product_text').hide();
               $('#text_'+$(this).attr('id').replace('thumb_','')).show();
               $('#text_'+$(thumbnails[currently_selected]).attr('id').replace('thumb_','')).hide();
            }else{               
               $('#text_'+$(thumbnails[currently_selected]).attr('id').replace('thumb_','')).hide();
               $('#product_text').show();
            }            
            currently_selected = index;            
        }
     })
   })
}
function init_subcategory_toggler(){
  if(results = window.location.href.match(/\#([a-z\-0-9]+)/)){
    $("#"+results[1]).next().addClass('open')
  }
   $('.navigation .subcategories a').click(function(){
    var link_parts = $(this).attr('href').match(/(.*)\#([a-z\-0-9]+)/)
    
    if( link_parts[1] == window.location.pathname){
      if(!$('#'+link_parts[2]).next().hasClass('open'))toggle_subcategory($('#'+link_parts[2]));
    }
   })
   
   
   $(".subcategory-top").each(function(){
      $(this).hover(
        function(){
          $(this).addClass('over');
          if($(this).next().height()<60){
              $(this).children().each(function(){ $(this).animate({'color':'#000000'}) })
              $(this).children('.tab-toggler').css("background-position","right -30px")
          }
        },
        function(){
          $(this).removeClass('over');
          if($(this).next().height()<60){
            $(this).children().each(function(){ $(this).animate({'color':'#8C8C8C'}) })
            $(this).children('.tab-toggler').css("background-position","right 0px")
          }
        }
      )
      $(this).next().data('default_height',$(this).next().height())
      $(this).next().children('ul').data('default_height',$(this).next().children('ul').height());
      if($(this).next().hasClass('open')==false){
        $(this).next().css('height',0).children('ul').css('height',0)
      }
      else{
        $(this).children().css({'color':'#000000'})
        $(this).children('.tab-toggler').css("background-position","right -30px")
      }
      $(this).click(function(e){
        toggle_subcategory($(this))
        //toggle_subcategory($(".subcategory-container.open").prev())
        return false
      })
      
  })
  
}
function toggle_subcategory(element){
  
  var next = $(element).next();
  var parent = $(element).parent();
  
  window.location = window.location.href.match(/#/) ? window.location.href.replace(/^(.*)\#[a-z\-0-9]*$/, "$1#"+$(element).attr('id')) : window.location.href+'#'+$(element).attr('id')
   
  next.animate({'height': next.hasClass('open') ? 0 : next.data('default_height')},500,'easeInOutExpo',function(){ }).children('ul')
      .animate({'height': next.hasClass('open') ? 0 : next.children('ul').data('default_height') },500,'easeInOutExpo',function(){ })
       
  parent.children(".tab-toggler").text(next.hasClass('open') ? window.open_str : window.close_str).css('background-position', next.hasClass('open') ? 'right -30px' : 'right -45px')
  next.toggleClass('open')
}

function init_navigation(){
  $('.navigation .categories').css({display:'block'}).hide();
  $('.navigation .subcategories').hide();
  $('.navigation li.products, .navigation li.buildings, .navigation li.about-us, .navigation li.production, .navigation li.download').hover(function(){
    $(this).children(':first').css({'background-color':'#000','color':'#FFF'})
    if($(this).children('.categories').is(":hidden")){
      $(this).children('.categories').show()
    }
  },function(){
    if($(this).children('.categories').is(":hidden")==false){
      $(this).children('.categories').hide()
    }
    $(this).children(':first').css({'background-color': $(this).children(':first').hasClass('selected')? '#3F3F3F':'transparent',
                                    'color': $(this).children(':first').hasClass('selected')? '#FFF':'#3F3F3F'})
  })
  $('.categories li').hover(function(){
    $(this).children('a').addClass("over").parent().children('.subcategories').stop().show()
  },function(){
    $(this).children('a').removeClass('over').parent().children('.subcategories').stop().hide()
  })
}

function init_gallery(){
  $('#building-gallery').css('height',155).children('.images').children('.image').css('height',155)
  var gallery_images = $('#building-gallery .images .image')
  var currentImage = gallery_images[0]
  var currentIndex = 0;
  gallery_images.each(function(index,el){
    if(index==0){
      $(el).children('.info').css('bottom',-80)
      $(el).css('top',-100);
    }else{
      $(el).css('opacity',0)
      $(el).children('.info').css({'opacity':0,'bottom':-80})
    }
  })
  // expand | collapse the gallery container
  $('#gallery_toggler').click(function(e){
    e.preventDefault()
    container = $('#building-gallery')
    $(this).text((container.height()>160) ? window.open_str : window.close_str).css('background-position',(container.height()>160) ? 'right -25px':'right 0px')
    container.animate({"height":(container.height()>160) ? 155 : 467},400).children('.images').css('height',(container.height()>160) ? 155 : 467).children('.image').css('height',(container.height()>160) ? 155 : 467)
    $(currentImage).animate({"top":(container.height()>160) ? -100 : 0 },400)
    $(currentImage).children('.info').animate({"bottom":(container.height()>160) ? -80 : 40},400)
  })
  $("#prev").click(function(e){
    e.preventDefault()
    if(currentIndex!=0){
      swap_images(currentImage,gallery_images[currentIndex-1],currentIndex,currentIndex-1)
      currentImage = $(gallery_images[--currentIndex]);
    }
  })
  $("#next").click(function(e){
    e.preventDefault()
    if(currentIndex<gallery_images.length-1){
      swap_images(currentImage,gallery_images[currentIndex+1],currentIndex,currentIndex+1)
      currentImage = $(gallery_images[++currentIndex]);
    }
  })
}
function swap_images(currentImage,newImage,currentIndex,newIndex){
  $(currentImage).children('.info').animate({'opacity':0,'bottom':-60},400,function(){
    $(currentImage).animate({'opacity':0},400)
    $(newImage).animate({'opacity':1},400,function(){
      $(this).children('.info').animate({'opacity':0.6,'bottom':40})
    })
  })
  newIndex==0 ? $('#prev').fadeOut() : $('#prev').fadeIn()
  newIndex!=$(currentImage).parent().children('.image').length-1 ? $('#next').fadeIn() : $('#next').fadeOut()
} 
