/* onload settings */
$().ready(function () {
  $('.more-hidden,.questions li .cms').hide();
  $('.xl .questions li:last-child').addClass('last');
  $('.xl .product-list ol li:last-child').addClass('last');
  $('#sec-nav ul li:last-child').addClass('last');
  $('#contact-form-uri').val(parent.location.href);  
});


/* tab behavior */
$('.tabset').tabs();

/* accordion */
$('.questions li h4').click(function() {
	var cms = $(this).parent().next()[0];
	if(cms.style.display == 'none') {
		$(cms).show();
    $(this).parent().parent().addClass('active');
	} else {
		$(cms).hide();
		$(this).parent().parent().removeClass('active');
  }
  return false;
});

/* accordion in packages */
$('.more-switch').click(function () {
  if ($(this).hasClass('open')) {
    $(this).removeClass('open');
    $(this).html('Več');
    $(this).next().hide();
  } else {
    $(this).addClass('open');
    $(this).html('Manj');
    $(this).next().slideDown();
  }
});


/* showing / hiding text */
$('.product-list ol li .desc .more').click(function () {
  if ($(this).prev().hasClass('excerpt')) {
    $(this).prev().removeClass('excerpt');
    $(this).parent().addClass('desc-active');
    $(this).html('Manj');
  } else {
    $(this).prev().addClass('excerpt');
    $(this).parent().removeClass('desc-active');
    $(this).html('Več');
  }
});

/* increase || decrease value */
$('.qty ul .add').click(function () {
  var tmpval = parseInt($(this).parent().prev().val(), '10');
  tmpval = tmpval + 1;
  $(this).parent().prev().attr('value', tmpval);
});
$('.qty ul .del').click(function () {
  var tmpval = parseInt($(this).parent().prev().val(), '10');
  if (tmpval >= 1) {
    tmpval = tmpval - 1;
    $(this).parent().prev().attr('value', tmpval);
  }
});


/* keyvisual */
$('#keyvisual .sw li').click(function() {
  var $active = $(this).attr('class').substr(0, 2);
  $('#keyvisual .sw li').removeClass('active');
  $(this).addClass('active');
  $('#keyvisual .kv li').removeClass('active');
  $('#keyvisual .kv li').hide();
  $('#keyvisual .kv li.' + $active).fadeIn(1000);
});


/* price calculation */
if ($('#slider').length) {
  $('#slider').slider({
    value: 0,
    min: 0,
    max: 500,
    slide: function(event, ui) {
      $('#amount').val(ui.value);
      $('#amount-cost').html((ui.value * '0.1').toFixed(2));
    }
  });
  $('#amount').val($('#slider').slider('value'));
  $('#amount').bind('change keyup', function() {
    $('#amount-cost').html((($('#amount').val()) * '0.1').toFixed(2));
  });
}

calculatePrices = function() {
  var sum1 = $('#pack-select').closest(".content").find('.pricetag big').html();
  var sum2 = $('#tel-select').closest(".content").find('.pricetag big').html();
  var sum3 = $('#tv-select').closest(".content").find('.pricetag big').html();
  var sum4 = $('#package-select').closest(".content").find('.pricetag big').html();
  var tempSum = 0;
  if (sum1 != null) {
    tempSum = tempSum + parseFloat(sum1.replace(",", "."));
  }
  if (sum2 != null) {
    tempSum = tempSum + parseFloat(sum2.replace(",", "."));
  }
  if (sum3 != null) {
    tempSum = tempSum + parseFloat(sum3.replace(",", "."));
  }
  if (sum4 != null) {
    tempSum = tempSum + parseFloat(sum4.replace(",", "."));
  }
  $('.order-service').each(function() {
    var check_id = $(this).attr("id");
    if ($(this).is(':checked')) {
      var value = $(this).val();
      $("#price-" + check_id).show();
      if (value) {
        if ($("#qty-" + check_id).length > 0) {
          tempSum = tempSum + (parseFloat(value) * parseInt($("#qty-" + check_id).val(), 10));
          $("#price-" + check_id + " .pricetag big").html((Math.round(100 * (parseFloat(value) * parseInt($("#qty-" + check_id).val(), 10))) / 100).toFixed(2) + "&euro;");
        } else {
          tempSum = tempSum + parseFloat(value);
        }
      }
    } else {
      $("#price-" + check_id).hide();
    }
  });
  $('.sum big strong').html(((Math.round(100 * tempSum) / 100).toFixed(2) + " &euro;").replace(".", ","));
}


