$.fn.equals = function (compareTo) {
    if (!compareTo || this.length != compareTo.length) {
        return false;
    }
    for (var i = 0; i < this.length; ++i) {
        if (this[i] !== compareTo[i]) {
            return false;
        }
    }
    return true;
};
function debug(x) {
    try {
        console.debug(x);
    } catch (e) {
        alert(x);
    }
}
// Form watermark
// Written by: David Stevens
///////////////////////////////////////
(function ($) {
    var txtfield = [];
    var methods = {
        init: function (options) {
            var defaults = {
                imageIndex: 0,
                container: '.c_content',
                padding: 10,
                teaserWidth: 219,
                paddingTop: 5,
                label: null
            };
            var options = $.extend(defaults, options);
            return this.each(function () {
                var obj = $(this);
                var wrapper = $('<div id="fwatermark-wrapper" />').css({ position: 'relative' }).insertAfter(obj);
                obj.find('input[type=text],textarea').each(function () {
                    var elm = $(this);
                    var bareelm = this;
                    var label = (options.label) ? elm.siblings(options.label) : elm.siblings("label[for=" + this.id + "]");
                    if (label.length <= 0) {
                        return;
                    }
                    label.addClass('watermark_label').css({ display: 'block', position: 'absolute', margin: '0 0 0 0', paddingTop: options.paddingTop, width: elm.innerWidth() - 20 })
                            .click(function () {
                                elm.trigger('focus');
                                //methods.set_focus(bareelm);
                            });
                    txtfield[this.id] = { input: elm, label: label };
                });
                obj.appendTo(wrapper);
                for (key in txtfield) {
                    var obj = txtfield[key];
                    methods.set_css(obj);
                    obj.input.focus(function () {
                        methods.set_focus(this);
                    }).blur(function () {
                        methods.set_blur(this);
                    });
                }
            });
        },
        set_css: function (obj) {
            var label = obj.label;
            var input = obj.input;
            input.css({ position: 'relative' });
            label.css({
                zIndex: 100,
                top: input.position().top,
                left: input.position().left + (input.css('paddingLeft').replace('px', '') * 1)
            });
            if (input.val().length > 0) {
                label.hide();
            }
        },
        set_focus: function (input) {
            var obj = txtfield[input.id];
            obj.label.hide('fast');
        },
        set_blur: function (input) {
            var obj = txtfield[input.id];
            if (obj.input.val().length <= 0) {
                obj.label.show('fast');
            }
        }
    };
    $.fn.formwatermark = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);


// Checklist
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                activestate: 'active'
            };
            var options = $.extend(defaults, options);

            return this.each(function () {
                var input = $(this);
                var label = input.siblings('label');
                input.data('options', options);
                input.focus(function () {
                    label.addClass('checklist-hover');
                }).blur(function () {
                    label.removeClass('checklist-hover');
                }).change(function () {
                    label.toggleClass('checklist-active');
                    label.removeClass('checklist-hover');
                });
            });

        }
    };
    $.fn.surveychecklist = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

// EQUAL HEIGHT
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                activestate: 'active',
                tomin : false
            };
            var options = $.extend(defaults, options);
            var obj = this;
            var maxheight = 0;

            this.each(function () {
                var elm = $(this).addClass('equal-height');
                var elmh = 0;//elm.height();
                if(elm.find('.accordian').length <= 0){
                    elm.children().not('img').each(function(){
                        elmh += $(this).outerHeight();
                    });
                }else{
                    elmh = elm.height();
                }
                if(options.tomin){
                    maxheight = (elmh < maxheight || maxheight == 0) ? elmh : maxheight; 
                }else{
                    maxheight = (elmh > maxheight) ? elmh : maxheight; 
                }          
            }).delay(300).each(function(){                
                var elm = $(this);
                elm.height(maxheight);
                if(elm.find('.accordian').length <= 0){
                    if(elm.find('img').length > 0){                    
                        elm.find('img').load(function(){
                            //debug(this.height);
                            elm.height(elm.height + this.height);
                        });
                    }
                }
                return this;
            });

        }
    };
    $.fn.equalheight = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

