﻿
function debug(x) {
    try {
        console.debug(x);
    } catch (e) {
        alert(x);
    }
}

jQuery.fn.ishiding = function () {
    var obj = $(this);
    obj.wrapInner('<div />');

    var hidden = obj.height() < obj.children('div').height();
    obj.children('div').replaceWith(obj.children('div').html());

    return hidden;
};
// 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];
            $(input).addClass('form-active');
            obj.label.hide('fast');
        },
        set_blur: function (input) {
            var obj = txtfield[input.id];            
            if (obj.input.val().length <= 0) {
                obj.label.show('fast');
                $(input).removeClass('form-active');
            }
        }
    };
    $.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);
// Newspaper Layout
// Written by: David Stevens
///////////////////////////////////////
(function ($) {
    var methods = {
        init: function (options) {
            var defaults = {
                imageIndex: 0,
                container: '.c_content',
                padding: 10,
                teaserWidth: 219,
                paddingTop: 5
            };
            var options = $.extend(defaults, options);
            return this.each(function () {
                var obj = $(this);
                
            });
        },
        set_blur: function (input) {
            var obj = txtfield[input.id];
            if (obj.input.val().length <= 0) {
                obj.label.show('fast');
            }
        }
    };
    $.fn.newspaper = 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');
                elm.css('height','');
                if(options.tomin){
                    maxheight = (elm.innerHeight() < maxheight || maxheight == 0) ? elm.height() : maxheight; 
                }else{
                    maxheight = (elm.height() > maxheight) ? elm.height() : maxheight; 
                }          
            }).delay(100).each(function(){                
                var elm = $(this);
                elm.height(maxheight);
                
                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);

// BV Work
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                activestate: 'active',
                inactivestate: 'inactive',
                hideclass : '.work-hide',
                closeclass : '.work-close',
                imagesclass : '.work-images'
            };
            var options = $.extend(defaults, options);            
            var collection = this;
            var ie7 = ($('html').hasClass('ie7'))?true:false;
            this.each(function () {
                var obj = $(this);
                obj.find('.work-content > .col-3').equalheight();
                 var cover = $('<div class="work-cover" />').insertAfter(obj.find('.work-title'));
                var height = 0;  
                var next = $('<div class="next" />');
                var prev = $('<div class="prev" />');
                var imagesobj = obj.find(options.imagesclass);
                        imagesobj.prepend(next, prev);               
                
                    
                var imagecontainer = $('<div class="image-container" />').appendTo(imagesobj);
                imagesobj.children('img').appendTo(imagecontainer);

                var portfolioCollection = imagesobj.attr('images').split(",");
                var totalcount = 0;
                if(portfolioCollection[0] != ""){
                    totalcount = imagecontainer.children().length + portfolioCollection.length;
                }else{
                    totalcount = imagecontainer.children().length;
                    portfolioCollection.pop();
                }
                
                if(totalcount <= 1){
                    next.hide();
                    prev.hide();
                }
                    
                imagecontainer.cycle({
                        fx: 'scrollHorz',
                        pause: 1,
                        next:next,
                        prev:prev,
                        timeout: 0,
                        easing: 'easeInOutExpo',
                        before: function (currslide, nextslide, opts) {
                            if (!opts.addSlide) 
                                return; 
                                
                            if (portfolioCollection.length == 0) { 
                                // final slide in our slide slideshow is about to be displayed 
                                // so there are no more to fetch 
                                opts.addSlide = null; 
                                return; 
                            } 

                            opts.addSlide('<img src="' + portfolioCollection.pop() + '" />');
                        }
                    });
                $(window).load(function(){
                    imagesobj.children('.image-container').children().each(function(){
                        var image = $(this);
                        if(image.parent().height() < this.height){
                            image.parent().css({height:this.height});
                        }
                    });
                    obj.children().children().not(cover).each(function(){
                        height += $(this).height();    
                    });
                    orig.height = height;
                    obj.children().append(cover);                    
                    
                });
                var orig = {height:height,top:obj.css('paddingTop'),bottom:obj.css('paddingBottom'),total:(obj.css('paddingBottom').replace('px','') * 1) + (obj.css('paddingTop').replace('px','') * 1)};
               
                obj.addClass(options.inactivestate);  
                
                //imagesobj.height(imagesobj.children().height());
                orig.hideobjs = obj.find(options.hideclass);
                orig.hideobjs.each(function(){
                    $(this).data('oheight',$(this).height()).hide();//.height(0);
                });
                orig.shortheight = obj.children().height();
                
                var close = obj.find(options.closeclass)
                    .click(function(){
                        if(!ie7){
                            methods.close(obj,options,orig);                        
                        }else{
                            methods.close_simple(obj,options,orig);
                        }
                        return false;
                    });

                obj.children().mouseenter(function(){
                    var elm = $(this).parent();//.removeClass(options.inactivestate).addClass(options.activestate);
                    if(elm.hasClass(options.inactivestate) && !ie7){
                        elm.stop(true,true).animate({paddingTop:0,paddingBottom:0},300,'easeOutExpo').children().stop(true,true).animate({height : '+=' + orig.total},300,'easeOutExpo');
                        cover.stop(true,true).fadeOut(300,'easeInBack');
                    }
                }).mouseleave(function(){
                    var elm = $(this).parent();//.removeClass(options.activestate).addClass(options.inactivestate);
                    if(elm.hasClass(options.inactivestate) && !ie7){
                        elm.stop(true,true).animate({paddingTop:orig.top,paddingBottom:orig.bottom},100,'easeOutBack').children().stop(true,true).animate({height:orig.shortheight},100,'easeOutBack');
                        cover.stop(true,true).fadeIn(200,'easeInExpo');
                    }
                }).click(function(){
                    
                    var elm = $(this).parent();
                    if(elm.hasClass(options.inactivestate) && !ie7){
                        //elm.unbind('mouseenter').unbind('mouseleave');
                        methods.close_all(collection,options,orig);
                        methods.open(elm,options,orig);
                    }else{
                        //elm.unbind('mouseenter').unbind('mouseleave');
                        methods.close_all_simple(collection,options,orig);
                        methods.open_simple(elm,options,orig);
                    }
                });
                
            });

        },
        close:function(obj,options,orig){
            obj.stop(true,true).slideUp(300, 'easeOutExpo',function(){
                $(this).removeClass(options.activestate).addClass(options.inactivestate)
                    .css({paddingTop:orig.top,paddingBottom:orig.bottom})
                    .children().find(options.hideclass).hide().end()
                        .height(orig.shortheight).end()
                    .slideDown(300,'easeInExpo')
                    .find(".work-cover").stop(true,true).slideDown(200,'easeInExpo');              
            });
        },
        close_simple:function(obj,options,orig){
            obj.removeClass(options.activestate).addClass(options.inactivestate)
                    .css({paddingTop:orig.top,paddingBottom:orig.bottom})
                    .children().find(options.hideclass).hide().end()
                        .height(orig.shortheight).end()
                    .find(".work-cover").stop(true,true).slideDown(0); 
        },
        /*close_all:function(collection, options, orig){            
            var objs = collection.filter('.'+options.activestate);            
            objs.each(function(){
                var elm = $(this);
                elm.css({paddingTop:orig.top,paddingBottom:orig.bottom}).stop(true,true).slideUp(400, 'easeOutExpo',function(){
                    $(this).removeClass(options.activestate).addClass(options.inactivestate)
                        .css({paddingTop:orig.top,paddingBottom:orig.bottom})
                        .children().find(options.hideclass).hide().end()
                            .height(orig.shortheight).end()
                        .slideDown(300,'easeInExpo').mouseenter(function(){
                            var elm = $(this);//.removeClass(options.inactivestate).addClass(options.activestate);
                            if(elm.hasClass(options.inactivestate)){
                                elm.stop(true,true).animate({paddingTop:0,paddingBottom:0},300,'easeOutExpo').children().stop(true,true).animate({height : '+=' + orig.total},300,'easeOutExpo');
                            }
                        }).mouseleave(function(){
                            var elm = $(this);//.removeClass(options.activestate).addClass(options.inactivestate);
                            if(elm.hasClass(options.inactivestate)){
                                elm.stop(true,true).animate({paddingTop:orig.top,paddingBottom:orig.bottom},100,'easeOutBack').children().stop(true,true).animate({height:'-=' + orig.total},100,'easeOutBack');
                            }
                        });
                                
                });
            });            
        },*/
        close_all:function(collection, options, orig){            
            var objs = collection.filter('.'+options.activestate);            
            objs.each(function(){
                var elm = $(this);
                elm.css({paddingTop:orig.top,paddingBottom:orig.bottom}).removeClass(options.activestate).addClass(options.inactivestate)
                        .css({paddingTop:orig.top,paddingBottom:orig.bottom})
                        .children().find(options.hideclass).hide().end()
                            .height(orig.shortheight).end()
                        .find(".work-cover").stop(true,true).slideDown(0);                      
                                
                
            });            
        },
        close_all_simple:function(collection,options,orig){
            var objs = collection.filter('.'+options.activestate);            
            
            objs.each(function(){
                var elm = $(this);
                elm.removeClass(options.activestate).addClass(options.inactivestate)
                        .css({paddingTop:orig.top,paddingBottom:orig.bottom})
                        .children().find(options.hideclass).hide().end()
                            .height(orig.shortheight).end()
                        .find(".work-cover").stop(true,true).slideDown(0); 
                
            });
        },
        open_simple:function(obj,options,orig){
            obj.removeClass(options.inactivestate).addClass(options.activestate)
                .children().height(orig.height).find(options.hideclass).show();
            orig.hideobjs.each(function(){
                    $(this).show().height($(this).data('oheight'));
                });
        },
        open:function(obj, options, orig){
            obj.removeClass(options.inactivestate).slideUp(200,'easeOutExpo',function(){
                orig.hideobjs.each(function(){
                    $(this).show().height($(this).data('oheight'));
                });
                $(this).addClass(options.activestate).children().height(orig.height).end().slideDown(1000,'easeInOutExpo');
            });          
        }
    };
    $.fn.bvwork = 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);

