﻿if (!window.console)
    window.console = { log: function () { } };

var Parcel = {
    Site: {},
    Browser: {},
    data: {},
    applyCountySelect: function (data) {
        $(this).toggleClass("loading-color", false);
        $(this).html("");

        $(this).append("<option value='-1'>Please Select a County</option>");
        for (var i = 0; i < data.length; i++) {
            var county = data[i];

            $(this).append("<option value='" + i + "'>" + county.CountyName + "</option>");

        }

    },
    action: {
        loginSuccess: function () {
            window.loginSuccess();
        },
        loginFailure: function () {
            window.loginFailure();
        }
    },
    load: function () {
        $(".body-main-img").fadeIn(1000, function () {
            $("body").toggleClass("bg-loaded", true);
        });

        $(".slideshow-widget").foundryShow({
            events: {
                onmoveto: function (i, d) {

                    $(this).find(".current-page").html((1 + Number(i)) + "/" + $(this).foundryShow("len"));
                }
            },
            animate: { orientation: ["horizontal", "vertical"] }

        });
        $(".group-home").each(function () {
            /*controls: {
            index: false,
            play: false,
            pause: false,
            next: false,
            prev: false
            }*/
            $(".promo-slider").foundryShow({
                events: {

            },
            controls: {
                index: $(".slide-controls .show-index"),
                playpause: $(".slide-controls .play-pause"),
                next: $(".slide-controls .next"),
                prev: $(".slide-controls .prev")
            },
            animate: { orientation: "horizontal", autoplay: true }
        });
        $(".play-pause").click(function () {
            if ($(".promo-slider").foundryShow("paused")) {
                $(".promo-slider").foundryShow("pause", false);
                $(this).toggleClass("pause", true);
                $(this).toggleClass("play", false);
            } else {
                $(".promo-slider").foundryShow("pause", true);
                $(this).toggleClass("pause", false);
                $(this).toggleClass("play", true);
            }


        });



    });

},
init: function () {
    $(".fn-fade").fadeOut(1, function () { $(this).toggleClass("fn-fade", false) });
    $(".fn-action").each(function () {
        var action = $(this).attr("data-action");
        Parcel.action[action]();
    });
    $(".fn-nav").hide(1, function () { $(this).toggleClass("fn-nav", false) });

    //--------------NAVIGATION

    $(".nav-menu .lev1").children("li").each(function () {
        var ul = $(this).find("ul");
        if (!ul.get(0)) {
            return;
        }
        var anch = $(this).children("a:first");

        $(this).hover(function () {
            $(ul).toggleClass("front", true);
            $(ul).show();
            //$(anch).toggleClass("active", true);
            $(".nav-menu .lev2").toggleClass("front", false);
        }, function () {
            //$(anch).toggleClass("active", false);
            $(this).toggleClass("front", false);
            $(ul).hide();
        });

    });

    //--------------END NAVIGATION


    /*
    $(".tween-test").tween(50, 900, function (e, tween) {

    $(this).css("top", (-200 + tween.pos) + "px");
    }, function () {
    console.log("completed!");
    dotween();
    });
    */
    //$.wait(50,function () { alert(1); });
    var url = window.location.pathname + window.location.search;
    if (url != "/")
        url = url.replace(/^\//, "");

    $("a").each(function () {
        var active = false;

        var href = $(this).attr("href");
        if (href && href != "/")
            href = href.replace(/^\//, "");

        if ($(this).hasClass("fn-active-regex")) {

            var pattern = $(this).attr("rel");
            if (pattern != "/")
                pattern = pattern.replace(/^\//, "");

            active = url.search(pattern) != -1;
        } else {
            active = url == href;
        }

        $(this).toggleClass("active", active);
    });

    $(".faq-accordian").each(function () {
        $(this).find(".faq-question").prepend("<span class='inline-block sprite faq-arrow'></span>");
        $(this).find(".faq-answer").slideUp(function (i) {
            $(this).toggleClass("closed", false);
            if (i == 0)
                $(".faq-question:first").click();
        });
        $(this).find(".faq-question").click(function () {
            var set = $(this).parent().parent();
            var rel = $(this).attr("rel");
            set.find(".faq-set:not(.faq-" + rel + ") .faq-answer").slideUp().parent().toggleClass("active", false);

            if (!$(this).parent().hasClass("active")) {
                set.find(".faq-" + rel + " .faq-answer").slideDown();
                $(this).parent().toggleClass("active", true);
            } else {
                set.find(".faq-" + rel + " .faq-answer").slideUp();
                $(this).parent().toggleClass("active", false);
            }

        });


    });
    $(".page-pq-online-data-availability").each(function () {
        $(this).find(".data-value").css("visibility", "hidden");
    });

    $(".fn-ignore-key").keypress(function (e) {
        return e.preventDefault();
    });


}
};


$(document).ready(Parcel.init);
$(window).load(Parcel.load);




var CountySearch = {
    tokenizeSearch: function (obj) {
        var d = obj.d;
        if (!d)
            return;

        if (d.length) {
            var locs = d;
            var data = [];
            for (var i = 0; i < locs.length; i++) {
                var loc = locs[i];

                data.push({ "id": loc.CountyCode,
                    "item": (loc),
                    "name": loc.CountyName
                });

            }

            return data;
        }
        return [];
    },
    formatToken: function (loc) {

        return ("<li><p>{0}</p></li>").format(loc.name);

    },
    handleIndexChoice: function (loc) {
        var county = Parcel.data.Counties[loc];
        $.history.load(county.CountyCode);
        //CountySearch.handleChoice({ item: county });
    },
    countyByCode: function (code) {
        if (!code)
            return;

        var counties = Parcel.data.Counties;
        var county;
        for (var i = 0; i < counties.length; i++) {

            county = counties[i];
            if (county.CountyCode == code) {
                break;
            }
        }
        return county;

    },
    handleCodeChoice: function (code) {


        CountySearch.handleChoice({ item: CountySearch.countyByCode(code) });
    },

    handleChoice: function (loc) {

        var item = loc.item;
        var name = item.CountyName;
        var code = item.CountyCode;

        $(".page-about-why-frequency").each(function () {





            item.Updated = item.Updated.replace('/Date(', '');
            item.Updated = item.Updated.replace(')/', '');

            var updated = new Date();
            updated.setTime(item.Updated);

            var updatedStr = updated.toString("M/dd/yyyy");



            $(".county-date-inner").parent().fadeOut(function () {

                $(this).children(".county-date-inner").html(updatedStr);
                $(this).fadeIn("FAST");
            });

            $(".county-inner").parent().fadeOut(function () {
                $(this).children(".county-inner").html(name);
                $(this).fadeIn("FAST");
            });
        });

        $(".page-pq-online-data-availability").each(function () {

            $(".data-county-name").parent().fadeOut(function () {
                $(".data-county-name").html("");
                $(".data-assessor").html("");
                $(".county-name").html("");
                if (item.Assessor)
                    $(".data-assessor").html(item.Assessor + ", ASSESSOR");

                if (item.CountyName) {
                    $(".data-county-name").html(item.CountyName);
                    $(".county-name").html(item.CountyName);
                }
                if (item.APNFormat) {
                    $(".field-APN .data-value").html(item.APNFormat);
                }

                $(this).fadeIn();
            });
            $(".county-row").fadeTo("FAST", .5, function () {
                //$(this).fadeIn();

                /*$(".county-row").each(function(i){
                setTimeout($(this).fadeIn,i*1000);
                    
                });*/
            });
            $pq(".county-row").County("SampleSheetByCountyId", item.CountyCode, function (d) {

                for (var i in d) {

                    if ($(".field-" + i).get(0)) {

                        if (Number(d[i]))
                            $(".field-" + i + " .data-value").css("visibility", "visible");
                        else {
                            $(".field-" + i + " .data-value").css("visibility", "hidden");
                        }
                    }

                    if ($(".data-field-" + i).get(0)) {

                        if (Number(d[i])) {
                            $(".data-field-" + i + "").parent().css("visibility", "visible");

                            $(".data-field-" + i + "").css("visibility", "visible");

                        } else {
                            $(".data-field-" + i + "").css("visibility", "hidden");
                        }
                    }

                }
                $(".field-APN .data-value").css("visibility", "visible");
                $(this).fadeTo("FAST", 1);

            });

        });




        return "";

    },
    formatDD: function (loc) {

        return ("<li><span class='api-pq-result-name'>{0}</span></li>").format(loc.name);
    },
    searchText: "enter county or county code",
    noResultsText: "No counties found",
    formatQuery: function (q) {

        return JSON.stringify(q);

    }
};
window.loginSuccess = function () {
    window.opener.location = "/PQWeb/Login.aspx";
    window.close();
    //alert(1);
}
window.loginFailure= function () {
    window.opener.location = "/PQWeb/Login.aspx?error=true";
    window.close();
    //alert(1);
}
window.referralRewards = function () {


    $.fancybox({
        href: "/subcontent/ajax/referral.aspx",
        type: "ajax",
        width: 560
    });


}

$(function () {



    $(".county-search-input").each(function () {
        var theme = $(this).hasClass("long") ? " county-search-dd long" : " county-search-dd";
        $(this).tokenInput(function (e) {
            if ($("body").hasClass("page-about-why-frequency"))
                return $pq.County("service", "SearchInfo");
            else
                return $pq.County("service", "Search");

        }, { theme: "pq" + theme,
            resultsFormatter: CountySearch.formatDD,
            animateDropdown: false,
            noResultsText: CountySearch.noResultsText,
            onResult: CountySearch.tokenizeSearch,
            hintText: CountySearch.searchText,
            onAdd: CountySearch.handleChoice,
            tokenLimit: 1,
            tokenFormatter: CountySearch.formatToken, formatQuery: CountySearch.formatQuery
            /*,
        
            
        
            onAdd: CountySearch.handleChoice, tokenDelimiter: "$", formatQuery: CountySearch.formatQuery*/
        });
    });

    $(".county-search-select").each(function () {

        $.history.init(function (hash) {

            var countycode = hash.toUpperCase();
            if (Parcel.data.Counties)
                CountySearch.handleCodeChoice(countycode);
            else {
                $pq(".county-search-select").County('AllCounties', function (d) {
                    Parcel.data.Counties = d;
                    Parcel.applyCountySelect.call(this, Parcel.data.Counties);
                    var countyCode = (hash);
                    for (var i = 0; i < Parcel.data.Counties.length; i++) {

                        if (countyCode == Parcel.data.Counties[i].CountyCode) {
                            $(this).val(i);
                            var self = this;
                            window.setTimeout(function () {
                                $("select").val(i);
                            }, 100);
                            break;
                        }

                    }
                    CountySearch.handleCodeChoice(countycode);
                });
            }


        });

        if (Parcel.data.Counties) {
            Parcel.applyCountySelect.apply(this, Parcel.data.Counties);
        } else {
            $pq(this).County('AllCounties', function (d) {
                Parcel.data.Counties = d;
                Parcel.applyCountySelect.call(this, Parcel.data.Counties);
            });
        }
        $(this).change(function () {
            CountySearch.handleIndexChoice($(this).val());
        });
    });
    /*$(".county-search-select-tri").click(function () {
    $(".county-search-select").focus().click();
    });*/
    $("input[title],textarea[title]").hint("hinted");
    $(".pass-hint").focus(function () {
        var passTarget = $(this).attr("data-pass");
        $(passTarget).show().focus();
        $(this).hide();

        var self = this;
        $(passTarget).blur(function () {

            if ($(this).val())
                return;
            $(self).show();
            $(this).hide();
        });
    });

    $(".anch-member").each(function () {
        return;
        $(this).attr("href", "javascript:void(0);");
        $(this).attr("target", "_self");

        $(this).click(function () {
            //window.location = "https://www.parcelquest.com/PQWeb/login.aspx";
            var popup = window.open("/subcontent/iframe/log-in.aspx", "loginwindow", "menubar=0,left=300,directories=0,location=0,scrollbars=0,status=0,toolbar=0,width=184,height=183");
            if (!popup) {
                window.location = "/PQWeb/login.aspx";
            }
        });

    });


    $(".fn").hide();
    $(".fn").toggleClass("fn", false);
    $(".fn-fancybox-ajax").each(function () {
        $(this).attr("href", "javascript:void(0);");

        $(this).click(function () {
            var rel = $(this).attr("rel");
            $.fancybox({
                href: rel,
                type: "ajax",
                width: 560
            });
        });
    });

    $(".fn-fancybox-iframe").each(function () {
        $(this).attr("href", "javascript:void(0);");

        $(this).click(function () {
            var rel = $(this).attr("rel");
            $.fancybox({
                href: rel,
                type: "iframe",
                width: 560
            });
        });
    });

    $("input").bind("beforeaspvalidate", function () {
        if ($(this).hasClass("hinted"))
            $(this).val("");
    });

    $("input").bind("aspinvalid", function () {
        Parcel.reHint();

    });

});
Parcel.reHint = function () {
    $("input[title]").hint("hinted");
}

/*PQ API*/


var $pqUtils = {
    XD: {},
    S4: function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    },
    guid: function () {
        return ($pqUtils.S4() + $pqUtils.S4() + "-" + $pqUtils.S4() + "-" + $pqUtils.S4() + "-" + $pqUtils.S4() + "-" + $pqUtils.S4() + $pqUtils.S4() + $pqUtils.S4());
    },
    parseHash: function (key) {
        var hash = window.location.hash.replace(/^#/, "");
        var pid;
        if (hash.indexOf("=") != -1) {
            kvps = hash.split("&");

            for (var i = 0; i < kvps.length; i++) {
                var kvp = kvps[i].split("=");
                if (kvp[0] == key) {
                    pid = kvp[1];
                    break;
                }
            }
        } else {
            pid = hash;
        }

        return pid;
    }
};



if (!String.prototype.format) {
    function _StringFormatInline() {
        var txt = this;
        for (var i = 0; i < arguments.length; i++) {
            var exp = new RegExp('\\{' + (i) + '\\}', 'gm');
            txt = txt.replace(exp, arguments[i]);
        }
        return txt;
    }

    String.prototype.format = _StringFormatInline;
}

if (!String.format) {
    function _StringFormatStatic() {
        for (var i = 1; i < arguments.length; i++) {
            var exp = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
            arguments[0] = arguments[0].replace(exp, arguments[i]);
        }
        return arguments[0];
    }
    String.format = _StringFormatStatic;
}

/*END Base Methods*/

/*PQ JS API*/
(function (window) {



    window.$pq = function (arg) {


        return new $pq.fn.init(arg);
    };
    var $pq = window.$pq;
    $pq.fn = $pq.prototype;
    $pq.fn.net = $pq.net = {};

    
    (function ($pq) {

        /*Base credentials*/
        $pq.fn.root = $pq.root = true;
        if ($pq.loaded)
            return;

        $pq.net.domain = "www.parcelquest.com";
        $pq.net.port = "80";

        $pq.net.local = function () {
            $pq.net.domain = "localhost";
            $pq.net.port = "53624";
        }

        $pq.net.base = "service";
        $pq.net.protocal = "http";
        $pq.net.cdn = "www.parcelquest.com";

        /*NET METHODS*/
        $pq.net.leftSide = function (self) {
            if (!self)
                self = this;
            return self.protocal + "://" + self.domain + ":" + self.port + "/" + self.base;
        }
        $pq.net.cdnLeftSide = function (self) {
            if (!self)
                self = this;
            return self.protocal + "://" + self.cdn;
        }
        $pq.net.contentLeftSide = function (self) {
            if (!self)
                self = this;
            return self.protocal + "://" + self.domain + ":" + self.port + "/";
        }

        $pq.cdn = $pq.net.cdnLeftSide($pq.net);

        $pq.fn.net = $pq.net;

        $pq.guid = $pqUtils.guid();
        $pq.fn.init = function (arg) {
            this.target = jQuery(arg);
            this.guid = $pqUtils.guid();
            return this;
        }

        $pq.fn.init.prototype = $pq.fn;


        $pq.Call = $pq.fn.Call = function (url, args) {
            var self;
            if (this.target) {
                self = this.target;
            } else
                self = $pq;

            args.target = self;

            if (!args.success)
                args.success = function (e) { console.log(e); }



            if (!args.error)
                args.error = function (e) { console.log(e); }

            for (var q in args.data)
                args.data[q] = JSON.stringify(args.data[q]);

            jQuery.ajax({
                type: "GET",
                url: url,
                context: args,
                dataType: "jsonp",
                success: function (e) {
                    this.success.call(this.target, e.d);

                    if (this.success.after) {
                        this.success.after.call(this.target, e.d);
                    };
                },
                error: function (e) { this.error.call(this.target, e); },
                contentType: "application/json; charset=utf-8",
                data: args.data
            });
            return self;
        }

        /*
        * Location - Store Methods and Searches
        */
        $pq.County = $pq.fn.County = function () {

            var self = this.root ? this : $pq;

            var cmd = arguments[0];


            var args
            var service = false;
            if (cmd == "service") {
                service = true;
                cmd = arguments[1];
                args = [].slice.call(arguments, 2);
            } else
                args = [].slice.call(arguments, 1);

            args = $pq.County[cmd].apply(self, args);


            var url = $pq.net.leftSide($pq.net) + "/County.asmx/get" + args.cmd + "?format=json";
            if (service)
                return url;

            return $pq.Call.call(self, url, args);



        };

        $pq.fn.County.CountyById = $pq.County.CountyById = function (pid, success, error) {


            var args = {
                success: success,
                error: error,
                data: {
                    pid: pid
                },
                cmd: "CountyById"
            };
            return (args);
        };

        $pq.fn.County.AllCounties = $pq.County.AllCounties = function (success, error) {


            var args = {
                success: success,
                error: error,
                cmd: "AllCounties"
            };
            return (args);
        };
        

        $pq.fn.County.CountyByCode = $pq.County.CountyByCode = function (pid, success, error) {


            var args = {
                success: success,
                error: error,
                data: {
                    pid: pid
                },
                cmd: "CountyByCode"
            };
            return (args);
        };

        $pq.fn.County.CountyByName = $pq.County.CountyByName = function (pid, success, error) {


            var args = {
                success: success,
                error: error,
                data: {
                    pid: pid
                },
                cmd: "CountyByName"
            };
            return (args);
        };

        $pq.fn.County.CountyInfoById = $pq.County.CountyInfoById = function (pid, success, error) {


            var args = {
                success: success,
                error: error,
                data: {
                    pid: pid
                },
                cmd: "CountyInfoById"
            };
            return (args);
        };

        $pq.fn.County.CountyInfoByCode = $pq.County.CountyInfoByCode = function (pid, success, error) {


            var args = {
                success: success,
                error: error,
                data: {
                    pid: pid
                },
                cmd: "CountyInfoByCode"
            };
            return (args);
        };


        $pq.fn.County.NearCountysByZip = $pq.County.NearCountysByZip = function (zip, radius, success, error) {


            var args = {
                success: success,
                error: error,
                data: {
                    zip: zip,
                    radius: radius
                },
                cmd: "NearCountysByZip"
            };
            return (args);
        };
        

        $pq.fn.County.Search = $pq.County.Search = function (q, success, error) {
            var data;
            var cmd;

            
            data = {
                q: q          
            };
            cmd = "CountySearch";
            
            var args = {
                success: success,
                error: error,
                data: data,
                cmd: cmd
            };
            return (args);
        };

        $pq.fn.County.SearchInfo = $pq.County.SearchInfo = function (q, success, error) {
            var data;
            var cmd;

            
            data = {
                q: q          
            };
            cmd = "CountyInfoSearch";
            
            var args = {
                success: success,
                error: error,
                data: data,
                cmd: cmd
            };
            return (args);
        };

       

        $pq.fn.County.SampleSheetByCountyId = $pq.County.SampleSheetByCountyId = function (pid, success, error) {


            var args = {
                success: success,
                error: error,
                data: {
                    pid: pid
                },
                cmd: "SampleSheetByCountyId"
            };
            return (args);
        };

        /*
        * Location - Store Methods and Searches
        */
        $pq.Support = $pq.fn.Support = function () {

            var self = this.root ? this : $pq;

            var cmd = arguments[0];


            var args
            var service = false;
            if (cmd == "service") {
                service = true;
                cmd = arguments[1];
                args = [].slice.call(arguments, 2);
            } else
                args = [].slice.call(arguments, 1);

            args = $pq.Support[cmd].apply(self, args);


            var url = $pq.net.leftSide($pq.net) + "/Support.asmx/get" + args.cmd + "?format=json";
            if (service)
                return url;

            return $pq.Call.call(self, url, args);



        };

        $pq.fn.Support.LatestWebinar = $pq.Support.LatestWebinar = function (pid, success, error) {


            var args = {
                success: success,
                error: error,
                data: {
                    pid: pid
                },
                cmd: "LatestWebinar"
            };
            return (args);
        };




        if (!window.jQuery) {

            (function () {
                //var jq = document.createElement('script'); jq.type = 'text/javascript'; jq.async = false;
                var src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js';
                //jq.src = src;
                //var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jq, s);
                document.write('<script type="text/javascript" src="' + src + '"></script>');
            })();


        }
        if (!window.JSON) {
            (function () {
                //var jq = document.createElement('script'); jq.type = 'text/javascript'; jq.async = false;
                var src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js';
                //jq.src = src;
                //var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jq, s);
                document.write('<script type="text/javascript" src="' + src + '"></script>');
            })();
        }

        window.onload = (function () {


           

            /*******PLUGINS*******/
            //hash event            
            (function () { (function ($, e, b) { var c = "hashchange", h = document, f, g = $.event.special, i = h.documentMode, d = "on" + c in e && (i === b || i > 7); function a(j) { j = j || location.href; return "#" + j.replace(/^[^#]*#?(.*)$/, "$1") } $.fn[c] = function (j) { return j ? this.bind(c, j) : this.trigger(c) }; $.fn[c].delay = 50; g[c] = $.extend(g[c], { setup: function () { if (d) { return false } $(f.start) }, teardown: function () { if (d) { return false } $(f.stop) } }); f = (function () { var j = {}, p, m = a(), k = function (q) { return q }, l = k, o = k; j.start = function () { p || n() }; j.stop = function () { p && clearTimeout(p); p = b }; function n() { var r = a(), q = o(m); if (r !== m) { l(m = r, q); $(e).trigger(c) } else { if (q !== m) { location.href = location.href.replace(/#.*/, "") + q } } p = setTimeout(n, $.fn[c].delay) } $.browser.msie && !d && (function () { var q, r; j.start = function () { if (!q) { r = $.fn[c].src; r = r && r + a(); q = $('<iframe tabindex="-1" title="empty"/>').hide().one("load", function () { r || l(a()); n() }).attr("src", r || "javascript:0").insertAfter("body")[0].contentWindow; h.onpropertychange = function () { try { if (event.propertyName === "title") { q.document.title = h.title } } catch (s) { } } } }; j.stop = k; o = function () { return a(q.location.href) }; l = function (v, s) { var u = q.document, t = $.fn[c].domain; if (v !== s) { u.title = h.title; u.open(); t && u.write('<script>document.domain="' + t + '"<\/script>'); u.close(); q.location.hash = v } } })(); return j })() })(jQuery, this); })();
        });
        $pq.loaded = true;

    })(window.$pq);


})(window);

/*END PQ API*/

