/**********************************************************************
* OSAKA WORDPRESS THEME 
* (Ideal For Business And Personal Use: Portfolio or Blog)   
* 
* File name:   
*      cms.js
* Brief:       
*      Core CMS javascript file file
* Author:      
*      DigitalCavalry
* Author URI:
*      http://themeforest.net/user/DigitalCavalry
* Contact:
*      digitalcavalry@gmail.com 
**********************************************************************/

/*
 Color animation jQuery-plugin
 http://www.bitstorm.org/jquery/color-animation/
 Copyright 2011 Edwin Martin <edwin@bitstorm.org>
 Released under the MIT and GPL licenses.
*/
(function(d){function i(){var b=d("script:first"),a=b.css("color"),c=false;if(/^rgba/.test(a))c=true;else try{c=a!=b.css("color","rgba(0, 0, 0, 0.5)").css("color");b.css("color",a)}catch(e){}return c}function g(b,a,c){var e="rgb"+(d.support.rgba?"a":"")+"("+parseInt(b[0]+c*(a[0]-b[0]),10)+","+parseInt(b[1]+c*(a[1]-b[1]),10)+","+parseInt(b[2]+c*(a[2]-b[2]),10);if(d.support.rgba)e+=","+(b&&a?parseFloat(b[3]+c*(a[3]-b[3])):1);e+=")";return e}function f(b){var a,c;if(a=/#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(b))c=
[parseInt(a[1],16),parseInt(a[2],16),parseInt(a[3],16),1];else if(a=/#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(b))c=[parseInt(a[1],16)*17,parseInt(a[2],16)*17,parseInt(a[3],16)*17,1];else if(a=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(b))c=[parseInt(a[1]),parseInt(a[2]),parseInt(a[3]),1];else if(a=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9\.]*)\s*\)/.exec(b))c=[parseInt(a[1],10),parseInt(a[2],10),parseInt(a[3],10),parseFloat(a[4])];return c}
d.extend(true,d,{support:{rgba:i()}});var h=["color","backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","outlineColor"];d.each(h,function(b,a){d.fx.step[a]=function(c){if(!c.init){c.a=f(d(c.elem).css(a));c.end=f(c.end);c.init=true}c.elem.style[a]=g(c.a,c.end,c.pos)}});d.fx.step.borderColor=function(b){if(!b.init)b.end=f(b.end);var a=h.slice(2,6);d.each(a,function(c,e){b.init||(b[e]={a:f(d(b.elem).css(e))});b.elem.style[e]=g(b[e].a,b.end,b.pos)});b.init=true}})(jQuery);
     
     
/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/
jQuery.easing.jswing=jQuery.easing.swing;
jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(e,a,c,b,d){return jQuery.easing[jQuery.easing.def](e,a,c,b,d)},easeInQuad:function(e,a,c,b,d){return b*(a/=d)*a+c},easeOutQuad:function(e,a,c,b,d){return-b*(a/=d)*(a-2)+c},easeInOutQuad:function(e,a,c,b,d){return 1>(a/=d/2)?b/2*a*a+c:-b/2*(--a*(a-2)-1)+c},easeInCubic:function(e,a,c,b,d){return b*(a/=d)*a*a+c},easeOutCubic:function(e,a,c,b,d){return b*((a=a/d-1)*a*a+1)+c},easeInOutCubic:function(e,a,c,b,d){return 1>(a/=d/2)?b/2*a*a*a+c:
b/2*((a-=2)*a*a+2)+c},easeInQuart:function(e,a,c,b,d){return b*(a/=d)*a*a*a+c},easeOutQuart:function(e,a,c,b,d){return-b*((a=a/d-1)*a*a*a-1)+c},easeInOutQuart:function(e,a,c,b,d){return 1>(a/=d/2)?b/2*a*a*a*a+c:-b/2*((a-=2)*a*a*a-2)+c},easeInQuint:function(e,a,c,b,d){return b*(a/=d)*a*a*a*a+c},easeOutQuint:function(e,a,c,b,d){return b*((a=a/d-1)*a*a*a*a+1)+c},easeInOutQuint:function(e,a,c,b,d){return 1>(a/=d/2)?b/2*a*a*a*a*a+c:b/2*((a-=2)*a*a*a*a+2)+c},easeInSine:function(e,a,c,b,d){return-b*Math.cos(a/
d*(Math.PI/2))+b+c},easeOutSine:function(e,a,c,b,d){return b*Math.sin(a/d*(Math.PI/2))+c},easeInOutSine:function(e,a,c,b,d){return-b/2*(Math.cos(Math.PI*a/d)-1)+c},easeInExpo:function(e,a,c,b,d){return 0==a?c:b*Math.pow(2,10*(a/d-1))+c},easeOutExpo:function(e,a,c,b,d){return a==d?c+b:b*(-Math.pow(2,-10*a/d)+1)+c},easeInOutExpo:function(e,a,c,b,d){return 0==a?c:a==d?c+b:1>(a/=d/2)?b/2*Math.pow(2,10*(a-1))+c:b/2*(-Math.pow(2,-10*--a)+2)+c},easeInCirc:function(e,a,c,b,d){return-b*(Math.sqrt(1-(a/=d)*
a)-1)+c},easeOutCirc:function(e,a,c,b,d){return b*Math.sqrt(1-(a=a/d-1)*a)+c},easeInOutCirc:function(e,a,c,b,d){return 1>(a/=d/2)?-b/2*(Math.sqrt(1-a*a)-1)+c:b/2*(Math.sqrt(1-(a-=2)*a)+1)+c},easeInElastic:function(e,a,c,b,d){var e=1.70158,f=0,g=b;if(0==a)return c;if(1==(a/=d))return c+b;f||(f=0.3*d);g<Math.abs(b)?(g=b,e=f/4):e=f/(2*Math.PI)*Math.asin(b/g);return-(g*Math.pow(2,10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f))+c},easeOutElastic:function(e,a,c,b,d){var e=1.70158,f=0,g=b;if(0==a)return c;if(1==
(a/=d))return c+b;f||(f=0.3*d);g<Math.abs(b)?(g=b,e=f/4):e=f/(2*Math.PI)*Math.asin(b/g);return g*Math.pow(2,-10*a)*Math.sin((a*d-e)*2*Math.PI/f)+b+c},easeInOutElastic:function(e,a,c,b,d){var e=1.70158,f=0,g=b;if(0==a)return c;if(2==(a/=d/2))return c+b;f||(f=d*0.3*1.5);g<Math.abs(b)?(g=b,e=f/4):e=f/(2*Math.PI)*Math.asin(b/g);return 1>a?-0.5*g*Math.pow(2,10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f)+c:0.5*g*Math.pow(2,-10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f)+b+c},easeInBack:function(e,a,c,b,d,f){void 0==
f&&(f=1.70158);return b*(a/=d)*a*((f+1)*a-f)+c},easeOutBack:function(e,a,c,b,d,f){void 0==f&&(f=1.70158);return b*((a=a/d-1)*a*((f+1)*a+f)+1)+c},easeInOutBack:function(e,a,c,b,d,f){void 0==f&&(f=1.70158);return 1>(a/=d/2)?b/2*a*a*(((f*=1.525)+1)*a-f)+c:b/2*((a-=2)*a*(((f*=1.525)+1)*a+f)+2)+c},easeInBounce:function(e,a,c,b,d){return b-jQuery.easing.easeOutBounce(e,d-a,0,b,d)+c},easeOutBounce:function(e,a,c,b,d){return(a/=d)<1/2.75?b*7.5625*a*a+c:a<2/2.75?b*(7.5625*(a-=1.5/2.75)*a+0.75)+c:a<2.5/2.75?
b*(7.5625*(a-=2.25/2.75)*a+0.9375)+c:b*(7.5625*(a-=2.625/2.75)*a+0.984375)+c},easeInOutBounce:function(e,a,c,b,d){return a<d/2?0.5*jQuery.easing.easeInBounce(e,2*a,0,b,d)+c:0.5*jQuery.easing.easeOutBounce(e,2*a-d,0,b,d)+0.5*b+c}});


/*! Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 *
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 * Thanks to: Seamus Leahy for adding deltaX and deltaY
 *
 * Version: 3.0.6
 * 
 * Requires: 1.2.2+
 */
(function(a){function d(b){var c=b||window.event,d=[].slice.call(arguments,1),e=0,f=!0,g=0,h=0;return b=a.event.fix(c),b.type="mousewheel",c.wheelDelta&&(e=c.wheelDelta/120),c.detail&&(e=-c.detail/3),h=e,c.axis!==undefined&&c.axis===c.HORIZONTAL_AXIS&&(h=0,g=-1*e),c.wheelDeltaY!==undefined&&(h=c.wheelDeltaY/120),c.wheelDeltaX!==undefined&&(g=-1*c.wheelDeltaX/120),d.unshift(b,e,g,h),(a.event.dispatch||a.event.handle).apply(this,d)}var b=["DOMMouseScroll","mousewheel"];if(a.event.fixHooks)for(var c=b.length;c;)a.event.fixHooks[b[--c]]=a.event.mouseHooks;a.event.special.mousewheel={setup:function(){if(this.addEventListener)for(var a=b.length;a;)this.addEventListener(b[--a],d,!1);else this.onmousewheel=d},teardown:function(){if(this.removeEventListener)for(var a=b.length;a;)this.removeEventListener(b[--a],d,!1);else this.onmousewheel=null}},a.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})})(jQuery);

/*
 * jQuery UI Nested Sortable
 * v 1.3.4 / 28 apr 2011
 * http://mjsarfatti.com/sandbox/nestedSortable
 *
 * Depends:
 *     jquery.ui.sortable.js 1.8+
 *
 * License CC BY-SA 3.0
 * Copyright 2010-2011, Manuele J Sarfatti
 */
 
if(!jQuery().nestedSortableDC) 
{
 
(function($) {

    $.widget("ui.nestedSortableDC", $.extend({}, $.ui.sortable.prototype, {

        options: {
            tabSize: 22,
            disableNesting: 'ui-nestedSortable-no-nesting',
            errorClass: 'ui-nestedSortable-error',
            listType: 'ol',
            maxLevels: 0,
            revertOnError: 1
        },

        _create: function() {
            this.element.data('sortable', this.element.data('nestedSortableDC'));
            return $.ui.sortable.prototype._create.apply(this, arguments);
        },

        destroy: function() {
            this.element
                .removeData("nestedSortableDC")
                .unbind(".nestedSortableDC");
            return $.ui.sortable.prototype.destroy.apply(this, arguments);
        },

        _mouseDrag: function(event) {

            //Compute the helpers position
            this.position = this._generatePosition(event);
            this.positionAbs = this._convertPositionTo("absolute");

            if (!this.lastPositionAbs) {
                this.lastPositionAbs = this.positionAbs;
            }

            //Do scrolling
            if(this.options.scroll) {
                var o = this.options, scrolled = false;
                if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML') {

                    if((this.overflowOffset.top + this.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity)
                        this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop + o.scrollSpeed;
                    else if(event.pageY - this.overflowOffset.top < o.scrollSensitivity)
                        this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop - o.scrollSpeed;

                    if((this.overflowOffset.left + this.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity)
                        this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft + o.scrollSpeed;
                    else if(event.pageX - this.overflowOffset.left < o.scrollSensitivity)
                        this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft - o.scrollSpeed;

                } else {

                    if(event.pageY - $(document).scrollTop() < o.scrollSensitivity)
                        scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
                    else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity)
                        scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);

                    if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity)
                        scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
                    else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity)
                        scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);

                }

                if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour)
                    $.ui.ddmanager.prepareOffsets(this, event);
            }

            //Regenerate the absolute position used for position checks
            this.positionAbs = this._convertPositionTo("absolute");

            //Set the helper position
            if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px';
            if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px';

            //Rearrange
            for (var i = this.items.length - 1; i >= 0; i--) {

                //Cache variables and intersection, continue if no intersection
                var item = this.items[i], itemElement = item.item[0], intersection = this._intersectsWithPointer(item);
                if (!intersection) continue;

                if(itemElement != this.currentItem[0] //cannot intersect with itself
                    &&    this.placeholder[intersection == 1 ? "next" : "prev"]()[0] != itemElement //no useless actions that have been done before
                    &&    !$.contains(this.placeholder[0], itemElement) //no action if the item moved is the parent of the item checked
                    && (this.options.type == 'semi-dynamic' ? !$.contains(this.element[0], itemElement) : true)
                    //&& itemElement.parentNode == this.placeholder[0].parentNode // only rearrange items within the same container
                ) {

                    $(itemElement).mouseenter();

                    this.direction = intersection == 1 ? "down" : "up";

                    if (this.options.tolerance == "pointer" || this._intersectsWithSides(item)) {
                        $(itemElement).mouseleave();
                        this._rearrange(event, item);
                    } else {
                        break;
                    }

                    // Clear emtpy ul's/ol's
                    this._clearEmpty(itemElement);

                    this._trigger("change", event, this._uiHash());
                    break;
                }
            }

            var parentItem = (this.placeholder[0].parentNode.parentNode
                       && $(this.placeholder[0].parentNode.parentNode).closest('.ui-sortable').length)
                       ? $(this.placeholder[0].parentNode.parentNode)
                       : null,
                level = this._getLevel(this.placeholder),
                childLevels = this._getChildLevels(this.helper),
                previousItem = this.placeholder[0].previousSibling ? $(this.placeholder[0].previousSibling) : null;

            if (previousItem != null) {
                while (previousItem[0].nodeName.toLowerCase() != 'li' || previousItem[0] == this.currentItem[0]) {
                    if (previousItem[0].previousSibling) {
                        previousItem = $(previousItem[0].previousSibling);
                    } else {
                        previousItem = null;
                        break;
                    }
                }
            }

            newList = document.createElement(o.listType);

            this.beyondMaxLevels = 0;

            // If the item is moved to the left, send it to its parent level
            if (parentItem != null && this.positionAbs.left < parentItem.offset().left) {
                parentItem.after(this.placeholder[0]);
                this._clearEmpty(parentItem[0]);
                this._trigger("change", event, this._uiHash());
            }
            // If the item is below another one and is moved to the right, make it a children of it
            else if (previousItem != null && this.positionAbs.left > previousItem.offset().left + o.tabSize) {
                this._isAllowed(previousItem, level+childLevels+1);
                if (!previousItem.children(o.listType).length) {
                    previousItem[0].appendChild(newList);
                }
                previousItem.children(o.listType)[0].appendChild(this.placeholder[0]);
                this._trigger("change", event, this._uiHash());
            }
            else {
                this._isAllowed(parentItem, level+childLevels);
            }

            //Post events to containers
            this._contactContainers(event);

            //Interconnect with droppables
            if($.ui.ddmanager) $.ui.ddmanager.drag(this, event);

            //Call callbacks
            this._trigger('sort', event, this._uiHash());

            this.lastPositionAbs = this.positionAbs;
            return false;

        },

        _mouseStop: function(event, noPropagation) {

            // If the item is in a position not allowed, send it back
            if (this.beyondMaxLevels) {

                this.placeholder.removeClass(this.options.errorClass);

                if (this.options.revertOnError) {
                    if (this.domPosition.prev) {
                        $(this.domPosition.prev).after(this.placeholder);
                    } else {
                        $(this.domPosition.parent).prepend(this.placeholder);
                    }
                    this._trigger("revert", event, this._uiHash());
                } else {
                    var parent = this.placeholder.parent().closest(this.options.items);

                    for (var i = this.beyondMaxLevels - 1; i > 0; i--) {
                        parent = parent.parent().closest(this.options.items);
                    }

                    parent.after(this.placeholder);
                    this._trigger("change", event, this._uiHash());
                }

            }

            // Clean last empty ul/ol
            for (var i = this.items.length - 1; i >= 0; i--) {
                var item = this.items[i].item[0];
                this._clearEmpty(item);
            }

            $.ui.sortable.prototype._mouseStop.apply(this, arguments);

        },

        serialize: function(o) {

            var items = this._getItemsAsjQuery(o && o.connected),
                str = []; o = o || {};

            $(items).each(function() {
                var res = ($(o.item || this).attr(o.attribute || 'id') || '')
                        .match(o.expression || (/(.+)[-=_](.+)/)),
                    pid = ($(o.item || this).parent(o.listType)
                        .parent('li')
                        .attr(o.attribute || 'id') || '')
                        .match(o.expression || (/(.+)[-=_](.+)/));

                if (res) {
                    str.push((o.key || res[1] + '[' + (o.key && o.expression ? res[1] : res[2]) + ']')
                        + '='
                        + (pid ? (o.key && o.expression ? pid[1] : pid[2]) : 'root'));
                }
            });

            if(!str.length && o.key) {
                str.push(o.key + '=');
            }

            return str.join('&');

        },

        toHierarchy: function(o) {

            o = o || {};
            var sDepth = o.startDepthCount || 0,
                ret = [];

            $(this.element).children('li').each(function () {
                var level = _recursiveItems($(this));
                ret.push(level);
            });

            return ret;

            function _recursiveItems(li) {
                var id = ($(li).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/));
                if (id) {
                    var item = {"id" : id[2]};
                    if ($(li).children(o.listType).children('li').length > 0) {
                        item.children = [];
                        $(li).children(o.listType).children('li').each(function() {
                            var level = _recursiveItems($(this));
                            item.children.push(level);
                        });
                    }
                    return item;
                }
            }
        },

        toArray: function(o) {

            o = o || {};
            var sDepth = o.startDepthCount || 0,
                ret = [],
                left = 2;

            ret.push({
                "item_id": 'root',
                "parent_id": 'none',
                "depth": sDepth,
                "left": '1',
                "right": ($('li', this.element).length + 1) * 2
            });

            $(this.element).children('li').each(function () {
                left = _recursiveArray(this, sDepth + 1, left);
            });

            ret = ret.sort(function(a,b){ return (a.left - b.left); });

            return ret;

            function _recursiveArray(item, depth, left) {

                var right = left + 1,
                    id,
                    pid;

                if ($(item).children(o.listType).children('li').length > 0) {
                    depth ++;
                    $(item).children(o.listType).children('li').each(function () {
                        right = _recursiveArray($(this), depth, right);
                    });
                    depth --;
                }

                id = ($(item).attr(o.attribute || 'id')).match(o.expression || (/(.+)[-=_](.+)/));

                if (depth === sDepth + 1) {
                    pid = 'root';
                } else {
                    var parentItem = ($(item).parent(o.listType)
                        .parent('li')
                        .attr(o.attribute || 'id'))
                        .match(o.expression || (/(.+)[-=_](.+)/));
                    pid = parentItem[2];
                }

                if (id) {
                        ret.push({"item_id": id[2], "parent_id": pid, "depth": depth, "left": left, "right": right});
                }

                left = right + 1;
                return left;
            }

        },

        _clearEmpty: function(item) {

            var emptyList = $(item).children(this.options.listType);
            if (emptyList.length && !emptyList.children().length) {
                emptyList.remove();
            }

        },

        _getLevel: function(item) {

            var level = 1;

            if (this.options.listType) {
                var list = item.closest(this.options.listType);
                while (!list.is('.ui-sortable')) {
                    level++;
                    list = list.parent().closest(this.options.listType);
                }
            }

            return level;
        },

        _getChildLevels: function(parent, depth) {
            var self = this,
                o = this.options,
                result = 0;
            depth = depth || 0;

            $(parent).children(o.listType).children(o.items).each(function (index, child) {
                    result = Math.max(self._getChildLevels(child, depth + 1), result);
            });

            return depth ? result + 1 : result;
        },

        _isAllowed: function(parentItem, levels) {
            var o = this.options;
            // Are we trying to nest under a no-nest or are we nesting too deep?
            if (parentItem == null || !(parentItem.hasClass(o.disableNesting))) {
                if (o.maxLevels < levels && o.maxLevels != 0) {
                    this.placeholder.addClass(o.errorClass);
                    this.beyondMaxLevels = levels - o.maxLevels;
                } else {
                    this.placeholder.removeClass(o.errorClass);
                    this.beyondMaxLevels = 0;
                }
            } else {
                this.placeholder.addClass(o.errorClass);
                if (o.maxLevels < levels && o.maxLevels != 0) {
                    this.beyondMaxLevels = levels - o.maxLevels;
                } else {
                    this.beyondMaxLevels = 1;
                }
            }
        }

    }));

    $.ui.nestedSortableDC.prototype.options = $.extend({}, $.ui.sortable.prototype.options, $.ui.nestedSortableDC.prototype.options);
})(jQuery); 

}

 
 
