﻿var neonet = neonet || {};

neonet = {

    sessionUrl : '',

    init : function(){
       // start all functions
       neonet.hijackLinks(); 
       nansen.searchBoxDefault('searchBox');
       nansen.imageHover();
       neonet.checkAll.init();
    },
    
    hijackLinks : function(){
        neonet.countrySelect.init();
        neonet.expandingBoxes.init();
    },
    
    setSession : function(officeId){
        setSessionUrl = '/ajaxSetSession.aspx?officeID='+officeId;
        //alert(setSessionUrl);
        new Ajax(setSessionUrl, {method: 'get'}).request();
    },
    
    countrySelect : {
        init : function(){
            var dropDiv = $('countryDropDown');
            if(dropDiv){
                var countryFx = new Fx.Slide(dropDiv,{duration: 200});
                $('linkChangeOffice').addEvent('click', function(e){
			        e = new Event(e).stop();
			        if(this.hasClass('active')){
			            this.removeClass('active');
			            countryFx.slideOut();
			        }else{
			            this.addClass('active');
			            dropDiv.getFirst().setStyle('display','block');
			            countryFx.slideIn();
			            dropDiv.addClass('active');
			        }
			    });
			    countryFx.hide();
			}
        }
    },
    
    expandingBoxes : {
        init : function(){
            fxExpand = [];
            header = [];
            $$('div.expandingBox').each(function(oObj, i){
                header[i] = oObj.getElement('img');
                toggledDiv = oObj.getElement('div');
                header[i].setStyle('cursor', 'pointer');
                neonet.expandingBoxes.setHover(i);
                fxExpand[i] = new Fx.Slide(toggledDiv, {duration: 100});
                header[i].addEvent('click', function(e){
                    e = new Event(e).stop();
                    neonet.expandingBoxes.click(i);
                }); 
            fxExpand[i].hide();
            });
        },
        click : function(oObj){
            for(i=0;i<fxExpand.length;i++){
                if(!fxExpand[i].open){
                    header[i].removeEvents('mouseover');
                    header[i].removeEvents('mouseout');
                    if(i==0){header[i].useMap='rssmap'}
                    if(i!=oObj){neonet.expandingBoxes.mouseIn(i);}
                }else{
                    neonet.expandingBoxes.setHover(i);
                    if(i!=oObj){neonet.expandingBoxes.mouseOut(i);}
                    if(i==0){header[i].useMap=''}
                }
                fxExpand[i].toggle();
            }
        },
        setHover : function(i){
            header[i].addEvent('mouseover', function(){
			    if(!fxExpand[i].open){
			        neonet.expandingBoxes.mouseIn(i);
			    }
		    });
		    header[i].addEvent('mouseout', function(){
			    if(!fxExpand[i].open){
			        neonet.expandingBoxes.mouseOut(i);
		        }
		    });
        },
        mouseIn : function(i){
            if(!header[i].src.match("Hover")){
                strExt = header[i].src.match(".jpg|.gif|.png");
		        imageSrc = header[i].src.split(strExt);
		        header[i].src = imageSrc[0]+'Hover'+strExt;
		        return true;
		    }
        },
        mouseOut : function(i){
            imageSrc = header[i].src.split('Hover.');
			header[i].src = imageSrc[0]+'.'+imageSrc[1];
			return true;
        }
    },
    
    checkAll : {
        init : function(){
            arCheckBoxes = [];
            // save all fieldsets we are gonna use in an array
            arCheckAll = $$('fieldset.nf_checkAll');
            arCheckAll.each(function(oObj, i){
                tmpInt = i;
                // iterate through all the checkboxes under the current fieldset
                arCheckBoxes = arCheckAll[i].getElements('input[type=checkbox]');
                // make a multidimensional array out of it
                arCheckAll[i] = arCheckBoxes;
                arCheckAll[i].each(function(oObj, i){
                    arCheckAll[tmpInt][i].addEvent('click',function(e){
                        neonet.checkAll.onChangeEventHandler(oObj);
                    });
                });
            });
        },
        onChangeEventHandler : function(oObj){
            if(oObj.hasClass('nf_checkAllHandler')){
                (oObj.checked)?neonet.checkAll.checkAll(true):neonet.checkAll.checkAll(false);
            }else{
                var i = 0;
                arCheckAll[0].each(function(oObj){
                    if(!oObj.hasClass('nf_checkAllHandler')){
                        if(oObj.checked){i++}else{i=i}
                    } 
                });
                if(i==(arCheckAll[0].length-1)){
                    arCheckAll[0][0].checked = true;
                }else{
                    arCheckAll[0][0].checked = false;
                }
                
            }
        },
        checkAll : function(boolCheck){
            arCheckAll[0].each(function(oObj, i){
                oObj.checked = boolCheck;
            });
        }
    }
}

window.addEvent('domready', function() {
	neonet.init();
});