$(document).ready(function() {
    /* Confirmation suppression */
    jQuery('.JSdelConf').live('click', function(){
        return confirm('Etes vous sûr de vouloir supprimer ?');
    });

    /* Etendre la zone cliquable */
    jQuery('.JScliquable').live('click', function(){
        window.location = $(this).find("a").attr("href");
        return false;
    }).css("cursor", "pointer");

    /* Ouverture dans un nouvel onglet */
    jQuery('a.JSblank').live('click', function(){
        this.target = "_blank";
    });    
    
    var $liste = $('.contenu dl');
    $liste.children('dd').hide();
    $liste.children('dt').click(function(){
        $liste.children('dd').slideUp();
        $(this).next('dd').slideToggle();
    });
    
    var $devisDpt = $('#devis_departement');
    $devisDpt.change(function(event){
        $('#devis_commune').removeAttr("disabled");
        $dptCode = $(this).val();
        if($dptCode > 0) {
            $.get("/devis/ajax-commune.html",{
                code: $dptCode
            }, function(j){
                $('#devis_commune').html(j);
                $('#devis_commune option:first').attr('selected', 'selected');
            });
            
        } else {
            $('#devis_commune').attr("disabled", true);
        }
    });
    
    $('a.infobulle').qtip({
        position: {
            my: 'bottom center',  // Position my top left...
            at: 'top center' // at the bottom right of...
        },
        style: {
            classes: 'ui-tooltip-green'
        }
    });
    
    if($('body').hasClass('body-call')) {
        var $devisTrans = $('#devis_transaction');
        $devisTrans.change(function(event){
            $('#aide-gaz').removeAttr("disabled");
            $('#aide-elec').removeAttr("disabled");

            if($(this).val() == 3) {
                $('#aide-gaz').attr("disabled", true);
                $('#aide-elec').attr("disabled", true);
            }
        });
        
        var $devisGaz = $('#verif-gaz');
        $devisGaz.change(function(event){
            $('#aide-gaz').removeAttr("disabled");
            if($(this).val() == 2 || $devisTrans.val() == 3) {
                $('#aide-gaz').attr("disabled", true);
            }
        });
    }
    
    if($('body').hasClass('annuaire')) {
        $("#annuaire-listeDiag").jPaginate({items: 15, previous: "Précédent", next: "Suivant", cookies: false}); 
    }
    
});

