/* global jQuery, document, redux, redux.args, confirm, relid:true, console, jsonView */

(function ( $ ) {
	'use strict';

	$.redux = $.redux || {};

	$( document ).ready(
		function () {
			$.fn.isOnScreen = function () {
				if ( !window ) {
					return;
				}

				var win = $( window );
				var viewport = {
					top : win.scrollTop(),
					left: win.scrollLeft()
				};

				viewport.right = viewport.left + win.width();
				viewport.bottom = viewport.top + win.height();

				var bounds = this.offset();

				bounds.right = bounds.left + this.outerWidth();
				bounds.bottom = bounds.top + this.outerHeight();

				return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
			};

			$.redux.checkRequired();
			$.redux.initEvents();
			$.redux.initQtip();
			$.redux.tabCheck();
			$.redux.notices();
			$.redux.tabControl();
			$.redux.devFunctions();
		}
	);

	$.redux.initEvents = function () {
		$( '.redux-action_bar, .redux-presets-bar' ).on(
			'click', function () {
				window.onbeforeunload = null;
			}
		);

		$( '#toplevel_page_' + redux.params.slug + ' .wp-submenu a, #wp-admin-bar-' + redux.params.slug + ' a.ab-item' ).click(
			function ( e ) {
				if ( $( '#toplevel_page_' + redux.params.slug ).hasClass( 'wp-menu-open' ) || $( this ).hasClass( 'ab-item' ) ) {
					e.preventDefault();

					var url = $( this ).attr( 'href' ).split( '&tab=' );
					$( '#' + url[1] + '_section_group_li_a' ).click();
					return false;
				}
			}
		);


		// Default button clicked
		$( 'input[name="' + redux.params.opt_name + '[defaults]"]' ).click(
			function () {
				if ( !confirm( redux.params.reset_confirm ) ) {
					return false;
				}
				window.onbeforeunload = null;
			}
		);

		// Default button clicked
		$( 'input[name="' + redux.params.opt_name + '[defaults-section]"]' ).click(
			function () {
				if ( !confirm( redux.params.reset_section_confirm ) ) {
					return false;
				}

				window.onbeforeunload = null;
			}
		);

		$( '.expand_options' ).click(
			function ( e ) {
				e.preventDefault();

				$.redux.expandOptions( $( this ).parents( '.redux-container:first' ) );
				return false;
			}
		);

		if ( $( '.saved_notice' ).is( ':visible' ) ) {
			$( '.saved_notice' ).slideDown();
		}

		$( document.body ).on(
			'change', '.redux-field input, .redux-field textarea, .redux-field select', function () {
				if ( !$( this ).hasClass( 'noUpdate' ) ) {
					redux_change( $( this ) );
				}
			}
		);

		var stickyHeight = $( '#redux-footer' ).height();

		$( '#redux-sticky-padder' ).css( {
			height: stickyHeight
		} );

		if ( $( '#redux-footer' ).length !== 0 ) {
			$( window ).scroll(
				function () {
					$.redux.stickyInfo();
				}
			);

			$( window ).resize(
				function () {
					$.redux.stickyInfo();
				}
			);
		}

		$( '.saved_notice' ).delay( 4000 ).slideUp();

		$( '.redux-save' ).click(
			function () {
				window.onbeforeunload = null;
			}
		);

	};

	$.redux.checkRequired = function () {
		$.redux.required();

		$( "body" ).on(
			'change',
			'.redux-main select, .redux-main radio, .redux-main input[type=checkbox], .redux-main input[type=hidden]',
			function ( e ) {
				$.redux.check_dependencies( this );
			}
		).on( 'check_dependencies', function ( e, variable ) {
				$.redux.check_dependencies( variable );
			} );

		$( 'td > fieldset:empty,td > div:empty' ).parent().parent().hide();
	};

	$.redux.initQtip = function () {
		if ( $().qtip ) {
			// Shadow
			var shadow = '';
			var tip_shadow = redux.params.hints.tip_style.shadow;

			if ( tip_shadow === true ) {
				shadow = 'qtip-shadow';
			}

			// Color
			var color = '';
			var tip_color = redux.params.hints.tip_style.color;

			if ( tip_color !== '' ) {
				color = 'qtip-' + tip_color;
			}

			// Rounded
			var rounded = '';
			var tip_rounded = redux.params.hints.tip_style.rounded;

			if ( tip_rounded === true ) {
				rounded = 'qtip-rounded';
			}

			// Tip style
			var style = '';
			var tip_style = redux.params.hints.tip_style.style;

			if ( tip_style !== '' ) {
				style = 'qtip-' + tip_style;
			}

			var classes = shadow + ',' + color + ',' + rounded + ',' + style;
			classes = classes.replace( /,/g, ' ' );

			// Get position data
			var myPos = redux.params.hints.tip_position.my;
			var atPos = redux.params.hints.tip_position.at;

			// Gotta be lowercase, and in proper format
			myPos = $.redux.verifyPos( myPos.toLowerCase(), true );
			atPos = $.redux.verifyPos( atPos.toLowerCase(), false );

			// Tooltip trigger action
			var showEvent = redux.params.hints.tip_effect.show.event;
			var hideEvent = redux.params.hints.tip_effect.hide.event;

			// Tip show effect
			var tipShowEffect = redux.params.hints.tip_effect.show.effect;
			var tipShowDuration = redux.params.hints.tip_effect.show.duration;

			// Tip hide effect
			var tipHideEffect = redux.params.hints.tip_effect.hide.effect;
			var tipHideDuration = redux.params.hints.tip_effect.hide.duration;

			$( 'div.redux-hint-qtip' ).each(
				function () {
					$( this ).qtip( {
						content : {
							text : $( this ).attr( 'qtip-content' ),
							title: $( this ).attr( 'qtip-title' )
						},
						show    : {
							effect: function () {
								switch ( tipShowEffect ) {
									case 'slide':
										$( this ).slideDown( tipShowDuration );
										break;
									case 'fade':
										$( this ).fadeIn( tipShowDuration );
										break;
									default:
										$( this ).show();
										break;
								}
							},
							event : showEvent
						},
						hide    : {
							effect: function () {
								switch ( tipHideEffect ) {
									case 'slide':
										$( this ).slideUp( tipHideDuration );
										break;
									case 'fade':
										$( this ).fadeOut( tipHideDuration );
										break;
									default:
										$( this ).show( tipHideDuration );
										break;
								}
							},
							event : hideEvent
						},
						style   : {
							classes: classes
						},
						position: {
							my: myPos,
							at: atPos
						}
					} );
				}
			);
			// });

			$( 'input[qtip-content]' ).each(
				function () {
					$( this ).qtip( {
						content : {
							text : $( this ).attr( 'qtip-content' ),
							title: $( this ).attr( 'qtip-title' )
						},
						show    : 'focus',
						hide    : 'blur',
						style   : classes,
						position: {
							my: myPos,
							at: atPos
						}
					} );
				}
			);
		}
	};

	$.redux.tabCheck = function () {
		$( '.redux-group-tab-link-a' ).click(
			function () {
				var el = $( this ).parents( '.redux-container:first' );
				var relid = $( this ).data( 'rel' ); // The group ID of interest
				var oldid = el.find( '.redux-group-tab-link-li.active .redux-group-tab-link-a' ).data( 'rel' );

				if ( oldid === relid ) {
					return;
				}

				$( '#currentSection' ).val( relid );

				if ( el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).length ) {
					var parentID = el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).attr( 'id' ).split( '_' );
					parentID = parentID[0];
				}

				el.find( '#toplevel_page_' + redux.params.slug + ' .wp-submenu a.current' ).removeClass( 'current' );
				el.find( '#toplevel_page_' + redux.params.slug + ' .wp-submenu li.current' ).removeClass( 'current' );

				el.find( '#toplevel_page_' + redux.params.slug + ' .wp-submenu a' ).each(
					function () {
						var url = $( this ).attr( 'href' ).split( '&tab=' );
						if ( url[1] == relid || url[1] == parentID ) {
							$( this ).addClass( 'current' );
							$( this ).parent().addClass( 'current' );
						}
					}
				);

				if ( el.find( '#' + oldid + '_section_group_li' ).find( '#' + oldid + '_section_group_li' ).length ) {
					//console.log('RELID is child of oldid');
					el.find( '#' + oldid + '_section_group_li' ).addClass( 'activeChild' );
					el.find( '#' + relid + '_section_group_li' ).addClass( 'active' ).removeClass( 'activeChild' );
				} else if ( el.find( '#' + relid + '_section_group_li' ).parents( '#' + oldid + '_section_group_li' ).length || el.find( '#' + oldid + '_section_group_li' ).parents( 'ul.subsection' ).find( '#' + relid + '_section_group_li' ).length ) {
					//console.log('RELID is sibling or child of OLDID');
					if ( el.find( '#' + relid + '_section_group_li' ).parents( '#' + oldid + '_section_group_li' ).length ) {
						//console.log('child of oldid');
						el.find( '#' + oldid + '_section_group_li' ).addClass( 'activeChild' ).removeClass( 'active' );
					} else {
						//console.log('sibling');
						el.find( '#' + relid + '_section_group_li' ).addClass( 'active' );
						el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' );
					}
					el.find( '#' + relid + '_section_group_li' ).removeClass( 'activeChild' ).addClass( 'active' );
				} else {
					el.find( '#' + relid + '_section_group_li' ).addClass( 'active' ).removeClass( 'activeChild' ).find( 'ul.subsection' ).slideDown();

					if ( el.find( '#' + oldid + '_section_group_li' ).find( 'ul.subsection' ).length ) {
						//console.log('oldid is parent')
						el.find( '#' + oldid + '_section_group_li' ).find( 'ul.subsection' ).slideUp(
							'fast', function () {
								el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' ).removeClass( 'activeChild' );
							}
						);
					} else if ( el.find( '#' + oldid + '_section_group_li' ).parents( 'ul.subsection' ).length ) {
						//console.log('oldid is a child');
						if ( !el.find( '#' + oldid + '_section_group_li' ).parents( '#' + relid + '_section_group_li' ).length ) {
							//console.log('oldid is child, but not of relid');
							el.find( '#' + oldid + '_section_group_li' ).parents( 'ul.subsection' ).slideUp(
								'fast', function () {
									el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' );
									el.find( '#' + oldid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).removeClass( 'active' ).removeClass( 'activeChild' );
								}
							);
						} else {
							el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' );
						}
					} else {
						//console.log('Normal remove active from child');
						el.find( '#' + oldid + '_section_group_li' ).removeClass( 'active' );
						if ( el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).length ) {
							//console.log('here');
							el.find( '#' + relid + '_section_group_li' ).parents( '.redux-group-tab-link-li' ).addClass( 'activeChild' ).find( 'ul.subsection' ).slideDown();
							el.find( '#' + relid + '_section_group_li' ).addClass( 'active' );
						}
					}
				}

				// Show the group
				el.find( '#' + oldid + '_section_group' ).hide();

				el.find( '#' + relid + '_section_group' ).fadeIn(
					200, function () {
						if ( el.find( '#redux-footer' ).length !== 0 ) {
							$.redux.stickyInfo(); // race condition fix
						}
						$.redux.initFields();
					}
				);
			}
		);

		var tab = decodeURI( (new RegExp( 'tab' + '=' + '(.+?)(&|$)' ).exec( location.search ) || [, ''])[1] );

		if ( tab !== "" ) {
			$( '#' + tab + '_section_group_li' ).click();
		}
