var aStores=[];
var geocoder;
var map;
$(document).ready(function(){
  if($('#home-banner').length>0){
    initBannerSlider();
  }
  
  if($('.detail-slide').length>0){
    initDetailSlide();
  }
  
  if($('.news-box').length>0){
    initNewsSlider();
  }
  if($('.steps-hide-form').length>0){
    initStepsHide();
  }
  if($('.login-info').length>0){
   initLoginSlider();
  }
  if($('#stores-map').length>0){
    initStoresMap();
  }
  if($('a.help-link').length>0){
    $('a.help-link').hover(function(e){
      var posX = e.pageX;
      var posY = e.pageY;
      var tipContent = $(this).attr('rel');
      initTip(tipContent,posX,posY);
    },function(){
      closeTip();
    });
  }
  if($('#slider').length>0){
    initBarSlider();
  }
});

function initTip(text,posX,posY){
  $('#tip-wrapper').html(text);
  var tipX = posX-$('#tooltip').width();
  var tipY = posY-$('#tooltip').height();
  $('#tooltip').css('top',tipY);
  $('#tooltip').css('left',tipX);
  $('#tooltip').css('display','block');
}

function closeTip(text){
  $('#tooltip').css('display','none');
  $('#tip-content').html('');
}

function initBannerSlider(){
  $('#home-banner-wrapper').cycle({
    fx: 'scrollHorz',
    timeout: 4500,
    next: '.btn-next',
    prev: '.btn-prev'
  });
  $('.btn-next').bind('click',function(){
    $('#home-banner-wrapper').cycle('pause');
  });
  $('.btn-prev').bind('click',function(){
    $('#home-banner-wrapper').cycle('pause');
  });
}

function initNewsSlider(){
  $('.news-items').cycle({
      fx: 'scrollUp',
      speed: 3000
  });
}

function initLoginSlider(){
  $('.login-info').cycle({
    fx: 'fade',
    speed: 3000
  });
}

function initDetailSlide(){
  $('.detail-slide').each(function(){
    if($(this).children('.detail-collapse').length>0){
      $(this).children('.detail-collapse').slideUp(0);
    }
  });
  
$('.detail-slide').each(function(){
    $(this).children('a.detail-link').click(function(){
      if($(this).parent().children('.detail-collapse').length>0){
        if($(this).parent().children('.detail-collapse').is(':hidden')) {
          $(this).parent('li').parent('ul').children('li').children('.detail-collapse').slideUp('fast');
          $(this).parent().children('.detail-collapse').slideDown('fast');
        }else{
          $(this).parent().children('.detail-collapse').slideUp('fast');
        }
      }
      return false;
    });
  });
  
}
function initStoresMap(){
  $('#stores-wrapper').children('.border-box').each(function(i){
    $(this).attr('id','store-box-'+i);
    $(this).children('.link').children('a').attr('id','store-box-link-'+i);
    aStores[i]=$(this).children('.store-adress').text();
    $(this).children('.link').children('a').click(function(e){
      var vRel=$(this).attr('rel');
      var vRel2=vRel.substring(1,vRel.length-1);
      var vRelSplit=vRel2.split(',');
      //console.log(test[1]);
      setStoresMap(vRelSplit[0],vRelSplit[1],19);
    });
  });
  setStoresMap(49.6100036,6.129596,9);
}
function codeAddress(address,counter) {
  if (geocoder) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        //map.setCenter(results[0].geometry.location);
        $('#store-box-link-'+counter).attr('rel',results[0].geometry.location);
        var image = 'imgs/cursor-gm-orange.gif'; // Please update the path to image if needed
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location,
            icon: image
        });
      } else {
        //alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }
}
function initStepsHide(){
  $('.steps-hide-form').slideUp(0);
  $('#check-ship-adress').click(function(){
    if($(this).is(':checked')){
      $('.steps-hide-form').slideUp('normal');
    }else{
      $('.steps-hide-form').slideDown('normal');
    }
  });
}
function setStoresMap(vLat,vLong,vZoom){
  //var latlng = new google.maps.LatLng(vLat);
  var myOptions = {
    zoom:vZoom,
    center: latlng,
    disableDefaultUI: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  geocoder = new google.maps.Geocoder();
  map = new google.maps.Map(document.getElementById('stores-map'),myOptions);
  for(i=0;i<aStores.length;i++){
    codeAddress(aStores[i],i);
  }
  var latlng = new google.maps.LatLng(vLat,vLong);
  map.setCenter(latlng);
}
function initBarSlider(){
  var slideMax=9;
  $('#slider').slider({
    value:0,
    min: 0,
    max: slideMax,
    step: 3,
    slide: function(event, ui) {
      $('#amount').val(ui.value);
      if(ui.value==0){
        $('#amount').val(5);
        //setBarSliderProducts(5,slideMax);
      }else{
        $('#amount').val(ui.value);
        //setBarSliderProducts(ui.value,slideMax);
      }
    }
  });
  $('#amount').val(5);
  /*$('.product-cols-wrapper').children('.product-col').each(function(cnt1){
    $(this).children('.product-box').each(function(cnt2){
        $(this).children('.button-holder').children('.btn-link').attr('rel',$(this).children('.button-holder').children('.btn-link').attr('href'));
        $(this).attr('id','product-link-'+cnt1+'-'+cnt2);
     })
  });;*/
  //setBarSliderProducts(5,slideMax);
}
function setBarSliderProducts(slideVal,iSlideMax){
  $('.product-cols-wrapper').children('.product-col').each(function(){
      $(this).children('.product-box').each(function(){
          sClassName=$(this).attr('class');
          aClassNames=sClassName.split(' ');
          for(i=0;i<aClassNames.length;i++){
            if(aClassNames[i].substring(0,11)=='product-val'){
              sCheckClassName=aClassNames[i].split('product-val-');
              if(sCheckClassName[1]>=slideVal && (parseInt(sCheckClassName[1])>parseInt(slideVal) && parseInt(slideVal) < parseInt(iSlideMax))){
                $(this).addClass('product-box-disabled');
                $(this).children('.button-holder').children('.btn-link').attr('href','#'+$(this).attr('id'));
              }else{
                $(this).removeClass('product-box-disabled');
                $(this).children('.button-holder').children('.btn-link').attr('href',$(this).children('.button-holder').children('.btn-link').attr('rel'));
              }
            }
          }
      });
  });
}
function jDebug($obj){
  if(window.console && window.console.log){window.console.log($obj);}
  else{alert($obj);}
}