/* execute at ajax call */
function modalLoaded(hash) {
  calculatePrices();
  /* setup */
  $('.norm-acc li .content').hide();
  $('.norm-acc li.selected .content').show();

  /* regular accordion */
  $('.norm-acc li h4').click(function () {
    if ($(this).parent().hasClass('active')) {
      $(this).parent().removeClass('active');
      $(this).next().toggle();
    } else {
      $(this).parent().addClass('active');
      $(this).next().slideDown();
    }
  });

  /* tabs */
  if (hash){
     var element = hash.t;
     if (element.getAttribute("rel")){
      $('.tabset').tabs({selected: element.getAttribute("rel")});
    }

    $('.tab_trigger li a').click(function(){
      $('.tabset').tabs({selected: $(this).attr("rel")});
    });
  }

  /* inc / dec */
  if($('.qty ul .add').length > 0){
    $('.qty ul .add').livequery("click", function () {
      var tmpval = parseInt($(this).parent().prev().val(), '10');
      tmpval = tmpval + 1;
      var max = parseInt($(this).parent().prev().attr("rel"), "10");
      if (tmpval > max) {
        tmpval = max;
      }
      $(this).parent().prev().attr('value', tmpval);
    });
  }
  if($('.qty ul .del').length > 0){
    $('.qty ul .del').livequery("click", function () {
    var tmpval = parseInt($(this).parent().prev().val(), '10');
    if (tmpval > 1) {
      tmpval = tmpval - 1;
      $(this).parent().prev().attr('value', tmpval);
    }
  });
  }  
  $('#pack-select').livequery("change", function(event) {
    $.post('/main/pack_select/', { id: $('#pack-select').val(), type: "internet" },
            function(data) {
              $('#pack-select').closest(".content").html(data);
              calculatePrices();
            }
            );
  });
  $('#package-select').livequery("change", function(event) {
    $.post('/main/package_pack_select/', { id: $('#package-select').val(), type: "package" },
            function(data) {
              $('#package-select').closest(".content").html(data);
              calculatePrices();
            }
            );
  });
  $('#tv-select').livequery("change", function(event) {
    $.post('/main/pack_select/', { id: $('#tv-select').val(), type: "tv" },
            function(data) {
              $('#tv-select').closest(".content").html(data);
              calculatePrices();
            }
            );
  });
  $('#tel-select').livequery("change", function(event) {
    $.post('/main/pack_select/', { id: $('#tel-select').val(), type: "tel" },
            function(data) {
              $('#tel-select').closest(".content").html(data);
              calculatePrices();
            }
            );
  });
  $('.qty input').livequery("change", function(event) {
    var tmpval = parseInt($(this).val(), '10');
    var max = parseInt($(this).attr("rel"), "10");
    if (tmpval > 1) {
      if (tmpval > max) {
        tmpval = max;
      }
      $(this).attr('value', tmpval);
    } else {
      $(this).attr('value', 1);
    }
    $.post('/main/service_quantity_change/', { id: $(this).attr('id').replace("qty-", ""), value: tmpval },
            function(data) {
              $('#order-pack').html(data);
            }
            );
    calculatePrices();
  });
  $('.qty .add').livequery("click", function(event) {
    a = $("#" + $(this).attr("rel"));
    $.post('/main/service_quantity_change/', { id: a.attr('id').replace("qty-", ""), value: a.val() },
            function(data) {
              $('#order-pack').html(data);
            }
            );
    calculatePrices();
  });
  $('.qty .del').livequery("click", function(event) {
    a = $("#" + $(this).attr("rel"));
    $.post('/main/service_quantity_change/', { id: a.attr('id').replace("qty-", ""), value: a.val() },
            function(data) {
              $('#order-pack').html(data);
            }
            );
    calculatePrices();
  });
  $('.order-service').livequery("click", function(event) {
    a = $(this);
    if ($("#qty-" + a.attr('id')).length > 0) {
      $.post('/main/service_quantity_change/', { id: a.attr('id'), value: $("#qty-" + a.attr('id')).val() },
              function(data) {
                $('#order-pack').html(data);
              }
              );
    }
    $.post('/main/service_toggle/', { id: a.attr('id') },
            function(data) {
              $('#order-pack').html(data);
            }
            );
    calculatePrices();
  });
  $('#order').livequery("click", function(event) {
    $.post('/main/order_form/', { id: $('#tel-select').val() },
            function(data) {
              $('#lightbox-container').html(data);
            }
            );
  });
  $('#order_submit').livequery("click", function(event) {
    $.post('/main/order_form/', getOrderFormData(),
            function(data) {
              $('#lightbox-container').html(data);
            }
            );
    return false;
  });
  $('#informative_order_submit').livequery("click", function(event) {
    $.post('/main/informative_order_form/', getOrderFormData(),
            function(data) {
              $('#lightbox-container').html(data);
            }
            );
    return false;
  });
  $('.estimate').livequery("click", function(event) {
    $.post('/main/informative_order_form/', { id: $('#tel-select').val() },
            function(data) {
              $('#lightbox-container').html(data);
            }
            );
  });
  $('#order_back').livequery("click", function(event) {    
    $.post('/main/lightbox_order/', { id: $('#back_package_id').val(), type: $('#back_form_type').val(), back: "1" },
            function(data) {
              $('#lightbox-container').html(data);              
              modalLoaded();
            }
            );
  });
}