//		else if ( $.cookie( 'redux_current_tab_get' ) !== "" ) {
//			$.removeCookie( 'redux_current_tab_get' );
//		}

		var sTab = $( '#' + tab + '_section_group_li_a' );

		// Tab the first item or the saved one
		if ( sTab.length === 0 ) {
			$( '.redux-container' ).find( '.redux-group-tab-link-a:first' ).click();
		} else {
			sTab.click();
		}

	};

	$.redux.initFields = function () {
		$( ".redux-field-init:visible" ).each( function () {
			var type = $( this ).attr( 'data-type' );
			//console.log(type);
			if ( redux.field_objects[type] ) {
				redux.field_objects[type].init();
			}
		} );
	};

	$.redux.notices = function () {
		if ( redux.errors !== undefined ) {
			$.each(
				redux.errors.errors, function ( sectionID, sectionArray ) {
					$.each(
						sectionArray.errors, function ( key, value ) {
							$( "#" + redux.params.opt_name + '-' + value.id ).addClass( "redux-field-error" );
							if ( $( "#" + redux.params.opt_name + '-' + value.id ).parent().find( '.redux-th-error' ).length === 0 ) {
								$( "#" + redux.params.opt_name + '-' + value.id ).append( '<div class="redux-th-error">' + value.msg + '</div>' );
							}
						}
					);
				}
			);

			$( '.redux-container' ).each(
				function () {
					var container = $( this );
					var totalErrors = container.find( '.redux-field-error' ).length;
					if ( totalErrors > 0 ) {
						container.find( ".redux-field-errors span" ).text( totalErrors );
						container.find( ".redux-field-errors" ).slideDown();
						container.find( '.redux-group-tab' ).each(
							function () {
								var total = $( this ).find( '.redux-field-error' ).length;
								if ( total > 0 ) {
									var sectionID = $( this ).attr( 'id' ).split( '_' );
									sectionID = sectionID[0];
									container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).prepend( '<span class="redux-menu-error">' + total + '</span>' );
									container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).addClass( "hasError" );
									var subParent = container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).parents( '.hasSubSections:first' );
									if ( subParent ) {
										subParent.find( '.redux-group-tab-link-a:first' ).addClass( 'hasError' );
									}
								}
							}
						);
					}
					var totalWarnings = container.find( '.redux-field-warning' ).length;
					if ( totalWarnings > 0 ) {
						container.find( ".redux-field-warnings span" ).text( totalWarnings );
						container.find( ".redux-field-warnings" ).slideDown();
						container.find( '.redux-group-tab' ).each(
							function () {
								var warning = $( this ).find( '.redux-field-warning' ).length;
								if ( warning > 0 ) {
									var sectionID = $( this ).attr( 'id' ).split( '_' );
									sectionID = sectionID[0];
									container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).prepend( '<span class="redux-menu-warning">' + total + '</span>' );
									container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).addClass( "hasWarning" );
									var subParent = container.find( '.redux-group-tab-link-a[data-key="' + sectionID + '"]' ).parents( '.hasSubSections:first' );
									if ( subParent ) {
										subParent.find( '.redux-group-tab-link-a:first' ).addClass( 'hasWarning' );
									}
								}
							}
						);
					}
				}
			);
		}
	};

	$.redux.tabControl = function () {
		$( '.redux-section-tabs div' ).hide();
		$( '.redux-section-tabs div:first' ).show();
		$( '.redux-section-tabs ul li:first' ).addClass( 'active' );

		$( '.redux-section-tabs ul li a' ).click(
			function () {
				$( '.redux-section-tabs ul li' ).removeClass( 'active' );
				$( this ).parent().addClass( 'active' );

				var currentTab = $( this ).attr( 'href' );

				$( '.redux-section-tabs div' ).hide();
				$( currentTab ).fadeIn( 'medium', function () {
					$.redux.initFields();
				} );

				return false;
			}
		);
	};

	$.redux.devFunctions = function () {
		$( '#consolePrintObject' ).on(
			'click', function ( e ) {
				e.preventDefault();
				console.log( $.parseJSON( $( "#redux-object-json" ).html() ) );
			}
		);

		if ( typeof jsonView === 'function' ) {
			jsonView( '#redux-object-json', '#redux-object-browser' );
		}
	};

	$.redux.required = function () {

		// Hide the fold elements on load ,
		// It's better to do this by PHP but there is no filter in tr tag , so is not possible
		// we going to move each attributes we may need for folding to tr tag
		$.each(
			redux.folds, function ( i, v ) {
				var fieldset = $( '#' + redux.params.opt_name + '-' + i );

				fieldset.parents( 'tr:first' ).addClass( 'fold' );

				if ( v == "hide" ) {
					fieldset.parents( 'tr:first' ).addClass( 'hide' );

					if ( fieldset.hasClass( 'redux-container-section' ) ) {
						var div = $( '#section-' + i );

						if ( div.hasClass( 'redux-section-indent-start' ) ) {
							$( '#section-table-' + i ).hide().addClass( 'hide' );
							div.hide().addClass( 'hide' );
						}
					}

					if ( fieldset.hasClass( 'redux-container-info' ) ) {
						$( '#info-' + i ).hide().addClass( 'hide' );
					}

					if ( fieldset.hasClass( 'redux-container-divide' ) ) {
						$( '#divide-' + i ).hide().addClass( 'hide' );
					}

					if ( fieldset.hasClass( 'redux-container-raw' ) ) {
						var rawTable = fieldset.parents().find( '.redux-raw-table' );
						rawTable.hide().addClass( 'hide' );
					}
				}
			}
		);
	};

	$.redux.get_container_value = function ( id ) {
		var value = $( '#' + redux.params.opt_name + '-' + id ).serializeForm();

		if ( value !== null && typeof value === 'object' && value.hasOwnProperty( redux.params.opt_name ) ) {
			//console.log('object');
			//console.log(value);
			value = value[redux.params.opt_name][id];
		}
		//console.log(value);
		return value;
	};

	$.redux.check_dependencies = function ( variable ) {
		if ( redux.required === null ) {
			return;
		}
		var current = $( variable ),
		    id = current.parents( '.redux-field:first' ).data( 'id' );

		if ( !redux.required.hasOwnProperty( id ) ) {
			return;
		}

		var container = current.parents( '.redux-field-container:first' ),
		    is_hidden = container.parents( 'tr:first' ).hasClass( '.hide' ),
		    hadSections = false;

		$.each(
			redux.required[id], function ( child, dependents ) {

				var current = $( this ),
				    show = false,
				    childFieldset = $( '#' + redux.params.opt_name + '-' + child ),
				    tr = childFieldset.parents( 'tr:first' );

				if ( !is_hidden ) {
					show = $.redux.check_parents_dependencies( child );
				}

				if ( show === true ) {
					// Shim for sections
					if ( childFieldset.hasClass( 'redux-container-section' ) ) {
						var div = $( '#section-' + child );

						if ( div.hasClass( 'redux-section-indent-start' ) && div.hasClass( 'hide' ) ) {
							$( '#section-table-' + child ).fadeIn( 300 ).removeClass( 'hide' );
							div.fadeIn( 300 ).removeClass( 'hide' );
						}
					}

					if ( childFieldset.hasClass( 'redux-container-info' ) ) {
						$( '#info-' + child ).fadeIn( 300 ).removeClass( 'hide' );
					}

					if ( childFieldset.hasClass( 'redux-container-divide' ) ) {
						$( '#divide-' + child ).fadeIn( 300 ).removeClass( 'hide' );
					}

					if ( childFieldset.hasClass( 'redux-container-raw' ) ) {
						var rawTable = childFieldset.parents().find( '.redux-raw-table' );
						rawTable.fadeIn( 300 ).removeClass( 'hide' );
					}

					tr.fadeIn(
						300, function () {
							$( this ).removeClass( 'hide' );
							if ( redux.required.hasOwnProperty( child ) ) {
								$.redux.check_dependencies( $( '#' + redux.params.opt_name + '-' + child ).children().first() );
							}
							$.redux.initFields();
						}
					);
					if ( childFieldset.hasClass( 'redux-container-section' ) || childFieldset.hasClass( 'redux-container-info' ) ) {
						tr.css( {display: 'none'} );
					}
				} else if ( show === false ) {
					tr.fadeOut(
						100, function () {
							$( this ).addClass( 'hide' );
							if ( redux.required.hasOwnProperty( child ) ) {
								//console.log('Now check, reverse: '+child);
								$.redux.required_recursive_hide( child );
							}
						}
					);
				}

				current.find( 'select, radio, input[type=checkbox]' ).trigger( 'change' );
			}
		);
	};

	$.redux.required_recursive_hide = function ( id ) {
		var toFade = $( '#' + redux.params.opt_name + '-' + id ).parents( 'tr:first' );

		toFade.fadeOut(
			50, function () {
				$( this ).addClass( 'hide' );

				if ( $( '#' + redux.params.opt_name + '-' + id ).hasClass( 'redux-container-section' ) ) {
					var div = $( '#section-' + id );
					if ( div.hasClass( 'redux-section-indent-start' ) ) {
						$( '#section-table-' + id ).fadeOut( 50 ).addClass( 'hide' );
						div.fadeOut( 50 ).addClass( 'hide' );
					}
				}

				if ( $( '#' + redux.params.opt_name + '-' + id ).hasClass( 'redux-container-info' ) ) {
					$( '#info-' + id ).fadeOut( 50 ).addClass( 'hide' );
				}

				if ( $( '#' + redux.params.opt_name + '-' + id ).hasClass( 'redux-container-divide' ) ) {
					$( '#divide-' + id ).fadeOut( 50 ).addClass( 'hide' );
				}

				if ( $( '#' + redux.params.opt_name + '-' + id ).hasClass( 'redux-container-raw' ) ) {
					var rawTable = $( '#' + redux.params.opt_name + '-' + id ).parents().find( '.redux-raw-table' );

					rawTable.fadeOut( 50 ).addClass( 'hide' );
				}

				if ( redux.required.hasOwnProperty( id ) ) {
					$.each(
						redux.required[id], function ( child ) {
							$.redux.required_recursive_hide( child );
						}
					);
				}
			}
		);
	};

	$.redux.check_parents_dependencies = function ( id ) {
		var show = "";

		if ( redux.required_child.hasOwnProperty( id ) ) {
			$.each(
				redux.required_child[id], function ( i, parentData ) {
					if ( $( '#' + redux.params.opt_name + '-' + parentData.parent ).parents( 'tr:first' ).hasClass( '.hide' ) ) {
						show = false;
					} else {
						if ( show !== false ) {
							var parentValue = $.redux.get_container_value( parentData.parent );
							show = $.redux.check_dependencies_visibility( parentValue, parentData );
						}
					}
				}
			);
		} else {
			show = true;
		}
		return show;
	};

	$.redux.check_dependencies_visibility = function ( parentValue, data ) {
		var show = false,
		    checkValue_array,
		    checkValue = data.checkValue,
		    operation = data.operation;

		switch ( operation ) {
			case '=':
			case 'equals':
				//if value was array
				if ( $.isArray( checkValue ) ) {
					if ( $.inArray( parentValue, checkValue ) != -1 ) {
						show = true;
					}
				} else {
					if ( parentValue == checkValue ) {
						show = true;
					} else if ( $.isArray( parentValue ) ) {
						if ( $.inArray( checkValue, parentValue ) != -1 ) {
							show = true;
						}
					}
				}
				break;
			case '!=':
			case 'not':
				//if value was array
				if ( $.isArray( checkValue ) ) {
					//if (checkValue.toString().indexOf('|') !== -1) {
					//    checkValue_array = checkValue.split('|');
					if ( $.inArray( parentValue, checkValue ) == -1 ) {
						show = true;
					}
				} else {
					if ( parentValue != checkValue ) {
						show = true;
					} else if ( $.isArray( parentValue ) ) {
						if ( $.inArray( checkValue, parentValue ) == -1 ) {
							show = true;
						}
					}
				}
				break;
			case '>':
			case 'greater':
			case 'is_larger':
				if ( parseFloat( parentValue ) > parseFloat( checkValue ) ) {
					show = true;
				}
				break;
			case '>=':
			case 'greater_equal':
			case 'is_larger_equal':
				if ( parseFloat( parentValue ) >= parseFloat( checkValue ) ) {
					show = true;
				}
				break;
			case '<':
			case 'less':
			case 'is_smaller':
				if ( parseFloat( parentValue ) < parseFloat( checkValue ) ) {
					show = true;
				}
				break;
			case '<=':
			case 'less_equal':
			case 'is_smaller_equal':
				if ( parseFloat( parentValue ) <= parseFloat( checkValue ) ) {
					show = true;
				}
				break;
			case 'contains':
				if ( parentValue.toString().indexOf( checkValue ) != -1 ) {
					show = true;
				}
				break;
			case 'doesnt_contain':
			case 'not_contain':
				if ( parentValue.toString().indexOf( checkValue ) == -1 ) {
					show = true;
				}
				break;
			case 'is_empty_or':
				if ( parentValue === "" || parentValue == checkValue ) {
					show = true;
				}
				break;
			case 'not_empty_and':
				if ( parentValue !== "" && parentValue != checkValue ) {
					show = true;
				}
				break;
			case 'is_empty':
			case 'empty':
			case '!isset':
				if ( !parentValue || parentValue === "" || parentValue === null ) {
					show = true;
				}
				break;
			case 'not_empty':
			case '!empty':
			case 'isset':
				if ( parentValue && parentValue !== "" && parentValue !== null ) {
					show = true;
				}
				break;
		}
		return show;

	};

	$.redux.verifyPos = function ( s, b ) {

		// trim off spaces
		s = s.replace( /^\s+|\s+$/gm, '' );

		// position value is blank, set the default
		if ( s === '' || s.search( ' ' ) == -1 ) {
			if ( b === true ) {
				return 'top left';
			} else {
				return 'bottom right';
			}
		}

		// split string into array
		var split = s.split( ' ' );

		// Evaluate first string.  Must be top, center, or bottom
		var paramOne = b ? 'top' : 'bottom';
		if ( split[0] == 'top' || split[0] == 'center' || split[0] == 'bottom' ) {
			paramOne = split[0];
		}

		// Evaluate second string.  Must be left, center, or right.
		var paramTwo = b ? 'left' : 'right';
		if ( split[1] == 'left' || split[1] == 'center' || split[1] == 'right' ) {
			paramTwo = split[1];
		}

		return paramOne + ' ' + paramTwo;
	};

	$.redux.stickyInfo = function () {
		var stickyWidth = $( '#info_bar' ).width() - 2;

		if ( !$( '#info_bar' ).isOnScreen() && !$( '#redux-footer-sticky' ).isOnScreen() ) {
			$( '#redux-sticky' ).addClass( 'sticky-save-warn' );

			$( '#redux-footer' ).css( {
				position: 'fixed',
				bottom  : '0',
				width   : stickyWidth
			} );

			$( '#redux-footer' ).addClass( 'sticky-footer-fixed' );
			$( '.redux-save-warn' ).css( 'left', $( '#redux-sticky' ).offset().left );
			$( '#redux-sticky-padder' ).show();
		} else {
			$( '#redux-sticky' ).removeClass( 'sticky-save-warn' );

			$( '#redux-footer' ).css( {
				background: '#eee',
				position  : 'inherit',
				bottom    : 'inherit',
				width     : 'inherit'
			} );

			$( '#redux-sticky-padder' ).hide();
			$( '#redux-footer' ).removeClass( 'sticky-footer-fixed' );
		}
	};

	$.redux.expandOptions = function ( parent ) {
		var trigger = parent.find( '.expand_options' );
		var width = parent.find( '.redux-sidebar' ).width();
		var id = $( '.redux-group-menu .active a' ).data( 'rel' ) + '_section_group';

		if ( trigger.hasClass( 'expanded' ) ) {
			trigger.removeClass( 'expanded' );
			parent.find( '.redux-main' ).removeClass( 'expand' );

			parent.find( '.redux-sidebar' ).stop().animate( {
				'margin-left': '0px'
			}, 500 );

			parent.find( '.redux-main' ).stop().animate( {
				'margin-left': width
			}, 500 );

			parent.find( '.redux-group-tab' ).each(
				function () {
					if ( $( this ).attr( 'id' ) !== id ) {
						$( this ).fadeOut( 'fast' );
					}
				}
			);
			// Show the only active one
		} else {
			trigger.addClass( 'expanded' );
			parent.find( '.redux-main' ).addClass( 'expand' );

			parent.find( '.redux-sidebar' ).stop().animate( {
				'margin-left': -width - 102
			}, 500 );

			parent.find( '.redux-main' ).stop().animate( {
				'margin-left': '0px'
			}, 500 );

			parent.find( '.redux-group-tab' ).fadeIn( 'medium', function () {
				$.redux.initFields();
			} );
		}
		return false;
	};
})( jQuery );

