//check to see if it's really ie8 or in ie7 compatibility mode
if ($j.browser.msie && parseInt($j.browser.version) == 8) {
    var browserVersion = navigator.appVersion.toString().toLowerCase();
    if (browserVersion.indexOf('msie 7') != -1) {
        $j.browser.version = "7.0";
    }
}

$j(document).ready(function() {
    $j('ignore,shape').addClass('ignore');

    //fix the menu fonts for ie
    fixMainNav();
    
    //make pop's clickable anywhere
    $j('.facility-pop').click(onPopClick);

    //fix enter press on search
    $j('#search').find('input[type=text]').keypress(function(e) {
        if (e.which == 13) {
            $j('#search').find('input[type=image]').click();
            return false;
        }
    });

    //textbox default switch
    var $inputs = $j('input[type=text][title]');
    $inputs.focus(function() {
        if (this.value == this.title) {
            this.value = '';
        }
    });
    $inputs.blur(function() {
        if (this.value.length == 0) {
            this.value = this.title;
        }
    });

    //font-size selector
    $j('#font-selector').click(function(event) {
        event.preventDefault();
        var size = 0;
        var $elm = $j(event.target);
        if ($elm.hasClass('font-selector-sm')) {
            size = 1;
        } else if ($elm.hasClass('font-selector-md')) {
            size = 1.2;
        } else if ($elm.hasClass('font-selector-lg')) {
            size = 1.4;
        }
        if (size > 0) {
            $j('#page').css('fontSize', size + 'em');
            fixMainNav();

            //save the fontSize in a cookie for the site
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + 900);
            document.cookie = 'fontSelectorSize=' + size + ';expires=' + exdate.toGMTString() + ';path=/';

            return false;
        }
    });

    //check for an already selected font size in the cookie and set it
    if (document.cookie.length > 0) {
        var c_name = 'fontSelectorSize';
        var 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;
            }
            var size = parseFloat(document.cookie.substring(c_start, c_end));

            $j('#page').css('fontSize', size + 'em');
            fixMainNav();
        }
    }

    //document download images
    $j('a[href$=.pdf]').attr('target', '_blank').after('<img src="/global/images/shared/icon-pdf.gif" alt="Download PDF" />');

    //fix the z-index ordering in ie6
    if ($j.browser.msie) {
        var $menuItems = $j('#nav > li');
        var count = $menuItems.length;
        $menuItems.each(function(index, elm) {
            $elm = $j(elm);
            $elm.css('z-index', $elm.css('z-index') + count - index);
        });
    }
});

function onPopClick(event) {
	if (event.target.tagName != 'a') {
		var link = $j('.pop-heading a', this).attr('href');
		if (link.length > 0) {
			window.location = link;
		}
	}
}

function inputDefaultSwitch()
{
    //textbox default switch
	var $inputs = $j('input[type=text][title]');
	$inputs.focus(function() {
		if (this.value == this.title) {
			this.value = '';
		}
	});
	$inputs.blur(function() {
		if (this.value.length == 0) {
			this.value = this.title;
		}
	});
}

function fixMainNav() {
	if ($j.browser.msie) {
		$j('#nav > li ul').each(function() {
			$j('li', this).width('auto');
			var menuWidth = $j(this).width();
			$j('li', this).width(menuWidth);
		});
	}
}

/*
function textboxFocus(defaultText, id)
{
    if ($j("#" + id).val() == defaultText)
        $j("#" + id).val("");
}

function textboxBlur(defaultText, id)
{
    if ($j.trim($j("#" + id).val()) == "")
        $j("#" + id).val(defaultText);
}
*/

/*----------------------------------------------+
 | DocLinks - Add icon after links to documents |
 +----------------------------------------------*/
 /*
var DocLinks = {
	init : function() {
		// Find all links
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var address = theLink.href.toLowerCase();
			
			// Check if link points to files with common extensions
			var matches = address.match(/\.(doc|pdf|xls|ppt)/);
			
			if (matches) {
				// Using "match" always returns two results (not sure why)
				var ext = matches[0].substr(1, 3);
				
				// Create new image and insert it
				var newImg = document.createElement("img");
				newImg.alt = "(" + ext.toUpperCase() + ")";
				newImg.className = "file-icon";
				newImg.src = "/PropertyManager/images/shared/icon-" + ext + ".gif";
				newImg.title = newImg.alt;
				
				if (theLink.getElementsByTagName("img").length <= 0)
					theLink.parentNode.insertBefore(newImg, theLink);
				
				// Make link open in new window/tab
				theLink.onclick = function () {
					window.open(this.href);
					return false;
				};
			}
		}
	}
};
*/

var Tog = {
	check : function (a, b) {
		for (var i = 0; i < a.length; i++) {
			if (document.getElementById(a[i]).checked) {
				Tog.toggle(b[i]);
			}
		}
	}, 
	checkRadioSelected : function (radiolist, toggleElement) {
		for(var i=0; i < radiolist.length;i++)
		{
			if( radiolist[i].checked)
			{
			  if(radiolist[i].value == "3")
			   {
				 Tog.toggle(toggleElement);
			   }
			}
		}
	},
	swap : function (a, b) {
		a = document.getElementById(a);
		b = document.getElementById(b);
		
		if (!a || !b) return false;
		
		if (a.className.indexOf("closed") != -1) {
			oldClass = a.className;
			newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
			
			b.className += " closed";
		} else {
			oldClass = b.className;
			newClass = oldClass.replace(/closed/g, "");
			b.className = newClass;
			
			a.className += " closed";
		}
	},
	toggle : function (a) {
		a = document.getElementById(a);
		if (!a) return false;
		
		if (a.className.indexOf("closed") != -1) {
			oldClass = a.className;
			newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
		} else {
			a.className += " closed";
		}
	},
	show : function (a) {
	    a = document.getElementById(a);
	    if (!a) return false;
	    
	    if (a.className.indexOf("closed") != -1) {
	        oldClass = a.className;
	        newClass = oldClass.replace(/closed/g, "");
	        a.className = newClass;
	    }
	},
	hide : function (a) {
	    a = document.getElementById(a);
	    if (!a) return false;
	    
	    if (a.className.indexOf("closed") == -1) {
	        a.className += " closed";
	    }
	},
	showVisibility : function (a) {
	    a = document.getElementById(a);
	    if (!a) return false;
	    
	    if (a.className.indexOf("off") != -1) {
	        oldClass = a.className;
	        newClass = oldClass.replace(/off/g, "");
	        a.className = newClass;
	    }
	},
	hideVisibility : function (a) {
	    a = document.getElementById(a);
	    if (!a) return false;
	    
	    if (a.className.indexOf("off") == -1) {
	        a.className += " off";
	    }
	},
	togglePropertyValue : function (a, prop, value1, value2) {
		a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a[prop].indexOf(value1) != -1) {
			a[prop] = value2;
		} else {
			a[prop] = value1;
		}
	},
	appendClass : function (a, classValue) {
	    a = $(a);
	    if (!a) return false;
	
	    if (!a.hasClassName(classValue)) {
	        a.addClassName(classValue);
	    }
	},
	removeClass : function (a, classValue) {
	    a = $(a);
	    if (!a) return false;
	
	    if (a.hasClassName(classValue)) {
	        a.removeClassName(classValue);
	    }
	}
};

