var wos_field = false;
wos_data = wos_data[0];
jQuery(document).ready(function(){
 jQuery('.wos_colorpicker').each(function () {
        var $this = jQuery(this);
        if( jQuery(this).prev('.wos_colorPreview').length ==0){
         jQuery(this).before('<div class="wos_colorPreview" style="background-color:#'+$this.val().substr(1, 7)+'"></div>');
        }
        jQuery(this).prev('.wos_colorPreview').click(function(){
            jQuery(this).next('.wos_colorpicker').trigger('click');
        });
        jQuery(this).colpick({
            color: $this.val().substr(1, 7),
            onChange: function (hsb, hex, rgb, el, bySetColor) {
                jQuery(el).val('#' + hex);
                 jQuery(el).prev('.wos_colorPreview').css({
                    backgroundColor: '#'+hex 
                 });
            }
        });
        jQuery(this).on('change',function(){
             jQuery(this).prev('.wos_colorPreview').css({
                backgroundColor: '#'+jQuery(this).val() 
             });
        });
    });
    jQuery("#osdb_bootstraped [data-toggle='switch']").bootstrapSwitch();
    jQuery('.imageBtn').click(function () {
        wos_formfield = jQuery(this).prev('input');
        tb_show('', 'media-upload.php?TB_iframe=true');

        return false;

    });
    window.old_tb_remove = window.tb_remove;
    window.tb_remove = function () {
        window.old_tb_remove();
        wos_formfield = null;
    };
    window.original_send_to_editor = window.send_to_editor;
    window.send_to_editor = function (html) {
        if (wos_formfield) {
            var alt = jQuery('img', html).attr('alt');
            fileurl = jQuery('img', html).attr('src');
            if(jQuery('img', html).length==0){
                fileurl = jQuery(html).attr('src');  
                alt = jQuery(html).attr('alt');                   
            }
            wos_formfield.val(fileurl);
            tb_remove();
        } else {
            window.original_send_to_editor(html);
        }
    };
    wos_loadSettings();
});
function wos_inIframe () {
    try {
        return window.self !== window.top;
    } catch (e) {
        return true;
    }
}

function wos_loadSettings(){
    jQuery.ajax({
      url: ajaxurl,
      type: 'post',
      data: {
        action: 'wos_loadSettings'
      },
      success: function(rep) {          
           rep = JSON.parse(rep);
           jQuery('#wos_settings [name="rolesAllowed"] option').removeAttr('selected');
          jQuery('#wos_settings').find('input,select,textarea').each(function () {
              if(jQuery(this).attr('name') == 'rolesAllowed'){
                  var roles = rep.rolesAllowed.split(',');
                  jQuery.each(roles,function(){
                      jQuery('#wos_settings [name="rolesAllowed"] option[value="'+this+'"]').attr('selected','selected');
                  });
              }else {
                if(jQuery(this).is('[data-toggle="switch"]')){
                    var value = false;
                    eval('if(rep.' + jQuery(this).attr('name') + ' == 1 && jQuery(this).attr(\'checked\') != \'checked\'){jQuery(this).trigger(\'click\');}');
                    eval('if(rep.' + jQuery(this).attr('name') + ' == 0 && jQuery(this).attr(\'checked\') == \'checked\'){jQuery(this).trigger(\'click\');}');
                    var self = this;
                    if (jQuery(self).closest('.form-group').find('small').length > 0) {
                        jQuery(self).closest('.has-switch').tooltip({
                            title: jQuery(self).closest('.form-group').find('small').html()
                        });
                    }
                }else {
                    eval('jQuery(this).val(rep.' + jQuery(this).attr('name') + ');');
                    jQuery(this).change();
                }
            }
            });   
            if(rep.purchaseCode == ""){
                jQuery('#wos_settings [name="purchaseCode"]').closest('.form-group').addClass('has-error');
            }
      }
  });
}

function wos_saveSettings(){
    var formData = {};
    jQuery('#wos_settings').find('input,select,textarea').each(function () {       
        if(jQuery(this).attr('name') != 'rolesAllowed'){
            if (!jQuery(this).is('[data-toggle="switch"]')) {           
              eval('formData.' + jQuery(this).attr('name') + ' = jQuery(this).val();');
            } else {
              var value = 0;
              if (jQuery(this).is(':checked')){
                value = 1;
              }
              eval('formData.' + jQuery(this).attr('name') + ' = value;');
            } 
        }      
    });
    var rolesAllowed = '';
    jQuery('#wos_settings [name="rolesAllowed"] option:selected').each(function(){
       rolesAllowed += jQuery(this).attr('value')+',';
    });
    if(rolesAllowed.length>0){
        rolesAllowed = rolesAllowed.substr(0,rolesAllowed.length-1);
    }
    formData.rolesAllowed = rolesAllowed;
    formData.action = 'wos_saveSettings';
    jQuery.ajax({
        url: ajaxurl,
        type: 'post',
        data: formData,
        success: function () {
            if(wos_inIframe()){
                window.parent.wos_reloadPage();
            } else {
                document.location.href = document.location.href;
            }
        }
    });
}
function wos_resetColors(){
    jQuery('#wos_settings [name="colorWinHeaderBg"]').val('#1cccaa').prev('.wos_colorPreview').css({
        backgroundColor: '#1cccaa'
     });
    jQuery('#wos_settings [name="colorWinHeader"]').val('#ecf0f1').prev('.wos_colorPreview').css({
        backgroundColor: '#ecf0f1'
     });
    jQuery('#wos_settings [name="colorWinIconsBg"]').val('#ecf0f1').prev('.wos_colorPreview').css({
        backgroundColor: '#ecf0f1'
     });
    jQuery('#wos_settings [name="colorWinIconsSelected"]').val('#95a5a6').prev('.wos_colorPreview').css({
        backgroundColor: '#95a5a6'
     });
    jQuery('#wos_settings [name="colorWinIcons"]').val('#95a5a6').prev('.wos_colorPreview').css({
        backgroundColor: '#95a5a6'
     });
    jQuery('#wos_settings [name="colorShortcuts"]').val('#ecf0f1').prev('.wos_colorPreview').css({
        backgroundColor: '#ecf0f1'
     });
}