jQuery.noConflict();

var confirmOnPageExit = function ( e ) {
	//return; // ONLY FOR DEBUGGING
	// If we haven't been passed the event get the window.event
	e = e || window.event;

	var message = redux.params.save_pending;

	// For IE6-8 and Firefox prior to version 4
	if ( e ) {
		e.returnValue = message;
	}

	window.onbeforeunload = null;

	// For Chrome, Safari, IE8+ and Opera 12+
	return message;
};

function redux_change( variable ) {
	jQuery( 'body' ).trigger( 'check_dependencies', variable );

	if ( variable.hasClass( 'compiler' ) ) {
		jQuery( '#redux-compiler-hook' ).val( 1 );
	}

	window.onbeforeunload = confirmOnPageExit;

	var rContainer = jQuery( variable ).parents( '.redux-container:first' );

	if ( jQuery( variable ).parents( 'fieldset.redux-field:first' ).hasClass( 'redux-field-error' ) ) {
		jQuery( variable ).parents( 'fieldset.redux-field:first' ).removeClass( 'redux-field-error' );
		jQuery( variable ).parent().find( '.redux-th-error' ).slideUp();

		var parentID = jQuery( variable ).closest( '.redux-group-tab' ).attr( 'id' );


		var errorCount = (parseInt( rContainer.find( '.redux-field-errors span' ).text() ) - 1);
		var warningCount = (parseInt( rContainer.find( '.redux-field-warnings span' ).text() ) - 1);

		if ( errorCount <= 0 ) {
			//console.log('HERE');
			jQuery( '#' + parentID + '_li .redux-menu-error' ).fadeOut( 'fast' ).remove();
			jQuery( '#' + parentID + '_li .redux-group-tab-link-a' ).removeClass( 'hasError' );

			jQuery( '#' + parentID + '_li' ).parents( '.inside:first' ).find( '.redux-field-errors' ).slideUp();
			jQuery( variable ).parents( '.redux-container:first' ).find( '.redux-field-errors' ).slideUp();
			jQuery( '#redux_metaboxes_errors' ).slideUp();
		} else {
			// Let's count down the errors now. Fancy.  ;)
			var id = parentID.split( '_' );
			id = id[0];

			var th = rContainer.find( '.redux-group-tab-link-a[data-key="' + id + '"]' ).parents( '.redux-group-tab-link-li:first' );

			var errorsLeft = (parseInt( th.find( '.redux-menu-error:first' ).text() ) - 1);
			if ( errorsLeft <= 0 ) {
				th.find( '.redux-menu-error:first' ).fadeOut().remove();
			} else {
				th.find( '.redux-menu-error:first' ).text( errorsLeft );
			}

			var warningsLeft = (parseInt( th.find( '.redux-menu-warning:first' ).text() ) - 1);
			if ( warningsLeft <= 0 ) {
				th.find( '.redux-menu-warning:first' ).fadeOut().remove();
			} else {
				th.find( '.redux-menu-warning:first' ).text( warningsLeft );
			}

			rContainer.find( '.redux-field-errors span' ).text( errorCount );
			rContainer.find( '.redux-field-warning span' ).text( warningCount );

		}
		var subParent = jQuery( '#' + parentID + '_li' ).parents( '.hasSubSections:first' );
		if ( subParent.length !== 0 ) {
			if ( subParent.find( '.redux-menu-error' ).length === 0 ) {
				subParent.find( '.hasError' ).removeClass( 'hasError' );
			}
		}
	}
	if ( !redux.params.disable_save_warn ) {
		rContainer.find( '.redux-save-warn' ).slideDown();
	}
}

