
// SWITCH STYLESHEETS FOR FONT SIZE ------------------------------
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  createCookie("style", title);
}
 
function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}
 
function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}
 
function createCookie(name,value,days) {
  if (days) {

   var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/"; }
 
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
     return null;
}
 
function onloadCookie(){
            var cookie = readCookie("style");
            var title = cookie ? cookie : getPreferredStyleSheet();
            setActiveStyleSheet(title);
}


// POPUP FOR EMAIL A FRIEND ---------------------------------------
function popupWindow(url){
  var aWindow = window.open(url,"sendEmail","width=455,height=330,menubar=no,toolbar=no,resizable=no,scrollbars=no,copyhistory=no");  
}

function popupWindowNew(url, width, height){
this.width = width;
this.height = height;

  var aWindow = window.open(url,"sendFeedback","width=" + width +",height="+ height +",menubar=no,toolbar=no,resizable=no,scrollbars=no,copyhistory=no");   //alert("height= " + height);
}

// AJAX SEARCH BOX ------------------------------------------------
$(function(){
$("#searchQuery").jSuggest({
url: "http://ask.miamidade.gov/search-suggest",
type: "GET",
autoChange: true,
loadingImg: 'http://ask.miamidade.gov/images/ajax-loader.gif',
data: "searchQuery"
});
});

// ON DOCUMENT.READY ---------------------------------------------
$(document).ready(function(){
	onloadCookie();
	wn();
	
$('ul.stripeList li:nth-child(odd)').addClass('rowOdd'); 
$('ul.stripeList li:nth-child(even)').addClass('rowEven');

$('table.stripeRow tr:odd').addClass('rowEven');


// BOTTOM BAR SHOW / HIDE FUNCTIONALITY
	$("#min").click(function(){
		$("#floatingBar").slideToggle('slow', function(){
			$("#floatingBarOpen").slideToggle('slow')});	
		return false;
	});
	
	$("#max").click(function(){
		$("#floatingBarOpen").slideToggle('slow', function(){
			$("#floatingBar").slideToggle('slow')});	
		return false;
	});

	
});

// .COM / .NET WARNING DISCLAIMER --------------------------------
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#pwBackgroundPopup").css({
			"opacity": "0.7"
		});
		$("#pwBackgroundPopup").fadeIn("slow");
		$("#popupWarning").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePwPopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#pwBackgroundPopup").fadeOut("slow");
		$("#popupWarning").fadeOut("slow");
		popupStatus = 0;
	}
}

$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	$("a").click(function(){
			ajmNewSiteURL = $(this).attr("href");
			var regex = /\.com|\.com\/|\.net|\.net\//;
			if(regex.test(ajmNewSiteURL)){
			
			if($(this).attr('class') == "exception")
			return true;	

			$("#popupWarning").centerInClient();
			loadPopup();
			return false;
		}
	});
			
	//CLOSING POPUP
	//Click the x event!
	$("#popupWarningClose").click(function(){
		disablePwPopup();
	});
	
	$("#pwAlertButton").click(function()
	{
		disablePwPopup();
		window.open(ajmNewSiteURL);
	});
	
	$("#cancelButton").click(function()
	{
		disablePwPopup();									  
	});
	
	//Click out event!
	$("#pwBackgroundPopup").click(function(){
		disablePwPopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27){
			disablePwPopup();
		}
	});
	

});
