$('.select-site').on('change', function (e) {
  var optionSelected = $("option:selected", this);
  var valueSelected = this.value;
  // console.log(optionSelected+', '+valueSelected);
  if ( valueSelected == "newsite" ) {
    $('#new-site-box').slideDown();
     $("#upgrade-box").slideUp();
    $('#maintenance-box').slideUp();
  }else if(valueSelected == "") {
    $('#new-site-box').slideUp();
    $('#maintenance-box').slideUp();
    $("#upgrade-box").slideUp();
  }else if(valueSelected == "upgrade") {
    $('#new-site-box').slideDown();
    $("#upgrade-box").slideDown();
    $('#maintenance-box').slideUp();
  }else{
    $('#new-site-box').slideUp();
    $('#maintenance-box').slideDown();
  }
});

$('.line-check-host .radio-check').click(function(){
  if($('.line-check-host .radio-check:checked').val() == "0"){
    $('#details-host').removeAttr('disabled');
  }else{
    $('#details-host').attr('disabled', 'disabled');
  }
});

//module custom
$('.custom-check').click(function(){
  if ($('.custom-check:checked').length > 0 ){
    $('.custom-module-detail').removeAttr('disabled');
  }else{
    $('.custom-module-detail').attr('disabled', 'disabled');
  }
});

//module custom
$('.custom-check2').click(function(){
  if ($('.custom-check2:checked').length > 0 ){
    $('.custom-template-detail').removeAttr('disabled');
  }else{
    $('.custom-template-detail').attr('disabled', 'disabled');
  }
});

var addPage = 1;
$('.add-next').click(function(){
  $('.box-form.form'+addPage).hide();
  addPage += 1;
  $('.box-form.form'+addPage).fadeIn();
  if ( addPage == 1) {
    $('.progress-bar').css('width','13px');
    $('.tag-step').removeClass('active');
    $('.tag-step.step'+addPage).addClass('active');
  }
  if ( addPage == 2) {
    $('.progress-bar').css('width','50%');
    $('.tag-step').removeClass('active');
    $('.tag-step.step'+addPage).addClass('active');
  }
  if ( addPage == 3) {
    $('.progress-bar').css('width','100%');
    $('.tag-step').removeClass('active');
    $('.tag-step.step'+addPage).addClass('active');
  }

  var p = $('.cp-content');
  var offset = p.offset();
  $("body, html").animate({scrollTop: offset.top}, 0);
});
$('.add-prev').click(function(){
  $('.box-form.form'+addPage).hide();
  addPage -= 1;
  $('.box-form.form'+addPage).fadeIn();
  if ( addPage == 1) {
    $('.progress-bar').css('width','13px');
    $('.tag-step').removeClass('active');
    $('.tag-step.step'+addPage).addClass('active');
  }
  if ( addPage == 2) {
    $('.progress-bar').css('width','50%');
    $('.tag-step').removeClass('active');
    $('.tag-step.step'+addPage).addClass('active');
  }
  if ( addPage == 3) {
    $('.progress-bar').css('width','100%');
    $('.tag-step').removeClass('active');
    $('.tag-step.step'+addPage).addClass('active');
  }

  var p = $('.cp-content');
  var offset = p.offset();
  $("body, html").animate({scrollTop: offset.top}, 0);
});

// sortable
var optionalModuleHolder = $("#optional-module-holder"),
    iframe = document.getElementById("store-frame"),
    templateBtn = $("#template-modal-btn"),
    modal = $("#template-modal");

templateBtn.on("click",function(){
  modal.modal("show");
  return false;
})

modal.on('shown.bs.modal', function () {
  iframe.src = "http://www.rulingstyle.net/?mode=store";
})
modal.on('hidden.bs.modal', function () {
  iframe.src = "http://www.rulingstyle.net/?mode=reset";
})

var receiveMessage = function(event){
  if(event.origin == "http://www.rulingstyle.net"){
    $("#site_request_template_number").val(event.data);
    modal.modal("hide");
  }
}

if (navigator.appName == 'Microsoft Internet Explorer' ||  !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie == 1)){
  window.attachEvent('onmessage',receiveMessage);
}else{
  window.addEventListener("message", receiveMessage, false);
}

$( ".connected" ).sortable({
  connectWith: ".connected",
  receive : function(event, ui){
    var id = ui.item.data("mod-id"),
        els = optionalModuleHolder.find("input[for=" + id +"]");
    if(els.length == 0){
      var el = $("<input type='hidden' name='site_request[optional_modules][]' />");
      el.val(id);
      el.attr("for",id);
      optionalModuleHolder.append(el);
    }else{
      els.remove();
    }
  }
});




