/*
    main.js
    - IE6 mouseover menu
    - Article font size js
    - 
*/


/* Function for altering filter fields for member search */

var oArticlePage = {};
oArticlePage.clickSmall = function()
{
    $(".articleToolBox .articleToolBoxFont a").removeClass("selectedFontSize");
    $(".articleToolBox .articleToolBoxFont .small").addClass("selectedFontSize");
    $(".articlePages").removeClass("largeFont");
    $(".articlePages").addClass("smallFont");
};
oArticlePage.clickMedium = function()
{
    $(".articleToolBox .articleToolBoxFont a").removeClass("selectedFontSize");
    $(".articleToolBox .articleToolBoxFont .medium").addClass("selectedFontSize");
    $(".articlePages").removeClass("largeFont");
    $(".articlePages").removeClass("smallFont");
};
oArticlePage.clickLarge = function()
{
	
    $(".articleToolBox .articleToolBoxFont a").removeClass("selectedFontSize");
    $(".articleToolBox .articleToolBoxFont .large").addClass("selectedFontSize");
    $(".articlePages").removeClass("smallFont");
    $(".articlePages").addClass("largeFont");
};
oArticlePage.initFont = function()
{
    $(".articleToolBoxFont .small").bind("click",oArticlePage.clickSmall);
    $(".articleToolBoxFont .medium").bind("click",oArticlePage.clickMedium);
    $(".articleToolBoxFont .large").bind("click",oArticlePage.clickLarge);
};

oArticlePage.showPage = function(pagenum)
{
    if ($(".articlePages #articlePage_"+pagenum).length>0)
    {
        $(".articlePages .articlePage").addClass("articleHide");
        $(".articlePages .articlePage").removeClass("articleShow");
        
        $(".articlePages #articlePage_"+pagenum).removeClass("articleHide");
        $(".articlePages #articlePage_"+pagenum).addClass("articleShow");
        
        $(".articlePageList a").removeClass("currentPage");
        $(".articlePageList a").each(function(){if($(this).html()==pagenum) $(this).addClass("currentPage");});
    }
    if ($(".articlePages #articlePage_"+ (parseInt(pagenum)+1) ).length<1)
    {
        $(".articlePageList a:last").addClass("noNext");
    }
    else
    {
        $(".articlePageList a:last").removeClass("noNext");
    }
    scroll(0,0);
};
oArticlePage.pageNumberClick = function()
{
    var target = $(this).html();
    //alert(target);
    oArticlePage.showPage(target);
    return false;
};
oArticlePage.nextPageClick = function()
{
    oArticlePage.showPage( parseInt($(".articlePageList .currentPage").html())+1 );
    return false;
};
oArticlePage.initATag = function()
{
    var content = $(this).html();
    if(content!="Next Page")
    {
        $(this).bind("click",oArticlePage.pageNumberClick);
    }
    else
    {
        $(this).bind("click",oArticlePage.nextPageClick);
    }
};
oArticlePage.initPaging = function()
{
    $(".articlePageList").removeClass("hide");
    oArticlePage.showPage(1);
    $(".articlePageList a").each(oArticlePage.initATag);
};
oArticlePage.init = function()
{
    oArticlePage.initFont();
    oArticlePage.initPaging();
};

function verifySearch(){
	var searchBox=document.getElementById("memberSearchSelect");
	var searchField=document.getElementById("memberText");
	searchField.name=searchBox[searchBox.selectedIndex].value;
	return true;
}

function topNavigationIE6MouseoverFix(theId)
{
    var closure_this = this;
    
    if(typeof theId == "undefined")
    {
        theId = "topNavigation";
    }

    /* Show Hide Menus */
    this.toggleMouseOver = function(theEl)
    {
        if(theEl.mouseOutTimer)
        {
            clearTimeout(theEl.mouseOutTimer);
            theEl.mouseOutTimer = false;
        }
        theEl.mouseOverTimer = setTimeout(function(){$(theEl).find("ul").css("display","block");},1);
    };    
    this.createMouseOverBinding = function(theEl)
    {
        /* closures are awesome */
        theEl.mouseOverTimer = false;
        return function(){closure_this.toggleMouseOver(theEl);};
    }
    this.toggleMouseOut = function(theEl)
    {
        theEl.mouseOutTimer = setTimeout(function(){$(theEl).find("ul").css("display","none");},1);
    };    
    this.createMouseOutBinding = function(theEl)
    {
        theEl.mouseOutTimer = false;
        return function(){closure_this.toggleMouseOut(theEl);};
    }
    var aEl = $("#"+theId+">ul>li");
    for(var i=0,j=aEl.length;i<j;i++)
    {
        $(aEl[i]).bind("mouseover",this.createMouseOverBinding(aEl[i]));
        $(aEl[i]).bind("mouseout",this.createMouseOutBinding(aEl[i]));
    }
    
    /* Menu Item Backgrounds */
    this.toggleMouseOver2 = function(theEl)
    {
        $(theEl)[0].style.backgroundColor = "#584528";
        //setTimeout(function(){$(theEl)[0].style.backgroundColor = "#584528";},1);
    };    
    this.createMouseOverBinding2 = function(theEl)
    {
        /* closures are awesome */
        return function(){closure_this.toggleMouseOver2(theEl);};
    }
    this.toggleMouseOut2 = function(theEl)
    {
        $(theEl)[0].style.backgroundColor = "#e17000";
        // Set timer to prevent flicker
        //setTimeout(function(){$(theEl)[0].style.backgroundColor = "#e17000";},1);
    };    
    this.createMouseOutBinding2 = function(theEl)
    {
        return function(){closure_this.toggleMouseOut2(theEl);};
    }
    aEl= $("#"+theId+">ul>li>ul>li");
    for(var i=0,j=aEl.length;i<j;i++)
    {
        $(aEl[i]).bind("mouseover",this.createMouseOverBinding2(aEl[i]));
        $(aEl[i]).bind("mouseout",this.createMouseOutBinding2(aEl[i]));
    }
}

