var switcher = new switcher({});
$j(function(){
switcher.init();
});
$j(document).ajaxComplete(function(){
switcher.ajaxInit();
});
function switcher( options ){
// Init vars of switcher
var o = this;
var $ = jQuery;
this.defaultOpt = {
'origin' : '.switcherChk',
'lclass' : '.feladat-indit-label',
'xlclass' : 'feladat-indit-label',
'xsclass' : 'switcher',
'sclass' : '.switcher',
'prefix' : 'switcher-',
'leftpos' : '-2px',
'rightpos' : '20px',
'pattern' :
'
'
}
o.options = $.extend( o.defaultOpt, options );
// Init of switcher
this.init = function( ){
delete o['defaultOpt'];
o.createSwitcher();
o.mkswitch();
}
// Ajax init function
this.ajaxInit = function(){
o.createSwitcher();
}
// Create switcher form original checkbox
this.createSwitcher = function(){
$( o.options.origin ).each(function(){
if( !$( this ).hasClass('rendered') ){
// Create uniqId for every original checkbox
var uniqId = o.uniqId();
// Add this uniqId as class
$(this).addClass(uniqId);
$(this).addClass('rendered');
// Check the input checked or not
var checked = 'off';
if( $(this).prop("checked") ) {
checked = 'on';
}
// Create switcher throught a pattern
var switcher = o.options.pattern.replace( '%chkID%', uniqId );
switcher = switcher.replace( '%chkState%', checked );
if($(this).hasClass('mcenter')){
$(this).removeClass('mcenter');
switcher = switcher.replace( '%center%', 'mcenter' );
}
else{
switcher = switcher.replace( '%center%', '' );
}
// Add switcher to html
$j(this).after(switcher);
$j(this).css({display: 'none'});
// Init default states
o.initStates(uniqId, checked);
}
});
}
// Add switch effect
this.mkswitch = function(){
$j(document).on('click touchstart', o.options.sclass+','+o.options.lclass, function(){
// Get the switcher id for modify checkbox data
var switcherId = "";
if($j(this).hasClass(o.options.xsclass)) switcherId = $j(this).attr("id");
else switcherId = $j(this).parent().find('.switcher').attr("id");
// Get the new state of switcher and checkbox
var switcherNewState = 'on';
if( $j("#"+switcherId).hasClass('on') )
{
switcherNewState = 'off';
}
// Use animate on switcher
o.switcherAnimate( switcherId , switcherNewState );
});
}
// Create uniqId with prefix
this.uniqId = function(){
var uniqId = o.options.prefix;
for(var i = 0; i < 2; i++){
uniqId += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return uniqId;
}
// Initailize switcher state
this.initStates = function( switcherId, state ){
var buttonItem = $('#'+switcherId+' .button');
if( state == 'on' ){
buttonItem.css({left: o.options.rightpos});
$('.' + switcherId).val('1').prop("checked",true);
}
else if( state == 'off' ) {
buttonItem.css({left: o.options.leftpos});
$('.' + switcherId).val('0').prop("checked",false);
}
}
// Make switch animate if trigger click on switcher
this.switcherAnimate = function( switcherId, switcherNewState ){
var buttonItem = $('#'+switcherId+' .button');
if(switcherNewState == 'on'){
buttonItem.animate({ left: o.options.rightpos }, 300, function(){
buttonItem.parent().removeClass("off").addClass("on");
$('.' + switcherId).val('1').prop("checked",true);
});
}
else if(switcherNewState == 'off') {
buttonItem.animate({ left: o.options.leftpos }, 300, function(){
buttonItem.parent().removeClass("on").addClass("off");
$('.' + switcherId).val('0').prop("checked",false);
});
}
$('.' + switcherId).trigger("click");
}
}