// Wizard Panel

jQuery(document).ready(function(){

	jQuery('.wizardpanel div.admincontent').hide(); // Hide all divs
	jQuery('.wizardpanel div.admincontent:first').show(); // Show the first div
	jQuery('.wizardpanel .wizardpanel-content-left li:first').addClass('active'); // Set the class for active state
	
	jQuery('.wizardpanel .wizardpanel-content-left li a').click(function() { // When link is clicked
	    jQuery('.wizardpanel .wizardpanel-content-left li').removeClass('active'); // Remove active class from links
		jQuery(this).parent().addClass('active'); //Set parent of clicked link class to active
		var currentTab = jQuery(this).attr('href'); // Set currentTab to value of href attribute
		jQuery('.wizardpanel div.admincontent').hide(); // Hide all divs
		jQuery('#wizardpanel-content-right').show();
		jQuery(currentTab).show(); // Show div with id equal to variable currentTab
		return false;
	});
	
	jQuery('.wizardpanel-tabsmenu select').change(function() {
	    var obj = jQuery(this);
		var currentTab = obj.val();
		jQuery('.wizardpanel div.admincontent').hide(); // Hide all divs
		jQuery("#" + currentTab).show(); 
		return false;
	});
	
});

// Content Tabs	

	(function($) {
    
    $.fn.tabdisplays = function ( options ) {
	var defaults = {
	    'defaultActive': 1
	},
	settings = $.extend({}, defaults, options);
	var obj = jQuery(this);
	var objid = obj.attr('id');
	
	var tabs = function () {
	   jQuery(obj).addClass("wizardpanel-tabdisplay"); 
	   jQuery( "#" + objid + " .wizardpanel-tablist li[data-tab='"+ settings.defaultActive +"']").addClass('wizardpanel-tabdisplayactive');
	   jQuery("#" + objid + " .wizardpanel-tabdisplay-" + settings.defaultActive).css({'display':'block'});
	   jQuery( "#" + objid + " .wizardpanel-tablist li").each(function (x,y) {
	       jQuery(y).click(function () {
	            jQuery("#" + objid + " .wizardpanel-tablist li").removeClass("wizardpanel-tabdisplayactive");
	            jQuery(y).addClass("wizardpanel-tabdisplayactive");
	            jQuery("#" + objid + " .wizardpanel-tabcontent").css( { "display":"none" } );
	            jQuery("#" + objid + " .wizardpanel-tabdisplay-" + jQuery(this).data('tab')).css({'display':'block'});
	       });
	    });
	}
	
	tabs();
	return this;
		
    }	
}(jQuery));


jQuery('document').ready(function () { // Set default tab 
    jQuery("#wizardpanel-tabblock1").tabdisplays( { 'defaultActive' : 3} );
    jQuery("#wizardpanel-tabblock2").tabdisplays( { 'defaultActive' : 1} );
    jQuery("#wizardpanel-tabblock3").tabdisplays( { 'defaultActive' : 1} );
    jQuery("#wizardpanel-tabblock4").tabdisplays( { 'defaultActive' : 1} );
    jQuery("#wizardpanel-tabblock5").tabdisplays( { 'defaultActive' : 1} );    
});

// Notice Hide

jQuery(document).ready(function(){
  setTimeout("jQuery('.noticehide').fadeOut('slow');",5000);
});

// Notice

(function($) {
	
		$.fn.wizardpanelnotice = function ( options ) {
			var defaults = {
			 dayshidden: 365 
			 
		},
		
		settings = $.extend({}, defaults, options);
		var obj = jQuery(this);
		
		var createCookie = function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		}
	
		var readCookie = function (name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
					var c = ca[i];
					while (c.charAt(0)==' ') c = c.substring(1,c.length);
					if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
		
		var eraseCookie = function(name) {
			createCookie(name,"",-1);
		}
		
		if( readCookie("wizardpanelnotice") === "false") {
			jQuery(obj.selector + " .highlightbox").css('display','none');
		}
		
		$.each(jQuery(obj.selector), function (x,y) {
			
			if( readCookie(jQuery(y).attr('id')) === "false") {
				jQuery("#"+jQuery(y).attr('id')).css('display','none');
			}
			
			jQuery("#"+jQuery(y).attr('id') + " .wizardpanel-closebutton").click(function () {
				jQuery("#"+jQuery(y).attr('id')).css('display','none');
				createCookie(jQuery(y).attr('id'), "false" , settings.dayshidden );
			});
			
		});
		
		return this;
		
	}}(jQuery));
		
// Scroll to top