jQuery(document).ready(function($) 
    {   
                          
    jQuery.fn.dc_cms_data = { author: 'Digital Cavalry', dc_formfield: null }; 
        
        
        
    function dcf_createCookie(name,value,days) 
    {
        var expires = '';
        if(days != 0) 
        {
            var date = new Date();  
            date.setTime(date.getTime()+(days*24*60*60*1000));
            expires = "; expires="+date.toGMTString();
        }
        document.cookie = name+"="+value+expires+"; path=/";
    }

    function dcf_readCookie(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;
    }

    function dcf_eraseCookie(name) 
    {
        createCookie(name,'',-1);
    }      
    
    function dcf_showAjaxProgrss()
    {
        var len = jQuery('.dc-cms-ajax-progress').length;
        if(len)
        {
            jQuery('.dc-cms-ajax-progress').stop().remove();     
        } 
        jQuery('body').append(dccp_s.ajax_box);                
        jQuery('.dc-cms-ajax-progress').css('opacity', 1.0);        
    }
    
    function dcf_hideAjaxProgrss()
    {
        jQuery('.dc-cms-ajax-progress').animate({opacity:0.0}, 1000, function() { jQuery(this).remove(); });    
    }   
    
        /******************************
        * DC Control Input Int
        *******************************/  
        if(!jQuery.fn.dcf_setupIntCtrl) 
        {           
            jQuery.fn.extend({
                dcf_setupIntCtrl: function(){
                    jQuery(this).find('.dc-cms-control-input-int').each(function()
            {                                    
                if(jQuery(this).data('dc_control_int') != 'set')
                {   
                    var disabled = jQuery(this).find('input').attr('disabled');
                    if(disabled != undefined && disabled == 'disabled')
                    {
                        disabled = true;
                    } else { disabled = false; }
                    
                    jQuery(this).data('dc_control_int', 'set');
                  //  jQuery(this).mouseout(function() { jQuery(this).find('input').blur(); });
                    var bg_color = jQuery(this).find('input').css('background-color');
                    
                    jQuery(this).find('.plus').data('dc_id', null);
                    jQuery(this).find('.minus').data('dc_id', null);
                    var $step = jQuery(this).find('span.step');
                    if($step.length == 0) { $step = 1; } else { $step = parseInt($step.text()); }
                    
                    function plus(obj, s)
                    {
                        var parent = jQuery(obj).parent();
                        var max = null;
                        if(parent.find('span.max').length)
                        {
                            max = parseInt(parent.find('span.max').text());    
                        }
                        
                        var input = parent.find('input');
                        var val = parseInt(input.val());                                                
                        if(isNaN(val)) { val = 0; }
                        val += s;
                        if(max != null)
                        {
                            if(val > max) { val = max; }
                        }
                        input.val(val);  
                        
                        if(jQuery(obj).data('dc_id') != null)
                        {
                            id = setTimeout(function(){plus(obj, s)}, 40);
                            jQuery(obj).data('dc_id', id);                  
                        }
                    }
                
                    if(!disabled)
                    {
                        jQuery(this).find('.plus').click(function(){
                            jQuery(this).data('dc_id', null);
                            plus(this, 1);
                        }).mousedown(function(){                                     
                            var $obj = this;
                            var id = setTimeout(function(){plus($obj, $step)}, 300);
                            jQuery(this).data('dc_id', id);        
                        }).mouseup(function(){                    
                            clearTimeout(jQuery(this).data('dc_id'));
                        }).mouseout(function(){                    
                            clearTimeout(jQuery(this).data('dc_id'));                    
                        });
                    }
                    function minus(obj, s)
                    {   
                        var parent = jQuery(obj).parent();
                        var min = null;
                        if(parent.find('span.min').length)
                        {
                            min = parseInt(parent.find('span.min').text());    
                        }                    
                        
                        var input = parent.find('input');
                        var val = parseInt(input.val());                                                
                        if(isNaN(val)) { val = 0; }
                        val -= s;
                        if(min != null)
                        {
                            if(val < min) { val = min; }
                        }                    
                        input.val(val);  
                        
                        if(jQuery(obj).data('dc_id') != null)
                        {
                            id = setTimeout(function(){minus(obj, s)}, 40);
                            jQuery(obj).data('dc_id', id);                  
                        }
                    }                
                    
                    if(!disabled)
                    {                    
                        jQuery(this).find('.minus').click(function(event){                     
                            jQuery(this).data('dc_id', null);
                            minus(this, 1);
                        }).mousedown(function(){                                     
                            var $obj = this;
                            var id = setTimeout(function(){minus($obj, $step)}, 400);
                            jQuery(this).data('dc_id', id);        
                        }).mouseup(function(){                    
                            clearTimeout(jQuery(this).data('dc_id'));
                        }).mouseout(function(){                    
                            clearTimeout(jQuery(this).data('dc_id'));
                        });
                    }
                    
                    if(!disabled)
                    {                  
                        jQuery(this).find('input').blur(function(event)
                        {
                            var val = jQuery(this).val();
                            if(val == '' || isNaN(val)) 
                            { 
                                val = 0;
                                jQuery(this).val(val); 
                            } else
                            {
                                parent = jQuery(this).parent();
                                var min = null;
                                if(parent.find('span.min').length)
                                {
                                    min = parseInt(parent.find('span.min').text());    
                                }
                                var max = null;
                                if(parent.find('span.max').length)
                                {
                                    max = parseInt(parent.find('span.max').text());    
                                }  
                                                    
                                if(min != null)
                                {
                                    if(val < min) { val = min; }
                                    jQuery(this).val(val);
                                }  
                                
                                if(max != null)
                                {
                                    if(val > max) { val = max; }
                                    jQuery(this).val(val);
                                }                                                                        
                            }       
                        });
                    
                        jQuery(this).find('input').keypress(function(event)
                        {                    
                            var key = window.event ? event.keyCode : event.which;
                            
                            if(event.keyCode == 8 || event.keyCode == 46 || 
                               event.keyCode == 37 || event.keyCode == 39 || 
                               event.keyCode == 45) 
                            {
                                return true;
                            } else 
                            if(key < 48 || key > 57 ) 
                            {
                                return false;
                            } else
                            {
                                return true;
                            }                                        
                        });
                     
                    }                                   
                }    
            });
                }
            });                           
        }
        
        jQuery('body').dcf_setupIntCtrl();     

    /*******************************
    * WordPress Widget Area  
    ********************************/
    jQuery('#widgets-right div.widgets-sortables').bind('sortstop',function(event,ui)
    {
        var $item = jQuery(ui.item);     
        var setup_dc_widget = function(ui) 
        {          
            $item.dcf_setupIntCtrl(); 
            $item.dcf_setupColorPicker(); 
            $item.dcf_setupCMSLightBox();  
            $item.dcf_setupCMSLoadImageBtn();          
        }       
        setTimeout(setup_dc_widget, 500);
    });       
                             
    /******************************
    /*  Content Builder     
    ******************************/     

        var cb_ss_sizes = new Array(
            {cls:'segment-1-4',up:1,down:4,val:1/4,label:'1/4',
            allowed:['blog','post','page-content','page-title','sidebar','gmap','cform','text','empty-space','top-anchor', 'line-divider', 'img-divider',
            'note', 'blockquote', 'ul-list', 'postbox', 'pagebox', 'tip', 'image', 'video', 'ngg', 'ngg-thumbs-slider', 'ngg-gallery',
            'service', 'project', 'basic-slider', 'code', 'anno-box', 'recent-posts-list', 'download', 'heading', 'member', 'colored-head',
            'wgt-member', 'wgt-twitter']}, 
            {cls:'segment-1-3',up:1,down:3,val:1/3,label:'1/3',
            allowed:['blog','post','page-content','page-title','sidebar','gmap','cform','text','empty-space','top-anchor', 'line-divider', 'img-divider',
            'note', 'blockquote', 'ul-list', 'postbox', 'pagebox', 'tip', 'image', 'video', 'ngg', 'ngg-thumbs-slider', 'ngg-gallery',
            'service', 'project', 'basic-slider', 'code', 'anno-box', 'recent-posts-list', 'download', 'heading', 'member', 'colored-head', 
            'wgt-member', 'wgt-twitter']}, 
            {cls:'segment-1-2',up:1,down:2,val:1/2,label:'1/2',
            allowed:['blog','post','page-content','page-title','sidebar','gmap','cform','text','empty-space','top-anchor', 'line-divider', 'img-divider',
            'note', 'blockquote', 'ul-list', 'postbox', 'pagebox', 'tip', 'image', 'video', 'ngg', 'ngg-thumbs-slider', 'ngg-gallery',
            'service', 'project', 'basic-slider', 'code', 'anno-box', 'recent-posts-list', 'download', 'heading', 'member', 'colored-head',
            'wgt-member', 'wgt-twitter']}, 
            {cls:'segment-2-3',up:2,down:3,val:2/3,label:'2/3',
            allowed:['blog','post','page-content','page-title','sidebar','gmap','cform','text','empty-space','top-anchor', 'line-divider', 'img-divider',
            'note', 'blockquote', 'ul-list', 'postbox', 'pagebox', 'tip', 'image', 'video', 'ngg', 'ngg-thumbs-slider', 'ngg-gallery',
            'service', 'project', 'basic-slider', 'code', 'anno-box', 'recent-posts-list', 'download', 'heading', 'member', 'colored-head', 
            'wgt-member', 'wgt-twitter']}, 
            {cls:'segment-3-4',up:3,down:4,val:3/4,label:'3/4',
            allowed:['blog','post','page-content','page-title','sidebar','gmap','cform','text','empty-space','top-anchor', 'line-divider', 'img-divider',
            'note', 'blockquote', 'ul-list', 'postbox', 'pagebox', 'tip', 'image', 'video', 'ngg', 'ngg-thumbs-slider', 'ngg-gallery',
            'service', 'project', 'basic-slider', 'code', 'anno-box', 'recent-posts-list', 'download', 'heading', 'member', 'colored-head', 
            'wgt-member', 'wgt-twitter']}, 
            {cls:'segment-1-1',up:1,down:1,val:1/1,label:'1/1',
            allowed:['blog','post','page-content','page-title','sidebar','gmap','cform','text','empty-space','top-anchor', 'line-divider', 'img-divider',
            'note', 'blockquote', 'ul-list', 'postbox', 'pagebox', 'tip', 'image', 'video', 'ngg', 'ngg-thumbs-slider', 'ngg-gallery',
            'service', 'project', 'basic-slider', 'code', 'anno-box', 'recent-posts-list', 'download', 'heading', 'member', 'colored-head', 
            'wgt-member', 'wgt-twitter']});    
                
    // 1-1            
    cb_ss_sizes[5].allowed.push('board-slider');              
    cb_ss_sizes[5].allowed.push('ngg-recent-slider');     
          
    jQuery('.dc-cms-content-builder').sortable(
    {
       cursor: "move",
       distance: 15, 
       placeholder: 'placeholder',
       start: function (e, ui) 
       { 
          var w = ui.item.width();
          ui.placeholder.css('width', (w-10));
       },
       
        update: function(event, ui) { 
           
            if(jQuery(ui.item).is('li'))
            {
                dcf_cb_update_segments_numeration();
                dcf_cb_update_segments_order();
     
            }                
            
        },       
                   
    });        

    function dcf_cb_update_segments_numeration()
    {
        var $wrapper = jQuery('#dc-cms-content-builder-wrapper'); 
        var $builder = $wrapper.find('.dc-cms-content-builder'); 
        
        var $li = $builder.find('li'); 
        var c = $li.length;
        
        for(var i = 0; i < c; i++)
        {
            $li.eq(i).find('.seg-order').text(i+1);    
        }      
    } 
    
    function dcf_cb_update_segments_order()
    {
        var $wrapper = jQuery('#dc-cms-content-builder-wrapper'); 
        var $builder = $wrapper.find('.dc-cms-content-builder');  
        
            var order = new Array(); 
            $builder.find('li').each(function() {
               
               var id = jQuery(this).find('input[name^="segment-cell-id-"]').val();    
               order.push(id); 
                
            });
            
       $wrapper.find('.builder-meta input[name="dc-builder-meta-order"]').val(order.toString());            
    }
    
    
    jQuery('#dc-cms-content-builder-wrapper').each(function() {
        
        var $wrapper = jQuery(this);
        var $builder = $wrapper.find('.dc-cms-content-builder');
        var $panel = $wrapper.find('.dc-cms-content-builder-control-panel');
        var $editor = $wrapper.find('.dc-cms-segment-editor'); 
        
        var index = dcf_readCookie('dc-builder-edited-cell-index');   
        if(index >= 0)
        {   
            var $seg_cell = $builder.find('.segment-cell:eq('+index+')');             
            if($seg_cell.length)
            { 
                var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
               
                $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
                 
                 var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
                 if(id == seg_id)
                 {           
                     $builder.find('.segment-cell').removeClass('segment-in-edit');                                                    
                     $wrapper.find('.dc-cms-segment-editor .segment-options').css('display', 'none');
                     jQuery(this).css('display', 'block');
                     $seg_cell.addClass('segment-in-edit'); 
                 }            
             });            
            }
        }   
        
        $panel.find('.insert-list').change(function() 
        { 
            var val = jQuery(this).find('option:selected').val();
            $panel.find('.insert-type-desc .item').css('display', 'none');
            $panel.find('.insert-type-desc > .item[name="'+val+'"]').css('display', 'block'); 
        });
        $panel.find('.insert-type-desc > .item:first').css('display', 'block');     
        
       $editor.find('.segment-options').each(function() 
       { 
            dc_cb_setup_common_settings_btn(jQuery(this));
           
       }); 
        
    });
   
    function dc_cb_setup_common_settings_btn($obj)
    {
        var $wrapper = jQuery('#dc-cms-content-builder-wrapper'); 
        var $editor = $wrapper.find('.dc-cms-segment-editor'); 
        var $builder = $wrapper.find('.dc-cms-content-builder'); 
        
        var open = dcf_readCookie('dc-builder-common-column-settings');
        if(open == 'open')
        {
            $editor.find('.segment-options > .common-settings > .open-close-btn').addClass('active'); 
            $editor.find('.segment-options > .common-settings > .controls-wrapper').css('display', 'block');           
        } 
        
        var multitab_open = dcf_readCookie('dc-builder-common-multi-tabs');
        
        if(multitab_open >= 0)
        {
            var idx = multitab_open;
            var $tab = $obj.find('> .common-settings .multi-tab-content:eq('+idx+')');
            var $p = $tab.parent();
            
            $p.find('.multi-tab:eq('+idx+')').addClass('active');
            $p.find('.multi-tab-content:eq('+idx+')').css('display', 'block');    
        }
        
        $obj.find('> .common-settings > .controls-wrapper .clear-select-btn-opt').change(function() {
            
            // find parent
            var $seg_opt = jQuery(this).parent();
            var iter = 20;
            while(!$seg_opt.hasClass('segment-options'))
            {
                $seg_opt = $seg_opt.parent();
                
                iter--;
                if(iter == 0) { break; }    
            }
            
            var id = $seg_opt.find('input[name$="-core-id"]').val();
            var c = jQuery(this).val();
            
            $builder.find('.segment-cell').each(function() {
                
                var $seg = jQuery(this);
                var i = jQuery(this).find('input[name="segment-cell-id-'+id+'"]');   
                if(i.length > 0)
                {
                    var $icon = jQuery(this).find('.clear-btn');                    
                    $icon.removeClass('clear-btn-none clear-btn-left clear-btn-right clear-btn-both').addClass('clear-btn-'+c);
                    $icon.attr('title', 'Clear '+c)
                    $seg.css('clear', c);
                }
                
            });
            
        });
        
        
        $obj.find('> .common-settings > .controls-wrapper .float-select-btn-opt').change(function() {
            
            // find parent
            var $seg_opt = jQuery(this).closest('div.segment-options');
            
            var id = $seg_opt.find('input[name$="-core-id"]').val();
            var c = jQuery(this).val();
            
            $builder.find('.segment-cell').each(function() {
                
                var $seg = jQuery(this);
                var i = jQuery(this).find('input[name="segment-cell-id-'+id+'"]');   
                if(i.length > 0)
                {
                    var $icon = jQuery(this).find('.float-btn');                    
                    $icon.removeClass('float-btn-left float-btn-right').addClass('float-btn-'+c);
                    $icon.attr('title', 'Float '+c)
                    $seg.css('float', c);
                    
                    if(c == 'right') 
                    { 
                        $seg.find('.segment-content').addClass('segment-in-float-right'); 
                    } 
                    else { $seg.find('.segment-content').removeClass('segment-in-float-right'); }
                }
                
            });
            
        });        
        
        $obj.find('> .common-settings > .controls-wrapper .display-checkbox-btn').click(function() {
            
            // find parent
            var $seg_opt = jQuery(this).parent();
            var iter = 20;
            while(!$seg_opt.hasClass('segment-options'))
            {
                $seg_opt = $seg_opt.parent();
                
                iter--;
                if(iter == 0) { break; }    
            }
            
            var id = $seg_opt.find('input[name$="-core-id"]').val();
            var c = jQuery(this).is(':checked');
            
            $builder.find('.segment-cell').each(function() {
                
                var i = jQuery(this).find('input[name="segment-cell-id-'+id+'"]');   
                if(i.length > 0)
                {   
                    var $btn = jQuery(this).find('.display-btn');              
                    if(c)      
                    {   $btn.removeClass('display-btn-off').attr('title', 'Segment ON');
                        jQuery(this).find('.segment-content').removeClass('segment-in-no-display'); } 
                    else
                    {
                        $btn.addClass('display-btn-off').attr('title', 'Segment OFF');
                        jQuery(this).find('.segment-content').addClass('segment-in-no-display');
                    }
                }
                
            });
            
        });        
        
        $obj.find('> .common-settings > .open-close-btn').click(function() { 
       
           var d = jQuery(this).parent().find('> .controls-wrapper').css('display')
           
           if(d == 'block')
           {
                $editor.find('.segment-options > .common-settings > .open-close-btn').removeClass('active'); 
                $editor.find('.segment-options > .common-settings > .controls-wrapper').css('display', 'none'); 
                
                jQuery(this).parent().find('> .controls-wrapper').css('display', 'none');
                jQuery(this).removeClass('active');
                dcf_createCookie('dc-builder-common-column-settings', -1, 10); 
           } else
           {
                $editor.find('.segment-options > .common-settings > .open-close-btn').addClass('active'); 
                $editor.find('.segment-options > .common-settings > .controls-wrapper').css('display', 'block');                
               
                jQuery(this).parent().find('> .controls-wrapper').css('display', 'block');    
                jQuery(this).addClass('active');
                dcf_createCookie('dc-builder-common-column-settings', 'open', 10); 
           }               
           
       });        
       
       $obj.find('> .common-settings .multi-tabs-wrapper .multi-tab').click(function() {
           
           var $p = jQuery(this).parent();
           var index = $p.find('.multi-tab').index(this);
           var d = $p.find('.multi-tab-content:eq('+index+')').css('display');
           
           if(d == 'block')
           {   
               $p.find('.multi-tab').removeClass('active');
               $p.find('.multi-tab-content').css('display', 'none');
               dcf_createCookie('dc-builder-common-multi-tabs', -1, 10);     
           } else
           {           
               $p.find('.multi-tab').removeClass('active');
               jQuery(this).addClass('active');
               $p.find('.multi-tab-content').css('display', 'none');
               $p.find('.multi-tab-content:eq('+index+')').css('display', 'block');
               dcf_createCookie('dc-builder-common-multi-tabs', index, 10);
           }
           
            
       });
    }
    
    function dc_cb_setup_plus_btn($obj)
    {                                              
        if($obj.hasClass('deactivated')) { return; } 
        
        var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $seg_cell = $obj.parent().parent().parent();
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
          
         
         $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
             if(id == seg_id)
             {   
                 var $seg_opt = jQuery(this);        
                 var type = $seg_opt.find('input[name$="-core-type"]').val();
                 var up = parseInt($seg_opt.find('input[name$="-core-size-up"]').val());
                 var down = parseInt($seg_opt.find('input[name$="-core-size-down"]').val());                                  
                 
                 var len = cb_ss_sizes.length;
                 var index = -1;
                 var index_current = -1;
                 for(var i = 0; i < len; i++)
                 {
                     if((cb_ss_sizes[i].up == up) && (cb_ss_sizes[i].down == down))
                     {
                         index_current = i;
                     }
                     
                     if((cb_ss_sizes[i].up/cb_ss_sizes[i].down) > up/down)
                     {
                         var types_len = cb_ss_sizes[i].allowed.length;
                         var type_in = false;
                         for(var j = 0; j < types_len; j++)
                         {
                             if(type == cb_ss_sizes[i].allowed[j])
                             {
                                type_in = true;    
                             }
                         }
                         
                         if(type_in)
                         {
                             index = i;
                             break;                             
                         }
                     }
                 }
                 
                 if(index >= 0)
                 {
                     $seg_cell.removeClass(cb_ss_sizes[index_current].cls);
                     $seg_cell.addClass(cb_ss_sizes[index].cls);
                     $seg_cell.find('.segment-size-info > .up').text(cb_ss_sizes[index].up);
                     $seg_cell.find('.segment-size-info > .down').text(cb_ss_sizes[index].down);
                     $seg_opt.find('input[name$="-core-size-up"]').val(cb_ss_sizes[index].up);
                     $seg_opt.find('input[name$="-core-size-down"]').val(cb_ss_sizes[index].down);  
                 }
                 
             }                 
         });            
    }   
       
    function dc_cb_setup_minus_btn($obj)
    {
        if($obj.hasClass('deactivated')) { return; }        
        
        var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $seg_cell = $obj.parent().parent().parent();
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
          
         
         $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
             if(id == seg_id)
             {   
                 var $seg_opt = jQuery(this);        
                 var type = $seg_opt.find('input[name$="-core-type"]').val();
                 var up = parseInt($seg_opt.find('input[name$="-core-size-up"]').val());
                 var down = parseInt($seg_opt.find('input[name$="-core-size-down"]').val());                                  
                 
                 var len = cb_ss_sizes.length;
                 var index = -1;
                 var index_current = -1;
                 for(var i = (len-1); i >= 0; i--)
                 {
                     if((cb_ss_sizes[i].up == up) && (cb_ss_sizes[i].down == down))
                     {
                         index_current = i;
                     }
                     
                     if((cb_ss_sizes[i].up/cb_ss_sizes[i].down) < up/down)
                     {
                         var types_len = cb_ss_sizes[i].allowed.length;
                         var type_in = false;
                         for(var j = 0; j < types_len; j++)
                         {
                             if(type == cb_ss_sizes[i].allowed[j])
                             {
                                type_in = true;    
                             }
                         }
                         
                         if(type_in)
                         {
                             index = i;
                             break;                             
                         }
                     }
                 }
                 
                 if(index >= 0)
                 {
                     $seg_cell.removeClass(cb_ss_sizes[index_current].cls);
                     $seg_cell.addClass(cb_ss_sizes[index].cls);
                     $seg_cell.find('.segment-size-info > .up').text(cb_ss_sizes[index].up);
                     $seg_cell.find('.segment-size-info > .down').text(cb_ss_sizes[index].down);
                     $seg_opt.find('input[name$="-core-size-up"]').val(cb_ss_sizes[index].up);
                     $seg_opt.find('input[name$="-core-size-down"]').val(cb_ss_sizes[index].down);
                     //alert(cb_ss_sizes[index].cls);    
                 }
                 
             }                 
         });            
    }   
    
    function dc_cb_setup_max_btn($obj)
    {                                              
        if($obj.hasClass('deactivated')) { return; }
        
        var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $seg_cell = $obj.parent().parent().parent();
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
          
         
         $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
             if(id == seg_id)
             {   
                 var $seg_opt = jQuery(this);        
                 var type = $seg_opt.find('input[name$="-core-type"]').val();
                 var up = parseInt($seg_opt.find('input[name$="-core-size-up"]').val());
                 var down = parseInt($seg_opt.find('input[name$="-core-size-down"]').val());                                  
                 
                 var len = cb_ss_sizes.length;
                 var index = -1;
                 var index_current = -1;
                 for(var i = 0; i < len; i++)
                 {
                     if((cb_ss_sizes[i].up == up) && (cb_ss_sizes[i].down == down))
                     {
                         index_current = i;
                     }                     
                 }
                 
                 for(var i = len-1; i >= 0; i--)
                 {                     
                     var types_len = cb_ss_sizes[i].allowed.length;
                     var type_in = false;
                     for(var j = 0; j < types_len; j++)
                     {
                         if(type == cb_ss_sizes[i].allowed[j])
                         {
                            type_in = true;    
                         }
                     }
                     
                     if(type_in)
                     {
                         index = i;
                         break;                             
                     }
                 }                 
                 
                 if(index >= 0)
                 {
                     $seg_cell.removeClass(cb_ss_sizes[index_current].cls);
                     $seg_cell.addClass(cb_ss_sizes[index].cls);
                     $seg_cell.find('.segment-size-info > .up').text(cb_ss_sizes[index].up);
                     $seg_cell.find('.segment-size-info > .down').text(cb_ss_sizes[index].down);
                     $seg_opt.find('input[name$="-core-size-up"]').val(cb_ss_sizes[index].up);
                     $seg_opt.find('input[name$="-core-size-down"]').val(cb_ss_sizes[index].down);  
                 }
                 
             }                 
         });            
    }  
    
    function dc_cb_setup_min_btn($obj)
    {
        if($obj.hasClass('deactivated')) { return; }        
        
        var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $seg_cell = $obj.parent().parent().parent();
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
          
         
         $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
             if(id == seg_id)
             {   
                 var $seg_opt = jQuery(this);        
                 var type = $seg_opt.find('input[name$="-core-type"]').val();
                 var up = parseInt($seg_opt.find('input[name$="-core-size-up"]').val());
                 var down = parseInt($seg_opt.find('input[name$="-core-size-down"]').val());                                  
                 
                 var len = cb_ss_sizes.length;
                 var index = -1;
                 var index_current = -1;
                 for(var i = (len-1); i >= 0; i--)
                 {
                     if((cb_ss_sizes[i].up == up) && (cb_ss_sizes[i].down == down))
                     {
                         index_current = i;
                     }                     
                 }

                 for(var i = 0; i < len; i++)
                 {
                     var types_len = cb_ss_sizes[i].allowed.length;
                     var type_in = false;
                     for(var j = 0; j < types_len; j++)
                     {
                         if(type == cb_ss_sizes[i].allowed[j])
                         {
                            type_in = true;    
                         }
                     }
                     
                     if(type_in)
                     {
                         index = i;
                         break;                             
                     }
                 }
                 
                 if(index >= 0)
                 {
                     $seg_cell.removeClass(cb_ss_sizes[index_current].cls);
                     $seg_cell.addClass(cb_ss_sizes[index].cls);
                     $seg_cell.find('.segment-size-info > .up').text(cb_ss_sizes[index].up);
                     $seg_cell.find('.segment-size-info > .down').text(cb_ss_sizes[index].down);
                     $seg_opt.find('input[name$="-core-size-up"]').val(cb_ss_sizes[index].up);
                     $seg_opt.find('input[name$="-core-size-down"]').val(cb_ss_sizes[index].down); 
                 }
                 
             }                 
         });            
    }       

    function dc_cb_setup_float_btn($obj)
    {
         var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $editor = $wrapper.find('.dc-cms-segment-editor');
         var $seg_cell = $obj.closest('li.segment-cell');         
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
         
         $editor.find('.segment-options').each(function() 
         {             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]');
             if(id.length)
             {     
                if($obj.hasClass('float-btn-left'))
                {
                    jQuery(this).find('.common-settings .float-select-btn-opt ').val('right');
                    $seg_cell.find('.segment-content').addClass('segment-in-float-right');
                    $seg_cell.css('float', 'right');
                    $obj.removeClass('float-btn-left').addClass('float-btn-right').attr('title', 'Float right');    
                } else
                {
                    jQuery(this).find('.common-settings .float-select-btn-opt').val('left');
                    $seg_cell.find('.segment-content').removeClass('segment-in-float-right');
                    $seg_cell.css('float', 'left');
                    $obj.removeClass('float-btn-right').addClass('float-btn-left').attr('title', 'Float left');  
                }   
                                
             }
         });    
                                     
    }     
    
    function dc_cb_setup_clear_btn($obj)
    {
         var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $editor = $wrapper.find('.dc-cms-segment-editor');
         var $seg_cell = $obj.closest('li.segment-cell');         
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
         
         $editor.find('.segment-options').each(function() 
         {             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]');
             if(id.length)
             {     
                if($obj.hasClass('clear-btn-none'))
                {
                    jQuery(this).find('.common-settings .clear-select-btn-opt').val('left');                
                    $seg_cell.css('clear', 'left');
                    $obj.removeClass('clear-btn-none').addClass('clear-btn-left').attr('title', 'Clear left');    
                } else
                if($obj.hasClass('clear-btn-left'))
                {
                    jQuery(this).find('.common-settings .clear-select-btn-opt').val('right');                
                    $seg_cell.css('clear', 'right');
                    $obj.removeClass('clear-btn-left').addClass('clear-btn-right').attr('title', 'Clear right');    
                } else
                if($obj.hasClass('clear-btn-right'))
                {
                    jQuery(this).find('.common-settings .clear-select-btn-opt').val('both');                
                    $seg_cell.css('clear', 'both');
                    $obj.removeClass('clear-btn-right').addClass('clear-btn-both').attr('title', 'Clear both');    
                } else                
                if($obj.hasClass('clear-btn-both'))
                {
                    jQuery(this).find('.common-settings .clear-select-btn-opt').val('none');                
                    $seg_cell.css('clear', 'none');
                    $obj.removeClass('clear-btn-both').addClass('clear-btn-none').attr('title', 'Clear none');    
                }
                                
             }
         });    
                                     
    }        
    
    function dc_cb_setup_display_btn($obj)
    {
         var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $editor = $wrapper.find('.dc-cms-segment-editor');
         var $seg_cell = $obj.closest('li.segment-cell');         
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
         
         $editor.find('.segment-options').each(function() 
         {             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]');
             if(id.length)
             {     
                if($obj.hasClass('display-btn-off'))
                {
                    jQuery(this).find('.common-settings .display-checkbox-btn').attr('checked', true);
                    $seg_cell.find('.segment-content').removeClass('segment-in-no-display');
                    $obj.removeClass('display-btn-off').attr('title', 'Segment ON');    
                } else
                {
                    jQuery(this).find('.common-settings .display-checkbox-btn').attr('checked', false);
                    $seg_cell.find('.segment-content').addClass('segment-in-no-display'); 
                    $obj.addClass('display-btn-off').attr('title', 'Segment OFF');
                }                   
             }
         });    
                                     
    } 
    
    function dc_cb_setup_pin_btn($obj)
    {
         var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $seg_cell = $obj.parent().parent().parent().parent();
         var $seg_cell_index = $builder.find('.segment-cell').index($seg_cell);
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
         var $note_editor = $wrapper.find('.dc-cms-note-editor');  
         
         if($seg_cell.hasClass('segment-in-pined'))
         {
            $builder.find('.segment-cell .right-panel .pin-btn').removeClass('active');     
            $builder.find('.segment-in-pined').removeClass('segment-in-pined');
         } else
         {
            $builder.find('.segment-cell .right-panel .pin-btn').removeClass('active'); 
            $builder.find('.segment-in-pined').removeClass('segment-in-pined');
            $seg_cell.addClass('segment-in-pined');
            $obj.addClass('active');
         }              
    }                
       
    function dc_cb_segment_note_update($note)
    {
        var txt = $note.find('textarea[name$="-core-note"]').val();
        var seg_id = $note.find('input[name^="segment-cell-id-"]').val();
     
         var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $seg_cell = null;
         
         $builder.find('.segment-cell').each(function() {
             
             var id = jQuery(this).find('input[name^="segment-cell-id-"]').val();
             if(id == seg_id) { $seg_cell = jQuery(this); }  
         });               
     
        txt = jQuery.trim(txt);

        if(txt != '')
        {
            var text = txt;
            if(text.length > 15)
            {
                text = txt.replace(/^(.{15}[^\s]*).*/, "$1") + '..';
            }
            var $center_panel = $seg_cell.find('.center-panel');
            $center_panel.find('.note').text(text).css('display', 'block');
            $center_panel.find('.name').removeClass('name-alone');
            
            var $book = $center_panel.find('.help-book-popup');
            if($book.length)
            {
                $book.find('div').text(txt);    
            } else
            {
                $center_panel.find('.name').append('<div class="help-book-popup"><div>'+txt+'</div></div>');       
            }
        } else
        {
           var $center_panel = $seg_cell.find('.center-panel');
           
           $center_panel.find('.note').css('display', 'none');
           $center_panel.find('.name').addClass('name-alone');
           $center_panel.find('.help-book-popup').remove(); 
        }     
    }     
    
    function dc_cb_segment_note_close_btn($btn)
    {
        var $note = $btn.parent();
        dc_cb_segment_note_update($note);
        $note.css('display', 'none');
    }         
       
    function dc_cb_setup_edit_btn($obj)
    {
         var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $seg_cell = $obj.parent().parent().parent();
         var $seg_cell_index = $builder.find('.segment-cell').index($seg_cell);
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
         var $note_editor = $wrapper.find('.dc-cms-note-editor');  
         
         dcf_createCookie('dc-builder-edited-cell-index', $seg_cell_index, 10); 
         
         $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
             if(id == seg_id)
             {           
                 $builder.find('.segment-cell').removeClass('segment-in-edit');                                                    
                 $wrapper.find('.dc-cms-segment-editor .segment-options').css('display', 'none');
                 jQuery(this).css('display', 'block');
                 $seg_cell.addClass('segment-in-edit'); 
                 
             var note_displayed = false;    
             $note_editor.find('.segment-note').each(function() 
             { 
                 var dis = jQuery(this).css('display'); 
                 if(dis == 'block') 
                 { 
                     note_displayed = true; 
                     dc_cb_segment_note_update(jQuery(this));
                 }
             });    
             
             if(note_displayed)
             {
                 $note_editor.find('.segment-note').each(function() {
                     
                     var id = jQuery(this).find('input[name="segment-cell-id-'+seg_id+'"]').val();
                     if(id == seg_id)
                     {     
                        $note_editor.find('.segment-note').css('display', 'none'); 
                        jQuery(this).css('display', 'block');       
                     }   
                 });
             }
                           
             }                 
         });            
    }   
    
    

    function dc_cb_setup_clone_btn($obj)
    {
         var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $seg_cell = $obj.parent().parent().parent();
         var $seg_cell_index = $builder.find('.segment-cell').index($seg_cell);
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
         
         var date = new Date();
         var milliseconds = date.getTime();
         var clone_time = Math.ceil(milliseconds / 1000);         
         
         var message = $obj.attr('title');
         
         $seg_cell.addClass('segment-in-clone');
         var action = confirm(message);                 
         
         if(action)
         {                  
            var len = $wrapper.find('.segment-cell').length;
            var new_cell_id = 0;             
            for(var i = 0; i < len; i++)
            {
                var id = parseInt($wrapper.find('.segment-cell:eq('+i+')').find('input[name^="segment-cell-id-"]').val()); 
                if(id > new_cell_id) { new_cell_id = id; }
            }
            new_cell_id += 1;            

            $clone = $builder.find('.segment-cell:eq('+$seg_cell_index+')').clone();
       
            $clone.removeClass('segment-in-edit'); 
            $clone.removeClass('segment-in-clone');
            $clone.removeClass('segment-in-pined');  
            $clone.find('.right-panel .pin-btn').removeClass('active'); 
            
            $clone.find('*').each(function()
            { 
                var name = jQuery(this).attr('name');
                if(name !== undefined)
                {
                    if(name.search('segment-cell-id-') > -1)
                    {
                        jQuery(this).val(new_cell_id);    
                    }
                    
                    if(name.search('segment-cell-id-'+seg_id) > -1)
                    {                            
                        name = name.replace('segment-cell-id-'+seg_id, 'segment-cell-id-'+new_cell_id);
                        jQuery(this).attr('name', name);
                    }
                    
                    if(name.search('segment-cell-time') > -1)
                    {
                        jQuery(this).text(clone_time);
                    }                    
                }            
                
            });          
            
            $seg_cell.after($clone);
            dcf_cb_update_segments_numeration();
            var $new_cell = $seg_cell.next();                          
            
           $new_cell.find('.left-panel > .edit-btn').click(function() { dc_cb_setup_edit_btn(jQuery(this)); }); 
           $new_cell.find('.left-panel > .delete-btn').click(function() { dc_cb_setup_delete_btn(jQuery(this)); }); 
           $new_cell.find('.left-panel > .clone-btn').click(function() { dc_cb_setup_clone_btn(jQuery(this)); }); 
           $new_cell.find('.left-panel > .note-btn').click(function() { dc_cb_setup_note_btn(jQuery(this)); });  
           $new_cell.find('.right-panel > .plus-btn').click(function() { dc_cb_setup_plus_btn(jQuery(this)); });
           $new_cell.find('.right-panel > .minus-btn').click(function() { dc_cb_setup_minus_btn(jQuery(this)); }); 
           $new_cell.find('.right-panel > .min-btn').click(function() { dc_cb_setup_min_btn(jQuery(this)); });
           $new_cell.find('.right-panel > .max-btn').click(function() { dc_cb_setup_max_btn(jQuery(this)); });            
           $new_cell.find('.right-panel .pin-btn').click(function() { dc_cb_setup_pin_btn(jQuery(this)); });   
           $new_cell.find('.right-panel .display-btn').click(function() { dc_cb_setup_display_btn(jQuery(this)); }); 
           $new_cell.find('.right-panel .float-btn').click(function() { dc_cb_setup_float_btn(jQuery(this)); }); 
           $new_cell.find('.right-panel .clear-btn').click(function() { dc_cb_setup_clear_btn(jQuery(this)); });    
             
             $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
                 
                 var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
                 if(id == seg_id)
                 {           
                    $clone = jQuery(this).clone();
                    $clone.css('display', 'none');

                    
                    $clone.find('*').each(function()
                    {                                                 
                        var name = jQuery(this).attr('name');
                        if(name !== undefined)
                        {
                            if(name.search('-core-id') > -1)
                            {
                                jQuery(this).val(new_cell_id);    
                            }                        

                            if(name.search('-core-time') > -1)
                            {
                                jQuery(this).val(clone_time);    
                            }      
                            
                            if(name.search('segment-cell-data-'+seg_id) > -1)
                            {                            
                                name = name.replace('segment-cell-data-'+seg_id, 'segment-cell-data-'+new_cell_id);
                                jQuery(this).attr('name', name);
                            }
                        }
                        
                        var id = jQuery(this).attr('id');
                        if(id !== undefined)
                        {
                            id = id.replace('segment-cell-data-'+seg_id, 'segment-cell-data-'+new_cell_id); 
                            jQuery(this).attr('id', id); 
                        } 
                    });  
                    
                    jQuery(this).after($clone);
                    var $new_opt = jQuery(this).next();  
                    
                   $new_opt.dcf_setupIntCtrl();
                   $new_opt.dcf_setupExcludableCBoxes(); 
                   $new_opt.dcf_setupShortCodeTextAreaTool();
                   dc_cb_setup_common_settings_btn($new_opt);
                   dc_cb_setup_basic_slider_type($new_opt);    
                   $new_opt.dcf_setupColorPicker();
                   // $new_opt.find('.dc-cms-submit-image-btn').each(function() { dcf_setupLoadImageBtn(jQuery(this)); });                                                             
                   $new_opt.dcf_setupCMSLoadImageBtn();
                 }                 
             });   
             
             $wrapper.find('.dc-cms-note-editor .segment-note').each(function() {
                 
                 var id = jQuery(this).find('input[name="segment-cell-id-'+seg_id+'"]').val();
                 if(id == seg_id)
                 {           
                    $clone = jQuery(this).clone();
                    $clone.css('display', 'none');
                    jQuery(this).after($clone);
                    var $new_note = jQuery(this).next();
                       
                    $new_note.find('*').each(function()
                    { 
                        var name = jQuery(this).attr('name');
                        if(name !== undefined)
                        {
                            if(name.search('segment-cell-id-') > -1)
                            {
                                jQuery(this).val(new_cell_id);    
                            }                     
                            
                            if(name.search('segment-cell-data-'+seg_id) > -1)
                            {                            
                                name = name.replace('segment-cell-data-'+seg_id, 'segment-cell-data-'+new_cell_id);
                                jQuery(this).attr('name', name);
                            }
                        }
                    });
                    
                    $new_note.find('.note').blur(function() { dc_cb_segment_note_update(jQuery(this).parent()); });
                    $new_note.find('.close-btn').click(function() { dc_cb_segment_note_close_btn(jQuery(this)); });   
                                           
                                                          
                 }                 
             });   
             
             $seg_cell.removeClass('segment-in-clone');        
             
             dcf_cb_update_segments_order();    
       } else
       {
            $seg_cell.removeClass('segment-in-clone');    
       }         
    }       

    function dc_cb_before_segment_delete($li)
    {        
        var seg_type = $li.find('span[name="segment-cell-type"]').text();
        var seg_time = $li.find('span[name="segment-cell-time"]').text();   
  
        if(seg_type == 'wgt-twitter')
        {        
            dcf_showAjaxProgrss();              
          
          jQuery.ajax({
           type: "POST",
           url: dccp_s.admin_url+'admin-ajax.php',
           data: { action: 'dc_builder_before_delete', dc_action:'twitter_seg', time:seg_time},
           dataType: 'text',
           success: function(str){           
                   if(str != 'error')
                   {                                    
                   }                 
                   
                   dcf_hideAjaxProgrss(); 
                   
               }}); // ajax call        
        }
           
    }  
    
    function dc_cb_setup_delete_btn($obj)
    {
             var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
             var $seg_cell = $obj.parent().parent().parent();
             
             
             var message = $obj.attr('title');
             
             $seg_cell.addClass('segment-in-delete');
             var action = confirm(message);                 
             
             if(action)
             {
                var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();  
                
                 $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
                     
                     var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
                     if(id == seg_id)
                     {   
                         dc_cb_before_segment_delete($seg_cell);                                                                                    
                         $seg_cell.remove();
                         jQuery(this).remove(); 
                     }                 
                 });             
                 
                 $wrapper.find('.dc-cms-note-editor .segment-note').each(function() {
                    var id = jQuery(this).find('input[name="segment-cell-id-'+seg_id+'"]').val();    
                     if(id == seg_id)
                     {                                                                                       
                         jQuery(this).remove(); 
                     }                        
                 });   
                 
                 dcf_createCookie('dc-builder-edited-cell-index', -1, 10); 
                 dcf_cb_update_segments_order();
                 dcf_cb_update_segments_numeration();  
                  
             } else
             {
                $seg_cell.removeClass('segment-in-delete'); 
             }        
    } 
    
    function dc_cb_setup_note_btn($obj)
    {
         var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
         var $builder = $wrapper.find('.dc-cms-content-builder');
         var $note_editor = $wrapper.find('.dc-cms-note-editor');
         var $seg_cell = $obj.parent().parent().parent();
         var $seg_cell_index = $builder.find('.segment-cell').index($seg_cell); 
         var seg_id = $seg_cell.find('input[name^="segment-cell-id-"]').val();
         
         dcf_createCookie('dc-builder-edited-cell-index', $seg_cell_index, 10);        
         
         $wrapper.find('.dc-cms-segment-editor .segment-options').each(function() {
             
             var id = jQuery(this).find('input[name="segment-cell-data-'+seg_id+'-core-id"]').val();
             if(id == seg_id)
             {           
                 $builder.find('.segment-cell').removeClass('segment-in-edit');                                                    
                 $wrapper.find('.dc-cms-segment-editor .segment-options').css('display', 'none');
                 jQuery(this).css('display', 'block');
                 $seg_cell.addClass('segment-in-edit'); 
             }
         });         
         
         $note_editor.find('.segment-note').each(function() {
         
             var dis = jQuery(this).css('display'); 
             if(dis == 'block') 
             { 
                 dc_cb_segment_note_update(jQuery(this));
             }         
             
             var id = jQuery(this).find('input[name^="segment-cell-id-"]').val();
             if(id == seg_id)
             {           
                 var dis = jQuery(this).css('display');
                 
                 
                 if(dis == 'block')
                 {
                     
                    $note_editor.find('.segment-note').css('display', 'none');
                    dc_cb_segment_note_update(jQuery(this));
               
                        
                 } else
                 {                 
                     $note_editor.find('.segment-note').css('display', 'none');
                     jQuery(this).css('display', 'block'); 
                 }
             }                 
         });         

    }   

    
  /*  
    jQuery('#dc-cms-content-builder-wrapper .segment-cell .left-panel > .plus-btn').click(function() {
          dc_cb_setup_plus_btn(jQuery(this));
    });

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .left-panel > .minus-btn').click(function() {
          dc_cb_setup_minus_btn(jQuery(this));
    });
   */    
    jQuery('#dc-cms-content-builder-wrapper .segment-cell .left-panel > .edit-btn').click(function() {
          dc_cb_setup_edit_btn(jQuery(this));
    });

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .left-panel > .clone-btn').click(function() {
          dc_cb_setup_clone_btn(jQuery(this));
    });

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .right-panel > .plus-btn').click(function() {
          dc_cb_setup_plus_btn(jQuery(this));
    });
    
    jQuery('#dc-cms-content-builder-wrapper .segment-cell .right-panel > .minus-btn').click(function() {
          dc_cb_setup_minus_btn(jQuery(this));
    });    

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .right-panel > .max-btn').click(function() {
          dc_cb_setup_max_btn(jQuery(this));
    });     

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .right-panel > .min-btn').click(function() {
          dc_cb_setup_min_btn(jQuery(this));
    });   

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .right-panel .pin-btn').click(function() {
          dc_cb_setup_pin_btn(jQuery(this));
    });   

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .right-panel .display-btn').click(function() {
          dc_cb_setup_display_btn(jQuery(this));
    }); 

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .right-panel .float-btn').click(function() {
          dc_cb_setup_float_btn(jQuery(this));
    }); 

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .right-panel .clear-btn').click(function() {
          dc_cb_setup_clear_btn(jQuery(this));
    }); 
    
    jQuery('#dc-cms-content-builder-wrapper .dc-cms-content-builder-control-panel > .meta-opt-btn').click(function() 
    { 
        var $panel = jQuery(this).parent();        
        var $meta = $panel.find('div.builder-meta');
        var d = $meta.css('display');
        
        if(d == 'block')
        {
            $meta.css('display', 'none');  
        } else
        {
            $meta.css('display', 'block');    
        }
    });

    jQuery('#dc-cms-content-builder-wrapper .dc-cms-content-builder-control-panel > .insert-place-btn').click(function() 
    { 
        if(jQuery(this).hasClass('state-start'))
        {
            jQuery(this).removeClass('state-start').attr('title', 'Insert after');
        } else
        {
            jQuery(this).addClass('state-start').attr('title', 'Insert before');
        }   
    });
    
    jQuery('#dc-cms-content-builder-wrapper .dc-cms-content-builder-control-panel > .insert-btn').click(function() 
    { 
        var $insert_btn = jQuery(this);
        if($insert_btn.data('dc_locked') == 'yes') { return; }
        $insert_btn.data('dc_locked', 'yes');
        
        var panel = jQuery(this).parent();
        var insert_at_start = panel.find('.insert-place-btn').hasClass('state-start');
        var type = panel.find('select.insert-list option:selected').val();
        var name = panel.find('select.insert-list option:selected').text();
        
        var $wrapper = jQuery('#dc-cms-content-builder-wrapper');
        var $builder = $wrapper.find('.dc-cms-content-builder');             
        var $editor = $wrapper.find('.dc-cms-segment-editor');
        var $note_editor = $wrapper.find('.dc-cms-note-editor'); 
        var len = $wrapper.find('.segment-cell').length;
        var cell_id = 0;             
        for(var i = 0; i < len; i++)
        {
            var id = parseInt($wrapper.find('.segment-cell:eq('+i+')').find('input[name^="segment-cell-id-"]').val());  
            if(id > cell_id) { cell_id = id; }
        }

        cell_id += 1;        
       
                dcf_showAjaxProgrss();              
      
            jQuery.ajax({
           type: "POST",
           url: dccp_s.admin_url+'admin-ajax.php',
           data: { action: 'dc_builder_add_cell', cell_id:cell_id, type:type, name:name},
           dataType: 'text',
           success: function(str){           
                   if(str != 'error')
                   {  
                       var $cell = null;
                       var $pined = $builder.find('.segment-in-pined:first'); 
                       if($pined.length)
                       {
                            if(insert_at_start)
                            {
                                $pined.before(str);
                                $cell = $pined.prev();                            
                            } else
                            {
                                $pined.after(str);
                                $cell = $pined.next();
                            }                           
                       } else
                       {                                                                        
                            if(insert_at_start)
                            {
                                $builder.find('li.segment-cell:first').before(str);
                                $cell = $builder.find('li.segment-cell:first'); 
                            } else
                            {
                                $builder.find('div.dc-cms-clear:last').before(str);
                                $cell = $builder.find('li.segment-cell:last');                                 
                            } 
                       }
                       
                       $cell.find('.left-panel > .edit-btn').click(function() { dc_cb_setup_edit_btn(jQuery(this)); }); 
                       $cell.find('.left-panel > .delete-btn').click(function() { dc_cb_setup_delete_btn(jQuery(this)); }); 
                       $cell.find('.left-panel > .clone-btn').click(function() { dc_cb_setup_clone_btn(jQuery(this)); }); 
                       $cell.find('.left-panel > .note-btn').click(function() { dc_cb_setup_note_btn(jQuery(this)); }); 
                       $cell.find('.right-panel > .plus-btn').click(function() { dc_cb_setup_plus_btn(jQuery(this)); });
                       $cell.find('.right-panel > .minus-btn').click(function() { dc_cb_setup_minus_btn(jQuery(this)); });                        
                       $cell.find('.right-panel > .min-btn').click(function() { dc_cb_setup_min_btn(jQuery(this)); });
                       $cell.find('.right-panel > .max-btn').click(function() { dc_cb_setup_max_btn(jQuery(this)); }); 
                       $cell.find('.right-panel .pin-btn').click(function() { dc_cb_setup_pin_btn(jQuery(this)); }); 
                       $cell.find('.right-panel .display-btn').click(function() { dc_cb_setup_display_btn(jQuery(this)); });
                       $cell.find('.right-panel .float-btn').click(function() { dc_cb_setup_float_btn(jQuery(this)); }); 
                       $cell.find('.right-panel .clear-btn').click(function() { dc_cb_setup_clear_btn(jQuery(this)); });                                                             
                                                            
                       dcf_cb_update_segments_order();
                       dcf_cb_update_segments_numeration();                                                            
                   }                 
                   
                   dcf_hideAjaxProgrss(); 
                   
               }}); // ajax call        


                dcf_showAjaxProgrss();               
               
               
            jQuery.ajax({
           type: "POST",
           url: dccp_s.admin_url+'admin-ajax.php',
           data: { action: 'dc_builder_add_cell_opt', cell_id:cell_id, type:type, name:name},
           dataType: 'text',
           success: function(str){           
                   if(str != 'error')
                   {                                                                     
                       $editor.append(str);
                       var $opt = $editor.find('.segment-options:last');
                       
                       $opt.dcf_setupIntCtrl();
                       $opt.dcf_setupExcludableCBoxes(); 
                       $opt.dcf_setupShortCodeTextAreaTool();
                       dc_cb_setup_common_settings_btn($opt);
                       dc_cb_setup_basic_slider_type($opt);
                       $opt.dcf_setupColorPicker();
                      // $opt.find('.dc-cms-submit-image-btn').each(function() { dcf_setupLoadImageBtn(jQuery(this)); });
                       $opt.dcf_setupCMSLoadImageBtn();                                           
                   }                   
                   
                   dcf_hideAjaxProgrss(); 
                   
               }}); // ajax call     
               
                dcf_showAjaxProgrss();               
               
               
            jQuery.ajax({
           type: "POST",
           url: dccp_s.admin_url+'admin-ajax.php',
           data: { action: 'dc_builder_add_cell_note', cell_id:cell_id, type:type, name:name},
           dataType: 'text',
           success: function(str){           
                   if(str != 'error')
                   {                                                                     
                       $note_editor.append(str);
                       var $note = $note_editor.find('.segment-note:last'); 
                       $note.find('.note').blur(function() { dc_cb_segment_note_update(jQuery(this).parent()); }); 
                       $note.find('.close-btn').click(function() { dc_cb_segment_note_close_btn(jQuery(this)); });
                                                               
                   }                   
                   
                   dcf_hideAjaxProgrss(); 
                   $insert_btn.data('dc_locked', 'no');
                   
               }}); // ajax call                            
        
        
    });
    

    
    jQuery('#dc-cms-content-builder-wrapper .segment-cell .left-panel > .delete-btn').click(function() {
    
           dc_cb_setup_delete_btn(jQuery(this));
    });    

    jQuery('#dc-cms-content-builder-wrapper .segment-cell .left-panel > .note-btn').click(function() {
    
           dc_cb_setup_note_btn(jQuery(this));
    }); 


    jQuery('#dc-cms-content-builder-wrapper .dc-cms-note-editor .segment-note > .note').blur(function() {
    
           dc_cb_segment_note_update(jQuery(this).parent()); 
    }); 
        
    jQuery('#dc-cms-content-builder-wrapper .dc-cms-note-editor .segment-note > .close-btn').click(function() {
    
           dc_cb_segment_note_close_btn(jQuery(this)); 
    });        
        
  
    
    /******************************
    * Textarea insert at cursor
    *******************************/      
    jQuery.fn.extend({
        dcf_insertAtCaret: function(myValue) {
            
            var obj;
              if( typeof this[0].name != 'undefined' ) { obj = this[0]; }
              else { obj = this; }            
            
            if (document.selection) {
                    obj.focus();
                    sel = document.selection.createRange();
                    sel.text = myValue;
                    obj.focus();
                    
                    
            }
            else 
            if (obj.selectionStart || obj.selectionStart == '0') 
            { 
                
                var startPos = obj.selectionStart;
                var endPos = obj.selectionEnd;
                var scrollTop = obj.scrollTop;
                obj.value = obj.value.substring(0, startPos)+myValue+obj.value.substring(endPos,obj.value.length);
                obj.focus();
                obj.selectionStart = startPos + myValue.length;
                obj.selectionEnd = startPos + myValue.length;
                obj.scrollTop = scrollTop;
            } else {
                obj.value += myValue;
                obj.focus();
            }
        }
    });    
        
    
    /******************************
    * Excludable checkboxes field
    *******************************/   
    jQuery.fn.extend({
        dcf_setupExcludableCBoxes: function(){
            jQuery(this).find('.dc-cms-excludable-checkbox-field, .dc-cms-excludable-checkbox-field-nomargin').each(function()
    {
            if(jQuery(this).hasClass('check-if-empty-field'))
            {        
                var $list = jQuery(this).find('input[type="checkbox"]:checked');            
                if($list.length == 0) { jQuery(this).find('input:first').attr('checked', true); }
            }
            
        
            jQuery(this).find('input[type=checkbox]').click(function(){
                if(jQuery(this).is(':checked'))
                {
                    var depth = 3;
                    var p = jQuery(this).parent();
                    
                    while(depth > 0)
                    {                    
                        if(p.hasClass('dc-cms-excludable-checkbox-field') || p.hasClass('dc-cms-excludable-checkbox-field-nomargin'))
                        {
                            break;
                        } 
                        p = p.parent();                       
                        depth--;
                    }
                    
                    p.find('input[type=checkbox]').not(this).attr('checked', false);
                } 
            });         
        
    });
        }
    });        
    jQuery('body').dcf_setupExcludableCBoxes();        
    
        /******************************
        * DC CMS Menu System
        *******************************/  
        var $ms_wrapper = jQuery('#dc-cms-menu-sys-content-wrappper');
         
        /* Left column */
        $ms_wrapper.find('.menu-sys-left-data-column .custom-link-panel .custom-link-name').focus(function() 
        { 
            var title = jQuery(this).attr('title'); 

            if(jQuery(this).val() == title)
            {   
                jQuery(this).val('');
                jQuery(this).removeClass('custom-link-name').addClass('custom-link-name-focus');
            }       
        });

        $ms_wrapper.find('.menu-sys-left-data-column .custom-link-panel .custom-link-name').blur(function() 
        { 
            var title = jQuery(this).attr('title'); 

            if(jQuery(this).val() == '' || jQuery(this).val() == title)
            {   
                jQuery(this).val(title);
                jQuery(this).removeClass('custom-link-name-focus').addClass('custom-link-name');
            }      
        });             
        
        /* Center column */
        $ms_wrapper.find('.menu-sys-center-data-column .center-column-submit-panel .dc-cms-submit-delete-btn').click(function() 
        { 
            if(confirm('Delete this whole menu?')) 
            {    
                return true;
            } else
            return false;     
        });            
                
        /* Right column */        
        $ms_wrapper.find('.menu-sys-right-data-column .create-new-menu-panel .menu-name').focus(function() 
        { 
            var title = jQuery(this).attr('title'); 

            if(jQuery(this).val() == title)
            {   
                jQuery(this).val('');
                jQuery(this).removeClass('menu-name').addClass('menu-name-focus');
            }       
        });

        $ms_wrapper.find('.menu-sys-right-data-column .create-new-menu-panel .menu-name').blur(function() 
        {    
            var title = jQuery(this).attr('title'); 
            
            if(jQuery(this).val() == '' || jQuery(this).val() == title)
            {   
                jQuery(this).val(title);
                jQuery(this).removeClass('menu-name-focus').addClass('menu-name');
            }      
        });   
        
      
        function dc_ms_show_opt_btn($obj)
        {
            var $li = $obj.parent();
            while(!$li.is('li'))
            {
                $li = $li.parent();
            }            
            $li.find('.item-data:first').slideToggle(400);
            
            if($li.hasClass('li-opened')) 
            { $li.removeClass('li-opened'); $obj.removeClass('active'); } 
            else { $li.addClass('li-opened'); $obj.addClass('active'); }            
        }     

        function dc_ms_node_pin_btn($obj)
        {
            var $menu = $obj.closest('ol.dc-cms-menu-sys-sortable'); 
            var $li = $obj.parent();
            
            if($li.hasClass('li-pined')) 
            { $li.removeClass('li-pined'); $obj.removeClass('active'); } 
            else 
            { 
                $menu.find('li .li-pined').removeClass('li-pined');
                $menu.find('li .node-pin-btn').removeClass('active');                
                
                $li.addClass('li-pined'); 
                $obj.addClass('active'); 
            }            
        }   
        
        function dc_ms_node_delete_btn($obj)
        {
        //    var color = $obj.parent().css('border-left-color');
        //    var $item_data_wrapper = $obj.parent().parent().find('.item-data');
            
        //    $obj.parent().css('border-color', '#D00');
            $obj.parent().addClass('in-delete-act');
       //     $item_data_wrapper.css('background-color', '#F5E8E8');
            if(confirm('Delete this menu item and all assigned child items?')) 
            {   
                var $li = $obj.parent();
                while(!$li.is('li'))
                {
                    $li = $li.parent();
                }                 
                $li.remove(); 
            } else
            {
                $obj.parent().removeClass('in-delete-act');
          //      $obj.parent().css('border-color', color);     
          //      $item_data_wrapper.css('background-color', '#FFF');
            }             
        }               
        
        function dc_ms_show_hide_adv_opt($obj)
        {
            var parent = $obj.parent().parent();
            var advopt = parent.find('div.advanced-opt:first');
            if($obj.hasClass('show-hide-open'))
            {                                                  
                advopt.css('display', 'block');
                $obj.removeClass('show-hide-open').addClass('show-hide-close');
               
                var last_tab = advopt.data('dc_last_tab_active');
                if(last_tab >= 0)
                {
                    advopt.find('.tab-content').css('display', 'none');
                    advopt.find('.tab-content:eq('+last_tab+')').css('display', 'block');
                } else
                {
                    advopt.find('.tab-content').css('display', 'none');
                    advopt.find('.tab-content:first').css('display', 'block');
                }
            
            } else
            if($obj.hasClass('show-hide-close'))
            {              
                advopt.css('display', 'none');
                $obj.removeClass('show-hide-close').addClass('show-hide-open');    
            }               
        }                    
        
        function dc_ms_item_switch_adv_opt_tab($obj)
        {
           var index = $obj.parent().find('a').index($obj);
           
           var advopt = $obj.parent().parent();
           advopt.data('dc_last_tab_active', index);
           advopt.find('.tab-content').css('display', 'none');
           advopt.find('.tab-content:eq('+index+')').css('display', 'block'); 
           $obj.parent().find('a.tab-active').removeClass('tab-active').addClass('tab');
           $obj.removeClass('tab').addClass('tab-active');            
        }      
        
        function dc_ms_setup_save_all_submit_btn($li)
        {
            var parent = jQuery($li).parent();
            while(!parent.is('li'))
            {
                parent = parent.parent();
            }                        
            
            var index = jQuery('ol.dc-cms-menu-sys-sortable li').index(parent);
            var tab_active =  jQuery('ol.dc-cms-menu-sys-sortable li:eq('+index+')').find('.tabs-bar a.tab-active');
            var tab_index = jQuery('ol.dc-cms-menu-sys-sortable li:eq('+index+')').find('.tabs-bar a').index(tab_active);
            
            if(parent.find('.advanced-opt-btn:first div').hasClass('show-hide-open'))
            {
                tab_index = -1;
            }
            
            dcf_createCookie('dc-menu-sys-li-index', index, 10); 
            dcf_createCookie('dc-menu-sys-li-tab-index', tab_index, 10);             
        }             
      
        function dc_ms_show_hide_cbox_hide($li)
        {
            var parent = jQuery($li).parent();
            while(!parent.is('li'))
            {
                parent = parent.parent();
            }
            var checked = jQuery($li).is(':checked');
            
            if(checked) 
            { 
                parent.addClass('hidden-object');
            } 
            else 
            { 
                parent.removeClass('hidden-object');
            }   
        }           
        
        function dc_ms_show_hide_cbox_mega($li)
        {
            var parent = jQuery($li).parent();
            
            while(!parent.is('li'))
            {
                parent = parent.parent();
            }
            var checked = jQuery($li).is(':checked');
            
            if(checked) { parent.find('.head:first .tag-icon-mega').css('display', 'block'); } 
            else { parent.find('.head:first .tag-icon-mega').css('display', 'none'); }       
        }          
        
        function dc_ms_show_hide_cbox_empty($li)
        {
            var parent = jQuery($li).parent();
            
            while(!parent.is('li'))
            {
                parent = parent.parent();
            }
            var checked = jQuery($li).is(':checked');
            
            if(checked) { parent.find('.head:first .tag-icon-empty').css('display', 'block'); } 
            else { parent.find('.head:first .tag-icon-empty').css('display', 'none'); }         
        }        
        
        function dc_ms_show_hide_cbox_hidechilds($li)
        {
            var parent = jQuery($li).parent();
            while(!parent.is('li'))
            {
                parent = parent.parent();
            }
            
            var checked = jQuery($li).is(':checked');
            
            if(checked) 
            { 
                parent.addClass('hidden-childs');
            } 
            else 
            { 
                parent.removeClass('hidden-childs');
            }            
        }        
      
        // custom link
        $ms_wrapper.find('.menu-sys-left-data-column .custom-link-panel input[name=add-custom-link]').click(function() 
        {
            var url = jQuery(this).parent().parent().find('input[name="data-custom-link-url"]').val();
            var label = jQuery(this).parent().parent().find('input[name="data-custom-link-name"]').val();                                
            var menu_id = jQuery(this).parent().find('input[name=menu-id]').val();
            var $menu = jQuery('ol.dc-cms-menu-sys-sortable');
            
           if(url != 'http://')
           { 
               jQuery(this).parent().parent().find('input[name="data-custom-link-url"]').val(url);           
               
                var len = jQuery('.dc-cms-ajax-progress').length;
                if(len)
                {
                    jQuery('.dc-cms-ajax-progress').stop().remove();     
                } 
                jQuery('body').append(dccp_s.ajax_box);                
                jQuery('.dc-cms-ajax-progress').css('opacity', 1.0);                  
               
            jQuery.ajax({
           type: "POST",
           url: dccp_s.admin_url+'admin-ajax.php',
           data: { action: 'dc_menu_sys_add_custom', label:label, url:url, menu_id:menu_id},
           dataType: 'text',
           success: function(str){           
                   if(str != 'error')
                   {  
                        var li = str;                           
                        
                        
                        var last_li = null;
                        var $pined_head = $menu.find('.li-pined:first');
                        
                        if($pined_head.length) 
                        { 
                            var $previous_li = $pined_head.closest('li'); 
                            
                            var parent_db_id = $previous_li.find('.item-data:first').find('> input[name$="-parent"]').val();
                            $previous_li.after(li);
                            last_li = $previous_li.next();
                            last_li.find('.item-data:first').find('> input[name$="-parent"]').val(parent_db_id);
                        } else
                        {
                            $menu.append(li);  
                            last_li = $menu.find('li:last');
                        }
                        
                        
                        last_li.find('.show-opt-btn').click(function() 
                        { 
                            dc_ms_show_opt_btn(jQuery(this));
                        });   

                        last_li.find('.node-pin-btn').click(function() 
                        { 
                            dc_ms_node_pin_btn(jQuery(this));
                        });   
                        
                        last_li.find('.node-delete-btn').click(function() 
                        { 
                             dc_ms_node_delete_btn(jQuery(this));
                        });       
                        
                        last_li.find('div.advanced-opt-btn div.show-hide-open').click(function() 
                        { 
                            dc_ms_show_hide_adv_opt(jQuery(this)); 
                        });   
                        
                        last_li.find('div.item-content-wrapper .item-data').find('.tab-content:first').css('display', 'block');     
                        
                        last_li.find('div.item-content-wrapper .item-data .tabs-bar a').click(function(){
                           
                           dc_ms_item_switch_adv_opt_tab(jQuery(this));
                            
                        });     
                        
                        last_li.find('.save-all-menu-items-panel .submit-btn-primary').click(function(){
                        
                            dc_ms_setup_save_all_submit_btn(jQuery(this));
                                                                    
                        }); 
                                                                                          
                        last_li.find('.item-data:first .hide-checkbox').click(function() { dc_ms_show_hide_cbox_hide(this); });                                           
                        last_li.find('.item-data:first .mega-checkbox').click(function() { dc_ms_show_hide_cbox_mega(this); });                           
                        last_li.find('.item-data:first .empty-checkbox').click(function() { dc_ms_show_hide_cbox_empty(this); });                                     
                        last_li.find('.item-data:first .hidechilds-checkbox').click(function() { dc_ms_show_hide_cbox_hidechilds(this); }); 
                        last_li.find('.item-data:first').dcf_setupExcludableCBoxes();  
                        
                        last_li.find('.item-data:first').dcf_setupIntCtrl();
                        last_li.find('.item-data:first').dcf_setupColorPicker();                                                                                       
                   }
                   
                   
                   jQuery('.dc-cms-ajax-progress').animate({opacity:0.0}, 1000, function() { jQuery(this).remove(); }); 
                   
               }}); // ajax call
         
           } else // if url  
           {
               alert('Please, set some valid URL or clear URL');
           }
                              
        }); // click        
             

        // page link
        $ms_wrapper.find('.menu-sys-left-data-column .page-link-panel input[name=add-page-link]').click(function() 
        {                                                                                 
            var wpid = jQuery(this).parent().parent().find('select[name="data-page-link-id"]').val();
            var label = jQuery(this).parent().parent().find('select[name="data-page-link-id"] option[value="'+wpid+'"]').text();                                
            var menu_id = jQuery(this).parent().find('input[name=menu-id]').val();              
            var $menu = jQuery('ol.dc-cms-menu-sys-sortable'); 
              
                var len = jQuery('.dc-cms-ajax-progress').length;
                if(len)
                {
                    jQuery('.dc-cms-ajax-progress').stop().remove();     
                } 
                jQuery('body').append(dccp_s.ajax_box);                
                jQuery('.dc-cms-ajax-progress').css('opacity', 1.0);                       
              
            jQuery.ajax({
           type: "POST",
           url: dccp_s.admin_url+'admin-ajax.php',
           data: { action:'dc_menu_sys_add_page', label:label, wpid:wpid, menu_id:menu_id},         
           dataType: 'text',
           success: function(str){
               
               if(str != 'error')
               {
                    var li = str;                           
                    
                    var last_li = null;
                    var $pined_head = $menu.find('.li-pined:first');
                    
                    if($pined_head.length) 
                    { 
                        var $previous_li = $pined_head.closest('li'); 
                        
                        var parent_db_id = $previous_li.find('.item-data:first').find('> input[name$="-parent"]').val();
                        $previous_li.after(li);
                        last_li = $previous_li.next();
                        last_li.find('.item-data:first').find('> input[name$="-parent"]').val(parent_db_id);
                    } else
                    {
                        $menu.append(li);  
                        last_li = $menu.find('li:last');
                    }
                    
                    
                    last_li.find('.show-opt-btn').click(function() 
                    { 
                        dc_ms_show_opt_btn(jQuery(this));
                    });   

                    last_li.find('.node-pin-btn').click(function() 
                    { 
                         dc_ms_node_pin_btn(jQuery(this));
                    });  
                    
                    last_li.find('.node-delete-btn').click(function() 
                    { 
                         dc_ms_node_delete_btn(jQuery(this));
                    });       
                    
                    last_li.find('div.advanced-opt-btn div.show-hide-open').click(function() 
                    { 
                        dc_ms_show_hide_adv_opt(jQuery(this)); 
                    });   
                    
                    last_li.find('div.item-content-wrapper .item-data').find('.tab-content:first').css('display', 'block');     
                    
                    last_li.find('div.item-content-wrapper .item-data .tabs-bar a').click(function(){
                       
                       dc_ms_item_switch_adv_opt_tab(jQuery(this));
                        
                    });     
                    
                    last_li.find('.save-all-menu-items-panel .submit-btn-primary').click(function(){
                    
                        dc_ms_setup_save_all_submit_btn(jQuery(this));
                                                                
                    });  
                    
                    last_li.find('.item-data:first .hide-checkbox').click(function() { dc_ms_show_hide_cbox_hide(this); });                                           
                    last_li.find('.item-data:first .mega-checkbox').click(function() { dc_ms_show_hide_cbox_mega(this); });                           
                    last_li.find('.item-data:first .empty-checkbox').click(function() { dc_ms_show_hide_cbox_empty(this); });                                     
                    last_li.find('.item-data:first .hidechilds-checkbox').click(function() { dc_ms_show_hide_cbox_hidechilds(this); }); 
                    last_li.find('.item-data:first').dcf_setupExcludableCBoxes();  
                    
                    last_li.find('.item-data:first').dcf_setupIntCtrl();
                    last_li.find('.item-data:first').dcf_setupColorPicker();                                                                  
               
               }
               
               jQuery('.dc-cms-ajax-progress').animate({opacity:0.0}, 1000, function() { jQuery(this).remove(); }); 

           }}); // ajax call               
        }); // click   
        
        
        // category link
        $ms_wrapper.find('.menu-sys-left-data-column .category-link-panel input[name=add-category-link]').click(function() 
        {                                                                                 
            var wpid = jQuery(this).parent().parent().find('select[name="data-category-link-id"]').val();
            var label = jQuery(this).parent().parent().find('select[name="data-category-link-id"] option[value="'+wpid+'"]').text();                                
            var menu_id = jQuery(this).parent().find('input[name=menu-id]').val();          
            var $menu = jQuery('ol.dc-cms-menu-sys-sortable');  
               
                var len = jQuery('.dc-cms-ajax-progress').length;
                if(len)
                {
                    jQuery('.dc-cms-ajax-progress').stop().remove();     
                } 
                jQuery('body').append(dccp_s.ajax_box);                
                jQuery('.dc-cms-ajax-progress').css('opacity', 1.0);                        
               
            jQuery.ajax({
           type: "POST",
           url: dccp_s.admin_url+'admin-ajax.php',
           data: {action:'dc_menu_sys_add_category', label:label, wpid:wpid, menu_id:menu_id }, 
           dataType: 'text',
           success: function(str){
               
               if(str != 'error')
               {
                    var li = str;                           
                    

                    var last_li = null;
                    var $pined_head = $menu.find('.li-pined:first');
                    
                    if($pined_head.length) 
                    { 
                        var $previous_li = $pined_head.closest('li'); 
                        
                        var parent_db_id = $previous_li.find('.item-data:first').find('> input[name$="-parent"]').val();
                        $previous_li.after(li);
                        last_li = $previous_li.next();
                        last_li.find('.item-data:first').find('> input[name$="-parent"]').val(parent_db_id);
                    } else
                    {
                        $menu.append(li);  
                        last_li = $menu.find('li:last');
                    }                    
                    
                    last_li.find('.show-opt-btn').click(function() 
                    { 
                        dc_ms_show_opt_btn(jQuery(this));
                    });   

                    last_li.find('.node-pin-btn').click(function() 
                    { 
                         dc_ms_node_pin_btn(jQuery(this));
                    });    
                    
                    last_li.find('.node-delete-btn').click(function() 
                    { 
                         dc_ms_node_delete_btn(jQuery(this));
                    });       
                    
                    last_li.find('div.advanced-opt-btn div.show-hide-open').click(function() 
                    { 
                        dc_ms_show_hide_adv_opt(jQuery(this)); 
                    });   
                    
                    last_li.find('div.item-content-wrapper .item-data').find('.tab-content:first').css('display', 'block');     
                    
                    last_li.find('div.item-content-wrapper .item-data .tabs-bar a').click(function(){
                       
                       dc_ms_item_switch_adv_opt_tab(jQuery(this));
                        
                    });     
                    
                    last_li.find('.save-all-menu-items-panel .submit-btn-primary').click(function(){
                    
                        dc_ms_setup_save_all_submit_btn(jQuery(this));
                                                                
                    });     
                    
                    last_li.find('.item-data:first .hide-checkbox').click(function() { dc_ms_show_hide_cbox_hide(this); });                                           
                    last_li.find('.item-data:first .mega-checkbox').click(function() { dc_ms_show_hide_cbox_mega(this); });                           
                    last_li.find('.item-data:first .empty-checkbox').click(function() { dc_ms_show_hide_cbox_empty(this); });                                     
                    last_li.find('.item-data:first .hidechilds-checkbox').click(function() { dc_ms_show_hide_cbox_hidechilds(this); }); 
                    last_li.find('.item-data:first').dcf_setupExcludableCBoxes();  
                    
                    last_li.find('.item-data:first').dcf_setupIntCtrl();
                    last_li.find('.item-data:first').dcf_setupColorPicker();                                                               
               
               }

               jQuery('.dc-cms-ajax-progress').animate({opacity:0.0}, 1000, function() { jQuery(this).remove(); }); 
               
           }}); // ajax call         
                              
        }); // click                   
             
          
        jQuery('ol.dc-cms-menu-sys-sortable').nestedSortableDC({
                disableNesting: 'no-nest',
                forcePlaceholderSize: true,
                handle: '.head',
                helper: 'clone',
                items: 'li',
                maxLevels: 6,
                distance: 15,
                opacity: 1.0,
                placeholder: 'placeholder',
                revert: 250,
                tabSize: 30,
                update:function(event, ui)
                {
                   var parent = jQuery(ui.item).parent(); 
                   if(parent.is('ol'))
                   {
                       parent = parent.parent();
                       if(parent.is('li'))
                       {
                           var db_id = parent.find('span.db-id:first').text();
                           var this_db_id = jQuery(ui.item).find('span.db-id:first').text();
                           jQuery(ui.item).find('input[name="data-item-'+this_db_id+'-parent"]').val(db_id);
                       } else
                       {
                           var this_db_id = jQuery(ui.item).find('span.db-id:first').text();
                           jQuery(ui.item).find('input[name="data-item-'+this_db_id+'-parent"]').val('0');    
                       }
                   }
                },
                tolerance: 'pointer',
                toleranceElement: '> div'
            });           
              
        jQuery('.menu-sys-center-data-column .center-column-submit-panel .dc-cms-submit-btn-primary:first').click(function(){
        
            var $li = jQuery('ol.dc-cms-menu-sys-sortable li.li-opened:first');
            var index = jQuery('ol.dc-cms-menu-sys-sortable li').index($li);
            dcf_createCookie('dc-menu-sys-li-index', index, 10); 
            
            var tab_active =  jQuery('ol.dc-cms-menu-sys-sortable li:eq('+index+')').find('.tabs-bar a.tab-active'); 
            var tab_index = jQuery('ol.dc-cms-menu-sys-sortable li:eq('+index+')').find('.tabs-bar a').index(tab_active);                                        
            if($li.find('.advanced-opt-btn:first div').hasClass('show-hide-open'))
            {
                tab_index = -1;
            }      
            dcf_createCookie('dc-menu-sys-li-tab-index', tab_index, 10);
        });


        
        jQuery('ol.dc-cms-menu-sys-sortable li .save-all-menu-items-panel .dc-cms-submit-btn-primary').click(function(){
        
            dc_ms_setup_save_all_submit_btn(jQuery(this));
                                                    
        });  
        
        var index = dcf_readCookie('dc-menu-sys-li-index');
        var tab_index = dcf_readCookie('dc-menu-sys-li-tab-index');
        if(index >= 0)
        {
            var $li = jQuery('ol.dc-cms-menu-sys-sortable li:eq('+index+')'); 
            if(tab_index >= 0)
            {
                var adv_btn = $li.find('div.advanced-opt-btn:first div.show-hide-open');
                dc_ms_show_hide_adv_opt(adv_btn);
                
                var tab_to_switch = $li.find('.tabs-bar:first a:eq('+tab_index+')');
                dc_ms_item_switch_adv_opt_tab(tab_to_switch);
            }            
            
            
            $li.find('.item-data:first').slideToggle(400);
            if($li.hasClass('li-opened')) { $li.removeClass('li-opened'); } else { $li.addClass('li-opened'); } 
            

        }
        
        jQuery('ol.dc-cms-menu-sys-sortable .show-opt-btn').click(function() 
        { 
            dc_ms_show_opt_btn(jQuery(this));
        });  

        jQuery('ol.dc-cms-menu-sys-sortable .node-pin-btn').click(function() 
        { 
            dc_ms_node_pin_btn(jQuery(this));
        });          
        
        jQuery('ol.dc-cms-menu-sys-sortable .node-delete-btn').click(function() 
        { 
            dc_ms_node_delete_btn(jQuery(this));
        });                    
        
        jQuery('ol.dc-cms-menu-sys-sortable .hide-checkbox').click(function() 
        { 
              dc_ms_show_hide_cbox_hide(this);
        });                   
        
        jQuery('ol.dc-cms-menu-sys-sortable .mega-checkbox').click(function() 
        { 
               dc_ms_show_hide_cbox_mega(this);
        });                
        
        jQuery('ol.dc-cms-menu-sys-sortable .empty-checkbox').click(function() 
        { 
              dc_ms_show_hide_cbox_empty(this);
        });                     
        
        
        jQuery('ol.dc-cms-menu-sys-sortable .hidechilds-checkbox').click(function() 
        { 
            dc_ms_show_hide_cbox_hidechilds(this);
        });                               
           
        jQuery('ol.dc-cms-menu-sys-sortable div.advanced-opt-btn div').click(function() 
        { 
            dc_ms_show_hide_adv_opt(jQuery(this)); 
        });             
        
        jQuery('ol.dc-cms-menu-sys-sortable li div.item-content-wrapper .item-data .tabs-bar a').click(function(){
           
           dc_ms_item_switch_adv_opt_tab(jQuery(this));
            
        });             
              
        /******************************
        * DC CMS Image Preview LightBox
        *******************************/         
        if(!jQuery.fn.dcf_setupCMSLightBox) 
        {           
            jQuery.fn.extend({
                dcf_setupCMSLightBox: function(){        

            jQuery(this).find('.dc-cms-image-preview-lb-item').each(function()
            {  
                
               if(jQuery(this).data('dc_cms_lb') == 1)
               {
                   return;
               }            
               jQuery(this).data('dc_cms_lb', 1);
               
               jQuery(this).click(function() {
                
                   var path = jQuery(this).find('span.image-path').text();
                   if(path == '')
                   {
                        return;
                   }
                   
               jQuery('body').append('<div id="dc-cms-image-preview-lb-bg"><div class="color-box"><div class="white-color"></div><div class="black-color"></div></div></div>');
               jQuery('#dc-cms-image-preview-lb-bg').click(function() { jQuery('#dc-cms-image-preview-lb').remove(); jQuery(this).remove(); }); 
               
               jQuery('#dc-cms-image-preview-lb-bg .white-color').click(function() { jQuery('#dc-cms-image-preview-lb').css('background-color', '#FFF'); return false; }); 
               jQuery('#dc-cms-image-preview-lb-bg .black-color').click(function() { jQuery('#dc-cms-image-preview-lb').css('background-color', '#000'); return false; }); 
               
               jQuery(window).resize(function(){
                    if(jQuery('#dc-cms-image-preview-lb').length)
                    {                
                        var win_w = jQuery(window).width();
                        var win_h = jQuery(window).height();   
                        
                        var w = jQuery('#dc-cms-image-preview-lb').data('dc_w');
                        var h = jQuery('#dc-cms-image-preview-lb').data('dc_h');
                        
                        var max_w = win_w - 120;
                        var max_h = win_h - 120;                                                       
                        if(w > max_w)
                        {
                            var ratio_w = max_w / w;
                            w = max_w;
                            h = Math.round(h * ratio_w);    
                        }

                        if(h > max_h)
                        {
                            var ratio_h = max_h / h;
                            h = max_h;
                            w = Math.round(w * ratio_h);    
                        }
                        
                        var pos_x = Math.round(win_w / 2) - Math.round(w / 2);
                        var pos_y = Math.round(win_h / 2) - Math.round(h / 2);
                        
                        jQuery('#dc-cms-image-preview-lb').css({left:pos_x,top:pos_y,width:w,height:h});                                                             
                    }                   
               });
               
               if(path != '')
               {
                    var win_w = jQuery(window).width();
                    var win_h = jQuery(window).height(); 
                   
                    var img = new Image(); 
                    jQuery(img).load(function(){
                        var w = img.width;
                        var h = img.height;
                        var org_w = w;
                        var org_h = h;
                     //   alert('('+w+', '+h+')'+win_h);
                    //    jQuery('#dcp-pm-image-preview-bg').append(this);
                        
                        var max_w = win_w - 120;
                        var max_h = win_h - 120;                                                       
                        if(w > max_w)
                        {
                            var ratio_w = max_w / w;
                            w = max_w;
                            h = Math.round(h * ratio_w);    
                        }

                        if(h > max_h)
                        {
                            var ratio_h = max_h / h;
                            h = max_h;
                            w = Math.round(w * ratio_h);    
                        }
                        
                        var pos_x = Math.round(win_w / 2) - Math.round(w / 2);
                        var pos_y = Math.round(win_h / 2) - Math.round(h / 2);
                        
                       jQuery('body').append('<img id="dc-cms-image-preview-lb" src="'+path+'" style="left:'+pos_x+'px;top:'+pos_y+'px;width:'+w+'px;height:'+h+'px;" />'); 
                       jQuery('#dc-cms-image-preview-lb').data('dc_w', org_w).data('dc_h', org_h); 
                        
                    }).attr('src', path);                           
               }
               
               });                
                
            }); }});
        } 
        jQuery('body').dcf_setupCMSLightBox();                      
                                                     

        /******************************
        * DC CMS HEADER INFO BOX
        *******************************/  
        
        function dc_cms_header_info_box_obj($obj_li)
        {                     
            $obj_li.find('input[name="add-span"]').click(function() {              
                var val = $obj_li.find('textarea').val();
                $obj_li.find('textarea').val(val+'<span style="color:#888;"></span>');                
            });
            
            $obj_li.find('input[name="add-anchor"]').click(function() {               
                var val = $obj_li.find('textarea').val();
                $obj_li.find('textarea').val(val+'<a href="#">Anchor</a>');                
            });  
            
            $obj_li.find('input[name="add-default"]').click(function() {              
                var val = $obj_li.find('textarea').val();
                $obj_li.find('textarea').val(val+'<span style="color:#888;">Phone: </span>881-604-720');                
            });                            
        }
        
        jQuery('.dc-cms-edit-header-info-list-left-column ul.dc-cms-edit-header-info-list').sortable(
        {
           // axis: 'y',
            cursor: 'move',
            distance: 15,
            placeholder: 'placeholder',
            update: function(event, ui) { 
               
                if(jQuery(ui.item).is('li'))
                {
                    var p = jQuery(ui.item).parent();
                    var len = p.find('> li').length;
                    for(var i = 0; i < len; i++)
                    {
                        p.find('> li:eq('+i+') .index-box').text((i+1));
                    }
                }                
                
            },
            
            start: function (e, ui) { 
                  var h = ui.item.height();
                  ui.placeholder.css('height', h);
                }               
            
        }).each(function(){ 
            // dc_index - li_index value will be used by php code to generate unique form name identifiers
            
            var li_index = jQuery(this).find('> li').length;
            jQuery(this).data('dc_index', li_index);  
        });          
        
        jQuery('.dc-cms-edit-header-info-list-left-column ul.dc-cms-edit-header-info-list li').each(function() { dc_cms_header_info_box_obj(jQuery(this)); });
        
       
         jQuery('.dc-cms-edit-header-info-list-right-column .dc-cms-submit-btn').click(function(){  
            
            var action = jQuery(this).parent().find('.dc-cms-action-name').text(); 
            var ul_list = jQuery(this).parent().parent().find('ul.dc-cms-edit-header-info-list');
            var index = ul_list.data('dc_index');
            ul_list.data('dc_index', (index+1));                                    
            
            dcf_showAjaxProgrss();               
             
            jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action="+action+'&index='+index,
               success: function(str){
                                  
                 ul_list.append(str);
                 var last_li = ul_list.find('> li:last');                                
                 dc_cms_header_info_box_obj(last_li);
                   
                 dcf_hideAjaxProgrss();
               }
             });   
             
         });       

        /****************************************
        * DC Content Builder Basic Slider Type
        *****************************************/ 
        
        function dcf_cb_basic_slider_type_slides_order($ul)
        {
            var $wrapper = $ul.parent();
            var $input = $wrapper.find('.slides-order');  
            
                var order = new Array(); 
                $ul.find('li').each(function() {
                   
                   var id = jQuery(this).find('input[name="slide-index"]').val();    
                   order.push(id); 
                    
                });
                
            $input.val(order.toString());            
        }        
        
        
        function dc_cb_setup_basic_slider_type($obj)
        {

            $obj.find('ul.dc-cms-builder-basic-slider-list').sortable(
            {
               // axis: 'y',
                cursor: 'move',
                distance: 15,
                placeholder: 'placeholder',
                update: function(event, ui) { 
                   
                    if(jQuery(ui.item).is('li'))
                    {
                        var $li = jQuery(ui.item);
                        var $ul = $li.parent();
                        var len = $ul.find('> li').length;
                        for(var i = 0; i < len; i++)
                        {
                            $ul.find('> li:eq('+i+') .index-box').text((i+1));
                        }
                        
                        dcf_cb_basic_slider_type_slides_order($ul);
                    }                
                    
                },
                
                start: function (e, ui) { 
                      var h = ui.item.height();
                      ui.placeholder.css('height', h);
                    }               
                
            }).each(function(){ 
                // dc_index - li_index value will be used by php code to generate unique form name identifiers
                
                var li_index = jQuery(this).find('> li').length;
                jQuery(this).data('dc_index', li_index);  
            }); 

             var setup_slide_delete_btn = function($li)
             {
                var $ul = $li.parent();
                
                $li.addClass('in-delete-process');
                if(confirm('Delete slide?'))
                {
                    $li.remove();
                    dcf_cb_basic_slider_type_slides_order($ul);   
                    
                    var len = $ul.find('> li').length;
                    for(var i = 0; i < len; i++)
                    {
                        $ul.find('> li:eq('+i+') .index-box').text((i+1));
                    }                     
                } else
                {
                    $li.removeClass('in-delete-process');    
                }                   
             }
            
             $obj.find('.dc-cms-builder-basic-slider-type-wrapper li .delete-box').click(function()
             {
                var $li = jQuery(this).parent();  
                setup_slide_delete_btn($li);              
             });
            
             $obj.find('.dc-cms-builder-basic-slider-type-wrapper input[name="dc-builder-basic-slider-add-slide"]').click(function()
             {  
                
                var $seg = jQuery(this).closest('div.segment-options');
                var seg_id = $seg.find('input[name$="-core-id"]').val();            
                 
                var action = jQuery(this).parent().find('.dc-cms-action-name').text(); 
                var $ul_list = jQuery(this).parent().parent().find('ul.dc-cms-builder-basic-slider-list');
                var index = $ul_list.data('dc_index');
                $ul_list.data('dc_index', (index+1));                                    
                
                
                dcf_showAjaxProgrss();               
                 
                jQuery.ajax({
                   type: "POST",
                   url: dccp_s.admin_url+'admin-ajax.php',
                   data: "action="+action+'&id='+seg_id+'&index='+index,
                   success: function(str){
                                 
                     $ul_list.append(str);
                     var $last_li = $ul_list.find('> li:last');
                     dcf_cb_basic_slider_type_slides_order($ul_list); 
                     // $last_li.find('.dc-cms-submit-image-btn').each(function() { dcf_setupLoadImageBtn(jQuery(this)); });                               
                     $last_li.dcf_setupCMSLoadImageBtn();
                     $last_li.find('.delete-box').click(function()
                     {
                        var $li = jQuery(this).parent();  
                        setup_slide_delete_btn($li);              
                     });

                    var len = $ul_list.find('> li').length;
                    for(var i = 0; i < len; i++)
                    {
                        $ul_list.find('> li:eq('+i+') .index-box').text((i+1));
                    }             
                       
                     dcf_hideAjaxProgrss();
                   }
                 });   
                 
             });             
            
        }
        dc_cb_setup_basic_slider_type(jQuery(document));
                  
       
        /******************************
        * DC Contact Form Creator
        *******************************/  
        jQuery('.dc-cms-edit-contact-form-wrapper ul.cf-items-list').sortable(
        {
            //axis: 'y',
            cursor: 'move',
            distance: 15,
            placeholder: 'placeholder',
            update: function(event, ui) { 
               
                if(jQuery(ui.item).is('li'))
                {
                    var p = jQuery(ui.item).parent();
                    var len = p.find('> li').length;
                    for(var i = 0; i < len; i++)
                    {
                        p.find('> li:eq('+i+') .index-box').text((i+1));
                    }
                }                
                
            },
            
            start: function (e, ui) { 
                  var h = ui.item.outerHeight();
                  ui.placeholder.css('height', h-2);
                }               
            
        }).each(function(){ 
            // dc_index - li_index value will be used by php code to generate unique form name identifiers
            
            var li_index = jQuery(this).find('> li').length;
            jQuery(this).data('dc_index', li_index);  
        });     

        jQuery('.dc-cms-edit-contact-form-wrapper ul.cf-items-list .cf-item ul.select-option-list ').sortable(
        {
            axis: "y",
            cursor: "move",
            distance: 5
        }).each(function(){ 
            var li_index = jQuery(this).find('li').length;
            jQuery(this).data('dc_index', li_index);  
        });  

        jQuery('.dc-cms-edit-contact-form-wrapper ul.cf-items-list .cf-item ul.select-option-list li .dc-cms-submit-delete-btn').click(
        function ()
        {
            if(confirm('Delete option?'))
            {
                var ul = jQuery(this).parent().parent();
                jQuery(this).parent().remove();
                if(ul.find('li').length == 0)
                {           
                    
                    ul.parent().find('span').css('display', 'none');
                }
            }      
            
            
         
            
        });          

        function dc_cms_cf_add_option_btn($obj)
        {
            var index = jQuery($obj).parent().find('span.dc-control-index').text();
            var action = jQuery($obj).parent().find('span.dc-cms-action-name').text();
            
            var cf_item = jQuery($obj).parent().parent();
            var option_ul = cf_item.find('ul.select-option-list');
            var li_index = option_ul.data('dc_index');              
            option_ul.data('dc_index', (li_index+1));
           
                dcf_showAjaxProgrss();         
            
           
            jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action="+action+'&index='+index+'&li_index='+li_index+'&operation=add_option',
               success: function(str){
                 
                   option_ul.append(str);
                   
                    option_ul.find('li:last .dc-cms-submit-delete-btn').click(
                    function ()
                    {
                        var ul = jQuery(this).parent().parent();
                        jQuery(this).parent().remove();
                        if(ul.find('li').length == 0)
                        {           
                            ul.parent().find('span').css('display', 'none');
                        }      
                     });                    
                   
                   option_ul.parent().find('span').css('display', 'block');
                   
                 dcf_hideAjaxProgrss();
               }
             });                
        }
        
        jQuery('.dc-cms-edit-contact-form-wrapper ul.cf-items-list .cf-item input.add-option-btn').click(
        function ()
        {
           
           dc_cms_cf_add_option_btn(this);                   
            
        });           
        
        
           
             
         jQuery('.dc-cms-edit-contact-form-wrapper .panel .dc-cms-submit-btn').click(function(){
            var panel = jQuery(this).parent().parent();
            var action = jQuery(this).parent().find('.dc-cms-action-name').text(); 
            var type = panel.find('select option:selected').val();
  
            var ul_list = panel.parent().parent().find('.edit-contact-form-left-column ul.cf-items-list');
            var index = ul_list.data('dc_index');
            ul_list.data('dc_index', (index+1));                                    
            
                dcf_showAjaxProgrss();                    
            
            jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action="+action+'&index='+index+'&operation=add_input'+'&type='+type,
               success: function(str){
                 
                 ul_list.append(str);
                 var last_li = ul_list.find('> li.cf-item:last');
                 last_li.dcf_setupExcludableCBoxes(); 
                 jQuery(last_li).dcf_setupIntCtrl(); 
                 ul_list.find('> li.cf-item:last ul.select-option-list').data('dc_index', 0);
                 ul_list.find('> li.cf-item:last input.add-option-btn').click(function(){ dc_cms_cf_add_option_btn(this); });
                 ul_list.find('> li.cf-item:last ul.select-option-list').sortable(
                    {
                        axis: "y",
                        cursor: "move",
                        distance: 5
                    });                                  
                   
                 dcf_hideAjaxProgrss();
               }
             });               
             
                
        });


        /******************************
        * DC CMS Short Code ToolBox
        *******************************/   
        function dcf_DCThemeInsertText(d, instext)
        {
            if(document.selection)
            {
                d.focus();
                var e=document.selection.createRange();
                if(e.text.length > 0)
                {
                    e.text=instext+e.text;
                } else
                {   
                    e.text=instext; 
                }
                
                d.focus()
            } else
            { 
                if(d.selectionStart||d.selectionStart=="0")
                {
                    var b=d.selectionStart,a=d.selectionEnd,g=a,f=d.scrollTop;
                    if(b!=a)
                    {   d.value=d.value.substring(0,b)+instext+d.value.substring(b,a)+d.value.substring(a,d.value.length);
                        g+=instext.length;
                    }   
                    else
                    {
                        d.value=d.value.substring(0,b)+instext+d.value.substring(a,d.value.length);
                        g+=instext.length;
                    }
                    d.focus();
                    d.selectionStart=g;
                    d.selectionEnd=g;
                    d.scrollTop=f
                } else
                { 
                    d.value=instext;
                    d.focus()
                }
            }
        } 
                    
        function dcf_DCThemeInsertTag(d, c)
        {       
            var tag = c;
            
            if(document.selection)
            {
                d.focus();
                var e=document.selection.createRange();
                if(e.text.length > 0)
                {
                    e.text=tag.tagStart+e.text+tag.tagEnd;
                } else
                {   
                    e.text=tag.tagStart+tag.tagEnd; 
                }
                
                d.focus()
            } else
            { 
                if(d.selectionStart||d.selectionStart=="0")
                {
                    var b=d.selectionStart,a=d.selectionEnd,g=a,f=d.scrollTop;
                    if(b!=a)
                    {   d.value=d.value.substring(0,b)+tag.tagStart+d.value.substring(b,a)+tag.tagEnd+d.value.substring(a,d.value.length);
                        g+=tag.tagStart.length+tag.tagEnd.length;
                    }   
                    else
                    {
                        d.value=d.value.substring(0,b)+tag.tagStart+tag.tagEnd+d.value.substring(a,d.value.length);
                        g+=tag.tagStart.length+tag.tagEnd.length;
                    }
                    d.focus();
                    d.selectionStart=g;
                    d.selectionEnd=g;
                    d.scrollTop=f
                } else
                { 
                    d.value=tag.tagStart+tag.tagEnd;
                    d.focus()
                }
            }
        }           
                
        if(!jQuery.fn.dcf_showShortCodeToolBox)
        {       
            jQuery.fn.extend({
                dcf_showShortCodeToolBox: function(args)
                {

                   jQuery('body').append('<div id="dc-cms-shortcode-box-bg"></div>');
                                   
                   jQuery('body').append('<div id="dc-cms-shortcode-box-wrapper" style="display:none;"></div>'); 
                   jQuery('#dc-cms-shortcode-box-wrapper').load(args.url, function()
                   {
                       $wrapper = jQuery('#dc-cms-shortcode-box-wrapper');
                       var $t = $wrapper.find('#dc-cms-shortcode-box-content-top-panel');
                       var $c = $wrapper.find('#dc-cms-shortcode-box-content');
                       var $b = $wrapper.find('#dc-cms-shortcode-box-content-bottom-panel'); 
                       var $btn = $wrapper.find('input[name="dc-tool-box-insert-btn"]');
                       
                       function compute_size_and_pos()
                       {
                            if(jQuery('#dc-cms-shortcode-box-wrapper').length)
                            {
                               var $wrapper = jQuery('#dc-cms-shortcode-box-wrapper');  
                               
                               var $t = $wrapper.find('#dc-cms-shortcode-box-content-top-panel');
                               var $c = $wrapper.find('#dc-cms-shortcode-box-content');
                               var $b = $wrapper.find('#dc-cms-shortcode-box-content-bottom-panel'); 

                               var selected_panel_id = $t.find('select:first').val(); 
                               var $panel = $c.find('#'+selected_panel_id);
                               var panel_h = $panel.height();
                               
                               var win_w = jQuery(window).width();
                               var win_h = jQuery(window).height(); 

                               var c_h = win_h - $t.outerHeight() - $b.outerHeight() - 420;
                               
                               if(c_h > panel_h) { c_h = panel_h; }
                               
                               $c.css('height', c_h);                               
                               
                               var w_w = $wrapper.outerWidth();
                               var w_h = $wrapper.outerHeight();

                               var left = Math.round(win_w / 2) - Math.round(w_w / 2);
                               var top = Math.round(win_h / 2) - Math.round(w_h / 2); 
                               
                               $wrapper.css({left: left, top:top});
                               
                                                                                                  
                            }                            
                       }                       
                       
                       $btn.click(function() 
                       { 
                           args.insert_js($wrapper, args.area); 

                           args.close_js($wrapper);
                           jQuery('#dc-cms-shortcode-box-wrapper').remove(); 
                           jQuery('#dc-cms-shortcode-box-bg').remove();
                           jQuery('#dc-color-picker-tool').remove(); 
                           jQuery(window).unbind('resize', compute_size_and_pos);                        
                       
                       });
                       
                       jQuery('#dc-cms-shortcode-box-bg, #dc-cms-shortcode-box-wrapper div.close-btn').click(function() 
                       { 
                           args.close_js($wrapper);
                           jQuery('#dc-cms-shortcode-box-wrapper').remove(); 
                           jQuery('#dc-cms-shortcode-box-bg').remove(); 
                           jQuery('#dc-color-picker-tool').remove();
                           jQuery(window).unbind('resize', compute_size_and_pos); 
                           
                       });     
                       
                       
                       
                       $wrapper.css('display', 'block');         
                       var selected_panel_id = $t.find('select:first').val();
                       var $panel = $c.find('#'+selected_panel_id);
                       var desc = $panel.find('.sc-description').html();
                       $wrapper.find('.shortcode-description').html(desc);
                       $panel.css('display', 'block').addClass('active-panel'); 
                       
                       $t.find('select:first').change(function () {
                       
                            var selected_panel_id = jQuery(this).val();
                            
                            $c.find('.shortcode-panel').css('display', 'none').removeClass('active-panel');
                            var $panel = $c.find('#'+selected_panel_id);
                            var desc = $panel.find('.sc-description').html();
                            $wrapper.find('.shortcode-description').html(desc);
                            $panel.css('display', 'block').addClass('active-panel');     
                           
                           compute_size_and_pos()
                       });                     
                       
                       compute_size_and_pos();

                       jQuery(window).bind('resize', compute_size_and_pos);   
                       args.open_js($wrapper);                                                                 
                       
                   }); // load
                }
            });        
        }
        
        function dcf_openShortCodeToolBox($wrapper)
        {         
            $wrapper.dcf_setupIntCtrl();  
            // $wrapper.find('.dc-cms-submit-image-btn').each(function() { dcf_setupLoadImageBtn(jQuery(this)); });
            $wrapper.dcf_setupCMSLoadImageBtn();
            $wrapper.dcf_setupExcludableCBoxes();
            
            $wrapper.find('input.dc-color-picker').each(function() 
            { 
                  var z = $wrapper.css('z-index');
                  jQuery(this).data('dc_z_index', z);
                  jQuery(this).data('dc_position', 'fixed');                                    
            });
            $wrapper.dcf_setupColorPicker();                       
            
            
            jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action=dc_ctrl_ngg_list",
               success: function(str){
                 $wrapper.find('select[name=ngg_list]').append(str); 
               }
             });    
                
             jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action=dc_ctrl_page_list",
               success: function(str){
                 $wrapper.find('select[name=page_list]').append(str); 
               }
             });             

             jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action=dc_ctrl_divider_list",
               success: function(str){
                 $wrapper.find('select[name=divider_list]').append(str); 
               }
             });   

             jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action=dc_box_ul_list_legend",
               success: function(str){
                 $wrapper.find('div[class=dcs-ul-list-legend]').append(str); 
               }
             });  
                 
        }
        
        function dcf_insertShortCodeToolBox($wrapper, $area)
        {
            $panel = $wrapper.find('.active-panel:first');                       
                        
            var code = '';        
            var code = $panel.attr('id'); 

            switch(code)
            {
                case 'dcs_simple_head':    
                {            
                    var color = $panel.find('input[name=color]').val();
                    var top = $panel.find('input[name=top]').val();
                    
                    code = '[dcs_simple_head top="'+top+'" color="'+color+'"]Your text here[/dcs_simple_head]';
                }
                break;
                
                case 'dcs_colored_head':    
                {            
                    var color = $panel.find('input[name=color]').val();
                    var bgcolor = $panel.find('input[name=bgcolor]').val();
                    var top = $panel.find('input[name=top]').val();
                    
                    var prefix = $panel.find('input[name=prefix]').val();
                    var prefix_bgcolor = $panel.find('input[name=prefix_bgcolor]').val();
                    var prefix_w = $panel.find('input[name=prefix_w]').val();
                    var prefix_align = $panel.find('select[name=prefix_align]').val(); 
                    var prefix_percent = $panel.find('input[name=prefix_percent]').is(':checked') ? true : false;
                    if(prefix_percent) { prefix_percent = 'true'; } else { prefix_percent = 'false'; }                     
                    
                    code = '[dcs_colored_head top="'+top+'" color="'+color+'" bgcolor="'+bgcolor+'" ' +
                           'prefix="'+prefix+'" prefix_bgcolor="'+prefix_bgcolor+'" prefix_align="'+prefix_align+'" ' +
                           'prefix_w="'+prefix_w+'" prefix_percent="'+prefix_percent+'" ' + 
                           ']Your text here[/dcs_colored_head]';
                }
                break;                                
                
                case 'dcs_empty_space':
                {
                    var h = $panel.find('input[name=h]').val();                                     
                    code = '[dcs_empty_space h="'+h+'" /]';    
                } break;

                case 'dcs_clear':
                {
                    var h = $panel.find('input[name=h]').val();
                    var clear = $panel.find('select[name=clear]').val();  
                                                                        
                    code = '[dcs_clear h="'+h+'" clear="'+clear+'" /]';    
                } break;                

                case 'dcs_span':
                {
                    var color = $panel.find('input[name=color]').val();                                                    
                    var color_use = $panel.find('input[name=color_use]').is(':checked') ? true : false; 
                    var bgcolor = $panel.find('input[name=bgcolor]').val(); 
                    var bgcolor_use = $panel.find('input[name=bgcolor_use]').is(':checked') ? true : false; 
                    
                    if(!color_use) { color = ''; }
                    if(!bgcolor_use) { bgcolor = ''; }
                    
                    code = '[dcs_span color="'+color+'" bgcolor="'+bgcolor+'"]Your text here[/dcs_span]';    
                } break; 
                
                case 'dcs_highlight':
                {
                    var bgcolor = $panel.find('input[name=bgcolor]').val();                                                    
                    
                    var bgcolor_use = $panel.find('input[name=bgcolor_use]').is(':checked') ? true : false;
                    if(!bgcolor_use) { bgcolor = ''; } 
                                
                    code = '[dcs_highlight bgcolor="'+bgcolor+'" ]Your text here[/dcs_highlight]';                    
                } break; 
                
                case 'dcs_top':
                {
                    var text = $panel.find('input[name=text]').val();
                    var top = $panel.find('input[name=top]').val();                                                    
                    var bottom = $panel.find('input[name=bottom]').val();                       
                                
                    code = '[dcs_top text="'+text+'" top="'+top+'" bottom="'+bottom+'" /]';                    
                } break; 
                
                case 'dcs_line_divider':
                {
                    var bsize = $panel.find('input[name=bsize]').val();
                    var bcolor = $panel.find('input[name=bcolor]').val();
                    var bstyle = $panel.find('select[name=bstyle]').val(); 
                    var top = $panel.find('input[name=top]').val();                                                    
                    var bottom = $panel.find('input[name=bottom]').val();                       
                                
                    code = '[dcs_line_divider bsize="'+bsize+'" bcolor="'+bcolor+'" bstyle="'+bstyle+'" top="'+top+'" bottom="'+bottom+'" /]';                       
                } break;
                
                case 'dcs_img_divider':
                {
                    var h = $panel.find('input[name=h]').val();

                    var top = $panel.find('input[name=top]').val();                                                    
                    var bottom = $panel.find('input[name=bottom]').val();    
                    
                    var bgimage = $panel.find('input[name=bgimage]').val(); 
                    var bgrepeat = $panel.find('select[name=bgrepeat]').val();
                    var bgpos = $panel.find('select[name=bgpos]').val();
                    
                    var bgcolor = $panel.find('input[name=bgcolor]').val();
                    var bgcolor_use = $panel.find('input[name=bgcolor_use]').is(':checked') ? true : false;
                    if(bgcolor_use) { bgcolor_use = 'true'; } else { bgcolor_use = 'false'; }                     
                    
                    var pattern = $panel.find('select[name=divider_list]').val(); 
                   
                                
                    code = '[dcs_img_divider h="'+h+'" pattern="'+pattern+'" '+
                           'bgimage="'+bgimage+'" bgrepeat="'+bgrepeat+'" bgpos="'+bgpos+'" '+
                           'bgcolor="'+bgcolor+'" bgcolor_use="'+bgcolor_use+'" '+
                           'top="'+top+'" bottom="'+bottom+'" /]';                       
                } break;                
                
                case 'dcs_note':
                {
                    var color = $panel.find('input[name=color]').val();
                    var bgcolor = $panel.find('input[name=bgcolor]').val(); 
                    var bcolor = $panel.find('input[name=bcolor]').val(); 
                    var bwidth = $panel.find('input[name=bwidth]').val(); 
                    var align = $panel.find('select[name=align]').val();                     
                    var rounded = $panel.find('select[name=rounded]').val(); 
                    var bottom = $panel.find('input[name=bottom]').val(); 
                    
                    
                    var execute = $panel.find('input[name=execute]').is(':checked') ? true : false;
                    if(execute) { execute = 'true'; } else { execute = 'false'; } 
                    
                    code = '[dcs_note color="'+color+'" bgcolor="'+bgcolor+'" bcolor="'+bcolor+'" bwidth="'+bwidth+'" align="'+align+'" rounded="'+rounded+'" bottom="'+bottom+'" execute="'+execute+'"]Your content here[/dcs_note]'; 
                     
                } break;
                
                case 'dcs_blockquote':
                {
                    var size = $panel.find('select[name=size]').val();
                    var color = $panel.find('input[name=color]').val(); 
                    var float_ = $panel.find('select[name=float]').val();
                    
                    var top = $panel.find('input[name=top]').val();
                    var right = $panel.find('input[name=right]').val();
                    var bottom = $panel.find('input[name=bottom]').val();
                    var left = $panel.find('input[name=left]').val(); 
                    
                    var align = $panel.find('select[name=align]').val(); 
                    
                    var author = $panel.find('input[name=author]').val(); 
                    var title = $panel.find('input[name=title]').val();                                      
                    
                    var fsize = $panel.find('input[name=fsize]').val();
                    var lheight = $panel.find('input[name=lheight]').val();
                    
                    code = '[dcs_blockquote size="'+size+'" color="'+color+'" float="'+float_+'" author="'+author+'" title="'+title+'" top="'+top+'" right="'+right+'" bottom="'+bottom+'" left="'+left+'" align="'+align+'" fsize="'+fsize+'" lheight="'+lheight+'"]Your text here[/dcs_blockquote]';     
                } break;                                                                
                
                case 'dcs_lightbox_link':
                {
                    var url = $panel.find('input[name=url]').val(); 
                    var title = $panel.find('input[name=title]').val();
                    var color = $panel.find('input[name=color]').val();
                    var group = $panel.find('input[name=group]').val();                          
                    
                    var color_use = $panel.find('input[name=color_use]').is(':checked') ? true : false;
                    if(color_use) { color_use = 'true'; } else { color_use = 'false'; }                     
                    
                    code = '[dcs_lightbox_link color="'+color+'" color_use="'+color_use+'" url="'+url+'" title="'+title+'" group="'+group+'"]Your text here[/dcs_lightbox_link]';   
                       
                } break;               
                
                case 'dcs_ul_list':
                {
                    var size = $panel.find('select[name=size]').val(); 
                    var ctype = $panel.find('select[name=ctype]').val(); 
                    var float_ = $panel.find('select[name=float]').val();                     
                    
                    var type = $panel.find('select[name=type]').val();    
                    var left = $panel.find('input[name=left]').val(); 
                    var bottom = $panel.find('input[name=bottom]').val(); 
                    
                    code = '[dcs_ul_list type="'+type+'" size="'+size+'" ctype="'+ctype+'" float="'+float_+'" '+
                           'left="'+left+'" bottom="'+bottom+'"]Your list items here[/dcs_ul_list]';  
                    
                } break;
                
                case 'dcs_postbox':
                {
                    var id = $panel.find('input[name=id]').val();
                    var size = $panel.find('select[name=size]').val();  
                    var desc = $panel.find('textarea[name=desc]').val();  
                    var words = $panel.find('input[name=words]').val(); 
                    var float_ = $panel.find('select[name=float]').val(); 
                    
                    var top = $panel.find('input[name=top]').val();
                    var right = $panel.find('input[name=right]').val();
                    var bottom = $panel.find('input[name=bottom]').val();
                    var left = $panel.find('input[name=left]').val();                     
                    
                    var url = $panel.find('input[name=url]').val(); 
                    
                    var border = $panel.find('input[name=border]').is(':checked') ? true : false;
                    if(border) { border = 'true'; } else { border = 'false'; }    
                    
                    code = '[dcs_postbox id="'+id+'" size="'+size+'" desc="'+desc+'" words="'+words+'" float="'+float_+'" '+
                           'top="'+top+'" right="'+right+'" bottom="'+bottom+'" left="'+left+'" url="'+url+'" border="'+border+'" /]';          
                } break;
                
                case 'dcs_pagebox':
                {
                    var id = $panel.find('input[name=id]').val();
                    var size = $panel.find('select[name=size]').val();  
                    var desc = $panel.find('textarea[name=desc]').val();  
                    var words = $panel.find('input[name=words]').val(); 
                    var float_ = $panel.find('select[name=float]').val(); 
                    
                    var top = $panel.find('input[name=top]').val();
                    var right = $panel.find('input[name=right]').val();
                    var bottom = $panel.find('input[name=bottom]').val();
                    var left = $panel.find('input[name=left]').val();                     
                    
                    var url = $panel.find('input[name=url]').val(); 
                    
                    var border = $panel.find('input[name=border]').is(':checked') ? true : false;
                    if(border) { border = 'true'; } else { border = 'false'; }                      
                    
                    code = '[dcs_pagebox id="'+id+'" size="'+size+'" desc="'+desc+'" words="'+words+'" float="'+float_+'" '+
                           'top="'+top+'" right="'+right+'" bottom="'+bottom+'" left="'+left+'" url="'+url+'" border="'+border+'" /]';          
                } break;
                          
                case 'dcs_lightbox_ngg_link':
                {
                    var id = $panel.find('input[name=id]').val(); 
                    var title = $panel.find('input[name=title]').val();
                    var color = $panel.find('input[name=color]').val();
                    var group = $panel.find('input[name=group]').val();                          
                    
                    var color_use = $panel.find('input[name=color_use]').is(':checked') ? true : false;
                    if(color_use) { color_use = 'true'; } else { color_use = 'false'; }                     
                    
                    code = '[dcs_lightbox_ngg_link id="'+id+'" color="'+color+'" color_use="'+color_use+'" title="'+title+'" group="'+group+'"]Your text here[/dcs_lightbox_ngg_link]';   
                       
                } break;                                
                    
                case 'dcs_image':
                {
                    var url = $panel.find('input[name=url]').val(); 
                    var alt = $panel.find('input[name=alt]').val();
                    var size = $panel.find('select[name=size]').val(); 
                    var ctype = $panel.find('select[name=ctype]').val(); 
                    var float_ = $panel.find('select[name=float]').val(); 
                    var clear = $panel.find('select[name=clear]').val();
                             
                    var top = $panel.find('input[name=top]').val();
                    var right = $panel.find('input[name=right]').val();
                    var bottom = $panel.find('input[name=bottom]').val();
                    var left = $panel.find('input[name=left]').val(); 
                    
                    var align = $panel.find('select[name=align]').val();
                    var title = $panel.find('textarea[name=title]').val();  
                    var desc = $panel.find('textarea[name=desc]').val();  
                    
                    var padding = $panel.find('input[name=padding]').val(); 
                    var link = $panel.find('input[name=link]').val();  
                    var group = $panel.find('input[name=group]').val(); 
                    
                    var border = $panel.find('input[name=border]').is(':checked') ? true : false;
                    if(border) { border = 'true'; } else { border = 'false'; }                      

                    var lightbox = $panel.find('input[name=lightbox]').is(':checked') ? true : false;
                    if(lightbox) { lightbox = 'true'; } else { lightbox = 'false'; }   

                    var image_as_link = $panel.find('input[name=image_as_link]').is(':checked') ? true : false;
                    if(image_as_link) { image_as_link = 'true'; } else { image_as_link = 'false'; }   
                    
                    var new_tab = $panel.find('input[name=new_tab]').is(':checked') ? true : false;
                    if(new_tab) { new_tab = 'true'; } else { new_tab = 'false'; }                       

                    var real_size = $panel.find('input[name=real_size]').is(':checked') ? true : false;
                    if(real_size) { real_size = 'true'; } else { real_size = 'false'; }   

                    var viewport_w = $panel.find('input[name=viewport_w]').val(); 
                    
                    var viewport_h = $panel.find('input[name=viewport_h]').val(); 
                    
                    var grayscale = $panel.find('input[name=grayscale]').is(':checked') ? true : false;
                    if(grayscale) { grayscale = 'true'; } else { grayscale = 'false'; } 
                    
                    code = '[dcs_image size="'+size+'" ctype="'+ctype+'" url="'+url+'" alt="'+alt+'" float="'+float_+'" clear="'+clear+'" top="'+top+'" right="'+right+'" bottom="'+bottom+'" '+
                           'left="'+left+'" align="'+align+'" title="'+title+'" desc="'+desc+'" padding="'+padding+'" border="'+border+'" '+
                           'lightbox="'+lightbox+'" image_as_link="'+image_as_link+'" new_tab="'+new_tab+'" link="'+link+'" group="'+group+'" '+
                           'real_size="'+real_size+'" viewport_w="'+viewport_w+'" viewport_h="'+viewport_h+'" grayscale="'+grayscale+'" /]';                    
                } break;   
                
                case 'dcs_column':
                {
                    var size = $panel.find('select[name=size]').val(); 
                    var ctype = $panel.find('select[name=ctype]').val(); 
                    var float_ = $panel.find('select[name=float]').val(); 
                    
                    var top = $panel.find('input[name=top]').val();
                    var right = $panel.find('input[name=right]').val();
                    var bottom = $panel.find('input[name=bottom]').val();
                    var left = $panel.find('input[name=left]').val(); 
                    
                    var sc_filter = $panel.find('input[name=sc_filter]').is(':checked') ? true : false;
                    if(sc_filter) { sc_filter = 'true'; } else { sc_filter = 'false'; }                                            

                    var wp_filter = $panel.find('input[name=wp_filter]').is(':checked') ? true : false;
                    if(wp_filter) { wp_filter = 'true'; } else { wp_filter = 'false'; }  
                    
                    var clear = $panel.find('select[name=clear]').val();
                      
                    code = '[dcs_column size="'+size+'" ctype="'+ctype+'" float="'+float_+'" '+
                           'top="'+top+'" right="'+right+'" bottom="'+bottom+'" left="'+left+'" '+
                           'clear="'+clear+'" sc_filter="'+sc_filter+'" wp_filter="'+wp_filter+'"]Your content here[/dcs_column]'  
                                              
                } break;        
                      
                case 'dcs_btn':
                {
                    var label = $panel.find('input[name=label]').val(); 
                    var url = $panel.find('input[name=url]').val();
                    var color = $panel.find('input[name=color]').val();
                    var hcolor = $panel.find('input[name=hcolor]').val();                          
                    
                    var align = $panel.find('select[name=align]').val(); 
                    var clear = $panel.find('select[name=clear]').val(); 
                    
                    var wrapper = $panel.find('input[name=wrapper]').is(':checked') ? true : false;
                    if(wrapper) { wrapper = 'true'; } else { wrapper = 'false'; }                     

                    var new_tab = $panel.find('input[name=new_tab]').is(':checked') ? true : false;
                    if(new_tab) { new_tab = 'true'; } else { new_tab = 'false'; }     
                    
                    code = '[dcs_btn label="'+label+'" url="'+url+'" color="'+color+'" hcolor="'+hcolor+'" wrapper="'+wrapper+'" new_tab="'+new_tab+'" align="'+align+'" clear="'+clear+'" /]';   
                       
                } break;                        
             
                case 'dcs_tip':
                {
                    var url = $panel.find('input[name=url]').val();
                    var title = $panel.find('input[name=title]').val();  
                    var link = $panel.find('input[name=link]').val();
                    var link_name = $panel.find('input[name=link_name]').val();
                    var color = $panel.find('input[name=color]').val();
                    
                    var bottom = $panel.find('input[name=bottom]').val(); 
                    var image_space = $panel.find('input[name=image_space]').val();   
                    
                    var new_tab = $panel.find('input[name=new_tab]').is(':checked') ? true : false;
                    if(new_tab) { new_tab = 'true'; } else { new_tab = 'false'; }                        
                    
                    code = '[dcs_tip title="'+title+'" url="'+url+'" link="'+link+'" link_name="'+link_name+'" '+
                           'bottom="'+bottom+'" new_tab="'+new_tab+'" color="'+color+'" image_space="'+image_space+'"]'+
                           'Your content here[/dcs_tip]';   
                       
                } break; 
                
                case 'dcs_video':
                {
                    var url = $panel.find('input[name=url]').val(); 
                    var size = $panel.find('select[name=size]').val(); 
                    var ctype = $panel.find('select[name=ctype]').val(); 
                    var float_ = $panel.find('select[name=float]').val(); 
                    var clear = $panel.find('select[name=clear]').val();
                    
                    var bottom = $panel.find('input[name=bottom]').val();
                    
                    var desc_use = $panel.find('input[name=desc_use]').is(':checked') ? true : false;
                    if(desc_use) { desc_use = 'true'; } else { desc_use = 'false'; }                       
                    
                    code = '[dcs_video url="'+url+'" size="'+size+'" ctype="'+ctype+'" float="'+float_+'" '+
                           'bottom="'+bottom+'" clear="'+clear+'" desc_use="'+desc_use+'"]'+
                           'Optional video description here[/dcs_video]';                      
                    
                } break;  
                
                case 'dcs_ngg':
                {
                    var id = $panel.find('select[name=ngg_list]').val(); 
                    var size = $panel.find('select[name=size]').val(); 
                    var ctype = $panel.find('select[name=ctype]').val(); 
                    var float_ = $panel.find('select[name=float]').val(); 
                    var clear = $panel.find('select[name=clear]').val();
                    
                    var bottom = $panel.find('input[name=bottom]').val();
                    
                    var w = $panel.find('input[name=w]').val();
                    var h = $panel.find('input[name=h]').val();
                    
                    var count = $panel.find('input[name=count]').val(); 
                    var tright = $panel.find('input[name=tright]').val(); 
                    var tbottom = $panel.find('input[name=tbottom]').val(); 
                    var group = $panel.find('input[name=group]').val(); 
                    var padding = $panel.find('input[name=padding]').val(); 
                    
                    var bcolor = $panel.find('input[name=bcolor]').val(); 
                    var bgcolor = $panel.find('input[name=bgcolor]').val(); 
                    
                    var border = $panel.find('input[name=border]').is(':checked') ? true : false;
                    if(border) { border = 'true'; } else { border = 'false'; }  
                   
                    var all = $panel.find('input[name=all]').is(':checked') ? true : false;
                    if(all) { all = 'true'; } else { all = 'false'; }  
                    
                    var bgcolor_use = $panel.find('input[name=bgcolor_use]').is(':checked') ? true : false;
                    if(bgcolor_use) { bgcolor_use = 'true'; } else { bgcolor_use = 'false'; }  
                    
                    var random = $panel.find('input[name=random]').is(':checked') ? true : false;
                    if(random) { random = 'true'; } else { random = 'false'; }                                                                                  
                    
                    code = '[dcs_ngg id="'+id+'" size="'+size+'" ctype="'+ctype+'" float="'+float_+'" clear="'+clear+'" '+
                           'bottom="'+bottom+'" w="'+w+'" h="'+h+'" count="'+count+'" tright="'+tright+'" tbottom="'+tbottom+'" '+
                           'group="'+group+'" padding="'+padding+'" bcolor="'+bcolor+'" bgcolor="'+bgcolor+'" '+
                           'border="'+border+'" all="'+all+'" bgcolor_use="'+bgcolor_use+'" random="'+random+'" /]';                      
                    
                } break;   
                
                case 'dcs_ngg_thumbs_slider':
                {
                    var id = $panel.find('select[name=ngg_list]').val(); 
                    var size = $panel.find('select[name=size]').val(); 
                    var ctype = $panel.find('select[name=ctype]').val(); 
                    var float_ = $panel.find('select[name=float]').val(); 
                    var clear = $panel.find('select[name=clear]').val();
                    
                    var bottom = $panel.find('input[name=bottom]').val();
                    var left = $panel.find('input[name=left]').val(); 
                    var right = $panel.find('input[name=right]').val();
                    
                    var desc = $panel.find('input[name=desc]').val(); 
                    var viewport_w = $panel.find('input[name=viewport_w]').val(); 
                    var viewport_h = $panel.find('input[name=viewport_h]').val();
                    
                    var transition = $panel.find('select[name=transition]').val();
                    var thumb_size = $panel.find('input[name=thumb_size]').val();
                    var count = $panel.find('input[name=count]').val();
                    var bcolor = $panel.find('input[name=bcolor]').val();
                    
                    var random = $panel.find('input[name=random]').is(':checked') ? true : false;
                    if(random) { random = 'true'; } else { random = 'false'; }                       
                    
                    var all = $panel.find('input[name=all]').is(':checked') ? true : false;
                    if(all) { all = 'true'; } else { all = 'false'; }                      

                    var slide_title = $panel.find('input[name=slide_title]').is(':checked') ? true : false;
                    if(slide_title) { slide_title = 'true'; } else { slide_title = 'false'; }  

                    var slide_desc = $panel.find('input[name=slide_desc]').is(':checked') ? true : false;
                    if(slide_desc) { slide_desc = 'true'; } else { slide_desc = 'false'; } 
                    
                    code = '[dcs_ngg_thumbs_slider id="'+id+'" size="'+size+'" ctype="'+ctype+'" float="'+float_+'" clear="'+clear+'" '+
                           'bottom="'+bottom+'" left="'+left+'" right="'+right+'" desc="'+desc+'" viewport_w="'+viewport_w+'" viewport_h="'+viewport_h+'" '+
                           'count="'+count+'" transition="'+transition+'" thumb_size="'+thumb_size+'" bcolor="'+bcolor+'" '+
                           'random="'+random+'" all="'+all+'" slide_title="'+slide_title+'" slide_desc="'+slide_desc+'" '+
                           '/]';                                          
                } break;                                             
            
                case 'dcs_code':  
                {
                    var size = $panel.find('select[name=size]').val(); 
                    var ctype = $panel.find('select[name=ctype]').val(); 
                    var float_ = $panel.find('select[name=float]').val(); 
                    var clear = $panel.find('select[name=clear]').val();  
                    
                    var lang = $panel.find('select[name=lang]').val();
                    var bottom = $panel.find('input[name=bottom]').val(); 
                    var title = $panel.find('textarea[name=title]').val(); 
                    var desc = $panel.find('textarea[name=desc]').val(); 
                    
                    var minimized = $panel.find('input[name=minimized]').is(':checked') ? true : false;
                    if(minimized) { minimized = 'true'; } else { minimized = 'false'; }                       
                    
                    var stripes = $panel.find('input[name=stripes]').is(':checked') ? true : false;
                    if(stripes) { stripes = 'true'; } else { stripes = 'false'; }                        
                    
                    code = '[dcs_code size="'+size+'" ctype="'+ctype+'" float="'+float_+'" clear="'+clear+'" '+
                           'bottom="'+bottom+'" lang="'+lang+'" stripes="'+stripes+'" minimized="'+minimized+'" '+
                           'title="'+title+'" desc="'+desc+'"]'+                             
                           'Your content here[/dcs_code]';                                          
                } break;             
                                
            } // switch     
                      
            
            if($area != null)
            {                
                $area.dcf_insertAtCaret(code);        
            } else
            {            
                // insert to wp html editor
                if(jQuery('#wp-content-wrap').hasClass('html-active'))
                {
                    dcf_DCThemeInsertText(window.edCanvas, code);    
                } else                               
                if(window.tinyMCE) 
                {
                    window.tinyMCE.execInstanceCommand('content', 'mceInsertContent', false, code);
                }
            }                
        }

        function dcf_closeShortCodeToolBox($wrapper) { }
        
        
        jQuery('.dc-cms-add-media-shortcodes').click(function(){
            var d = new Date();
            var t = d.getTime();
            jQuery(this).dcf_showShortCodeToolBox({url:dccp_s.theme_url+'/cms/shortcodes/sc.html'+'?'+t, open_js:dcf_openShortCodeToolBox, insert_js:dcf_insertShortCodeToolBox, close_js:dcf_closeShortCodeToolBox, area:null });
            
        });           
        
        jQuery.fn.extend({
            dcf_setupShortCodeTextAreaTool: function(){
                jQuery(this).find('.dc-cms-add-shortcode-textarea-btn').click(function()
        {      
            var d = new Date();
            var t = d.getTime();            
            var $area = jQuery(this).parent().find('textarea:first');
            jQuery(this).dcf_showShortCodeToolBox({url:dccp_s.theme_url+'/cms/shortcodes/sc.html'+'?'+t, open_js:dcf_openShortCodeToolBox, insert_js:dcf_insertShortCodeToolBox, close_js:dcf_closeShortCodeToolBox, area:$area });        
            
        }) }});      
        jQuery('body').dcf_setupShortCodeTextAreaTool();
        

   
       

        /******************************
        /*  Sortable Icons List     
        ******************************/           
        jQuery('ul.dc-cms-sortable-icons-box-list').sortable(
        {
            cursor: "move",
            update:function(event, ui)
            {
                if(jQuery(ui.item).is('li'))
                {
                    var p = jQuery(ui.item).parent();
                    var len = p.find('li').length;
                    for(var i = 0; i < len; i++)
                    {
                        p.find('li:eq('+i+') .index-box').text((i+1));
                    }
                }
            }
        });
       
        /******************************
        /*  Sortable Sidebars List     
        ******************************/         
        jQuery('ul.dc-cms-sortable-sidebars-list').sortable(
        {
            axis: "y",
            cursor: "move",
            update: function() { }
        }); 
       
         jQuery('.dc-cms-sidebars-list .item > .dc-cms-submit-btn').each(function() { jQuery(this).click(
            function() 
            { 
                var parent = jQuery(this).parent();
                var ul = parent.find('ul');  
                var data = parent.find('ul').sortable('toArray');
                var action = parent.find('.dc-cms-action-name').text();
                var index = parent.find('.index').text(); 
                
                var len = jQuery('.dc-cms-ajax-progress').length;
                if(len)
                {
                    jQuery('.dc-cms-ajax-progress').stop().remove();     
                } 
                jQuery('body').append(dccp_s.ajax_box);                
                jQuery('.dc-cms-ajax-progress').css('opacity', 1.0);
                
                function element(id, use)
                {
                    this.id = id;
                    this.use = use;    
                }
                var sids = new Array();
                
                
                ul.find('li').each(function() 
                { 
                    var id = jQuery(this).find('span.id').text(); 
                    
                    var use = jQuery(this).find('input.use').is(':checked');
                    var object = new element(id, use);
                    sids[sids.length] = object;
                    // alert(id);
                });
                data = JSON.stringify(sids);
                //alert(data)
                jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action="+action+'&data='+data+'&index='+index,
               success: function(str){
                  // alert(str);
                  
                 var count = parent.find('ul li').length;  
                 for(i = 0; i < count; i++)
                 {
                    parent.find('ul li:eq('+(i)+') span.number').text(i+1);    
                 }
                 jQuery('.dc-cms-ajax-progress').animate({opacity:0.0}, 1000, function() { jQuery(this).remove(); });
               }
             });                  
                
            });
            
         });        
        
        
        /******************************
        /* Image loading      
        ******************************/ 
