var Navigation =  {
    setRollovers : function() {
        $( ".rollover" ).each( function() {
            var iIndex = $( this ).attr( "src" ).indexOf( "_" );
            var iLength = $( this ).attr( "src" ).length;
            if ( iIndex > 0 ) {
                $( this ).bind( "mouseover", function() {
                    $( this ).attr( "src", "/images/btn_" + $( this ).attr( "src" ).substring( iIndex + 1, iLength - 4 ) + "_over.png" );
                } ).bind( 'mouseout', function() {
                    $( this ).attr( "src", "/images/btn_" + $( this ).attr( "src" ).substring( iIndex + 1, iLength - 4 ) + ".png" );
                } );
            }
        } );
    },
    applyRollover : function( oNode ) {
        var iIndex = $( oNode ).attr( "src" ).indexOf( "_" );
        var iLength = $( oNode ).attr( "src" ).length;
        if ( iIndex > 0 ) {
            $( oNode ).bind( "mouseover", function() {
                $( this ).attr( "src", "/images/btn_" + $( this ).attr( "src" ).substring( iIndex + 1, iLength - 4 ) + "_over.png" );
            } ).bind( 'mouseout', function() {
                $( this ).attr( "src", "/images/btn_" + $( this ).attr( "src" ).substring( iIndex + 1, iLength - 4 ) + ".png" );
            } );
        }
    },
    applyRolloverAsync : function( oNode ) {
        var iIndex = $( oNode ).attr( "src" ).indexOf( "_" );
        var iLength = $( oNode ).attr( "src" ).length;
        if ( iIndex > 0 ) {
            $( oNode ).bind( "mouseover", function() {
                $( this ).attr( "src", "/images/btn_" + $( this ).attr( "src" ).substring( iIndex + 1, iLength - 4 ) + "_over.png" );
            } ).bind( 'mouseout', function() {
                $( this ).attr( "src", "/images/btn_" + $( this ).attr( "src" ).substring( iIndex + 1, iLength - 4 ) + ".png" );
            } )
            oNode.onload = function() { };
        }
    },
    selectNav : function( sCurrent ) {
        
    }
}

var Dialog = {
    setDialogs : function() {
        $( "a.dialog" ).each( function( i ) {
            $( this ).click( function() {
                var sUrl = $( this ).attr( "href" );
                var sParams = "";
                var iIndex = sUrl.indexOf( "?" );
                if ( iIndex > 0 ) {
                    var aTokens = sUrl.split( "?" );
                    sUrl = aTokens[ 0 ];
                    sParams = aTokens[ 1 ];
                }
                Dialog.show( $( this ).attr( "title" ), sUrl, sParams );
                return false;
            } );
        } );
        $( "a.large_dialog" ).each( function( i ) {
            $( this ).click( function() {
                var sUrl = $( this ).attr( "href" );
                var sParams = "";
                var iIndex = sUrl.indexOf( "?" );
                if ( iIndex > 0 ) {
                    var aTokens = sUrl.split( "?" );
                    sUrls = aTokens[ 0 ];
                    sParams = aTokens[ 1 ];
                }
                Dialog.showLarge( $( this ).attr( "title" ), sUrl, sParams );
                return false;
            } );
        } );
    },
    show : function( sTitle, sUrl, sParams ) {
        var oWrap = document.createElement( 'div' );
        var oTop = document.createElement( 'div' );
        oTop.className = 'seen_popup_top';
        var oMiddle = document.createElement( 'div' );
        oMiddle.className = 'seen_popup_middle';
        var oBottom = document.createElement( 'div' );
        oBottom.className = 'seen_popup_bottom';
        oWrap.className = 'seen_popup_wrap';
        oWrap.id = new Date().getTime();
        oWrap.style.display = 'block';
        var oPopup = document.createElement( 'div' );
        oPopup.className = 'seen_popup';
        var oTitle = document.createElement( 'div' );
        oTitle.className = 'seen_popup_title';
        oTitle.innerHTML = sTitle;
        var oContent = document.createElement( 'div' );
        var oImgWrap = document.createElement( 'div' );
        oImgWrap.style.padding = '30px 0px';
        oImgWrap.style.textAlign = 'center';
        var oImg = document.createElement( 'img' );
        oImg.src = '/images/loading.gif';
        oImgWrap.appendChild( oImg );
        oContent.appendChild( oImgWrap );

        // set position
        var iScrollOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
        oWrap.style.top = ( iScrollOffset + 100 ) + 'px';
        oWrap.style.left = ( ( document.documentElement.clientWidth / 2 ) - 230 ) + 'px';

        // add to document
        oPopup.appendChild( oTitle );
        oPopup.appendChild( oContent );
        oMiddle.appendChild( oPopup );
        oWrap.appendChild( oTop );
        oWrap.appendChild( oMiddle );
        oWrap.appendChild( oBottom );
        document.body.appendChild( oWrap );

        // make content call
        sParams += sParams == '' ? 'popup=' + oWrap.id : '&popup=' + oWrap.id;
        $.ajax( {
            type : "GET",
            url : sUrl,
            data : sParams,
            success : function( html ) {
                $( oContent ).html( html );
            }
        } );
    },
    showLarge : function( sTitle, sUrl, sParams ) {
        var oPopup = document.createElement( 'div' );
        oPopup.id = new Date().getTime();
        oPopup.style.position = "absolute";
        var iScrollOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
        oPopup.style.top = ( iScrollOffset + 80 ) + 'px';
        oPopup.style.left = ( ( document.documentElement.clientWidth / 2 ) - 414 ) + 'px';

        var oPopupTop = document.createElement( 'div' );
        oPopupTop.id = "provider_popup_top";
        oPopup.appendChild( oPopupTop );
        var oPopupMiddle = document.createElement( 'div' );
        oPopupMiddle.id = 'provider_popup_middle';
        oPopup.appendChild( oPopupMiddle );
        var oPopupInner = document.createElement( 'div' );
        oPopupInner.id = 'provider_popup_inner';
        oPopupMiddle.appendChild( oPopupInner );
        var oPopupTitle = document.createElement( 'div' );
        oPopupTitle.id = 'provider_popup_title';
        oPopupTitle.innerHTML = sTitle;
        oPopupInner.appendChild( oPopupTitle );
        var oPopupPhotos = document.createElement( 'div' );
        oPopupPhotos.id = 'provider_popup_photos';
        oPopupInner.appendChild( oPopupPhotos );
        var oPopupLoading = document.createElement( 'div' );
        oPopupLoading.style.margin = '0px auto';
        oPopupLoading.style.padding = "100px 0px";
        oPopupLoading.style.width = '32px';
        var oImgLoad = document.createElement( 'img' );
        oImgLoad.src = '/images/loading.gif';
        oPopupLoading.appendChild( oImgLoad );
        oPopupPhotos.appendChild( oPopupLoading );
        var oPopupBottom = document.createElement( 'div' );
        oPopupBottom.id = 'provider_popup_bottom';
        oPopup.appendChild( oPopupBottom );

        document.body.appendChild( oPopup );

        sParams += sParams == '' ? 'popup=' + oPopup.id : '&popup=' + oPopup.id;

        $.ajax( {
            type : "GET",
            url : sUrl,
            data : sParams,
            success : function( html ) {
                $( "#provider_popup_photos" ).html( html );
            }
        } );
    },
    showLoading : function( sId ) {
        $( "#" + sId ).html( "<div class=\"news_feed_loading\"><img src=\"/images/loading.gif\" alt=\"\" /></div>" );
    },
    showLoadingLarge : function( sId ) {
        $( "#" + sId ).html( '<div style="padding: 100px 0px; text-align: center;"><img src="/images/loading.gif" alt="" /></div>' );
    },
    hide : function( sId ) {
        $( "#" + sId ).fadeOut( 550, function() {
            $( this ).remove();
        } );
    },
    scheduleHide : function( sId, iTimeout ) {
        setTimeout( function() { Dialog.hide( sId ); }, iTimeout );
    }
}

