//switcher
/////////////////////////////////////////////
//DELETE FOLLOWING CODE TO DISABLE SWITCHER//
/////////////////////////////////////////////
jQuery(document).ready(function(){

window.patternClasses = [];

var deleteAllCookiesHTML = '';
if (jQuery.cookie) {
	if(!jQuery.isEmptyObject( jQuery.cookie() )) {
		deleteAllCookiesHTML = '<span class="rt-icon2-delete-outline" data-toggle="tooltip" data-placement="top" title="Clear All Styles (Page will be reloaded)"></span>';
	}
}
		
var switcherHTML = '<div id="switcher">';
		switcherHTML +=    '<div class="color_switcher_header"><h4>Styles Selector</h4>';
		switcherHTML +=    		'<span class="rt-icon2-brush"></span>';
		switcherHTML +=    		deleteAllCookiesHTML;
		switcherHTML +=    '</div>';
		// switcherHTML +=    '<div id="switcher_accent_color">';
		// switcherHTML +=    		'<p>Accent color:</p>';
		// switcherHTML +=    		'<ul id="switcher-colors" class="list-inline">';
		// switcherHTML +=        		'<li>';
		// switcherHTML +=            		'<a href="#" data-color="" class="color1"></a>';
		// switcherHTML +=        		'</li>';
		// switcherHTML +=        		'<li>';
		// switcherHTML +=            		'<a href="#" data-color="2" class="color2"></a>';
		// switcherHTML +=        		'</li>';
		// switcherHTML +=        		'<li>';
		// switcherHTML +=            		'<a href="#" data-color="3" class="color3"></a>';
		// switcherHTML +=        		'</li>';
		// switcherHTML +=    		'</ul>';
		// switcherHTML +=    	'</div>';
		switcherHTML +=    '<div id="switcher_color_scheme">';
		switcherHTML +=    		'<p>Color scheme:</p>';
		switcherHTML +=    		'<ul id="switcher-version" class="list-inline">';
		switcherHTML +=        		'<li class="active">';
		switcherHTML +=            		'<a href="#" class="light">Light</a>';
		switcherHTML +=        		'</li>';
		switcherHTML +=        		'<li>';
		switcherHTML +=            		'<a href="#" class="dark">Dark</a>';
		switcherHTML +=        		'</li>';
		switcherHTML +=    		'</ul>';
		switcherHTML +=    '</div>';
		switcherHTML +=    '<div id="switcher_layout">';
		switcherHTML +=    		'<p>Layout style:</p>';
		switcherHTML +=    		'<div class="checkbox checkbox-slider--b-flat">';
		switcherHTML +=    		    '<label>';
		switcherHTML +=    		        '<input type="checkbox" id="layout"> <span>Boxed</span>';
		switcherHTML +=    		    '</label>';
		switcherHTML +=    		'</div>';
		switcherHTML +=    		'<div class="checkbox checkbox-slider--b-flat">';
		switcherHTML +=    		    '<label id="boxed_margin" class="hidden">';
		switcherHTML +=    		        '<input type="checkbox"> <span>Vertical Margins</span>';
		switcherHTML +=    		    '</label>';		
		switcherHTML +=    		'</div>';
		switcherHTML +=		'</div>';
		switcherHTML +=    '<div id="switcher_patterns">';
		switcherHTML +=    		'<p class="for-toggle hidden">Boxed Patterns</p>';
		switcherHTML +=    		'<ul id="switcher-patterns" class="list-inline for-toggle hidden">';

		for (var i = 0; i < 12; i++) {
			switcherHTML +=    		    '<li>';
			switcherHTML +=    		        '<a href="#" class="pattern' + i + '" data-pattern="pattern' + i + '">';
			switcherHTML +=    		            '<img src="img/pattern' + i + '.png" alt="" width="40" height="40" title="pattern' + i + '">';
			switcherHTML +=    		        '</a>';
			switcherHTML +=    		    '</li>';
        	//available patterns
        	window.patternClasses.push('pattern' + i);
		};

		switcherHTML +=    		'</ul>';
		switcherHTML +=    '</div>';
		switcherHTML +='</div>';

		jQuery('body').append(switcherHTML);

		//switcher toggle
        jQuery('#switcher span[class="rt-icon2-brush"]').on('click', function(){
            jQuery('#switcher').toggleClass('active');
        });
        jQuery('body').on('click', function( e ) {
			if ( !jQuery(e.target).closest('#switcher').length ) {
				jQuery('#switcher').removeClass('active');
			}
		});
		
		//switcher reset all styles
        jQuery('#switcher span[class="rt-icon2-delete-outline"]').on('click', function(){
            cookieClass.deleteAllCookies();
        });


        //boxed or wide
        jQuery('#layout').on('click', {patterns: patternClasses}, switcherClass.switchBoxedWide);

        //boxed with top and bottom margins (enable only whet "boxed" is active)
        jQuery('#boxed_margin').find("input").on('change', switcherClass.switchBoxedMargins);
        
        //pattern switcher
        jQuery('#switcher-patterns a').on('click', {patterns: patternClasses}, function( e ) {
			switcherClass.switchPatterns( e, jQuery(this).data('pattern'));
        });

        //color switcher
        jQuery('#switcher-colors a').on('click', switcherClass.switchColorScheme);

		//version switcher
        jQuery('#switcher-version a').on('click', switcherClass.switchDarkLight);


        //if cookie set - changing color scheme
        if (cookieClass.returnTrueOrFalseCookie('colorScheme')) {
        	jQuery('#switcher-colors a[data-color="' + cookieClass.getCookieValue('colorScheme') + '"]').trigger('click');
        }


        //if cookie set - changing to dark version
        if (cookieClass.returnTrueOrFalseCookie('dark')) {
        	cookieClass.toggleTrueOrFalseCookie('dark');
        	jQuery('#switcher-version .dark').trigger('click');
        }


        //if cookies set - changing layout
        if (cookieClass.returnTrueOrFalseCookie('boxed')) {
        	if (cookieClass.returnTrueOrFalseCookie('topBottomMargins')) {
        		var topBottomMarginsCookie = cookieClass.getCookieValue('topBottomMargins');
        	}
        	if (cookieClass.returnTrueOrFalseCookie('pattern')) {
        		var patternCookie = cookieClass.getCookieValue('pattern');
        	}
        	switcherClass.switchBoxedWide();
        	cookieClass.toggleTrueOrFalseCookie("boxed");
        	jQuery('#layout').prop('checked', true);

        	if (topBottomMarginsCookie) {
        		jQuery('#boxed_margin').find('input').prop('checked', true).trigger('change');
        	}
        	if (patternCookie) {
        		jQuery('#switcher-patterns').find('a[data-pattern="' + patternCookie + '"]').trigger('click');
        	}
        	
        }
});

