//
// DEVNET.JS - DEVNET's javascript goodie-bag.  Borrowed in some part from
//             the Krang project (http://krang.sourceforge.net).  Builds
//             on JQuery where necessary.
//

//
// Create the DEVNET namespace
//
var DEVNET;
if (typeof DEVNET === 'undefined') {
    DEVNET = {};
}

//
// CODE MANAGEMENT
//

// Run the specified code block
DEVNET.run_code = function( code_array ) {
    var size = code_array.length;
    for(var i=0; i< size; i++) {
        var code = code_array.shift();
        if( code ) code();
    }
}

// This should run when a page gets loaded.
DEVNET.load = function( target ) {
    DEVNET.loadControls();
    DEVNET.run_code( DEVNET.onload_code );

    //Shadowbox.init();
};

// Add some code that will get executed after the DOM is loaded.  Multiple
// calls will not overwrite previous calls and all code will be executed
// in the order given.
DEVNET.onload_code = [];
DEVNET.onload = function( code ) {
    DEVNET.onload_code.push( code );
};

// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
function log(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  arguments.callee = arguments.callee.caller;
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});

// Set up whatever controls we need on this page
DEVNET.loadControls = function() {
    jQuery(function($){
        $("#house_number_low").livequery(function(){
        	$(this).mask( "9?9999" );
        });
        $("#house_number_high").livequery(function(){
        	$(this).mask( "9?9999" );
        });
        $("#sale_date_low").livequery(function(){
        	$(this).mask( "99/99/9999" );
        });
        $("#sale_date_high").livequery(function(){
        	$(this).mask( "99/99/9999" );
        });
        $("#sale_price_low").livequery(function(){
        	$(this).mask( "$9?99999999" );
        });
        $("#sale_price_high").livequery(function(){
        	$(this).mask( "$9?99999999" );
        });
        $("#sq_ft_low").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#sq_ft_high").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#year_built_low").livequery(function(){
        	$(this).mask( "9999", { completed: function() { DEVNET.setFocus("year_built_high"); } } );
        });
        $("#year_built_high").livequery(function(){
        	$(this).mask( "9999", { completed: function() { DEVNET.setFocus("sq_ft_low"); } } );
        });
        $("#acreage_low").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#acreage_high").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#bedrooms_low").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#bedrooms_high").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#fireplaces_low").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#fireplaces_high").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#fullbath_low").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#fullbath_high").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#halfbath_low").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#halfbath_high").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#rooms_low").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#rooms_high").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#stories_low").livequery(function(){
        	$(this).mask( "9?999999" );
        });
        $("#stories_high").livequery(function(){
        	$(this).mask( "9?999999" );
        });
    });

    $(document).ready(function() {
        $('.township').livequery(function(){
        	$(this).multiselectable({
                selectableLabel: 'All Townships/Municipalities',
                selectedLabel:   'Selected'
            });
        });
        $('.neighborhood').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Neighborhoods',
	            selectedLabel:   'Selected'
	        });
        });
        $('.styles').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Styles',
	            selectedLabel:   'Selected'
	        });
        });
        $('.taxcode').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Tax Codes',
	            selectedLabel:   'Selected'
	        });
        });
        $('.taxdistrict').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Tax Districts',
	            selectedLabel:   'Selected'
	        });
        });
        $('.polysub').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Political Subdivisions',
	            selectedLabel:   'Selected'
	        });
        });
        $('.school_district').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All School Districts',
	            selectedLabel:   'Selected'
	        });
        });
        $('.propuse').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Property Classes',
	            selectedLabel:   'Selected'
	        });
        });
        $('.exemption').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Exemption Types',
	            selectedLabel:   'Selected'
	        });
        });
        $('.luse').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Land Uses',
	            selectedLabel:   'Selected'
	        });
        });
        $('.nrel').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Name Relationships',
	            selectedLabel:   'Selected'
	        });
        });
        $('.owntype').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Owner Types',
	            selectedLabel:   'Selected'
	        });
        });
        $('.propclass').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Property Classes',
	            selectedLabel:   'Selected'
	        });
        });
        $('.basement_type').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Basement Types',
	            selectedLabel:   'Selected'
	        });
        });
        $('.garage_type').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Garage Types',
	            selectedLabel:   'Selected'
	        });
        });
        $('.heating_type').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Heating Types',
	            selectedLabel:   'Selected'
	        });
        });
        $('.appraisal_area').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Appraisal Areas',
	            selectedLabel:   'Selected'
	        });
        });
        $('.sa_project').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Special Assessment Projects',
	            selectedLabel:   'Selected'
	        });
        });
        $('.sa_type').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Special Assessment Types',
	            selectedLabel:   'Selected'
	        });
        });
        $('.subdivision').livequery(function(){
        	$(this).multiselectable({
	            selectableLabel: 'All Subdivisions',
	            selectedLabel:   'Selected'
	        });
        });
        $(".sbox").livequery(function(){
            //Shadowbox.init();
        });

	//DP: Call Site specific (TaxInquiry namespace) stuff here
        if (typeof TaxInquiry != 'undefined') { TaxInquiry.onready(); }
        //DP: Call Site specific Style overrides here (styles that can't be acheived with CSS, kludges, etc)
        if (typeof Style != 'undefined') { Style.onready(); }
	// Initialize ECommerce
        if (typeof ECommerce != 'undefined') { ECommerce.onready(); }
	// Initialize Google Maps
        if (typeof googleMaps != 'undefined') { googleMaps.onready(); }
	// Initialize client-specific needs
        if (typeof Client != 'undefined') { Client.onready(); }
    });
};

