$(document).ready(function () {
  
  Shadowbox.init({ players: ['swf, img, flv, iframe, html'], path: '/static/js/' });
  
  // IE embedd ff
  
  if ( $.browser.msie ) {
    $('body').css({'font-family': 'Verdana, Helvetica, "Helvetica Neue", sans-serif;'});
    $('#main_menu, #sub_menu, #footer_menu').css({'font-size': '14px'});
    $('#footer_menu').css('margin-bottom', '40px');
    $('#footer_fixed').css('height', '125px');
    $('#footer_fixed #lang').css('margin-bottom', '5px');
    $('#footer_fixed ul#info_menu').css('margin-top', '5px');
    $('#main_menu').css('margin', '50px 0 5px 0');
    $('.footer_para').css('width', '740px');
    $('li#eyego_credit').css('margin-left', '325px');
    $('#content_navi a#prev2, #content_navi a#next2').css({'font-size': '120%', 'font-weight': 'bold'});
    $('.content_placement h4').css('font-size', '150%');
    $('.close_box.log').css('margin-left', '50px');
    $('#img_keeper_front img').css('width','315px');
    $('#img_keeper_front img').css('height','472px');
    var img_height = $('#img_keeper_front img').height();
    var img_width = $('#img_keeper_front img').width();
    var ratio = $('#img_keeper_front img').width()/$('#img_keeper_front img').height();
  }
  
  // window resize over all
  
  
$(window).resize(function () {
    if($(this).width() < 1577) {
      
      if ( $.browser.msie ) {
        $('#img_keeper_front img').css('height', img_height*ratio);
        $('#img_keeper_front img').css('width', img_width*ratio);
      }
      
      $('body').css({
        'margin-left': -((1577 - $(this).width())/2),
        'overflow-x' : 'hidden' 
      });
      
      $('#img_keeper_front').width($(this).width()).height('auto') ;
      $('#img_keeper_front li').each(function (){
        $(this).width(($('#img_keeper_front').width())/5).height('auto');
      });
      $('#img_keeper_front img').each(function (){
        $(this).width(($('#img_keeper_front').width())/5).height('auto');
      });
      $('.sizer').css({'width': '1575px'});
    
    }
  });
  
  
$(window).load(function () {
    if($(this).width() < 1577) {
      
      if ( $.browser.msie ) {
        $('#img_keeper_front img').css('height', img_height*ratio);
        $('#img_keeper_front img').css('width', img_width*ratio);
      }
      
      $('body').css({
        'margin-left': -((1577 - $(this).width())/2),
        'overflow-x' : 'hidden' 
      });
      
      $('#img_keeper_front').width($(this).width()).height('auto') ;
      $('#img_keeper_front li').each(function (){
        $(this).width(($('#img_keeper_front').width())/5).height('auto');
      });
      $('#img_keeper_front img').each(function (){
        $(this).width(($('#img_keeper_front').width())/5).height('auto');
      });
      $('.sizer').css({'width': '1575px'});

    }
  });
  
  // ajax content
  
  if ($('.jq_ajax_content').length){
    $.ajax({
      url:('/' + $('.jq_ajax_content').attr('data-group') + '/ajax_list'),
      type: 'get',
      dataType: 'html',
        success: function (response){
          $('.jq_ajax_content').append(response);
        }
    });
  
  if (! ($.browser.msie) ) {     
    $('#container, .fb img').animate({opacity: 0.0}, 10);
  }
  $('body').css({'background': 'url(/static/images/ajax-loader.gif) no-repeat 50% 180px #e6e6e6', 'overflow-y': 'hidden'});
  }
  
  if ($('.jq_ajax_shop').length){
    $.ajax({
      url:('/butikk/ajax_list'),
      type: 'get',
      dataType: 'html',
        success: function (response){
          $('.jq_ajax_shop').append(response);
        }
    });
  if (! ($.browser.msie) ) {
    $('#container, .fb img').animate({opacity: 0.0}, 10);
  } 
  $('body').css({'background': 'url(/static/images/ajax-loader.gif) no-repeat 50% 180px #e6e6e6', 'overflow-y': 'hidden'});
  }
  
  // infobox animation
  $('#frontpage, #credits, #press').children().hide();
  if($('#slide_box').find('#info') || $('#slide_box').find('#cart')) {
    if (! ($.browser.msie) ) {
      $('#container #slide_box').find('#credits, #frontpage, #press').css('top', '93px');
    } else {
      $('#container #slide_box').find('#credits, #frontpage, #press').css('top', '92px');
    }
  }
  $('#slide_box').find('#info').click(function (){
    $(this).closest('#container').find('#press').hide().css('height', '0');
    $(this).closest('#container').find('#frontpage').hide().css('height', '0');
    $(this).closest('#container').find('#credits').hide().css('height', '0');
    $(this).closest('#container').find('#group_info').animate({
      opacity: 1,
      height: '460px'
    }, 2000 );
  });
  $('#group_info').find('.close_box').click(function (){
    $(this).closest('#slide_box').find('#group_info').animate({
      opacity: 0,
      height: '0px'
    }, 2000 );
  });
  
  //******* frontpage
  // ** press
  $('#press').find('#load_press').load('/presse #this');
  $('#footer_fixed').find('#press_info').click(function (){
    
    $(this).closest('#container').find('#frontpage').hide().css('height', '0');
    $(this).closest('#container').find('#group_info').hide().css('height', '0');
    $(this).closest('#container').find('#cart_info').hide().css('height', '0');
    $(this).closest('#container').find('#credits').hide().css('height', '0');
    $('#login_form').hide().css('height', '0');
    $('.close_box.log').hide();
    $('#press').children().show();
    $(this).closest('#container').find('#press').animate({
      opacity: 1,
      height: '420px'
    }, 2000 );
  });
  $('#press').find('.close_box').click(function (){
    $(this).closest('#container').find('#press').animate({
      opacity: 0,
      height: '0px'
    }, 2000 );
    $('#press').children().hide();
  });
  
    // ** credits
    
  $('#footer_fixed').find('#credit_info').click(function (){
    
    $(this).closest('#container').find('#frontpage').hide().css('height', '0');
    $(this).closest('#container').find('#group_info').hide().css('height', '0');
    $(this).closest('#container').find('#cart_info').hide().css('height', '0');
    $(this).closest('#container').find('#press').hide().css('height', '0');
    $('#login_form').hide().css('height', '0');
    $('.close_box.log').hide();
    $('#credits').children().show();
    $(this).closest('#container').find('#credits').animate({
      opacity: 1,
      height: '420px'
    }, 2000 );
  });
  $('#credits').find('.close_box').click(function (){
    $(this).closest('#container').find('#credits').animate({
      opacity: 0,
      height: '0px'
    }, 2000 );
    $('#credits').children().hide();
  });
    // ** info 
  
  $('#footer_fixed').find('#front_info').click(function (){
  
    $(this).closest('#container').find('#credits').hide().css('height', '0');
    $(this).closest('#container').find('#group_info').hide().css('height', '0');
    $(this).closest('#container').find('#cart_info').hide().css('height', '0');
    $(this).closest('#container').find('#press').hide().css('height', '0');
    $('#login_form').hide().css('height', '0');
    $('.close_box.log').hide();
    $('#frontpage').children().show();
    $(this).closest('#container').find('#frontpage').animate({
      opacity: 1,
      height: '420px'
    }, 2000 );
  });
  $('#frontpage').find('.close_box').click(function (){
    $(this).closest('#container').find('#frontpage').animate({
      opacity: 0,
      height: '0px'
    }, 2000 );
    $('#frontpage').children().hide();
  });
    
  //******* frontpage end
  
  // ** shop
  
  $('#slide_box').find('#cart').click(function (){
  
    $('#frontpage, #credits').children().hide();
    $(this).closest('#container').find('#frontpage').hide().css('height', '0');
    $(this).closest('#container').find('#credits').hide().css('height', '0');
    $(this).closest('#container').find('#press').hide().css('height', '0');
    $('#login_form').hide().css('height', '0');
    $('.close_box.log').hide();
    
    $(this).closest('#slide_box').find('#cart_info').load('/butikk/handlevogn/innhold .wrapit');
    $(this).closest('#slide_box').find('#cart_info').animate({
      opacity: 1,
      height: '460px'
    }, 2000 );
  });
  
  $('#slide_box').find('#login').click(function (){
    $('#frontpage, #credits').children().hide();
    $(this).closest('#container').find('#frontpage').hide().css('height', '0');
    $(this).closest('#container').find('#credits').hide().css('height', '0');
    $(this).closest('#container').find('#press').hide().css('height', '0');
    $(this).closest('#container').find('#cart_info').hide().css('height', '0');
    
    $(this).closest('#slide_box').find('#login_info').load('/register #login_form');
    $(this).closest('#slide_box').find('#login_info').animate({
      opacity: 1,
      height: '120px'
    }, 2000 );
    $('.close_box.log').fadeIn('slow');
  });
  
  $('.close_box.log').click(function (){
    $('#login_form').animate({
      opacity: 0,
      height: 0
    }, 2000 );
    $('.close_box.log').hide();
  });
    
  //home
  
  $('#logo').bind('click', function (ev) {
    window.location.href = "/";
  });
  
  // clothing size
  $('.choice .var_title').click(function (){
    $('.choice a').each(function (index) { /* looper over alle, fjerner class selected på alle */
      $(this).removeClass('selected');
    });
    $(this).addClass('selected'); /* legger til class selected på meg */
    $(this).closest('.choice').next('.choice').find('.product_sizes').val($(this).attr('data-id')).attr('data-id', $(this).attr('data-id'));
    return false;
  });
  
  // scrollbar in cart slidedown
  
  $('#cart_info').bind('mouseover', function (){
    $(this).css({'overflow-y': 'scroll'});
  });
  
  $('#cart_info').bind('mouseout', function (){
    $(this).css({'overflow-y': 'hidden'});
  });
  
  // choose product

  
  if ($('.product_sizes').length > 0){
    if($('.product_sizes').val().length > 0) {
      myid = $('.product_sizes').val();
      $('ul#product_varieties').find('li').each( function (index) {
        if ($(this).find('.var_title').attr('data-id') == myid){
          $(this).find('.var_title').trigger('click');  
        }
      });
    }
  }
  
  $('#buy').find('input').click(function(){
    $('.error').empty();
    var numberItems = $('#product_amount').find('.counter').val();
    var varietyItems = $('#product_amount').find('.product_sizes').val();
    
    errorCount = 0;
    if(numberItems.length == 0 ){
      $('#buy li:first').append('<span class="error_size">Velg antall</span>');
      errorCount ++ ;
    }
    if(varietyItems.length == 0 ){
      $('#product_varieties').append('<li style="display: inline"><span class="error_size">Velg størrelse</span></li>');
      errorCount ++ ;
    }
     if( errorCount > 0 ){
      return false;    
     }  
  }); 
  
});