/*
 * qTip2 - Pretty powerful tooltips
 * http://craigsworks.com/projects/qtip2/
 *
 * Version: nightly
 * Copyright 2009-2010 Craig Michael Thompson - http://craigsworks.com
 *
 * Dual licensed under MIT or GPLv2 licenses
 *   http://en.wikipedia.org/wiki/MIT_License
 *   http://en.wikipedia.org/wiki/GNU_General_Public_License
 *
 * Date: Fri Jun  3 02:52:35 PDT 2011
 *//*jslint browser: true, onevar: true, undef: true, nomen: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true *//*global window: false, jQuery: false */(function(a,b,c){
    function y(b){
        var c=this,f=b.elements.tooltip,g=b.options.content.ajax,h=".qtip-ajax",i=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,j=d;
        b.checks.ajax={
            "^content.ajax":function(a,b,d){
                b==="ajax"&&(g=d),b==="once"?c.init():g&&g.url?c.load():f.unbind(h)
            }
        },a.extend(c,{
            init:function(){
                g&&g.url&&f.unbind(h)[g.once?"one":"bind"]("tooltipshow"+h,c.load);
                return c
            },
            load:function(d,h){
                function p(a,c,d){
                    b.set("content.text",c+": "+d),n()
                }
                function o(c){
                    l&&(c=a("<div/>").append(c.replace(i,"")).find(l)),b.set("content.text",c),n()
                }
                function n(){
                    m&&(f.css("visibility",""),h=e)
                }
                if(d&&d.isDefaultPrevented())return c;
                var j=g.url.indexOf(" "),k=g.url,l,m=g.once&&!g.loading&&h;
                m&&f.css("visibility","hidden"),j>-1&&(l=k.substr(j),k=k.substr(0,j)),a.ajax(a.extend({
                    success:o,
                    error:p,
                    context:b
                },g,{
                    url:k
                }));
                return c
            }
        }),c.init()
    }
    function x(b,c){
        var i,j,k,l,m=a(this),n=a(document.body),o=this===document?n:m,p=m.metadata?m.metadata(c.metadata):f,q=c.metadata.type==="html5"&&p?p[c.metadata.name]:f,r=m.data(c.metadata.name||"qtipopts");
        try{
            r=typeof r==="string"?(new Function("return "+r))():r
        }catch(s){
            a.error("Unable to parse HTML5 attribute data: "+r)
        }
        l=a.extend(d,{},g.defaults,c,typeof r==="object"?v(r):f,v(q||p)),j=l.position,l.id=b;
        if("boolean"===typeof l.content.text){
            k=m.attr(l.content.attr);
            if(l.content.attr!==e&&k)l.content.text=k;else return e
        }
        j.container===e&&(j.container=n),j.target===e&&(j.target=o),l.show.target===e&&(l.show.target=o),l.show.solo===d&&(l.show.solo=n),l.hide.target===e&&(l.hide.target=o),l.position.viewport===d&&(l.position.viewport=j.container),j.at=new h.Corner(j.at),j.my=new h.Corner(j.my);
        if(a.data(this,"qtip"))if(l.overwrite)m.qtip("destroy");
        else if(l.overwrite===e)return e;
        a.attr(this,"title")&&(a.attr(this,t,a.attr(this,"title")),this.removeAttribute("title")),i=new w(m,l,b,!!k),a.data(this,"qtip",i),m.bind("remove.qtip",function(){
            i.destroy()
        });
        return i
    }
    function w(c,r,s,w){
        function P(){
            var c=[r.show.target[0],r.hide.target[0],x.rendered&&E.tooltip[0],r.position.container[0],r.position.viewport[0],b,document];
            x.rendered?a([]).pushStack(a.grep(c,function(a){
                return typeof a==="object"
            })).unbind(D):r.show.target.unbind(D+"-create")
        }
        function O(){
            function t(a){
                C.is(":visible")&&x.reposition(a)
            }
            function p(a){
                if(C.hasClass(m))return e;
                clearTimeout(x.timers.inactive),x.timers.inactive=setTimeout(function(){
                    x.hide(a)
                },r.hide.inactive)
            }
            function o(b){
                if(C.hasClass(m))return e;
                var c=a(b.relatedTarget||b.target),d=c.closest(n)[0]===C[0],g=c[0]===h.show[0];
                clearTimeout(x.timers.show),clearTimeout(x.timers.hide);
                f.target==="mouse"&&d||r.hide.fixed&&(/mouse(out|leave|move)/.test(b.type)&&(d||g))?b.preventDefault():r.hide.delay>0?x.timers.hide=setTimeout(function(){
                    x.hide(b)
                },r.hide.delay):x.hide(b)
            }
            function l(a){
                if(C.hasClass(m))return e;
                h.show.trigger("qtip-"+s+"-inactive"),clearTimeout(x.timers.show),clearTimeout(x.timers.hide);
                var b=function(){
                    x.toggle(d,a)
                };
                
                r.show.delay>0?x.timers.show=setTimeout(b,r.show.delay):b()
            }
            var f=r.position,h={
                show:r.show.target,
                hide:r.hide.target,
                viewport:a(f.viewport),
                document:a(document),
                window:a(b)
            },j={
                show:a.trim(""+r.show.event).split(" "),
                hide:a.trim(""+r.hide.event).split(" ")
            },k=a.browser.msie&&parseInt(a.browser.version,10)===6;
            C.bind("mouseenter"+D+" mouseleave"+D,function(a){
                var b=a.type==="mouseenter";
                b&&x.focus(a),C.toggleClass(q,b)
            }),r.hide.fixed&&(h.hide=h.hide.add(C),C.bind("mouseover"+D,function(){
                C.hasClass(m)||clearTimeout(x.timers.hide)
            })),/mouse(out|leave)/i.test(r.hide.event)?r.hide.leave&&h.window.bind("mouse"+(r.hide.leave.indexOf("frame")>-1?"out":"leave")+D,function(a){
                    /select|option/.test(a.target)&&!a.relatedTarget&&x.hide(a)
            }):/mouse(over|enter)/i.test(r.show.event)&&h.hide.bind("mouseleave"+D,function(a){
                clearTimeout(x.timers.show)
            }),(""+r.hide.event).indexOf("unfocus")>-1&&h.document.bind("mousedown"+D,function(b){
                var d=a(b.target),e=!C.hasClass(m)&&C.is(":visible");
                d.parents(n).length===0&&d.add(c).length>1&&x.hide(b)
            }),"number"===typeof r.hide.inactive&&(h.show.bind("qtip-"+s+"-inactive",p),a.each(g.inactiveEvents,function(a,b){
                h.hide.add(E.tooltip).bind(b+D+"-inactive",p)
            })),a.each(j.hide,function(b,c){
                var d=a.inArray(c,j.show),e=a(h.hide);
                d>-1&&e.add(h.show).length===e.length||c==="unfocus"?(h.show.bind(c+D,function(a){
                    C.is(":visible")?o(a):l(a)
                }),delete j.show[d]):h.hide.bind(c+D,o)
            }),a.each(j.show,function(a,b){
                h.show.bind(b+D,l)
            }),"number"===typeof r.hide.distance&&h.show.bind("mousemove"+D,function(a){
                var b=F.origin||{},c=r.hide.distance,d=Math.abs;
                (d(a.pageX-b.pageX)>=c||d(a.pageY-b.pageY)>=c)&&x.hide(a)
            }),f.target==="mouse"&&(h.show.bind("mousemove"+D,function(a){
                i={
                    pageX:a.pageX,
                    pageY:a.pageY,
                    type:"mousemove"
                }
            }),f.adjust.mouse&&(r.hide.event&&C.bind("mouseleave"+D,function(a){
                (a.relatedTarget||a.target)!==h.show[0]&&x.hide(a)
            }),h.document.bind("mousemove"+D,function(a){
                    !C.hasClass(m)&&C.is(":visible")&&x.reposition(a||i)
            }))),(f.adjust.resize||h.viewport.length)&&(a.event.special.resize?h.viewport:h.window).bind("resize"+D,t),(h.viewport.length||k&&C.css("position")==="fixed")&&h.viewport.bind("scroll"+D,t)
        }
        function N(b,d){
            function g(a){
                function c(c){
                    (b=b.not(this)).length===0&&(x.redraw(),d!==e&&x.reposition(F.event),a())
                }
                var b;
                if((b=f.find("img:not([height]):not([width])")).length===0)return c.call(b);
                b.each(function(a,b){
                    (function d(){
                        var e=x.timers.img;
                        if(b.height&&b.width){
                            clearTimeout(e[a]);
                            return c.call(b)
                        }
                        e[a]=setTimeout(d,20)
                    })()
                })
            }
            var f=E.content;
            if(!x.rendered||!b)return e;
            a.isFunction(b)&&(b=b.call(c,x)||""),b.jquery&&b.length>0?f.empty().append(b.css({
                display:"block"
            })):f.html(b),x.rendered<0?C.queue("fx",g):(B=0,g(a.noop));
            return x
        }
        function M(b,d){
            var f=E.title;
            if(!x.rendered||!b)return e;
            a.isFunction(b)&&(b=b.call(c,x)||""),b.jquery&&b.length>0?f.empty().append(b.css({
                display:"block"
            })):f.html(b),x.redraw(),d!==e&&x.rendered&&C.is(":visible")&&x.reposition(F.event)
        }
        function L(a){
            var b=E.button,c=E.title;
            if(!x.rendered)return e;
            a?(c||K(),J()):b.remove()
        }
        function K(){
            var b=z+"-title";
            E.titlebar&&I(),E.titlebar=a("<div />",{
                "class":k+"-titlebar "+(r.style.widget?"ui-widget-header":"")
            }).append(E.title=a("<div />",{
                id:b,
                "class":k+"-title",
                "aria-atomic":d
            })).insertBefore(E.content),r.content.title.button?J():x.rendered&&x.redraw()
        }
        function J(){
            var b=r.content.title.button,c=typeof b==="string",d=c?b:"Close tooltip";
            E.button&&E.button.remove(),b.jquery?E.button=b:E.button=a("<a />",{
                "class":"ui-state-default "+(r.style.widget?"":k+"-icon"),
                title:d,
                "aria-label":d
            }).prepend(a("<span />",{
                "class":"ui-icon ui-icon-close",
                html:"&times;"
            })),E.button.appendTo(E.titlebar).attr("role","button").hover(function(b){
                a(this).toggleClass("ui-state-hover",b.type==="mouseenter")
            }).click(function(a){
                C.hasClass(m)||x.hide(a);
                return e
            }).bind("mousedown keydown mouseup keyup mouseout",function(b){
                a(this).toggleClass("ui-state-active ui-state-focus",b.type.substr(-4)==="down")
            }),x.redraw()
        }
        function I(){
            E.title&&(E.titlebar.remove(),E.titlebar=E.title=E.button=f,x.reposition())
        }
        function H(){
            var a=r.style.widget;
            C.toggleClass(l,a).toggleClass(o,!a),E.content.toggleClass(l+"-content",a),E.titlebar&&E.titlebar.toggleClass(l+"-header",a),E.button&&E.button.toggleClass(k+"-icon",!a)
        }
        function G(a){
            var b=0,c,d=r,e=a.split(".");
            while(d=d[e[b++]])b<e.length&&(c=d);
            return[c||r,e.pop()]
        }
        var x=this,y=document.body,z=k+"-"+s,A=0,B=0,C=a(),D=".qtip-"+s,E,F;
        x.id=s,x.rendered=e,x.elements=E={
            target:c
        },x.timers={
            img:[]
        },x.options=r,x.checks={},x.plugins={},x.cache=F={
            event:{},
            target:a(),
            disabled:e,
            attr:w
        },x.checks.builtin={
            "^id$":function(b,c,f){
                var h=f===d?g.nextid:f,i=k+"-"+h;
                h!==e&&h.length>0&&!a("#"+i).length&&(C[0].id=i,E.content[0].id=i+"-content",E.title[0].id=i+"-title")
            },
            "^content.text$":function(a,b,c){
                N(c)
            },
            "^content.title.text$":function(a,b,c){
                if(!c)return I();
                    !E.title&&c&&K(),M(c)
            },
            "^content.title.button$":function(a,b,c){
                L(c)
            },
            "^position.(my|at)$":function(a,b,c){
                "string"===typeof c&&(a[b]=new h.Corner(c))
            },
            "^position.container$":function(a,b,c){
                x.rendered&&C.appendTo(c)
            },
            "^show.ready$":function(){
                x.rendered?x.toggle(d):x.render(1)
            },
            "^style.classes$":function(b,c,d){
                a.attr(C[0],"class",k+" qtip ui-helper-reset "+d)
            },
            "^style.widget|content.title":H,
            "^events.(render|show|move|hide|focus|blur)$":function(b,c,d){
                C[(a.isFunction(d)?"":"un")+"bind"]("tooltip"+c,d)
            },
            "^(show|hide|position).(event|target|fixed|inactive|leave|distance|viewport|adjust)":function(){
                var a=r.position;
                C.attr("tracking",a.target==="mouse"&&a.adjust.mouse),P(),O()
            }
        },a.extend(x,{
            render:function(b){
                if(x.rendered)return x;
                var f=r.content.title.text,g=r.position,i=a.Event("tooltiprender");
                a.attr(c[0],"aria-describedby",z),C=E.tooltip=a("<div/>",{
                    id:z,
                    "class":k+" qtip ui-helper-reset "+o+" "+r.style.classes,
                    width:r.style.width||"",
                    tracking:g.target==="mouse"&&g.adjust.mouse,
                    role:"alert",
                    "aria-live":"polite",
                    "aria-atomic":e,
                    "aria-describedby":z+"-content",
                    "aria-hidden":d
                }).toggleClass(m,F.disabled).data("qtip",x).appendTo(r.position.container).append(E.content=a("<div />",{
                    "class":k+"-content",
                    id:z+"-content",
                    "aria-atomic":d
                })),x.rendered=-1,B=1,f&&(K(),M(f)),N(r.content.text,e),x.rendered=d,H(),a.each(r.events,function(b,c){
                    a.isFunction(c)&&C.bind(b==="toggle"?"tooltipshow tooltiphide":"tooltip"+b,c)
                }),a.each(h,function(){
                    this.initialize==="render"&&this(x)
                }),O(),C.queue("fx",function(a){
                    i.originalEvent=F.event,C.trigger(i,[x]),B=0,x.redraw(),(r.show.ready||b)&&x.toggle(d,F.event),a()
                });
                return x
            },
            get:function(a){
                var b,c;
                switch(a.toLowerCase()){
                    case"dimensions":
                        b={
                            height:C.outerHeight(),
                            width:C.outerWidth()
                        };
                
                        break;
                    case"offset":
                        b=h.offset(C,r.position.container);
                        break;
                    default:
                        c=G(a.toLowerCase()),b=c[0][c[1]],b=b.precedance?b.string():b
                }
                return b
            },
            set:function(b,c){
                function m(a,b){
                    var c,d,e;
                    for(c in k)for(d in k[c])if(e=(new RegExp(d,"i")).exec(a))b.push(e),k[c][d].apply(x,b)
                }
                var g=/^position\.(my|at|adjust|target|container)|style|content|show\.ready/i,h=/^content\.(title|attr)|style/i,i=e,j=e,k=x.checks,l;
                "string"===typeof b?(l=b,b={},b[l]=c):b=a.extend(d,{},b),a.each(b,function(c,d){
                    var e=G(c.toLowerCase()),f;
                    f=e[0][e[1]],e[0][e[1]]="object"===typeof d&&d.nodeType?a(d):d,b[c]=[e[0],e[1],d,f],i=g.test(c)||i,j=h.test(c)||j
                }),v(r),A=B=1,a.each(b,m),A=B=0,C.is(":visible")&&x.rendered&&(i&&x.reposition(r.position.target==="mouse"?f:F.event),j&&x.redraw());
                return x
            },
            toggle:function(b,c){
                function q(){
                    b?(a.browser.msie&&C[0].style.removeAttribute("filter"),C.css("overflow","")):C.css({
                        display:"",
                        visibility:"",
                        width:r.style.width||"",
                        opacity:"",
                        left:"",
                        top:""
                    })
                }
                if(!x.rendered)if(b)x.render(1);else return x;
                var g=b?"show":"hide",h=r[g],j=C.is(":visible"),k=!c||F.target[0]===c.target,l=r.position,m=r.content,o,p;
                (typeof b).search("boolean|number")&&(b=!j);
                if(!C.is(":animated")&&j===b&&k)return x;
                if(c){
                    if(/over|enter/.test(c.type)&&/out|leave/.test(F.event.type)&&c.target===r.show.target[0]&&C.has(c.relatedTarget).length)return x;
                    F.event=a.extend({},c)
                }
                p=a.Event("tooltip"+g),p.originalEvent=c?F.event:f,C.trigger(p,[x,90]);
                if(p.isDefaultPrevented())return x;
                a.attr(C[0],"aria-hidden",!b),b?(F.origin=a.extend({},i),x.focus(c),a.isFunction(m.text)&&N(m.text,e),a.isFunction(m.title.text)&&M(m.title.text,e),!u&&l.target==="mouse"&&l.adjust.mouse&&(a(document).bind("mousemove.qtip",function(a){
                    i={
                        pageX:a.pageX,
                        pageY:a.pageY,
                        type:"mousemove"
                    }
                }),u=d),x.reposition(c),h.solo&&a(n,h.solo).not(C).qtip("hide",p)):(clearTimeout(x.timers.show),delete F.origin,u&&!a(n+'[tracking="true"]:visible',h.solo).not(C).length&&(a(document).unbind("mousemove.qtip"),u=e),x.blur(c)),k&&C.stop(0,1),h.effect===e?(C[g](),q.call(C)):a.isFunction(h.effect)?(h.effect.call(C,x),C.queue("fx",function(a){
                    q(),a()
                })):C.fadeTo(90,b?1:0,q),b&&h.target.trigger("qtip-"+s+"-inactive");
                return x
            },
            show:function(a){
                return x.toggle(d,a)
            },
            hide:function(a){
                return x.toggle(e,a)
            },
            focus:function(b){
                if(!x.rendered)return x;
                var c=a(n),d=parseInt(C[0].style.zIndex,10),e=g.zindex+c.length,f=a.extend({},b),h,i;
                C.hasClass(p)||(i=a.Event("tooltipfocus"),i.originalEvent=f,C.trigger(i,[x,e]),i.isDefaultPrevented()||(d!==e&&(c.each(function(){
                    this.style.zIndex>d&&(this.style.zIndex=this.style.zIndex-1)
                }),c.filter("."+p).qtip("blur",f)),C.addClass(p)[0].style.zIndex=e));
                return x
            },
            blur:function(b){
                var c=a.extend({},b),d;
                C.removeClass(p),d=a.Event("tooltipblur"),d.originalEvent=c,C.trigger(d,[x]);
                return x
            },
            reposition:function(c,d){
                if(!x.rendered||A)return x;
                A=1;
                var f=r.position.target,g=r.position,j=g.my,l=g.at,m=g.adjust,n=m.method.split(" "),o=C.outerWidth(),p=C.outerHeight(),q=0,s=0,t=a.Event("tooltipmove"),u=C.css("position")==="fixed",v=g.viewport,w={
                    left:0,
                    top:0
                },z=x.plugins.tip,B={
                    horizontal:n[0],
                    vertical:n[1]||n[0],
                    left:function(a){
                        var b=B.horizontal==="shift",c=v.offset.left+v.scrollLeft,d=j.x==="left"?o:j.x==="right"?-o:-o/2,e=l.x==="left"?q:l.x==="right"?-q:-q/2,f=z?z.size.width||0:0,g=z&&z.corner.precedance==="x"&&!b?f:0,h=c-a+g,i=a+o-v.width-c+g,k=d-(j.precedance==="x"||j.x===j.y?e:0),n=j.x==="center";
                        b?(g=z&&z.corner.precedance==="y"?f:0,k=(j.x==="left"?1:-1)*d-g,w.left+=h>0?h:i>0?-i:0,w.left=Math.max(v.offset.left+(g&&z.corner.x==="center"?z.offset:0),a-k,Math.min(Math.max(v.offset.left+v.width,a+k),w.left))):(h>0&&(j.x!=="left"||i>0)?w.left-=k+(n?0:2*m.x):i>0&&(j.x!=="right"||h>0)&&(w.left-=n?-k:k+2*m.x),w.left!==a&&n&&(w.left-=m.x),w.left<c&&-w.left>i&&(w.left=a));
                        return w.left-a
                    },
                    top:function(a){
                        var b=B.vertical==="shift",c=v.offset.top+v.scrollTop,d=j.y==="top"?p:j.y==="bottom"?-p:-p/2,e=l.y==="top"?s:l.y==="bottom"?-s:-s/2,f=z?z.size.height||0:0,g=z&&z.corner.precedance==="y"&&!b?f:0,h=c-a+g,i=a+p-v.height-c+g,k=d-(j.precedance==="y"||j.x===j.y?e:0),n=j.y==="center";
                        b?(g=z&&z.corner.precedance==="x"?f:0,k=(j.y==="top"?1:-1)*d-g,w.top+=h>0?h:i>0?-i:0,w.top=Math.max(v.offset.top+(g&&z.corner.x==="center"?z.offset:0),a-k,Math.min(Math.max(v.offset.top+v.height,a+k),w.top))):(h>0&&(j.y!=="top"||i>0)?w.top-=k+(n?0:2*m.y):i>0&&(j.y!=="bottom"||h>0)&&(w.top-=n?-k:k+2*m.y),w.top!==a&&n&&(w.top-=m.y),w.top<0&&-w.top>i&&(w.top=a));
                        return w.top-a
                    }
                };
    
                if(f==="mouse"&&(c&&c.pageX||F.event.pageX))l={
                    x:"left",
                    y:"top"
                },c=c&&(c.type==="resize"||c.type==="scroll")?F.event:c&&c.pageX&&c.type==="mousemove"?c:i&&(m.mouse||!c||!c.pageX)?{
                    pageX:i.pageX,
                    pageY:i.pageY
                }:!m.mouse&&F.origin?F.origin:c,w={
                    top:c.pageY,
                    left:c.pageX
                };
                else{
                    f==="event"?c&&c.target&&c.type!=="scroll"&&c.type!=="resize"?f=F.target=a(c.target):f=F.target:F.target=a(f),f=a(f).eq(0);
                    if(f.length===0)return x;
                    f[0]===document||f[0]===b?(q=h.iOS?b.innerWidth:f.width(),s=h.iOS?b.innerHeight:f.height(),f[0]===b&&(w={
                        top:!u||h.iOS?(v||f).scrollTop():0,
                        left:!u||h.iOS?(v||f).scrollLeft():0
                    })):f.is("area")&&h.imagemap?w=h.imagemap(f,l):f[0].namespaceURI==="http://www.w3.org/2000/svg"&&h.svg?w=h.svg(f,l):(q=f.outerWidth(),s=f.outerHeight(),w=h.offset(f,g.container,u)),w.offset&&(q=w.width,s=w.height,w=w.offset),w.left+=l.x==="right"?q:l.x==="center"?q/2:0,w.top+=l.y==="bottom"?s:l.y==="center"?s/2:0
                }
                w.left+=m.x+(j.x==="right"?-o:j.x==="center"?-o/2:0),w.top+=m.y+(j.y==="bottom"?-p:j.y==="center"?-p/2:0),v.jquery&&f[0]!==b&&f[0]!==y&&B.vertical+B.horizontal!=="nonenone"?(v={
                    elem:v,
                    height:v[(v[0]===b?"h":"outerH")+"eight"](),
                    width:v[(v[0]===b?"w":"outerW")+"idth"](),
                    scrollLeft:u?0:v.scrollLeft(),
                    scrollTop:u?0:v.scrollTop(),
                    offset:v.offset()||{
                        left:0,
                        top:0
                    }
                },w.adjusted={
                    left:B.horizontal!=="none"?B.left(w.left):0,
                    top:B.vertical!=="none"?B.top(w.top):0
                }):w.adjusted={
                    left:0,
                    top:0
                },C.attr("class",function(b,c){
                    return a.attr(this,"class").replace(/ui-tooltip-pos-\w+/i,"")
                }).addClass(k+"-pos-"+j.abbreviation()),t.originalEvent=a.extend({},c),C.trigger(t,[x,w,v.elem||v]);
                if(t.isDefaultPrevented())return x;
                delete w.adjusted,d===e||isNaN(w.left)||isNaN(w.top)||f==="mouse"||!a.isFunction(g.effect)?C.css(w):a.isFunction(g.effect)&&(g.effect.call(C,x,a.extend({},w)),C.queue(function(b){
                    a(this).css({
                        opacity:"",
                        height:""
                    }),a.browser.msie&&this.style.removeAttribute("filter"),b()
                })),A=0;
                return x
            },
            redraw:function(){
                if(x.rendered<1||B)return x;
                var b=k+"-fluid",c=r.position.container,d,e,f,g;
                B=1,r.style.width?C.css("width",r.style.width):(C.css("width","").addClass(b),e=C.width()+(a.browser.mozilla?1:0),f=C.css("max-width")||"",g=C.css("min-width")||"",d=(f+g).indexOf("%")>-1?c.width()/100:0,f=(f.indexOf("%")>-1?d:1)*parseInt(f,10)||e,g=(g.indexOf("%")>-1?d:1)*parseInt(g,10)||0,e=f+g?Math.min(Math.max(e,g),f):e,C.css("width",Math.round(e)).removeClass(b)),B=0;
                return x
            },
            disable:function(b){
                var c=m;
                "boolean"!==typeof b&&(b=!C.hasClass(c)&&!F.disabled),x.rendered?(C.toggleClass(c,b),a.attr(C[0],"aria-disabled",b)):F.disabled=!!b;
                return x
            },
            enable:function(){
                return x.disable(e)
            },
            destroy:function(){
                var b=c[0],d=a.attr(b,t);
                x.rendered&&(C.remove(),a.each(x.plugins,function(){
                    this.destroy&&this.destroy()
                })),clearTimeout(x.timers.show),clearTimeout(x.timers.hide),P(),a.removeData(b,"qtip"),d&&(a.attr(b,"title",d),c.removeAttr(t)),c.removeAttr("aria-describedby").unbind(".qtip"),delete j[x.id];
                return c
            }
        })
    }
    function v(b){
        var c;
        if(!b||"object"!==typeof b)return e;
        "object"!==typeof b.metadata&&(b.metadata={
            type:b.metadata
        });
        if("content"in b){
            if("object"!==typeof b.content||b.content.jquery)b.content={
                text:b.content
            };
            
            c=b.content.text||e,!a.isFunction(c)&&(!c&&!c.attr||c.length<1||"object"===typeof c&&!c.jquery)&&(b.content.text=e),"title"in b.content&&("object"!==typeof b.content.title&&(b.content.title={
                text:b.content.title
            }),c=b.content.title.text||e,!a.isFunction(c)&&(!c&&!c.attr||c.length<1||"object"===typeof c&&!c.jquery)&&(b.content.title.text=e))
        }
        "position"in b&&("object"!==typeof b.position&&(b.position={
            my:b.position,
            at:b.position
        })),"show"in b&&("object"!==typeof b.show&&(b.show.jquery?b.show={
            target:b.show
        }:b.show={
            event:b.show
        })),"hide"in b&&("object"!==typeof b.hide&&(b.hide.jquery?b.hide={
            target:b.hide
        }:b.hide={
            event:b.hide
        })),"style"in b&&("object"!==typeof b.style&&(b.style={
            classes:b.style
        })),a.each(h,function(){
            this.sanitize&&this.sanitize(b)
        });
        return b
    }
    "use strict";
    var d=!0,e=!1,f=null,g,h,i,j={},k="ui-tooltip",l="ui-widget",m="ui-state-disabled",n="div.qtip."+k,o=k+"-default",p=k+"-focus",q=k+"-hover",r="-31000px",s="_replacedByqTip",t="oldtitle",u=e;
    g=a.fn.qtip=function(b,h,i){
        var j=(""+b).toLowerCase(),k=f,l=j==="disable"?[d]:a.makeArray(arguments).slice(1),m=l[l.length-1],n=this[0]?a.data(this[0],"qtip"):f;
        if(!arguments.length&&n||j==="api")return n;
        if("string"===typeof b){
            this.each(function(){
                var b=a.data(this,"qtip");
                if(!b)return d;
                m&&m.timeStamp&&(b.cache.event=m);
                if(j!=="option"&&j!=="options"||!h)b[j]&&b[j].apply(b[j],l);
                else if(a.isPlainObject(h)||i!==c)b.set(h,i);
                else{
                    k=b.get(h);
                    return e
                }
            });
            return k!==f?k:this
        }
        if("object"===typeof b||!arguments.length){
            n=v(a.extend(d,{},b));
            return g.bind.call(this,n,m)
        }
    },g.bind=function(b,f){
        return this.each(function(i){
            function q(b){
                function d(){
                    o.render(typeof b==="object"||k.show.ready),l.show.add(l.hide).unbind(n)
                }
                if(o.cache.disabled)return e;
                o.cache.event=a.extend({},b),o.cache.target=b?a(b.target):[c],k.show.delay>0?(clearTimeout(o.timers.show),o.timers.show=setTimeout(d,k.show.delay),m.show!==m.hide&&l.hide.bind(m.hide,function(){
                    clearTimeout(o.timers.show)
                })):d()
            }
            var k,l,m,n,o,p;
            p=a.isArray(b.id)?b.id[i]:b.id,p=!p||p===e||p.length<1||j[p]?g.nextid++:j[p]=p,n=".qtip-"+p+"-create",o=x.call(this,p,b);
            if(o===e)return d;
            k=o.options,a.each(h,function(){
                this.initialize==="initialize"&&this(o)
            }),l={
                show:k.show.target,
                hide:k.hide.target
            },m={
                show:a.trim(""+k.show.event).replace(/ /g,n+" ")+n,
                hide:a.trim(""+k.hide.event).replace(/ /g,n+" ")+n
            },/mouse(over|enter)/i.test(m.show)&&!/mouse(out|leave)/i.test(m.hide)&&(m.hide+=" mouseleave"+n),l.show.bind(m.show,q),(k.show.ready||k.prerender)&&q(f)
        })
    },h=g.plugins={
        Corner:function(a){
            a=(""+a).replace(/([A-Z])/," $1").replace(/middle/gi,"center").toLowerCase(),this.x=(a.match(/left|right/i)||a.match(/center/)||["inherit"])[0].toLowerCase(),this.y=(a.match(/top|bottom|center/i)||["inherit"])[0].toLowerCase(),this.precedance=a.charAt(0).search(/^(t|b)/)>-1?"y":"x",this.string=function(){
                return this.precedance==="y"?this.y+this.x:this.x+this.y
            },this.abbreviation=function(){
                var a=this.x.substr(0,1),b=this.y.substr(0,1);
                return a===b?a:a==="c"||a!=="c"&&b!=="c"?b+a:a+b
            }
        },
        offset:function(c,d,e){
            function l(a,b){
                f.left+=b*a.scrollLeft(),f.top+=b*a.scrollTop()
            }
            var f=c.offset(),g=d,i=0,j=document.body,k;
            if(g){
                do{
                    g.css("position")!=="static"&&(k=g[0]===j?{
                        left:parseInt(g.css("left"),10)||0,
                        top:parseInt(g.css("top"),10)||0
                    }:g.position(),f.left-=k.left+(parseInt(g.css("borderLeftWidth"),10)||0),f.top-=k.top+(parseInt(g.css("borderTopWidth"),10)||0),i++);
                    if(g[0]===j)break
                }while(g=g.offsetParent());
                (d[0]!==j||i>1)&&l(d,1),(h.iOS<4.1&&h.iOS>3.1||!h.iOS&&e)&&l(a(b),-1)
            }
            return f
        },
        iOS:parseFloat((""+(/CPU.*OS ([0-9_]{1,3})|(CPU like).*AppleWebKit.*Mobile/i.exec(navigator.userAgent)||[0,""])[1]).replace("undefined","3_2").replace("_","."))||e,
        fn:{
            attr:function(b,c){
                if(this.length){
                    var d=this[0],e="title",f=a.data(d,"qtip");
                    if(b===e){
                        if(arguments.length<2)return a.attr(d,t);
                        if(typeof f==="object"){
                            f&&f.rendered&&f.options.content.attr===e&&f.cache.attr&&f.set("content.text",c),a.fn["attr"+s].apply(this,arguments),a.attr(d,t,a.attr(d,e));
                            return this.removeAttr(e)
                        }
                    }
                }
            },
            clone:function(b){
                var c=a([]),d="title",e;
                e=a.fn["clone"+s].apply(this,arguments).filter("[oldtitle]").each(function(){
                    a.attr(this,d,a.attr(this,t)),this.removeAttribute(t)
                }).end();
                return e
            },
            remove:a.ui?f:function(b,c){
                a(this).each(function(){
                    c||(!b||a.filter(b,[this]).length)&&a("*",this).add(this).each(function(){
                        a(this).triggerHandler("remove")
                    })
                })
            }
        }
    },a.each(h.fn,function(b,c){
        if(!c)return d;
        var e=a.fn[b+s]=a.fn[b];
        a.fn[b]=function(){
            return c.apply(this,arguments)||e.apply(this,arguments)
        }
    }),g.version="nightly",g.nextid=0,g.inactiveEvents="click dblclick mousedown mouseup mousemove mouseleave mouseenter".split(" "),g.zindex=15e3,g.defaults={
        prerender:e,
        id:e,
        overwrite:d,
        content:{
            text:d,
            attr:"title",
            title:{
                text:e,
                button:e
            }
        },
        position:{
            my:"top left",
            at:"bottom right",
            target:e,
            container:e,
            viewport:e,
            adjust:{
                x:0,
                y:0,
                mouse:d,
                resize:d,
                method:"flip flip"
            },
            effect:function(b,c,d){
                a(this).stop().animate(c,200)
            }
        },
        show:{
            target:e,
            event:"mouseenter",
            effect:d,
            delay:90,
            solo:e,
            ready:e
        },
        hide:{
            target:e,
            event:"mouseleave",
            effect:d,
            delay:0,
            fixed:e,
            inactive:e,
            leave:"window",
            distance:e
        },
        style:{
            classes:"",
            widget:e,
            width:e
        },
        events:{
            render:f,
            move:f,
            show:f,
            hide:f,
            toggle:f,
            focus:f,
            blur:f
        }
    },h.ajax=function(a){
        var b=a.plugins.ajax;
        return"object"===typeof b?b:a.plugins.ajax=new y(a)
    },h.ajax.initialize="render",h.ajax.sanitize=function(a){
        var b=a.content,c;
        b&&"ajax"in b&&(c=b.ajax,typeof c!=="object"&&(c=a.content.ajax={
            url:c
        }),"boolean"!==typeof c.once&&c.once&&(c.once=!!c.once))
    },a.extend(d,g.defaults,{
        content:{
            ajax:{
                loading:d,
                once:d
            }
        }
    })
})(jQuery,window);