/*
        function dcf_setupLoadImageBtn($obj)
        {         
            $obj.click(function() {
                   
             jQuery.fn.dc_cms_data.dc_formfield = jQuery(this).attr('name');  
             tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true'); 
             return false;

            });               
        }
        
        jQuery('.dc-cms-submit-image-btn').each(function(){
            
             dcf_setupLoadImageBtn(jQuery(this));
            
        });
*/  
                              
        if(!jQuery.fn.dcf_setupCMSLoadImageBtn) 
        {           
            jQuery.fn.extend({
                dcf_setupCMSLoadImageBtn: function(){

            jQuery(this).find('.dc-cms-submit-image-btn').each(function()
            {                      
                if(jQuery(this).data('dc_img_btn') != 'set')
                {
                    jQuery(this).data('dc_img_btn', 'set'); 
                    jQuery(this).click(function() {                                                                                
                         jQuery.fn.dc_cms_data.dc_formfield = jQuery(this).attr('name');  
                         tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true'); 
                         return false;
                    }); 
                }                  
                    
            }); }});
        } 
        jQuery('body').dcf_setupCMSLoadImageBtn();                      
                           
            
        if(typeof window.original_send_to_editor === 'undefined')
        {
            window.original_send_to_editor = null;
            window.original_send_to_editor = window.send_to_editor;

            window.send_to_editor = function(html) 
            {

                if(jQuery.fn.dc_cms_data.dc_formfield)
                {
                   // imgurl = jQuery('img',html).attr('src');
                   var imgurl = jQuery(html).attr('href');  

                    jQuery('#'+jQuery.fn.dc_cms_data.dc_formfield).val(imgurl);
                    tb_remove();
                    jQuery.fn.dc_cms_data.dc_formfield = null;
                } else
                {
                    window.original_send_to_editor(html);
                }
            }                 
        }                       
         