var SeenUtil = {
    countWords : function( sText ) {
        if ( sText == null || sText == "" )
            return 0;
        
        var iNumWords = 0;
        sText.replace( / +/, ' ' );
        var aTokens = sText.split( ' ' );
        for ( var i = 0; i < aTokens.length; i++ )
            if ( aTokens[ i ].match( /[0-9A-Za-z�-��-��-�]/ ) )
                iNumWords++;

        return iNumWords;
    },
    getWindowSize : function() {
        var iWidth = 0;
        var iHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            iWidth = window.innerWidth;
            iHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            iWidth = document.documentElement.clientWidth;
            iHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            iWidth = document.body.clientWidth;
            iHeight = document.body.clientHeight;
        }
        var a = new Array();
        a.push( iWidth );
        a.push( iHeight );
        return a;
    },
    getXPosition : function( oObj ) {
        var xPos = 0;
        if ( oObj.offsetParent ) {
            do {
                xPos += oObj.offsetLeft;
            } while ( oObj = oObj.offsetParent );
        }

        return xPos;
    },
    getYPosition : function( oObj ) {
        var yPos = 0;
        if ( oObj.offsetParent ) {
            do {
                yPos += oObj.offsetTop;
            } while ( oObj = oObj.offsetParent );
        }

        return yPos;
    }
}

var Feedback = {
    submit : function() {
        var sBody = $( "#feedback_body" ).val();
        var sCat = $( "#feedback_category" ).val();
        var sPopupId = $( "#feedback_popup" ).val();
        if ( sBody != null && sBody != '' ) {
            Dialog.showLoading( "feedback_container" );
            $.ajax( {
                type : "GET",
                url : "/contact/feedback",
                data : "cat=" + sCat + "&fb=" + escape( sBody ) + "&popup=" + sPopupId,
                success : function( html ) {
                    $( "#feedback_container" ).html( html );
                    Dialog.scheduleHide( sPopupId, 4000 );
                }
            } );
        }
    }
}

var Photo = {
    scale : function( oImg, iWidth, iHeight ) {
        var iOrigWidth = oImg.width;
        var iOrigHeight = oImg.height;
        var fScale = this.computeScale( iOrigWidth, iOrigHeight, iWidth, iHeight );
        oImg.width = Math.round( iOrigWidth * fScale );
        oImg.height = Math.round( iOrigHeight * fScale );
        $( oImg ).css( "display", "block" );
    },
    computeScale : function( iWidth, iHeight, iMaxWidth, iMaxHeight ) {
        var fScale = 1.0;
        if ( iHeight > iMaxHeight && iWidth > iMaxWidth )
            fScale = Math.min( iMaxWidth / ( iWidth * 1.0 ), iMaxHeight / ( iHeight * 1.0 ) );
        else if ( iHeight > iMaxHeight )
            fScale = iMaxHeight / ( iHeight * 1.0 );
        else if ( iWidth > iMaxWidth )
            fScale = iMaxWidth / ( iWidth * 1.0 );
        return fScale;
    }
}