if(jQuery.browser.msie && parseInt(jQuery.browser.version)==6)
    $(document).ready(function(){window.topNavigationIE6MouseoverFixObject = topNavigationIE6MouseoverFix();});
    
$(document).ready( function() { 
  $("a[@rel=*external]").click( function() { 
    window.open( $(this).attr('href') ); 
    return false; 
  }); 
  $("a.external_link").click( function() { 
    window.open( $(this).attr('href') ); 
    return false; 
  });
});

$(document).ready( function() {
  $.ifixpng('/sites/all/themes/seventhgen/images/pixel.gif');
  $('img.png24').ifixpng(); 
});

/*
    ccp.js - cornerControlPanel
    Top right control panel menu, depends on jQuery framework.
*/

var cornerControlPanelClass = function(sContainer, sMenuItems, sContent)
{
    var closure_this = this;
    
    if(typeof sContainer == "undefined")
        sContainer = "#cornerControlPanel";
    if(typeof sMenuItems == "undefined")
        sMenuItems = ".ccp_menuItems";
    if(typeof sContent == "undefined")
        sContent = ".ccp_content";

    this.containerSel = sContainer;
    this.menuItemsSel = sMenuItems;
    this.contentSel = sContent;
    
    this.containerEl = $(sContainer)[0];
    this.menuItemsEl = $(sMenuItems)[0];
    this.contentEl = $(sContent)[0];
    
    this.menuItemClickFn = function(theId,theTargetId)
    {
        this.hideContentBoxes();
        this.deactivateMenuItems();
        this.activateMenuItem(theId);
        this.showContentBox(theTargetId);
    };
    
    this.hideContentBoxes = function()
    {
        $(this.containerSel+" "+this.contentSel+" div").each(function(i) {
            $(this).removeClass("active");
        });
    };
    this.deactivateMenuItems = function()
    {
        $(this.containerSel+" "+this.menuItemsSel+" div").each(function(i) {
            $(this).removeClass("active");
            if(i>0)
                $(this).removeClass("noLeft");
        });
    };
    this.activateMenuItem = function(theId)
    {
        var flagNum = -1;
        $(this.containerSel+" "+this.menuItemsSel+" div").each(function(i) {
            if( flagNum > -1 && typeof $(this).attr("id") != "undefined")
            {
                $(this).addClass("noLeft");
                flagNum = -1;
            }
            if( $(this).attr("id") == theId && typeof $(this).attr("id") != "undefined")
            {
                $(this).addClass("active");
                flagNum = i;
            }
        });
    };
    this.showContentBox = function(theTargetId)
    {
        $(this.containerSel+" "+this.contentSel+" #"+theTargetId).each(function(i) {
            $(this).addClass("active");
        });
    };


    /* Initialization Code */
    $(this.containerSel+" "+this.menuItemsSel+" div").each(function (i) {
            var theId = $(this).attr("id");
            var theTargetId = $(this).attr("contenttargetid");
            if(typeof theTargetId != "undefined")
            {
                $(this).bind("click",function(){closure_this.menuItemClickFn(theId,theTargetId)})
            }
    });
    $(this.containerEl).width($(this.menuItemsEl).width() + 1);
};

$(document).ready(function(){window.cornerControlPanel = new cornerControlPanelClass();});

function validZip(zip) {
  if (zip.match(/^[0-9]{5}$/)) {
    return true;
  }
  zip=zip.toUpperCase();
  if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
    return true;
  }
  if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
    return true;
  }
  alert('Please enter a valid postal code.');
  return false;
}

function validEmail(inputvalue){	
  if (!inputvalue) return false;
  if (inputvalue.match(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/)) {
    return true;
  } else {
    alert('Please enter a valid email address.');
    return false;
  }
}

$("document").ready(function() {
  $("form[@name='newsletter-join']").submit(function() {
    return validEmail($("input#email").val()) && validZip($("input#zipcode").val());
    return false;
  });                  
})
