$(document).ready(function(){
  
  $('#ph').innerfade({
    speed: 1000,
    timeout: 5000,
    type: 'sequence',
    containerheight: '410px'
    
  });
  
  

  $('a.fancy').fancybox({
    'hideOnContentClick': true, 
    'centerOnScroll': true, 
    'titlePosition': 'over', 
    'overlayOpacity': '0.8', 
    'overlayColor': '#222222'
  });
  
  $('#ph2').innerfade({
    speed: 1000,
    timeout: 5000,
    type: 'sequence',
    containerheight: '268px'
    
  });
  
  /* var params = {
        changedEl: "#cs",
        visRows: 10,
        scrollArrows: true
    }
    cuSel(params); */
  
  $("ul.tabs").tabs("div.panes > div", {
    current: 'active',
    effect: 'slide',
    tabs: 'li'
  });
  
  $('#askform').ajaxForm({
    beforeSubmit: function(){
	    
      if ($('#ask_ask_name').val() == '' || $('#ask_ask_email').val() == ''|| $('#ask_ask_phone').val() == ''|| $('#ask_ask_message').val() == ''|| $('#ask_ask_message').val() == 'Сообщение') {
        alert('Все параметры обязательны для заполения!');
        return false;
      }
      $('.fstatus').show();
    }, 
	  
    success: function(text, status){
      alert(text.message);
	    
      if (text.status == 'success') {
        //$('#ask_subm').remove()	
        $('#modal01 span.close').click();	
        $('#askform').clearForm();
      }
	    
	    
      $('.fstatus').hide();
	    
    }
  });
    
    
  $('.add2cart').click(function() {
    var data = [];
    data.prod_id = $(this).attr('prod_id');
    data.type_id = $(this).attr('type_id');
    data.cnt = $('#type_'+data.type_id+'_cnt').val();
    data.rewrite = $(this).attr('rewrite');
	
    add2Cart(data);   
  });
  
  $('.add2cart_silent').click(function() {
    var data = [];
    data.prod_id = $(this).attr('prod_id');
    data.type_id = $(this).attr('type_id');
    data.cnt = $('#type_'+data.type_id+'_cnt').val();
    data.rewrite = $(this).attr('rewrite');
	
    add2cart_silent(data);   
  });
    
  $('.removeCart').click(function(){
    var data = [];
    data.prod_id = $(this).attr('prod_id');
    data.type_id = $(this).attr('type_id');
    //data.rewrite = 'true';
    data.cnt = 0;
	
    add2Cart(data);   
  });
  
  $('.removeCart2').click(function(){
    var data = [];
    data.prod_id = $(this).attr('prod_id');
    data.type_id = $(this).attr('type_id');
    data.rewrite = 'true';
    data.cnt = 0;
    
    $(this).parent().parent().remove();
	
    add2cart_silent(data);   
  });
  
    
});    


function fastOrder() {
  buiMessage();
  $.post(
    (debug ? '/frontend_dev.php' : '') + '/catalog/fast', 
    { 
      fast: {
        'name': $('#fast_name').val(),
        'phone': $('#fast_phone').val(),
        'product_id': $('#fast_pid').val(),
        'info': $('#fast_info').val(), 
        'captcha': $('#fast_captcha').val(),
        '_csrf_token': $('#fast__csrf_token').val()
      }
    }, 
        
    function (data) {
      buiMessage(data.message, 6000); 
      if (data.status == 'success') {
        $('#fast_name').val('Ваше имя').toggleClass('grey');
        $('#fast_phone').val('Телефон').toggleClass('grey');
        $('#fast_info').val('Дополнительная информация').toggleClass('grey'); 
        $('#cp2').attr('src', '/captcha?prefix=fast&rand='+Math.random(99999));
        $('#fast_captcha').val('');
      }
    }, 
    'json');
}

function add2cart_silent(data) {
  
  $.post(
    (debug ? '/frontend_dev.php' : '') + '/cart/addcart', 
    { 
      'prod_id': data.prod_id, 
      'type_id': data.type_id, 
      'rewrite': data.rewrite, 
      'cnt': data.cnt
    }, 
    function (data) {

      $('#topcart').html(data.topcart);
	    
      if (data.cnt == 0) {
        $('#cart'+data.type_id).remove();
      }    
	    
      if (data.total1) {
        $('#total1').html(data.total1); 
      }
    }, 
    'json');
}