// jPaginate Plugin for jQuery - Version 0.3
// by Angel Grablev for Enavu Web Development network (enavu.com)
// Dual license under MIT and GPL :) enjoy
/*

To use simply call .paginate() on the element you wish like so:
$("#content").jPaginate(); 

you can specify the following options:
items = number of items to have per page on pagination
next = the text you want to have inside the text button
previous = the text you want in the previous button
active = the class you want the active paginaiton link to have
pagination_class = the class of the pagination element that is being generated for you to style
minimize = minimizing will limit the overall number of elements in the pagination links
nav_items = when minimize is set to true you can specify how many items to show
cookies = if you want to use cookies to remember which page the user is on, true by default
position = specify the position of the pagination, possible options: "before", "after", or "both"
equal = implements an equal height main element by using the highest possible element use true false
offset = unfortunately calculating heights with javascript isn't always 100% accurate, so please use this value to make it perfect :) its defaultly set to 50

*/
(function($){
    $.fn.jPaginate = function(options) {
        var defaults = {
            items: 4,
            next: "Next",
            previous: "Previous",
            active: "active",
            pagination_class: "pagination",
            minimize: false,
            nav_items: 6,
			cookies: true,
			position: "after",
			equal: false,
			offset: 50
        };
        var options = $.extend(defaults, options);

        return this.each(function() {
            // object is the selected pagination element list
            obj = $(this);
            // this is how you call the option passed in by plugin of items
            var show_per_page = options.items;
            //getting the amount of elements inside parent element
            var number_of_items = obj.children().size();
            //calculate the number of pages we are going to have
            var number_of_pages = Math.ceil(number_of_items/show_per_page);
            
			//create the pages of the pagination
            var array_of_elements = [];
            var numP = 0;
            var nexP = show_per_page;
			
			var height = 0;
			var max_height = 0;
            //loop through all pages and assign elements into array
            for (i=1;i<=number_of_pages;i++)
            {    
                array_of_elements[i] = obj.children().slice(numP, nexP);
				
				if (options.equal) {	
					obj.children().slice(numP, nexP).each(function(){
						height += $(this).outerHeight(); 
					});
					if (height > max_height) max_height = height;
					height = 0;
				}
				
                numP += show_per_page;
                nexP += show_per_page;
            }
			if (options.equal) {	
				max_height += options.offset;
				obj.css({"height":max_height});
			}
			
			
            // display first page and set first cookie
			if (options.cookies == true) {
				if (get_cookie("current")) {
					showPage(get_cookie("current"));
					createPagination(get_cookie("current"));
				} else {
					set_cookie( "current", "1");
					showPage(get_cookie("current"));
					createPagination(get_cookie("current"));
				}
			} else {
				showPage(1);
				createPagination(1);
			}
            //show selected page
            function showPage(page) {
                obj.children().hide();
                array_of_elements[page].show();
            }
            
            // create the navigation for the pagination 
            function createPagination(curr) {
                var start, items = "", end, nav = "";
                start = "<ul class='"+options.pagination_class+"'>";
                var previous = "<li><a class='goto_previous' href='#'>"+options.previous+"</a></li>";
                var next = "<li><a class='goto_next' href='#'>"+options.next+"</a></li>";
				var previous_inactive = "<li><a class='inactive'>"+options.previous+"</a></li>";
                var next_inactive = "<li><a class='inactive'>"+options.next+"</a></li>";
                end = "</ul><br clear='all' />"
                var after = number_of_pages - options.after + 1;
                var pagi_range = paginationCalculator(curr);
				for (i=1;i<=number_of_pages;i++)
                {
                    if (options.minimize == true) {
						var half = Math.ceil(number_of_pages/2)
                    	if (i >= pagi_range.start && i <= pagi_range.end) {
							if (i == curr) { items += '<li><a class="'+options.active+'" title="'+i+'">'+i+'</a></li>';} 
                        	else { items += '<li><a href="#" class="goto" title="'+i+'">'+i+'</a></li>';}
						} else if (curr <= half) {
							if (i >= (number_of_pages - 2)) {
								if (i == curr) { items += '<li><a class="'+options.active+'" title="'+i+'">'+i+'</a></li>';} 
                        		else { items += '<li><a href="#" class="goto" title="'+i+'">'+i+'</a></li>';}
							} 
						} else if (curr >= half) {
							if (i <= 2) {
								if (i == curr) { items += '<li><a class="'+options.active+'" title="'+i+'">'+i+'</a></li>';} 
                        		else { items += '<li><a href="#" class="goto" title="'+i+'">'+i+'</a></li>';}
							}
						}
                    } else {
                        if (i == curr) { items += '<li><a class="'+options.active+'" title="'+i+'">'+i+'</a></li>';} 
                        else { items += '<li><a href="#" class="goto" title="'+i+'">'+i+'</a></li>';}
                    }
                }
                if (curr != 1 && curr != number_of_pages) {
                    nav = start + previous + items + next + end;
                } else if (number_of_pages == 1) {
					nav = start + previous_inactive + items + next_inactive + end;
				} else if (curr == number_of_pages){
                    nav = start + previous + items + next_inactive + end;
                } else if (curr == 1) {
                    nav = start + previous_inactive + items + next + end;
                }
				if (options.position == "before") {
					obj.before(nav);
				} else if (options.position == "after") {
					obj.after(nav);
				} else {
					obj.after(nav);
					obj.before(nav)
				}
                
            }
			
			/* code to handle cookies */
			/* code to handle cookies */
			function set_cookie(c_name,value)
			{
				var expiredays = 999;
				var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);document.cookie=c_name+"="+escape(value)+
((expiredays==null)?"":";expires="+exdate.toUTCString());
			}
			function get_cookie(c_name)
			{
				if(document.cookie.length>0)
				{c_start=document.cookie.indexOf(c_name+"=");if(c_start!=-1)
				{c_start=c_start+c_name.length+1;c_end=document.cookie.indexOf(";",c_start);if(c_end==-1)c_end=document.cookie.length;return unescape(document.cookie.substring(c_start,c_end));}}
				return"";
			}
            
			function paginationCalculator(curr)  {
				var half = Math.floor(options.nav_items/2);
				var upper_limit = number_of_pages - options.nav_items;
				var start = curr > half ? Math.max( Math.min(curr - half, upper_limit), 0 ) : 0;
				var end = curr > half?Math.min(curr + half + (options.nav_items % 2), number_of_pages):Math.min(options.nav_items, number_of_pages);
				return {start:start, end:end};
			}
			
            // handle click on pagination 
            $(".goto").live("click", function(e){
                e.preventDefault();
                showPage($(this).attr("title"));
				set_cookie( "current", $(this).attr("title"));
                $(".pagination").remove();
                createPagination($(this).attr("title"));
            });
            $(".goto_next").live("click", function(e) {
                e.preventDefault();
                var act = "."+options.active;
                var newcurr = parseInt($(".pagination").find(".active").attr("title")) + 1;
                set_cookie( "current", newcurr);
				showPage(newcurr);
                $(".pagination").remove();
                createPagination(newcurr);
            });
            $(".goto_previous").live("click", function(e) {
                e.preventDefault();
                var act = "."+options.active;
                var newcurr = parseInt($(".pagination").find(".active").attr("title")) - 1;
				set_cookie( "current", newcurr);
                showPage(newcurr);
                $(".pagination").remove();
                createPagination(newcurr);
            });
        });
        
       
    };
})(jQuery);