function cleanSession(hash) {
  $('.estimate').expire();
  $('#informative_order_submit').expire();
  $('#order_submit').expire();
  $('#order').expire();
  $('.order-service').expire();
  $('.qty .del').expire();
  $('.qty .add').expire();
  $('.qty input').expire();
  $('#tel-select').expire();
  $('#tv-select').expire();
  $('#package-select').expire();
  $('#pack-select').expire();
  $('.qty ul .del').expire();
  $('.qty ul .add').expire();
  $.post('/main/clean_session/', 1);
  hash.w.fadeOut('2000', function() {
    hash.o.remove();
  });
}

getOrderFormData = function() {
  var formData = {};
  if ($('#checkavailability').length > 0) {
    $('#checkavailability input').each(function() {
      if ($(this).val() != "undefined" && $(this).val() != null && $(this).val() != "") {
        formData[$(this).attr("name")] = $(this).val();
      }
    });
  }
  return formData;
}
/* lightbox switch */
if ($('.new-window').length) {
  $('#lightbox').jqm({ajax: '@href', trigger: 'a.new-window', onLoad: modalLoaded, onHide: cleanSession});
}

/* a title */
if ($('a[title]').length > 0){
  $('a[title]').qtip({style:{name:'light',tip:true}})
}

/* form check */
if ($('#checkavailability').length > 0){
  $('#checkavailability').validate();
}

/*
 * Translated default messages for the jQuery validation plugin.
 * Language: SL
 * Skipped date/dateISO/number.
 */
jQuery.extend(jQuery.validator.messages, {
  required: "To polje je obvezno",
  maxlength: jQuery.validator.format("Vnesete lahko največ {0} znakov."),
  minlength: jQuery.validator.format("Vnesite najmanj {0} znakov."),
  rangelength: jQuery.validator.format("Vnesete lahko najmanj {0} in največ {1} znakov."),
  email: "Vnesite pravilen e-poštni naslov.",
  url: "Vnesite pravilen URL.",
  dateSL: "Vnesite veljaven datum.",
  numberSL: "Vnesite številko.",
  digits: "Vnesete lahko samo številke.",
  equalTo: "Ponovno vnesite enak zapis.",
  range: jQuery.validator.format("Vnesite vrednost med {0} in {1}."),
  max: jQuery.validator.format("Vnesite vrednost manjšo ali enako {0}."),
  min: jQuery.validator.format("Vnesite vrednost večjo ali enako {0}."),
  creditcard: "Vnesite veljavno številko kreditne kartice."
});