var cookieClass = {
	deleteAllCookies: function () {
		if (jQuery.cookie) {
			jQuery.each(jQuery.cookie(), function(key, value) {
				jQuery.removeCookie(key);
				location.reload();
			});
		} else {
			return false;
		}
	},
	deleteCookie: function (cookieName) {
		if (jQuery.cookie) {
			jQuery.removeCookie(cookieName);
		} else {
			return false;
		}
	},
	setCookieValue: function (cookieName, cookieValue) {
		if (jQuery.cookie) {
			jQuery.cookie(cookieName, cookieValue);
		} else {
			return false;
		}
	},
	getCookieValue: function (cookieName) {
		if (jQuery.cookie) {
			return jQuery.cookie(cookieName);
		} else {
			return false;
		}
	},
	toggleTrueOrFalseCookie: function(cookieName) {
		if (jQuery.cookie) {
			if(!jQuery.cookie(cookieName) || jQuery.cookie(cookieName) == '0') {
				jQuery.cookie(cookieName, '1');
				return true;
				
			} else {
				jQuery.cookie(cookieName, '0');
				return true;
			}
		} else {
			return false;
		}
		
	},
	returnTrueOrFalseCookie: function( cookieName ) {
		if (jQuery.cookie) {
			cookieName = jQuery.cookie( cookieName );
			switch ( cookieName ) {
				case '0':
					return false;
					break;
				case 0:
					return false;
					break;
				case false:
					return false;
					break;
				case undefined:
					return false;
					break;
				case null:
					return false;
					break;
				default:
					return true;
					break;
			}
		} else {
			return false;
		}
	},
	setCookieToFalse: function(cookieName) {
		if (jQuery.cookie) {
				jQuery.cookie(cookieName, '0');
		} else {
			return false;
		}
		
	}
}