// BV Capabilities
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                add: 'Add to Quote Request',
                added: 'You have {0} item(s) in {1}',
                outsideadded: '#request-info',
                capability : '.capability'
            };
            var options = $.extend(defaults, options);
            return this.each(function(){                
                var main = $(this);
                var wrapper = $('<div id="capabilities-wrapper" />').insertBefore(main).append(main);
                var navcontainer = $('<div id="capabilities-nav" />').prependTo(wrapper);                
                var prev = $('<img class="prev" src="../images/btn-prev-2.png" />').appendTo(navcontainer);
                var nav = $('<ul />').appendTo(navcontainer);
                nav.wrap("<div id='cap-nav' />");
                var next = $('<img class="next" src="../images/btn-next-2.png" />').appendTo(navcontainer);
                var count = 0;
                

                main.cycle({
                    fx: 'scrollHorz',
                    pause: 1,
                    next:next,
                    prev:prev,
                    timeout: 10000,
                    easing: 'easeInOutExpo',
                    before: function (currslide, nextslide, options) {
                        nav.children('li[class*="current-item"]').removeClass('current-item').end().children()
                            .eq($(nextslide).siblings().andSelf().index($(nextslide))).addClass('current-item');
                        
                    }
                }).children().each(function(i){
                    var elm = $(this);
                    var link = $("<a href='#'>" + (count + 1) + " - " + (count + elm.children().length) + "</a>")
                        .click(function(){                            
                            main.cycle(i);
                            return false;    
                        });
                        count += elm.children().length
                        if(i == 0){
                            nav.append($("<li class='current-item' />").append(link));
                        }else{
                            nav.append($("<li />").append(link));
                        }
                    
                }).end().find(options.capability).each(function (i) {
                    var obj = $(this);
                    var name = obj.attr('capname');
                    var funcontainer = $('<div class="function" />').appendTo(obj).css({
                        bottom: 0
                    }).hide();
                    var addreq = $('<div class="add-request" />').append(options.add)
                        .click(function () {
                            methods.set(name);
                            obj.parent().siblings().andSelf().children('.capability').each(function () {
                                if (methods.issaved($(this).attr('capname'))) {
                                    $(this).find('.function').capabilities('addedbutton', options);
                                }
                            });
                            $(options.outsideadded).capabilities('addedbutton', options);

                        });

                    if (methods.issaved(name)) {
                        funcontainer.capabilities('addedbutton', options);

                        $(options.outsideadded).capabilities('addedbutton', options);

                    } else {
                        funcontainer.append(addreq);
                    }
                    obj.mouseenter(function () {
                        //obj.stop(true,true).animate({height: '+=' + funcontainer.height()});
                        funcontainer.stop(true, true).slideDown(200, 'easeOutExpo');
                    }).mouseleave(function () {
                        //obj.stop(true,true).animate({height: '-=' + funcontainer.height()});
                        funcontainer.stop(true, true).slideUp(300, 'easeInBack');
                    });
                
                });
            });
            
            
        },
        addedbutton: function (options) {
            var container = $(this);
            var count = methods.getcount();
            if (count) {
                var addedreq = $('<div class="added-request" />').append(options.added.replace("{0}", "<em>" + count + "</em>").replace("{1}", "<a href='" + url_frag + "/contact/#form'>Quote Request</a>"));
                container.animate({ left: container.outerWidth() * -1 }, 300, 'easeOutExpo', function () {
                    $(this).children().remove();
                    container.append(addedreq).animate({ left: 0 }, 200, 'easeInOutExpo');
                });
            }else{
                $(this).children().remove();
            }
            //container.append(addedreq);
        },
        set: function (name) {
            var caprequest;
            if (Modernizr.localstorage) {
                caprequest = localStorage["caprequest"];
                if (caprequest) {
                    if (!methods.issaved(name)) {
                        caprequest += "," + name;
                    }
                } else {
                    caprequest = name;
                }
                localStorage["caprequest"] = caprequest;
            } else {
                caprequest = $.cookie("caprequest");
                if (caprequest) {
                    if (!methods.issaved(name)) {
                        caprequest += "," + name;
                    }
                } else {
                    caprequest = name;
                }
                $.cookie("caprequest", caprequest, { path: '/' });
            }

            return caprequest;
        },
        get: function () {
            return (Modernizr.localstorage) ? localStorage["caprequest"] : $.cookie("caprequest");
        },
        getcount: function () {
            var caprequest = (Modernizr.localstorage) ? localStorage["caprequest"] : $.cookie("caprequest");
            if (caprequest) {
                return caprequest.split(",").length;
            } else {
                return false;
            }
        },
        issaved: function (name) {
            var caprequest = (Modernizr.localstorage) ? localStorage["caprequest"] : $.cookie("caprequest");
            var patt = new RegExp(name, 'i');
            return (caprequest && caprequest.match(name)) ? true : false;
            //return false;
        },
        remove:function(name){
            var request = methods.get().split(",");
            if(request && request.length > 0){
                if (Modernizr.localstorage) {
                    localStorage.removeItem('caprequest');
                } else {                    
                    $.cookie('caprequest', null);
                }
                $(request).each(function(i, x){
                    if(x != name){
                        methods.set(x);
                    }
                });
            }
            return methods.get();
        },
        form:function(){
            var form  = $(this);
            var request = methods.get();
            if(request && request.length > 0){
                $('input[id$="hfCartItems"]').val(request);
                request = request.split(",");
                request.sort();
                var wrapper = $('<li />').insertBefore(form.children('div[id$="pnlForm"]').children().children(":last-child"));
                $(request).each(function(i, name){
                    var container = $("<div class='request-item' />").append(name).appendTo(wrapper)
                        .click(function(){
                            
                            methods.remove(name);
                            $(this).fadeOut(500,function(){$(this).remove()});
                            $('#request-info').capabilities('addedbutton', {added: 'You have {0} item(s) in {1}'});
                            
                        });
                });
            }
            
        }
    };
    $.fn.capabilities = 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);

