var frontpage_timer = null;

// JavaScript Document
$(document).ready(function(){
    showBasketErrors();
//   var thisContent = 0;
//   $('#topLinkList ul li, #hovering').mouseover(function(){
//     $(this).attr('id','hovering');
//     var thisTop = ($(this).offset().top)+$(this).height();
//     var thisLeft = ($(this).offset().left);
//     $('#naviHover').css('top',thisTop+'px');
//     $('#naviHover').css('left',thisLeft+'px');
//     $('#naviHover').css('display','block');
//     var k=0;
//     $('#naviHover_center .naviHov_col').each(function(){
//       k=k+$(this).width()+24;
//     });
//     $('#naviHover_center').css('width',k+'px');
//     thisContent = $(this).children('span').children('span').children('a').html();
//   }).mouseout(function(){
//     var linkLeft = $(this).offset().left;
//     var linkTop = $(this).offset().top;
//     var linkRight = linkLeft+$(this).width();
//     var linkBottom = linkTop+$(this).height();
//     var leftPos = $('#naviHover').offset().left;
//     var topPos = $('#naviHover').offset().top;
//     var rightPos = leftPos+$('#naviHover').width();
//     var bottomPos = topPos+$('#naviHover').height();
//     
//     $('body').mousemove(function(e){
//       if(leftPos<e.pageX && e.pageX<rightPos && topPos<e.pageY && e.pageY<bottomPos || linkLeft<e.pageX && e.pageX<linkRight && linkTop<e.pageY && e.pageY<linkBottom){
//         $('#hii').html(e.pageX+' '+e.pageY);
//         $('#hii').css('display','block');
//         $('#naviHover').css('display','block');
//       }else{
//         $('#naviHover').css('display','none');
//         $('#topLinkList ul li').each(function(){
//           if($(this).children('span').children('span').children('a').html()==thisContent){
//             $(this).removeAttr('id');
//             $('#hii').html(thisContent);
//           }
//         });
//       }
//     });
//   });
  

  $('.dropClick').click(function(e){openDrop(e, this)});
  $('.coinTsr .content div').mouseover(function(e){coinFocus(e, this)});
  $('.coinTsr .content div').mouseout(function(e){coinRemoveFocus(e, this)});
  $('#topLinkList ul li span span a').click(function(e){naviFocus(e, this)});
  $('#materialSearch div.checkBox').click(function(e){boxTagging(e, this)});
  if($('#materialSearch').html()){
    $('#materialSearch div.checkBox').each(function(){
      var thisId = $(this).attr('id').split('_')[1];
      if($(this).hasClass('checked')){
        $('#materialSearch input.checkBox').each(function(){
          if($(this).attr('value')==thisId){
            $(this).attr('checked', true);
          }
        });
      }
      
    });
  }
  $('.searchResult .resultImg').each(function(){
    if(!$(this).html()){
      $(this).css('display','none');
    }
  });
  $('.focusClear').focus(function(){ 
    this.select();
  });
  
  // Highslide imagehandler
  $('a.highslideImage').each(function() {
    this.onclick = function() {
      return hs.expand( this );
    };
  });  
  
  $.fn.corner.defaults.useNative = false; 
  $('.rounded').corner("10px");

  	$('td.info a').click(function(e) {
      e.preventDefault();
      var divId = $(this).attr("rel");
      $('div#'+divId).slideToggle(150);
    });
});

function boxTagging(e, val){
  var thisChoice = $(val).attr('id').split('_')[1];
  if($(val).hasClass('checked')){
    $(val).removeClass(' checked');
    $('input.checkBox').each(function(){
      if($(this).attr('value')==thisChoice){
        $(this).attr('checked', false);  
      }
    });
  }else{
    $(val).attr('class',$(val).attr('class')+' checked');
    $('input.checkBox').each(function(){
      if($(this).attr('value')==thisChoice){
        $(this).attr('checked', true);
      }
    });
  }
}

function naviFocus(e, val){
  $('#topLinkList ul li').each(function(){
    $(this).removeClass('active');
  });
  $(val).parent().parent().parent().attr('class','active');
}

function coinFocus(e, val){
  $('table.coinTsr').each(function(){
    if($(this).hasClass('active')){
      $(this).removeClass(' active');
    }
  });
  if ( $(val).closest('table.coinTsr').hasClass('active') != true ){
  	$(val).closest('table.coinTsr').toggleClass('active');
  }
  //$(val).parent().parent().parent().parent().toggleClass('active');
  //console.info($(val).parent().parent().parent().parent().parent().html()); 
//  $(val).parent().parent().parent().parent().attr('class',$(val).parent().parent().parent().parent().attr('class')+' active');
}

function coinRemoveFocus(e, val){
  $('table.coinTsr').each(function(){
    if($(this).hasClass('active')){
      $(this).removeClass(' active');
    }
  });
}