// ACCORDIAN
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                activestate: 'active',
                handle: '.a-handle',
                content: '.a-content',
                single: true
            };
            var options = $.extend(defaults, options);

            return this.each(function () {
                var obj = $(this);
                obj.data('options', options);
                var handle = $(options.handle, obj);
                var content = $(options.content, obj);
                var oheight = content.outerHeight();
                obj.data('oheight',oheight);
                var wrapper = $('<div class="a_wrapper" />').insertAfter(content).append(content).slideUp(0);
                var parent = wrapper.parent().parent().parent();
                handle.click(function () {
                    
                    if(obj.hasClass('active')){
                        obj.accordian('close');
                    }else{
                        obj.accordian('open');
                    }
                    return false;
                });
            });

        },
        open:function(){
            var obj = $(this);
            var parent = obj.parent().parent();
            obj.children('div[class$="wrapper"]').stop(true, true).slideDown(600, 'easeInExpo').end().addClass(obj.data('options').activestate);
            
            parent.stop(true,true).animate({ height: parent.height() + obj.data('oheight') }, 500, 'easeOutSine',function(){
                $('.equal-height').equalheight({tomin:false});
            });
            if(obj.data('options').single){
                obj.siblings().each(function(){
                    var elm = $(this);
                    if(elm.hasClass(obj.data('options').activestate)){
                        elm.accordian('close');
                    }
                });  
            }
        },
        close:function(){  
            var obj = $(this);
            var parent = obj.parent().parent();
            obj.children('div[class$="wrapper"]').stop(true, true).slideUp(500, 'easeOutSine').end().removeClass(obj.data('options').activestate);
            parent.stop(true,true).animate({ height: parent.height() - obj.data('oheight') }, 500, 'easeOutSine',function(){
                $('.equal-height').equalheight({tomin:false});
            });
        }
    };
    $.fn.accordian = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);



// ALT
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                classname: 'alt',
                selector: 'tr'
            };
            var options = $.extend(defaults, options);

            return this.each(function () {
                var obj = $(this);
                obj.data('options', options);
                obj.find(options.selector).each(function (i) {
                    if (i % 2 == 0) {
                        $(this).addClass(options.classname);
                    }
                });
            });

        }
    };
    $.fn.alt = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

// Navigation
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                activestate: 'active'
            };
            var options = $.extend(defaults, options);

            return this.each(function () {
                var toplis = $(this).children('ul').children('li').not('.main_nav-item-on');
                var uls = $(this).children('ul').find('ul');
                toplis.each(function (i, li) {
                    li = $(li);
                    li.mouseover(function () {
                        $(this).addClass('hover').append('<div class="right"/><div class="left" />');
                    }).mouseleave( function () {
                        $(this).removeClass('hover').find('.right,.left').remove();
                    });
                });
                $(this).children('ul').children('li.main_nav-item-on').each(function () {
                    $(this).append('<div class="right"/><div class="left" />');
                });

                uls.each(function (i, ul) {
                    ul = $(ul);
                    ul.parent().mouseover(
                        function () {
                            ul.css({zIndex:'999'}).stop(true,true).fadeIn(150);
                        }).mouseleave(function () {
                            ul.css({zIndex:'0'}).stop(true,true).fadeOut(150);
                        }).children('a')
                        .append('<div class="arrow"/>').end().end().append('<div class="top" /><div class="bottom" />').hide();
                });
            });

        }
    };
    $.fn.navigation = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

// Custom Background
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                activestate: 'active'
            };
            var options = $.extend(defaults, options);

            return this.each(function () {                
                
                var obj = $(this);
                obj.addClass('w-container');
                if($(window).width() > obj.width()){
                    obj.children().each(function(){
                        var child = $(this);
                        var owidth = (child.data('width'))? child.data('width'):child.width();
                        var calcwidth = owidth + child.offset().left - 5;
                        child.data('width',owidth);
                        child.width(calcwidth);
                    }).equalheight();
                    obj.removeClass('w-container');
                }else{
                    obj.addClass('w-container').children().each(function(){
                        var child = $(this);                        
                        child.width(child.data('width'));
                    }).equalheight();
                }
                
                
            });

        }
    };
    $.fn.background = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

// Operations
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                activestate: 'active'
            };
            var options = $.extend(defaults, options);

            return this.each(function () {
                var obj = $(this);
                var map = obj.siblings('.feature-image').prependTo(obj);
                var points = obj.children('a').each(function(i,item){
                    item = $(this);
                    var opos = item.position().top;
                    var oheight = item.height();
                    item.hide().css({
                        top:'150px'
                    }).delay(300 * i).show().animate({top:opos},1000,'easeOutBounce')
                    .hover(function(){
                        $(this).addClass('active').animate({top:opos - 10,height:oheight + 10},100,'easeOutExpo');
                        
                    },function(){
                        $(this).animate({top:opos, height:oheight},400,'easeOutBounce').removeClass('active');
                    });
                    
                });
            });

        }
    };
    $.fn.operations = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

jQuery.fn.externallink = function () {
    var args = arguments[0] || {}; // It's your object of arguments
    var url = '/' + args.url + '/i';

    return this.each(function () {
        var obj = jQuery(this);

        if (!obj.attr('href').match(url)) {
            if (obj.attr('target') != "_blank") {
                obj.attr('target', '_blank');
            }
            obj.click(function () {
                var leave = confirm(args.message);
                if (leave) {
                    return true;
                } else {
                    return false;
                }
            });
        }
    });
};