// BV Filter
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                startfilter: null
            };
            var options = $.extend(defaults, options);
            var collection = this;

            return this.each(function () {
                var obj = $(this);
                var patt = /#(.+)$/i;
                var name = obj.attr('href').match(patt);
                name = (name.length > 0) ? $.trim(name[1]) : name;
                obj.click(function () {
                    var filterobj = $('div[filter]');
                    if (filterobj.length > 0) {
                        patt = /#(.+)$/i;
                        name = obj.attr('href').match(patt);
                        name = (name.length > 0) ? $.trim(name[1]) : name;

                        collection.parent().removeClass('current-item');
                        collection.filter(':contains("' + name + '")').parent().addClass('current-item');

                        filterobj.fadeOut(300, 'easeOutExpo', function () {
                            if (name.toLowerCase() != "all") {
                                $(this).filter('[filter*="' + name + '"]').slideDown(300);
                            } else {
                                $(this).slideDown(300);
                            }
                        });
                        return false;
                    }
                });
                if (window.location.hash != "") {
                    var patt = /#(.+)$/i;
                    var location = window.location.hash.match(patt)[1];

                    if (location == name) {

                        obj.trigger('click');
                    }
                }

            });

        },
        set: function (name) {

        }
    };
    $.fn.bvfilter = 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);