jQuery(document).ready(function() {

	var offset = 300,

		offset_opacity = 1200,
		scroll_top_duration = 700,
		$back_to_top = jQuery('.cd-top');

	jQuery(window).scroll(function(){
		( jQuery(this).scrollTop() > offset ) ? $back_to_top.addClass('cd-is-visible') : $back_to_top.removeClass('cd-is-visible cd-fade-out');
		if( jQuery(this).scrollTop() > offset_opacity ) { 
			$back_to_top.addClass('cd-fade-out');
		}
	});

	$back_to_top.on('click', function(event){
		event.preventDefault();
		jQuery('body,html').animate({
			scrollTop: 0 ,
		 	}, scroll_top_duration
		);
	});

});

// Image Upload

(function($) {
    
	$.fn.uploadMedia = function ( options ) {
	    
		var defaults = { },
		
		settings = $.extend({}, defaults, options);
		
		var obj = jQuery(this);
	    var mediaUploader;
	    var wrapperid = "";
	    
	    jQuery(obj).click(function () {
			
			wrapperid = $(this).parent().next().find(".wizardpanel-form-textinput").attr("name");
			
			if ( ! $(this).parent().parent().is( "div.wrapped_" + wrapperid ) ) {
                $( this ).parent().addClass(wrapperid);
                $( this ).parent().next().addClass(wrapperid);
                $( this ).parent().next().next().addClass(wrapperid);
                $("." + wrapperid).wrapAll( "<div id='"+ wrapperid + "' class='wrapped_" + wrapperid + "'></div>" );
		    }
		    
			var preview = $( this ).parent().parent().attr('id');
			
			if(mediaUploader) {
				mediaUploader.open()
				return;
			}
			
			mediaUploader = wp.media.frames.file_frame = wp.media({
				title: 'Choose a picture',
				button: {
					text: "Choose Picture"
				},
				multiple: false
			});
			
			mediaUploader.on('select', function () {
			
				attachment = mediaUploader.state().get('selection').first().toJSON(); 	
				$("#" + wrapperid + " [type=text]").val(attachment.url);
				$("#" + wrapperid + " .open-media-preview").html('<img class="wizardpanel-field-imagepreview" src="'+ attachment.url +'" />');
				$("."+wrapperid).unwrap();
                $('.wizardpanel-field').removeClass(wrapperid);
				//removeimagepreview();
			});
			
			mediaUploader.on('close', function () {
			    
			});
        	
			mediaUploader.open();
			
		});
		
		var removeimagepreview = function () {
    		$(".open-media-preview span").click( function () {
    			$(this).parent().parent().find('[type=text]').val(""); 
    			$(this).parent().html("");
    		});
    	}
	    
	    removeimagepreview();
	    
		return this;
	}
}(jQuery));


jQuery('document').ready(function () {
	jQuery(".open-media").uploadMedia();
});

// Tab Links

jQuery('document').ready(function () {

	jQuery(".wizardpanel-header-right .tablinks a").click(function () {
	    jQuery('.wizardpanel .wizardpanel-content-left li').removeClass('active');
	    jQuery('.wizardpanel div.admincontent').hide(); // Hide all divs
	    var currentTab = jQuery(this).attr('href');
	    jQuery(currentTab).show();
	    jQuery('#z' + currentTab.substring(1)).addClass('active');
	    return false;
	});

});

// Notice

(function($) {
    
	$.fn.wizardpanelnotice = function ( options ) {
	    
		var defaults = { 
		    dayshidden: 1
		},
		settings = $.extend(defaults, options);
		var obj = jQuery(this);
		
		jQuery("#"+obj.attr("id") +" .wizardpanel-highlightbox-close a").click(function (e) {
		    if (localStorage.getItem('wizardpanelnotice') === null ) {
		      localStorage.setItem("wizardpanelnotice", noticedate);  
		      obj.remove();
		    }
		    e.preventDefault();
		});
		
		var checkNotice = function () {
		    
		    if (localStorage.getItem('wizardpanelnotice') !== null) {
		        if( days() <= settings.dayshidden ) {
                    obj.remove();
		        } else {
		            obj.removeAttr("style");
		            localStorage.removeItem('wizardpanelnotice')
		        }
		    } else {
		        obj.removeAttr("style");
		    }
		    
		}
		
		var days = function() {
		    
            const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
            const firstDate = new Date(noticedate);
            const secondDate = new Date(localStorage.getItem('wizardpanelnotice'));
            const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));
            return diffDays;
        }
		
		checkNotice();
		
		return this;
	}
}(jQuery));


jQuery('document').ready(function () {
    
	jQuery("#wizardpanelnotice-review").wizardpanelnotice({ dayshidden: 5 });
	
});