//
// PAGE MANIPULATION
//

// Show, hide, or toggle a page element
DEVNET.show = function( theElement ) {
    document.getElementById(theElement).style.display = '';
}

DEVNET.hide = function( theElement ) {
    document.getElementById(theElement).style.display = 'none';
}

DEVNET.showFade = function(theElement, duration) {
	$("#" + theElement + ":first").fadeIn(duration);
}

DEVNET.showPuff = function(theElement, duration) {
	$("#" + theElement + ":first").show("puff", { }, duration);
}

DEVNET.hideFade = function(theElement, duration) {
	$("#" + theElement + ":first").fadeOut(duration);
}

DEVNET.hidePuff = function(theElement, duration) {
	$("#" + theElement + ":first").hide("puff", { }, duration);
}

DEVNET.toggle = function( theElement ) {
    if ( $("#" + theElement).is(':visible') ) {
        $("#" + theElement).hide();
        $("input:visible:text:first").focus();
    }
    else {
        $("#" + theElement).show();
        $("#" + theElement + " input:visible:first").focus();
    }
}

DEVNET.highlight = function( row, newclass ) {
    row.className = newclass;
}

DEVNET.setFocus = function( arg_focus ) {
    document.getElementById(arg_focus).focus();
}

//
// APPLICATION FUNCTIONS
//
DEVNET.openStatement = function( parcelURL ) {
    var printWin = window.open( parcelURL, "Printing", 'scrollbars=yes,resizable=yes,width=1000,height=600' );
    return printWin;
}

DEVNET.printStatement = function( parcelURL ) {
    var printWin = window.open( parcelURL, "Printing", 'scrollbars=yes,resizable=yes,width=1000,height=600' );
    printWin.print();
    return printWin;
}

DEVNET.formToJSON = function( formName ) {
	return $(formName + " :input").serializeArray();
}

//lazy load a div via ajax if it doesn't yet exist. Executes optional callback if provided.
DEVNET.lazyLoad = function( template, idToLoad, parent, callback ) {
	if( ($("#" + idToLoad)).length == 0 ) {
		$.getJSON('/wedge/ajax/loadpage/' + template + '/', function(data){
			$("#" + parent).append( data.content );
			$("#" + idToLoad).hide()
			if( !(callback === undefined) ) {
				callback();
			}
		});
	} else {
		//call callback anyway if provided
		if( !(callback === undefined) ) {
			callback();
		}
	}
}

//provide lazy loading for images
DEVNET.lazyImages = function() {
    $("img.lazy").each(function() {
	    $(this).attr("src", $(this).attr("data-src")).removeClass("lazy");
    });
}

//
//LIB FUNCTIONS
//
//adds a trim() function to string objects
String.prototype.trim = function(){
	return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
}

//adds a startsWith() function to string objects
String.prototype.startsWith = function(str){
	return (this.toLowerCase().match("^" + str.toLowerCase()) == str.toLowerCase())
}

//adds an endsWith() function to string objects
String.prototype.endsWith = function(str){
	return (this.toLowerCase().match(str.toLowerCase() + "$") == str.toLowerCase())
}