// BV Map
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                iframe:'<iframe width="678" height="394" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="{0}"></iframe>',
                mapcontainer:'.map-container'

            };
            var options = $.extend(defaults, options);              
            return this.each(function () {
                var obj = $(this);   

                var links = obj.find('.map-nav a')
                    .click(function(){
                        var link = $(this);
                        if($(options.mapcontainer,obj).children().length > 0){
                            $(options.mapcontainer,obj).children().fadeOut(function(){       
                                $(options.mapcontainer,obj).html(options.iframe.replace("{0}",link.attr('href')));
                            });//.html(options.iframe.replace("{0}",$(this).attr('href')));
                        }else{
                            $(options.mapcontainer,obj).html(options.iframe.replace("{0}",link.attr('href')));
                        }
                        links.removeClass("current-item");
                        $(this).addClass("current-item");
                        return false;
                    });
                
                links.eq(0).trigger('click');
            });

        }
    };
    $.fn.bvmap = 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'
            };
            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();
                var wrapper = $('<div class="a_wrapper" />').insertAfter(content).append(content).slideUp(0);
                var parent = wrapper.parent().parent().parent();
                handle.toggle(function () {
                    wrapper.stop(true,true).slideDown(600, 'easeInExpo').parent().addClass('active');
                    parent.stop(true,true).animate({ height: parent.height() + oheight }, 600, 'easeInExpo');
                }, function () {
                    wrapper.stop(true,true).slideUp(500, 'easeOutSine').parent().removeClass('active');
                    parent.stop(true,true).animate({ height: parent.height() - oheight }, 500, 'easeOutSine');
                });
            });

        }
    };
    $.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);