function colorValidate( field ) {
	var value = jQuery( field ).val();

	var hex = colorNameToHex( value );
	if ( hex !== value.replace( '#', '' ) ) {
		return hex;
	}

	return value;
}

function colorNameToHex( colour ) {
	var tcolour = colour.replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' ).replace( "#", "" );

	var colours = {
		"aliceblue"           : "#f0f8ff",
		"antiquewhite"        : "#faebd7",
		"aqua"                : "#00ffff",
		"aquamarine"          : "#7fffd4",
		"azure"               : "#f0ffff",
		"beige"               : "#f5f5dc",
		"bisque"              : "#ffe4c4",
		"black"               : "#000000",
		"blanchedalmond"      : "#ffebcd",
		"blue"                : "#0000ff",
		"blueviolet"          : "#8a2be2",
		"brown"               : "#a52a2a",
		"burlywood"           : "#deb887",
		"cadetblue"           : "#5f9ea0",
		"chartreuse"          : "#7fff00",
		"chocolate"           : "#d2691e",
		"coral"               : "#ff7f50",
		"cornflowerblue"      : "#6495ed",
		"cornsilk"            : "#fff8dc",
		"crimson"             : "#dc143c",
		"cyan"                : "#00ffff",
		"darkblue"            : "#00008b",
		"darkcyan"            : "#008b8b",
		"darkgoldenrod"       : "#b8860b",
		"darkgray"            : "#a9a9a9",
		"darkgreen"           : "#006400",
		"darkkhaki"           : "#bdb76b",
		"darkmagenta"         : "#8b008b",
		"darkolivegreen"      : "#556b2f",
		"darkorange"          : "#ff8c00",
		"darkorchid"          : "#9932cc",
		"darkred"             : "#8b0000",
		"darksalmon"          : "#e9967a",
		"darkseagreen"        : "#8fbc8f",
		"darkslateblue"       : "#483d8b",
		"darkslategray"       : "#2f4f4f",
		"darkturquoise"       : "#00ced1",
		"darkviolet"          : "#9400d3",
		"deeppink"            : "#ff1493",
		"deepskyblue"         : "#00bfff",
		"dimgray"             : "#696969",
		"dodgerblue"          : "#1e90ff",
		"firebrick"           : "#b22222",
		"floralwhite"         : "#fffaf0",
		"forestgreen"         : "#228b22",
		"fuchsia"             : "#ff00ff",
		"gainsboro"           : "#dcdcdc",
		"ghostwhite"          : "#f8f8ff",
		"gold"                : "#ffd700",
		"goldenrod"           : "#daa520",
		"gray"                : "#808080",
		"green"               : "#008000",
		"greenyellow"         : "#adff2f",
		"honeydew"            : "#f0fff0",
		"hotpink"             : "#ff69b4",
		"indianred "          : "#cd5c5c",
		"indigo "             : "#4b0082",
		"ivory"               : "#fffff0",
		"khaki"               : "#f0e68c",
		"lavender"            : "#e6e6fa",
		"lavenderblush"       : "#fff0f5",
		"lawngreen"           : "#7cfc00",
		"lemonchiffon"        : "#fffacd",
		"lightblue"           : "#add8e6",
		"lightcoral"          : "#f08080",
		"lightcyan"           : "#e0ffff",
		"lightgoldenrodyellow": "#fafad2",
		"lightgrey"           : "#d3d3d3",
		"lightgreen"          : "#90ee90",
		"lightpink"           : "#ffb6c1",
		"lightsalmon"         : "#ffa07a",
		"lightseagreen"       : "#20b2aa",
		"lightskyblue"        : "#87cefa",
		"lightslategray"      : "#778899",
		"lightsteelblue"      : "#b0c4de",
		"lightyellow"         : "#ffffe0",
		"lime"                : "#00ff00",
		"limegreen"           : "#32cd32",
		"linen"               : "#faf0e6",
		"magenta"             : "#ff00ff",
		"maroon"              : "#800000",
		"mediumaquamarine"    : "#66cdaa",
		"mediumblue"          : "#0000cd",
		"mediumorchid"        : "#ba55d3",
		"mediumpurple"        : "#9370d8",
		"mediumseagreen"      : "#3cb371",
		"mediumslateblue"     : "#7b68ee",
		"mediumspringgreen"   : "#00fa9a",
		"mediumturquoise"     : "#48d1cc",
		"mediumvioletred"     : "#c71585",
		"midnightblue"        : "#191970",
		"mintcream"           : "#f5fffa",
		"mistyrose"           : "#ffe4e1",
		"moccasin"            : "#ffe4b5",
		"navajowhite"         : "#ffdead",
		"navy"                : "#000080",
		"oldlace"             : "#fdf5e6",
		"olive"               : "#808000",
		"olivedrab"           : "#6b8e23",
		"orange"              : "#ffa500",
		"orangered"           : "#ff4500",
		"orchid"              : "#da70d6",
		"palegoldenrod"       : "#eee8aa",
		"palegreen"           : "#98fb98",
		"paleturquoise"       : "#afeeee",
		"palevioletred"       : "#d87093",
		"papayawhip"          : "#ffefd5",
		"peachpuff"           : "#ffdab9",
		"peru"                : "#cd853f",
		"pink"                : "#ffc0cb",
		"plum"                : "#dda0dd",
		"powderblue"          : "#b0e0e6",
		"purple"              : "#800080",
		"red"                 : "#ff0000",
		"redux"               : "#01a3e3",
		"rosybrown"           : "#bc8f8f",
		"royalblue"           : "#4169e1",
		"saddlebrown"         : "#8b4513",
		"salmon"              : "#fa8072",
		"sandybrown"          : "#f4a460",
		"seagreen"            : "#2e8b57",
		"seashell"            : "#fff5ee",
		"sienna"              : "#a0522d",
		"silver"              : "#c0c0c0",
		"skyblue"             : "#87ceeb",
		"slateblue"           : "#6a5acd",
		"slategray"           : "#708090",
		"snow"                : "#fffafa",
		"springgreen"         : "#00ff7f",
		"steelblue"           : "#4682b4",
		"tan"                 : "#d2b48c",
		"teal"                : "#008080",
		"thistle"             : "#d8bfd8",
		"tomato"              : "#ff6347",
		"turquoise"           : "#40e0d0",
		"violet"              : "#ee82ee",
		"wheat"               : "#f5deb3",
		"white"               : "#ffffff",
		"whitesmoke"          : "#f5f5f5",
		"yellow"              : "#ffff00",
		"yellowgreen"         : "#9acd32"
	};

	if ( colours[tcolour.toLowerCase()] !== 'undefined' ) {
		return colours[tcolour.toLowerCase()];
	}

	return colour;
}