function add2Cart(data) {
  buiMessage();
  $.post(
    (debug ? '/frontend_dev.php' : '') + '/cart/addcart', 
    { 
      'prod_id': data.prod_id, 
      'type_id': data.type_id, 
      'rewrite': data.rewrite, 
      'cnt': data.cnt
    }, 
    function (data) {
      buiMessage(data.message, 2000); 
      $('.cntInput').val('1');
            
      $('#topcart').html(data.topcart);
      
          
      if (data.cnt == 0) {
        $('#cart'+data.type_id).remove();
      }    
	    
      if (data.refresh) {
        setTimeout(refreshPage, 3000);
      }
      
      if (data.total1) {
        $('#total1').html(data.total1); 
      }
	    
    //refreshTopCart();    
    }, 
    'json');
}

function refreshPage() {
    
  document.location = location.href;
}

function refreshTopCart() {
    
  $.post(
    (debug ? '/frontend_dev.php' : '') + '/cart/gettopcart', 
    {},
    function (data) {
      $('#topcart').html(data.cart);
      $('.totalSum').html(data.total_sum);
    },
    'json');
	
/*  $.post(
	'/frontend_dev.php/cart/refreshtypes', 
	{},
	function (data) {
	    $('#prepare').html(data.tmpl);
	},
	'json'); */
	
}

function buiMessage(text, timeout) {
  if (!text) {
    text = '<h3>Пожалуйста, <br />подождите</h3><a class="back close" >Закрыть</a>'
  }
  
  text = '<div class="added">'+ text + '</div>';
  
  $('#_modal').html(text);
  $('#modal2trigger').click();
  
  if (timeout)
    setTimeout(function(){
      $('#_modal').find('.close').click()
    }, timeout)
}

function simplebuiMessage(text, timeout) {
    text = text ? text : '<h2 style="font-size: 22px;"><img src="/coreimages/load.gif" /><br/>Пожалуйста, подождите...</h2>';
    text += '<a id="popupclose" class="close"  style="cursor:pointer; margin-top: 20px; display:block;" onclick="$.unblockUI()" >закрыть</a>';
    
    $.blockUI({
      message: text,
      timeout: (timeout ?  timeout : 0),
      overlayCSS: {
        opacity: 0.8,
        'z-index': '1001'
      },
      blockMsgClass: 'itempopup',
      css: { 
        width: '60%',
        left: '19%',
        border: 'none', 
        padding: '15px', 
        backgroundColor: '#000', 
        '-webkit-border-radius': '10px', 
        '-moz-border-radius': '10px', 
        opacity: .7, 
        color: '#fff',
        'z-index': 99999
      }
    });
  }

function starhoveroff() {
  currstar=document.getElementById('starcount').value;
  for (i=1;i<6;i++){
    cfsid='cfstar'+i;
    //alert(cfsid);
    if (currstar<i) {
      document.getElementById(cfsid).style.backgroundPosition='bottom';
    } else {
      document.getElementById(cfsid).style.backgroundPosition='top';
    }
  }
}

function starhover(star) {
  currstar=star.id.substr(6,1);
  for (i=1;i<6;i++){
    cfsid='cfstar'+i;
    //alert(cfsid);
    if (currstar<i) {
      document.getElementById(cfsid).style.backgroundPosition='bottom';
    } else {
      document.getElementById(cfsid).style.backgroundPosition='top';
    }
  }
}
function starclick(star) {
  currstar=star.id.substr(6,1);
  document.getElementById('starcount').value=currstar;
}


function checklogin() {
  var login = $('#reg_username').val();
  $.post(
    (debug ? '/frontend_dev.php' : '') + '/index/checkusername', 
    { 'username' : login },
    function (data) {
      if (data.user) {
        $('#login_status').css('color', 'red');
        $('#login_status').html('Занят');
      } else if (login) {
        $('#login_status').css('color', 'green');
        $('#login_status').html('Свободен');
      }
    },
    'json');
}