var switcherClass = {
	switchBoxedWide : function  ( e ) {
		//wide boxed changing
	    jQuery("#boxed_margin").toggleClass("hidden").find("input").prop("checked", false);
	    jQuery(".for-toggle").toggleClass("hidden");
	    jQuery("#canvas").toggleClass("boxed").removeClass(patternClasses.join(' '));;
	    jQuery("#box_wrapper").toggleClass("container").removeClass('top-bottom-margins');
	    jQuery(".page_header_wrapper").attr('style', '');
	    if (jQuery().isotope) {
	    	var $isotopeContainers = jQuery('#isotopeContainer, .isotope_container');
	    	if ($isotopeContainers.length) {
	    		jQuery("#isotopeContainer, .isotope_container").isotope("reLayout");
	    	}
	    }
	    jQuery(window).trigger("resize");
	    cookieClass.toggleTrueOrFalseCookie("boxed");
	    cookieClass.setCookieToFalse("topBottomMargins");
	    cookieClass.setCookieToFalse("pattern");

	},
	switchBoxedMargins : function  ( e ) {
		if(jQuery('#layout').prop('checked')) {
		    jQuery('#box_wrapper').toggleClass('top-bottom-margins');
		    cookieClass.toggleTrueOrFalseCookie('topBottomMargins');
		}
	},
	switchPatterns: function  ( e, newPattern ) {
	    e.preventDefault();
	    e.stopPropagation();
	    jQuery('#canvas').removeClass(e.data.patterns.join(' '));
	    jQuery('#canvas').addClass(newPattern);
	    cookieClass.setCookieValue('pattern', newPattern);
	},

	switchColorScheme: function ( e ) {
	    e.preventDefault();
	    e.stopPropagation();
	    var color = jQuery(this).data('color');
	    jQuery('.color-switcher-link').each(function(){
	    	var $thisLink = jQuery(this);
	    	var newHref = $thisLink.attr('href').replace(/[1-9]*(\.css)/, color + '.css');
	    	$thisLink.attr('href', newHref);
	    });
	    cookieClass.setCookieValue('colorScheme', color);
	},

	switchDarkLight: function ( e ) {
	    e.preventDefault();
	    e.stopPropagation();
	    if (jQuery(this).parent().hasClass("active")) {
	    	return;
	    }
	    jQuery("#switcher-version").find("li").toggleClass("active");

		jQuery("#box_wrapper > *").each(function() {
    		//sections to ignore light-dark switch 
    		var $thisSection = jQuery(this);
			if (
				   $thisSection.hasClass('page_header_side') 
				|| $thisSection.hasClass('texture_bg') 
				|| $thisSection.hasClass('page_footer') 
				|| $thisSection.hasClass('page_contacts') 
				|| $thisSection.hasClass('page_subscribe') 
				|| $thisSection.hasClass('page_partners_carousel') 
				|| $thisSection.hasClass('page_breadcrumbs') 
				|| $thisSection.hasClass('page_copyright') 
				|| $thisSection.hasClass('intro_section') 
				|| $thisSection.hasClass("gradient")
				|| $thisSection.hasClass("section_bottom_overlap")
				|| $thisSection.hasClass("background_cover")
				|| $thisSection.hasClass("parallax")
				|| $thisSection.hasClass("cs")
				|| ($thisSection.attr("id") == "featured")
			) {
				return;
			}
			
			if($thisSection.hasClass("ls")) {
				$thisSection.toggleClass("ls ds");
			} else if($thisSection.hasClass("ds")) {
				$thisSection.toggleClass("ds ls");
			}


		});

		jQuery('.page_mainslider').each(function() {
			var $curSlider = jQuery(this);
			$curSlider.toggleClass('light_md_bg_color');
		})


		jQuery(".page_header, .page_header_wrapper").each(function() {
			//sections to ignore light-dark switch
    		var $thisHeader = jQuery(this);
			if (
				//for dashboard
				$thisHeader.hasClass('header_darkblue') ||  $thisHeader.hasClass('background_cover') || $thisHeader.hasClass('header_transparent_wrap')
			) {
				return;
			}
			if($thisHeader.hasClass("header_white")) {
				$thisHeader.toggleClass("header_white header_darkgrey");
			} else if($thisHeader.hasClass("header_darkgrey")) {
				$thisHeader.toggleClass("header_white header_darkgrey");
			}
		});

	    cookieClass.toggleTrueOrFalseCookie('dark');
	}
}