$('#print_me').bind('click', function(){
  window.print();
  return false
});


/*
function callPrint(strid)
{
  var prtContent = document.getElementById(strid);
  var WinPrint = window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
  WinPrint.document.write(prtContent.innerHTML);
  WinPrint.document.close();
  WinPrint.focus();
  WinPrint.print();
  WinPrint.close();
 //prtContent.innerHTML=strOldOne;
}
*/

// window & content resize ajax content
function forceLoad(){
  if ( $.browser.msie ) {
    $('#img_keeper img').css('width','308px');
    $('#img_keeper img').css('height','472px');
    var img_height2 = $('#img_keeper img').height();
    var img_width2 = $('#img_keeper img').width();
    var ratio2 = $('#img_keeper img').width()/$('#img_keeper img').height();
  }
  
  $('#img_keeper').css({'width': '1577px', 'height': '472px'});
  var keeper_height = $('#img_keeper').height();
  var keeper_width = $('#img_keeper').width();
  var keeper_ratio = keeper_width/keeper_height;
  
  if($(this).width() < 1577) {
    
    if ( $.browser.msie ) {
      $('#img_keeper img').css('height', img_height2*ratio2);
      $('#img_keeper img').css('width', img_width2*ratio2);
    }

    $('body').css({
      'margin-left': -((1577 - $(this).width())/2),
      'overflow-x' : 'hidden' 
    });
    
    $('#img_keeper').width($(this).width()) ;
    $('#img_keeper li.findme').each(function (){
      if ( $.browser.msie ) {
      $(this).width($('#img_keeper img').width());
      $(this).height($('#img_keeper img').height());
      } else {
      $(this).width(($('#img_keeper').width())/5)/* .height('auto') */;
      }
    });
    $('#img_keeper img, .img_temp_holder img').each(function (){
      $(this).attr({'width': $('#img_keeper li.findme').width()});
    });
    //$('#img_keeper').height($('#img_keeper li.findme').height());
    
    $('.sizer').css({'width': '1575px'});
  }
  if (! ($.browser.msie) ) {  
    $('#container, .fb img').animate({opacity: 1.0}, 10);
  }
  $('body').css({'background': '#e6e6e6', 'overflow-y': 'auto'});
}



/*
  denne lå opprinnelig på shop sin js fil
  var winWidth = $(window);
  var winHeight  = $(window).height();

  if(winWidth.width() < 1577) {
    $('body').css({
      'margin-left': -((1577 - $(this).width())/2),
      'overflow' : 'hidden' 
    });
    $('#img_keeper').width(winWidth.width()).height('auto') ;
    $('#img_keeper li').each(function(){
      $(this).width(($('#img_keeper').width())/5).height('auto');
    });
    $('#img_keeper img, .img_temp_holder img').each(function(){
      $(this).attr('width', $('#img_keeper li').width());
    });
    $('.sizer').css({'width' : $(window).width()});
    
  }
  
*/