/*        
        function dcf_setupColorPicker($obj)
        {
            if($obj.data('dc_colorpicker') != 'set')
            {                                    
                var color = $obj.val();
                if(color == '')
                {
                    color = '#FFFFFF';
                    $obj.val(color);
                }
                
                var rgb = dc_cp_ogl.unpack(color);
                $obj.css({'background-color':color});                 
                $obj.data('dc_colorpicker', 'set'); 
                
                if((0.213 * rgb[0] + 0.715 * rgb[1] + 0.072 * rgb[2]) > 0.5) { $obj.css('color', '#000'); } else { $obj.css('color', '#FFF'); }         
                
                $obj.keyup(function()
                {
                    var val = jQuery(this).val();
                    if (val && val != dc_cp_ogl.color) {
                      dc_cp_ogl.setColor(val);
                    }                        
                });
         
                $obj.click(function(event) 
                {                     
                    var color = jQuery(this).val();
                    dc_cp_ogl.color = color;
                    var create = false;
                    if(jQuery('#dc-color-picker-tool').length == 0)
                    {
                        create = true;
                    }
                    
                    if(jQuery('#dc-color-picker-tool').length > 0 && dc_cp_ogl.obj != this)
                    {  
                       create = true;   
                       jQuery('#dc-color-picker-tool').remove();    
                    }
                    
                    if(create)
                    {
                        dc_cp_ogl.obj = this;
                        dc_cp_ogl.query = jQuery(this);
                        dc_cp_ogl.hsl = dc_cp_ogl.RGBToHSL(color);
 
                        var pos = jQuery(event.target).offset();
                        var height = jQuery(event.target).outerHeight() + 2;
                        var pos_type = 'absolute';
 
                        var p = jQuery(this).data('dc_position');
                        if(p !== undefined)
                        {
                            pos_type = 'fixed';
                            var scroll_y = jQuery(window).scrollTop();
                            pos.top = pos.top - scroll_y;     
                        }
 
                        var z = jQuery(this).data('dc_z_index');
                        if(z == undefined) { z = 'auto';}
                                            
                        jQuery('body').append('<div id="dc-color-picker-tool" style="left:'+pos.left+'px;z-index:'+z+';position:'+pos_type+';'+';top:'+(pos.top+height)+'px;"><div class="dc-color-picker-color"></div><div class="dc-color-picker-hue"></div><div class="dc-color-picker-saturation"></div><div class="dc-color-pikcer-hue-pointer"></div><div class="dc-color-picker-saturation-pointer"></div><div class="dc-color-picker-close-btn">x</div></div>');    
                        jQuery('#dc-color-picker-tool').mousedown(function(event) { dc_cp_ogl.mousedown(event); }); 
                        jQuery('#dc-color-picker-tool .dc-color-picker-color').css({backgroundColor:color});
                        
                        jQuery('#dc-color-picker-tool .dc-color-picker-close-btn').mousedown(function(event){jQuery(this).parent().remove(); dc_cp_ogl.obj = null; dc_cp_ogl.query = null; dc_cp_ogl.dragging = false; event.stopPropagation();});                                              
                        
                        dc_cp_ogl.setColor(color);   
                    }    
                }) 
            }            
        }
*/     
        


            
        /******************************
        /* Color picker      
        ******************************/            
        if(!jQuery.fn.dcf_setupColorPicker) 
        {           
            jQuery.fn.extend({
                dcf_setupColorPicker: function(){
            
                var dc_cp_ogl = new Object();
                dc_cp_ogl.color = '#FFFFFF';
                dc_cp_ogl.radius = 84;
                dc_cp_ogl.square = 100;
                dc_cp_ogl.width = 194;
                dc_cp_ogl.halfpointer = 8;
                dc_cp_ogl.obj = null;
                dc_cp_ogl.query = null;
                dc_cp_ogl.hsl = [0, 0, 0];
                dc_cp_ogl.rgb = [1.0, 1.0, 1.0];
                dc_cp_ogl.dragging = false;
                dc_cp_ogl.can_change = true;
                        
                // set color from css hex syntax #123456 or #123
                dc_cp_ogl.setColor = function(color) 
                {            
                    var unpacked = dc_cp_ogl.unpack(color);
                    if(dc_cp_ogl.can_change)
                    {
                        if(color.length == 3 || color.length == 6)
                        {
                            if(-1 == color.search('/#/'))
                            {
                                color = '#'+color;  
                            }  
                        }
                        dc_cp_ogl.color = color;
                    }
                    dc_cp_ogl.rgb = unpacked;
                    dc_cp_ogl.hsl = dc_cp_ogl.RGBToHSL(dc_cp_ogl.rgb);
                    dc_cp_ogl.updateDisplay();
                    return this;
                };               
                                  
                dc_cp_ogl.setHSL = function(hsl) 
                {
                    dc_cp_ogl.hsl = hsl;
                    dc_cp_ogl.rgb = dc_cp_ogl.HSLToRGB(hsl);
                    dc_cp_ogl.color = dc_cp_ogl.pack(dc_cp_ogl.rgb);
                    dc_cp_ogl.can_change = true;
                    dc_cp_ogl.updateDisplay();
                    
                    return this;
                };                            
                                          
                dc_cp_ogl.updateDisplay = function() 
                {         
                    var angle = dc_cp_ogl.hsl[0] * 6.28;

                    jQuery('#dc-color-picker-tool .dc-color-pikcer-hue-pointer').css({
                        left: (Math.round(Math.sin(angle) * dc_cp_ogl.radius + dc_cp_ogl.width / 2)-dc_cp_ogl.halfpointer) + 'px',
                        top: (Math.round(-Math.cos(angle) * dc_cp_ogl.radius + dc_cp_ogl.width / 2)-dc_cp_ogl.halfpointer) + 'px'
                    });

                    jQuery('#dc-color-picker-tool .dc-color-picker-saturation-pointer').css({
                        left: (Math.round(dc_cp_ogl.square * (.5 - dc_cp_ogl.hsl[1]) + dc_cp_ogl.width / 2)-dc_cp_ogl.halfpointer) + 'px',
                        top: (Math.round(dc_cp_ogl.square * (.5 - dc_cp_ogl.hsl[2]) + dc_cp_ogl.width / 2)-dc_cp_ogl.halfpointer) + 'px'
                    });

                    jQuery('#dc-color-picker-tool .dc-color-picker-color').css('backgroundColor', dc_cp_ogl.pack(dc_cp_ogl.HSLToRGB([dc_cp_ogl.hsl[0], 1, 0.5])));

                    dc_cp_ogl.query.css({
                        backgroundColor: dc_cp_ogl.color,
                        color: (0.213 * dc_cp_ogl.rgb[0] + 0.715 * dc_cp_ogl.rgb[1] + 0.072 * dc_cp_ogl.rgb[2]) > 0.5 ? '#000' : '#FFF'
                    });                     
                    
                    if(dc_cp_ogl.can_change)
                    {  
                        dc_cp_ogl.query.attr('value', dc_cp_ogl.color.toUpperCase());
                        dc_cp_ogl.query.val(dc_cp_ogl.color.toUpperCase());  
                    }                      
                };                
                         
                            
                dc_cp_ogl.widgetCoords = function(event) 
                {
                    var pos = jQuery('#dc-color-picker-tool').offset();                       
                    return { x: (event.pageX - pos.left) - dc_cp_ogl.width / 2, y: (event.pageY - pos.top) - dc_cp_ogl.width / 2 };    
                }               
                            
                dc_cp_ogl.mousedown = function(event) 
                {            
                    if(!dc_cp_ogl.dragging) 
                    {
                      jQuery('#dc-color-picker-tool').bind('mousemove', dc_cp_ogl.mousemove);
                      jQuery('#dc-color-picker-tool').bind('mouseup', dc_cp_ogl.mouseup);
                      
                      dc_cp_ogl.dragging = true;
                    }

                    var pos = dc_cp_ogl.widgetCoords(event);
                    dc_cp_ogl.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > dc_cp_ogl.square;
                    dc_cp_ogl.mousemove(event);
                    
                    event.stopPropagation(); 
                    return false;
                };                 
                         
                dc_cp_ogl.pack = function(rgb) 
                {
                    var r = Math.round(rgb[0] * 255);
                    var g = Math.round(rgb[1] * 255);
                    var b = Math.round(rgb[2] * 255);
                    
                    return '#' + (r < 16 ? '0' : '') + r.toString(16) + (g < 16 ? '0' : '') + g.toString(16) + (b < 16 ? '0' : '') + b.toString(16);
                };                   

                dc_cp_ogl.mouseup = function() 
                {
                    jQuery('#dc-color-picker-tool').unbind('mousemove', dc_cp_ogl.mousemove);
                    jQuery('#dc-color-picker-tool').unbind('mouseup', dc_cp_ogl.mouseup);
                    dc_cp_ogl.dragging = false;
                };                    
                  
                dc_cp_ogl.mousemove = function(event) 
                {
                    var pos = dc_cp_ogl.widgetCoords(event);

                    if (dc_cp_ogl.circleDrag) 
                    {
                        var hue = Math.atan2(pos.x, -pos.y) / 6.28;
                        if (hue < 0) 
                        {
                            hue += 1;    
                        }
                        dc_cp_ogl.setHSL([hue, dc_cp_ogl.hsl[1], dc_cp_ogl.hsl[2]]);
                    }
                    else 
                    {
                      var sat = Math.max(0, Math.min(1, -(pos.x / dc_cp_ogl.square) + .5));
                      var lum = Math.max(0, Math.min(1, -(pos.y / dc_cp_ogl.square) + .5));
                      dc_cp_ogl.setHSL([dc_cp_ogl.hsl[0], sat, lum]);
                    }
                    
                    return false;
                };                                                          
                            
                dc_cp_ogl.unpack = function(color) 
                {
                    if(color.substring(0,1) == '#')
                    {
                        if(color.length == 7) 
                        {
                            dc_cp_ogl.can_change = true; 
                            return [parseInt('0x' + color.substring(1, 3)) / 255, parseInt('0x' + color.substring(3, 5)) / 255, parseInt('0x' + color.substring(5, 7)) / 255];
                            
                        } else 
                        if(color.length == 4) 
                        {
                            dc_cp_ogl.can_change = true; 
                            return [parseInt('0x' + color.substring(1, 2)) / 15, parseInt('0x' + color.substring(2, 3)) / 15, parseInt('0x' + color.substring(3, 4)) / 15];
                            
                        } else
                        {
                            dc_cp_ogl.can_change = false;
                            dc_cp_ogl.color = '#FFFFFF';
                            return [1.0, 1.0, 1.0]                                       
                        }
                    } else
                    {
                        if(color.length == 6) 
                        {
                            dc_cp_ogl.can_change = true; 
                            return [parseInt('0x' + color.substring(0, 2)) / 255, parseInt('0x' + color.substring(2, 4)) / 255, parseInt('0x' + color.substring(4, 6)) / 255];
                            
                        } else 
                        if(color.length == 3) 
                        {
                            dc_cp_ogl.can_change = true; 
                            return [parseInt('0x' + color.substring(0, 1)) / 15, parseInt('0x' + color.substring(1, 2)) / 15, parseInt('0x' + color.substring(2, 3)) / 15];
                            
                        } else
                        {
                            dc_cp_ogl.can_change = false;
                            dc_cp_ogl.color = '#FFFFFF';
                            return [1.0, 1.0, 1.0]
                        }                
                    }
                    return false;
                };    
                              
                dc_cp_ogl.HSLToRGB = function (hsl) 
                {
                    var m1, m2, r, g, b;
                    var h = hsl[0], s = hsl[1], l = hsl[2];
                    m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
                    m1 = l * 2 - m2;                
                    
                    return [this.hueToRGB(m1, m2, h+0.33333), this.hueToRGB(m1, m2, h), this.hueToRGB(m1, m2, h-0.33333)];
                };

                dc_cp_ogl.hueToRGB = function (m1, m2, h) 
                {
                    h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
                    if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
                    if (h * 2 < 1) return m2;
                    if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
                    
                    return m1;
                };                      
                              
                dc_cp_ogl.RGBToHSL = function(rgb) 
                {
                    var min, max, delta, h, s, l;
                    var r = rgb[0], g = rgb[1], b = rgb[2];
                    min = Math.min(r, Math.min(g, b));
                    max = Math.max(r, Math.max(g, b));
                    delta = max - min;
                    l = (min + max) / 2;
                    s = 0;
                    if (l > 0 && l < 1) 
                    {
                        s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
                    }
                    
                    h = 0;
                    if (delta > 0) 
                    {
                        if (max == r && max != g) h += (g - b) / delta;
                        if (max == g && max != b) h += (2 + (b - r) / delta);
                        if (max == b && max != r) h += (4 + (r - g) / delta);
                        h /= 6;
                    }
                    return [h, s, l];
                };                     
                            
                jQuery('body').mousedown(function(event)
                {
                    var $picker = jQuery('#dc-color-picker-tool');
                    if($picker.length)   
                    {   
                        $picker.remove();
                        dc_cp_ogl.obj = null; 
                        dc_cp_ogl.query = null; 
                        dc_cp_ogl.dragging = false; 
                    }
                });                       
                    
            jQuery(this).find('input.dc-color-picker').each(function()
            {  
            
                if(jQuery(this).data('dc_colorpicker') != 'set')
                {                                    
                    var color = jQuery(this).val();
                    if(color == '')
                    {
                        color = '#FFFFFF';
                        jQuery(this).val(color);
                    }
                    
                    var rgb = dc_cp_ogl.unpack(color);
                    jQuery(this).css({'background-color':color});                 
                    jQuery(this).data('dc_colorpicker', 'set'); 
                    
                    if((0.213 * rgb[0] + 0.715 * rgb[1] + 0.072 * rgb[2]) > 0.5) { jQuery(this).css('color', '#000'); } else { jQuery(this).css('color', '#FFF'); }         
                    
                    jQuery(this).keyup(function()
                    {
                        var val = jQuery(this).val();
                        if (val && val != dc_cp_ogl.color) {
                          dc_cp_ogl.setColor(val);
                        }                        
                    });
             
                    jQuery(this).click(function(event) 
                    {                     
                        var color = jQuery(this).val();
                        dc_cp_ogl.color = color;
                        var create = false;
                        if(jQuery('#dc-color-picker-tool').length == 0)
                        {
                            create = true;
                        }
                        
                        if(jQuery('#dc-color-picker-tool').length > 0 && dc_cp_ogl.obj != this)
                        {  
                           create = true;   
                           jQuery('#dc-color-picker-tool').remove();    
                        }
                        
                        if(create)
                        {
                            dc_cp_ogl.obj = this;
                            dc_cp_ogl.query = jQuery(this);
                            dc_cp_ogl.hsl = dc_cp_ogl.RGBToHSL(color);
     
                            var pos = jQuery(event.target).offset();
                            var height = jQuery(event.target).outerHeight() + 2;
                            var pos_type = 'absolute';
     
                            var p = jQuery(this).data('dc_position');
                            if(p !== undefined)
                            {
                                pos_type = 'fixed';
                                var scroll_y = jQuery(window).scrollTop();
                                pos.top = pos.top - scroll_y;     
                            }
     
                            var z = jQuery(this).data('dc_z_index');
                            if(z == undefined) { z = 'auto';}
                                                
                            jQuery('body').append('<div id="dc-color-picker-tool" style="left:'+pos.left+'px;z-index:'+z+';position:'+pos_type+';'+';top:'+(pos.top+height)+'px;"><div class="dc-color-picker-color"></div><div class="dc-color-picker-hue"></div><div class="dc-color-picker-saturation"></div><div class="dc-color-pikcer-hue-pointer"></div><div class="dc-color-picker-saturation-pointer"></div><div class="dc-color-picker-close-btn">x</div></div>');    
                            jQuery('#dc-color-picker-tool').mousedown(function(event) { dc_cp_ogl.mousedown(event); }); 
                            jQuery('#dc-color-picker-tool .dc-color-picker-color').css({backgroundColor:color});
                            
                            jQuery('#dc-color-picker-tool .dc-color-picker-close-btn').mousedown(function(event){jQuery(this).parent().remove(); dc_cp_ogl.obj = null; dc_cp_ogl.query = null; dc_cp_ogl.dragging = false; event.stopPropagation();});                                              
                            
                            dc_cp_ogl.setColor(color);   
                        }    
                    }) 
                }                  
                
            }); }});
        } 
        jQuery('body').dcf_setupColorPicker();         
        

        
        
        /***************************************************
        *  POST and PAGE SEO SETTINGS            
        ****************************************************/
        jQuery('.dc-post-private-seo-meta-wrapper').each( 
            function()
            {
                var $wrapper = jQuery(this);
                
                jQuery(this).find('.meta-item > .delete-btn').click(function() {
                    var $item = jQuery(this).parent();
                    $item.addClass('in-delete-process');
                    if(confirm('Delete this item?'))
                    {
                        $item.remove();   
                       
                       var list = '';
                       var counter = 0;
                        $wrapper.find('.meta-item').each(function() {
                            var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                            if(counter > 0)
                            {
                                list += ',';
                            }
                            list += idx.toString();
                            counter++;
                        });
                        
                        $wrapper.find('input[name="post_seo_private_meta_list"]').val(list);                          
                         
                    } else
                    {
                        $item.removeClass('in-delete-process');    
                    }                                       
                });    
            }
        );
        
        /* post seo */
        jQuery('input[name="dc-post-meta-add-private-seo-meta"]').click(
            function() 
            {
                var $btn = jQuery(this);                                
                var $parent = $btn.parent();
                var $wrapper = $parent.parent().find('.dc-post-private-seo-meta-wrapper'); 
                var action = $parent.find('.dc-cms-action-name').text(); 
                var index = 0;
                
                if($btn.data('dc_locked') == true)
                {
                    return;
                }
                
                $btn.data('dc_locked', true);
                
                
                $wrapper.find('.meta-item').each(function() {
                    var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                    if(idx > index) { index = idx; }
                });
                index += 1;
                
                    dcf_showAjaxProgrss(); 
                
                jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action="+action+'&index='+index,
               success: function(str){
                  
                   dcf_hideAjaxProgrss(); 
                  
                   $wrapper.append(str);
                   var $new_item = $wrapper.find('.meta-item:last');
                   
                   var list = '';
                   var counter = 0;
                    $wrapper.find('.meta-item').each(function() {
                        var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                        if(counter > 0)
                        {
                            list += ',';
                        }
                        list += idx.toString();
                        counter++;
                    });
                    
                    $wrapper.find('input[name="post_seo_private_meta_list"]').val(list);                   
                    
                    
                $new_item.find('.delete-btn').click(function() {
                    var $item = jQuery(this).parent();
                    $item.addClass('in-delete-process');
                    if(confirm('Delete this item?'))
                    {
                        $item.remove();   
                       
                       var list = '';
                       var counter = 0;
                        $wrapper.find('.meta-item').each(function() {
                            var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                            if(counter > 0)
                            {
                                list += ',';
                            }
                            list += idx.toString();
                            counter++;
                        });
                        
                        $wrapper.find('input[name="post_seo_private_meta_list"]').val(list);                                                   
                    } else
                    {
                        $item.removeClass('in-delete-process');    
                    }                                       
                });                     
                    
                    
                    $btn.data('dc_locked', false);                                                            
               }
             });                    
            });
            
            
        /* page seo */
        jQuery('.dc-page-private-seo-meta-wrapper').each( 
            function()
            {
                var $wrapper = jQuery(this);
                
                jQuery(this).find('.meta-item > .delete-btn').click(function() {
                    var $item = jQuery(this).parent();
                    $item.addClass('in-delete-process');
                    if(confirm('Delete this item?'))
                    {
                        $item.remove();   
                       
                       var list = '';
                       var counter = 0;
                        $wrapper.find('.meta-item').each(function() {
                            var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                            if(counter > 0)
                            {
                                list += ',';
                            }
                            list += idx.toString();
                            counter++;
                        });
                        
                        $wrapper.find('input[name="post_seo_private_meta_list"]').val(list);                          
                         
                    } else
                    {
                        $item.removeClass('in-delete-process');    
                    }                                       
                });    
            }
        );
        
        jQuery('input[name="dc-page-meta-add-private-seo-meta"]').click(
            function() 
            {
                var $btn = jQuery(this);                                
                var $parent = $btn.parent();
                var $wrapper = $parent.parent().find('.dc-page-private-seo-meta-wrapper'); 
                var action = $parent.find('.dc-cms-action-name').text(); 
                var index = 0;
                
                if($btn.data('dc_locked') == true)
                {
                    return;
                }
                
                $btn.data('dc_locked', true);
                
                
                $wrapper.find('.meta-item').each(function() {
                    var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                    if(idx > index) { index = idx; }
                });
                index += 1;
                
                    dcf_showAjaxProgrss(); 
                
                jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action="+action+'&index='+index,
               success: function(str){
                  
                   dcf_hideAjaxProgrss(); 
                  
                   $wrapper.append(str);
                   var $new_item = $wrapper.find('.meta-item:last');
                   
                   var list = '';
                   var counter = 0;
                    $wrapper.find('.meta-item').each(function() {
                        var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                        if(counter > 0)
                        {
                            list += ',';
                        }
                        list += idx.toString();
                        counter++;
                    });
                    
                    $wrapper.find('input[name="page_seo_private_meta_list"]').val(list);                   
                    
                    
                $new_item.find('.delete-btn').click(function() {
                    var $item = jQuery(this).parent();
                    $item.addClass('in-delete-process');
                    if(confirm('Delete this item?'))
                    {
                        $item.remove();   
                       
                       var list = '';
                       var counter = 0;
                        $wrapper.find('.meta-item').each(function() {
                            var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                            if(counter > 0)
                            {
                                list += ',';
                            }
                            list += idx.toString();
                            counter++;
                        });
                        
                        $wrapper.find('input[name="page_seo_private_meta_list"]').val(list);                                                   
                    } else
                    {
                        $item.removeClass('in-delete-process');    
                    }                                       
                });                     
                    
                    
                    $btn.data('dc_locked', false);                                                            
               }
             });                    
            });    
            
            
        /* global seo */
        jQuery('.dc-cms-global-seo-meta-wrapper').each( 
            function()
            {
                var $wrapper = jQuery(this);
                
                jQuery(this).find('.meta-item > .delete-btn').click(function() {
                    var $item = jQuery(this).parent();
                    $item.addClass('in-delete-process');
                    if(confirm('Delete this item?'))
                    {
                        $item.remove();   
                       
                       var list = '';
                       var counter = 0;
                        $wrapper.find('.meta-item').each(function() {
                            var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                            if(counter > 0)
                            {
                                list += ',';
                            }
                            list += idx.toString();
                            counter++;
                        });
                        
                        $wrapper.find('input[name="global_seo_private_meta_list"]').val(list);                          
                         
                    } else
                    {
                        $item.removeClass('in-delete-process');    
                    }                                       
                });    
            }
        );
        
        jQuery('input[name="dc-global-meta-add-private-seo-meta"]').click(
            function() 
            {
                var $btn = jQuery(this);                                
                var $parent = $btn.parent();
                var $wrapper = $parent.parent().find('.dc-cms-global-seo-meta-wrapper'); 
                var action = $parent.find('.dc-cms-action-name').text(); 
                var index = 0;
                
                if($btn.data('dc_locked') == true)
                {
                    return;
                }
                
                $btn.data('dc_locked', true);
                
                
                $wrapper.find('.meta-item').each(function() {
                    var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                    if(idx > index) { index = idx; }
                });
                index += 1;
                
                    dcf_showAjaxProgrss(); 
                
                jQuery.ajax({
               type: "POST",
               url: dccp_s.admin_url+'admin-ajax.php',
               data: "action="+action+'&index='+index,
               success: function(str){
                  
                   dcf_hideAjaxProgrss(); 
                  
                   $wrapper.append(str);
                   var $new_item = $wrapper.find('.meta-item:last');
                   
                   var list = '';
                   var counter = 0;
                    $wrapper.find('.meta-item').each(function() {
                        var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                        if(counter > 0)
                        {
                            list += ',';
                        }
                        list += idx.toString();
                        counter++;
                    });
                    
                    $wrapper.find('input[name="global_seo_private_meta_list"]').val(list);                   
                    
                    
                $new_item.find('.delete-btn').click(function() {
                    var $item = jQuery(this).parent();
                    $item.addClass('in-delete-process');
                    if(confirm('Delete this item?'))
                    {
                        $item.remove();   
                       
                       var list = '';
                       var counter = 0;
                        $wrapper.find('.meta-item').each(function() {
                            var idx = parseInt(jQuery(this).find('input.dc-idx').val());
                            if(counter > 0)
                            {
                                list += ',';
                            }
                            list += idx.toString();
                            counter++;
                        });
                        
                        $wrapper.find('input[name="global_seo_private_meta_list"]').val(list);                                                   
                    } else
                    {
                        $item.removeClass('in-delete-process');    
                    }                                       
                });                     
                    
                    
                    $btn.data('dc_locked', false);                                                            
               }
             });                    
            });                         
            
       

        
}); 
    
    
    