function openDrop(e, val){
  if($(val).parents('#coinView .extend').html()){
    $(val).parent().parent().parent().children().children().children('.extend').each(function(){
      if($(this).html()!=$(val).parent().children('.extend').html()){
        $(this).slideUp(150);
        $(this).parent().children('.dropClick').css('background-position','right 6px');
      }
    });
  }
  if($(val).parent().children('.extend').css('display')=='none'){
    $(val).css('background-position','right bottom');
  }else{
    $(val).css('background-position','right 6px');
  }
  $(val).parent().children('.extend').slideToggle(150);
  var selected = '';
}

function prevBillboard(maxIndex) {
    var curBillboard = $("div.billboard:visible");
    var curBillboardId = curBillboard.attr('id');
    var index = curBillboardId.substr(curBillboardId.lastIndexOf('_')+1);
    var prevIndex = parseInt(index)-1;
    prevIndex = (prevIndex < 0) ? maxIndex : prevIndex;
    $('#billboard_'+index).fadeOut('fast', function() {
        $('#billboard_'+prevIndex).fadeIn('fast');
    });
    $('#billboardText_'+index).fadeOut('fast', function() {
        $('#billboardText_'+prevIndex).fadeIn('fast');
    });
    $('#curBillboardIndex').text(prevIndex+1);
}

function nextBillboard(maxIndex) {
    var curBillboard = $("div.billboard:visible");
    var curBillboardId = curBillboard.attr('id');
    var index = curBillboardId.substr(curBillboardId.lastIndexOf('_')+1);
    var nextIndex = parseInt(index)+1;
    nextIndex = (nextIndex > maxIndex) ? 0 : nextIndex;
    $('#billboard_'+index).fadeOut('fast', function() {
        $('#billboard_'+nextIndex).fadeIn('fast');
    });
    $('#billboardText_'+index).fadeOut('fast', function() {
        $('#billboardText_'+nextIndex).fadeIn('fast');
    });
    $('#curBillboardIndex').text(nextIndex+1);
}

function changeCoinImage(nodeid) {
    var linkClass1 = 'reverseImgLink'+nodeid;
    var linkClass2 = 'obverseImgLink'+nodeid;
    $('a.'+linkClass1).toggleClass('hidden');
    $('a.'+linkClass2).toggleClass('hidden');
}

function changeVariation(orderForm, variation) {
    var nodeID = variation.substr(0, variation.lastIndexOf('_'));
    var objectID = variation.substr(variation.lastIndexOf('_')+1);
    orderForm.ContentNodeID.value = nodeID;
    orderForm.ContentObjectID.value = objectID;
}

function addProductAmount(index) {
    if (document.basketForm.elements['ProductItemCountList[]'][index]) {
        document.basketForm.elements['ProductItemCountList[]'][index].value = parseInt(document.basketForm.elements['ProductItemCountList[]'][index].value)+1;
    } else {
        document.basketForm.elements['ProductItemCountList[]'].value = parseInt(document.basketForm.elements['ProductItemCountList[]'].value)+1;
    }
}

function subProductAmount(index) {
    if (document.basketForm.elements['ProductItemCountList[]'][index]) {
        document.basketForm.elements['ProductItemCountList[]'][index].value = parseInt(document.basketForm.elements['ProductItemCountList[]'][index].value)-1;
    } else {
        document.basketForm.elements['ProductItemCountList[]'].value = parseInt(document.basketForm.elements['ProductItemCountList[]'].value)-1;
    }
}

function addToBasket(form) {
    $.cookie('basket_updated', '1', {path: '/', expires: 0});
    form.submit();
}

function highlightBasket() {
    if ($.cookie('basket_updated') == '1') {
        $.cookie('basket_updated', null, {path: '/', expires: 0});
        $("#shopCartContainer").effect("pulsate", {times: 1}, 1000);
    }
}

function setShipping() {
    var sel = document.basketForm.ShippingCountry;
    var shippingVal = sel.options[sel.selectedIndex].value;
    if (shippingVal != '') {
        var shipping = shippingVal.substr(shippingVal.lastIndexOf('_')+1);
        var country = shippingVal.substr(0, shippingVal.lastIndexOf('_'));
        $("#shippingCost").text(shipping);
        var cost = parseFloat($("#productsTotalCost").text());
        cost = cost + parseFloat(shipping);
        $("#totalCost").text(cost.toFixed(2));
    } else {
        $("#totalCost").text('');
    }
    $.cookie('country', country, {path: '/', expires: 0});
}

function subscribeNewsletter() {
  var serializedForm = $("#newsletterForm").serialize();
  var requestURI = "/"+eZSiteAccess+"/ezjscore/call/newsletter::subscribe?http_accept=html";
  serializedForm = serializedForm + "&lang="+eZSiteAccess;
  $.post(requestURI, serializedForm, function(data) {
      var returnData = data;
      $("#newsletterResult").html(returnData);
  });

  return false;
}

function showBasketErrors() {
  var requestURI = "/"+eZSiteAccess+"/ezjscore/call/basket::get_errors?http_accept=html";
  $.get(requestURI, null, function(data) {
      if (data) {
        alert(data);
      }
  });
}

